1 //=======- VirtualCallChecker.cpp --------------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a checker that checks virtual function calls during
11 // construction or destruction of C++ objects.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/StmtVisitor.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19 #include "clang/StaticAnalyzer/Core/Checker.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/SaveAndRestore.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
30 class WalkAST : public StmtVisitor<WalkAST> {
32 AnalysisDeclContext *AC;
34 typedef const CallExpr * WorkListUnit;
35 typedef SmallVector<WorkListUnit, 20> DFSWorkList;
37 /// A vector representing the worklist which has a chain of CallExprs.
40 // PreVisited : A CallExpr to this FunctionDecl is in the worklist, but the
41 // body has not been visited yet.
42 // PostVisited : A CallExpr to this FunctionDecl is in the worklist, and the
43 // body has been visited.
44 enum Kind { NotVisited,
45 PreVisited, /**< A CallExpr to this FunctionDecl is in the
46 worklist, but the body has not yet been
48 PostVisited /**< A CallExpr to this FunctionDecl is in the
49 worklist, and the body has been visited. */
52 /// A DenseMap that records visited states of FunctionDecls.
53 llvm::DenseMap<const FunctionDecl *, Kind> VisitedFunctions;
55 /// The CallExpr whose body is currently being visited. This is used for
56 /// generating bug reports. This is null while visiting the body of a
57 /// constructor or destructor.
58 const CallExpr *visitingCallExpr;
61 WalkAST(BugReporter &br, AnalysisDeclContext *ac)
64 visitingCallExpr(0) {}
66 bool hasWork() const { return !WList.empty(); }
68 /// This method adds a CallExpr to the worklist and marks the callee as
70 void Enqueue(WorkListUnit WLUnit) {
71 const FunctionDecl *FD = WLUnit->getDirectCallee();
72 if (!FD || !FD->getBody())
74 Kind &K = VisitedFunctions[FD];
78 WList.push_back(WLUnit);
81 /// This method returns an item from the worklist without removing it.
82 WorkListUnit Dequeue() {
83 assert(!WList.empty());
89 WorkListUnit WLUnit = Dequeue();
90 const FunctionDecl *FD = WLUnit->getDirectCallee();
91 assert(FD && FD->getBody());
93 if (VisitedFunctions[FD] == PreVisited) {
94 // If the callee is PreVisited, walk its body.
96 SaveAndRestore<const CallExpr *> SaveCall(visitingCallExpr, WLUnit);
99 // Mark the function as being PostVisited to indicate we have
101 VisitedFunctions[FD] = PostVisited;
105 // Otherwise, the callee is PostVisited.
106 // Remove it from the worklist.
107 assert(VisitedFunctions[FD] == PostVisited);
112 // Stmt visitor methods.
113 void VisitCallExpr(CallExpr *CE);
114 void VisitCXXMemberCallExpr(CallExpr *CE);
115 void VisitStmt(Stmt *S) { VisitChildren(S); }
116 void VisitChildren(Stmt *S);
118 void ReportVirtualCall(const CallExpr *CE, bool isPure);
121 } // end anonymous namespace
123 //===----------------------------------------------------------------------===//
125 //===----------------------------------------------------------------------===//
127 void WalkAST::VisitChildren(Stmt *S) {
128 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
129 if (Stmt *child = *I)
133 void WalkAST::VisitCallExpr(CallExpr *CE) {
138 void WalkAST::VisitCXXMemberCallExpr(CallExpr *CE) {
140 bool callIsNonVirtual = false;
142 // Several situations to elide for checking.
143 if (MemberExpr *CME = dyn_cast<MemberExpr>(CE->getCallee())) {
144 // If the member access is fully qualified (i.e., X::F), then treat
145 // this as a non-virtual call and do not warn.
146 if (CME->getQualifier())
147 callIsNonVirtual = true;
149 // Elide analyzing the call entirely if the base pointer is not 'this'.
150 if (Expr *base = CME->getBase()->IgnoreImpCasts())
151 if (!isa<CXXThisExpr>(base))
156 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CE->getDirectCallee());
157 if (MD && MD->isVirtual() && !callIsNonVirtual)
158 ReportVirtualCall(CE, MD->isPure());
163 void WalkAST::ReportVirtualCall(const CallExpr *CE, bool isPure) {
164 SmallString<100> buf;
165 llvm::raw_svector_ostream os(buf);
167 os << "Call Path : ";
168 // Name of current visiting CallExpr.
169 os << *CE->getDirectCallee();
171 // Name of the CallExpr whose body is current walking.
172 if (visitingCallExpr)
173 os << " <-- " << *visitingCallExpr->getDirectCallee();
174 // Names of FunctionDecls in worklist with state PostVisited.
175 for (SmallVectorImpl<const CallExpr *>::iterator I = WList.end(),
176 E = WList.begin(); I != E; --I) {
177 const FunctionDecl *FD = (*(I-1))->getDirectCallee();
179 if (VisitedFunctions[FD] == PostVisited)
180 os << " <-- " << *FD;
183 PathDiagnosticLocation CELoc =
184 PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
185 SourceRange R = CE->getCallee()->getSourceRange();
188 os << "\n" << "Call pure virtual functions during construction or "
189 << "destruction may leads undefined behaviour";
190 BR.EmitBasicReport(AC->getDecl(),
191 "Call pure virtual function during construction or "
198 os << "\n" << "Call virtual functions during construction or "
199 << "destruction will never go to a more derived class";
200 BR.EmitBasicReport(AC->getDecl(),
201 "Call virtual function during construction or "
209 //===----------------------------------------------------------------------===//
210 // VirtualCallChecker
211 //===----------------------------------------------------------------------===//
214 class VirtualCallChecker : public Checker<check::ASTDecl<CXXRecordDecl> > {
216 void checkASTDecl(const CXXRecordDecl *RD, AnalysisManager& mgr,
217 BugReporter &BR) const {
218 WalkAST walker(BR, mgr.getAnalysisDeclContext(RD));
220 // Check the constructors.
221 for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), E = RD->ctor_end();
223 if (!I->isCopyOrMoveConstructor())
224 if (Stmt *Body = I->getBody()) {
230 // Check the destructor.
231 if (CXXDestructorDecl *DD = RD->getDestructor())
232 if (Stmt *Body = DD->getBody()) {
240 void ento::registerVirtualCallChecker(CheckerManager &mgr) {
241 mgr.registerChecker<VirtualCallChecker>();