Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 1 | //=======- VirtualCallChecker.cpp --------------------------------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 10 | // This file defines a checker that checks virtual function calls during |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 11 | // construction or destruction of C++ objects. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ClangSACheckers.h" |
| 16 | #include "clang/AST/DeclCXX.h" |
| 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/SaveAndRestore.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ento; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class WalkAST : public StmtVisitor<WalkAST> { |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 31 | const CheckerBase *Checker; |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 32 | BugReporter &BR; |
| 33 | AnalysisDeclContext *AC; |
| 34 | |
| 35 | typedef const CallExpr * WorkListUnit; |
| 36 | typedef SmallVector<WorkListUnit, 20> DFSWorkList; |
| 37 | |
| 38 | /// A vector representing the worklist which has a chain of CallExprs. |
| 39 | DFSWorkList WList; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 41 | // PreVisited : A CallExpr to this FunctionDecl is in the worklist, but the |
| 42 | // body has not been visited yet. |
| 43 | // PostVisited : A CallExpr to this FunctionDecl is in the worklist, and the |
| 44 | // body has been visited. |
| 45 | enum Kind { NotVisited, |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 46 | PreVisited, /**< A CallExpr to this FunctionDecl is in the |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 47 | worklist, but the body has not yet been |
| 48 | visited. */ |
| 49 | PostVisited /**< A CallExpr to this FunctionDecl is in the |
| 50 | worklist, and the body has been visited. */ |
Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 51 | }; |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 52 | |
| 53 | /// A DenseMap that records visited states of FunctionDecls. |
| 54 | llvm::DenseMap<const FunctionDecl *, Kind> VisitedFunctions; |
| 55 | |
| 56 | /// The CallExpr whose body is currently being visited. This is used for |
| 57 | /// generating bug reports. This is null while visiting the body of a |
| 58 | /// constructor or destructor. |
| 59 | const CallExpr *visitingCallExpr; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 61 | public: |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 62 | WalkAST(const CheckerBase *checker, BugReporter &br, |
| 63 | AnalysisDeclContext *ac) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 64 | : Checker(checker), BR(br), AC(ac), visitingCallExpr(nullptr) {} |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 66 | bool hasWork() const { return !WList.empty(); } |
| 67 | |
| 68 | /// This method adds a CallExpr to the worklist and marks the callee as |
| 69 | /// being PreVisited. |
| 70 | void Enqueue(WorkListUnit WLUnit) { |
| 71 | const FunctionDecl *FD = WLUnit->getDirectCallee(); |
| 72 | if (!FD || !FD->getBody()) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 73 | return; |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 74 | Kind &K = VisitedFunctions[FD]; |
| 75 | if (K != NotVisited) |
| 76 | return; |
| 77 | K = PreVisited; |
| 78 | WList.push_back(WLUnit); |
| 79 | } |
| 80 | |
| 81 | /// This method returns an item from the worklist without removing it. |
| 82 | WorkListUnit Dequeue() { |
| 83 | assert(!WList.empty()); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 84 | return WList.back(); |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 85 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 87 | void Execute() { |
| 88 | while (hasWork()) { |
| 89 | WorkListUnit WLUnit = Dequeue(); |
| 90 | const FunctionDecl *FD = WLUnit->getDirectCallee(); |
| 91 | assert(FD && FD->getBody()); |
| 92 | |
| 93 | if (VisitedFunctions[FD] == PreVisited) { |
| 94 | // If the callee is PreVisited, walk its body. |
| 95 | // Visit the body. |
| 96 | SaveAndRestore<const CallExpr *> SaveCall(visitingCallExpr, WLUnit); |
| 97 | Visit(FD->getBody()); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 99 | // Mark the function as being PostVisited to indicate we have |
| 100 | // scanned the body. |
| 101 | VisitedFunctions[FD] = PostVisited; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | // Otherwise, the callee is PostVisited. |
| 106 | // Remove it from the worklist. |
| 107 | assert(VisitedFunctions[FD] == PostVisited); |
| 108 | WList.pop_back(); |
| 109 | } |
| 110 | } |
| 111 | |
| 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); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 118 | void ReportVirtualCall(const CallExpr *CE, bool isPure); |
| 119 | |
| 120 | }; |
| 121 | } // end anonymous namespace |
| 122 | |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | // AST walking. |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
| 127 | void WalkAST::VisitChildren(Stmt *S) { |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame] | 128 | for (Stmt *Child : S->children()) |
| 129 | if (Child) |
| 130 | Visit(Child); |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void WalkAST::VisitCallExpr(CallExpr *CE) { |
| 134 | VisitChildren(CE); |
| 135 | Enqueue(CE); |
| 136 | } |
| 137 | |
| 138 | void WalkAST::VisitCXXMemberCallExpr(CallExpr *CE) { |
| 139 | VisitChildren(CE); |
| 140 | bool callIsNonVirtual = false; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 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; |
| 148 | |
Benjamin Kramer | cb4efc1 | 2014-08-21 10:25:03 +0000 | [diff] [blame] | 149 | if (Expr *base = CME->getBase()->IgnoreImpCasts()) { |
| 150 | // Elide analyzing the call entirely if the base pointer is not 'this'. |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 151 | if (!isa<CXXThisExpr>(base)) |
| 152 | return; |
Benjamin Kramer | cb4efc1 | 2014-08-21 10:25:03 +0000 | [diff] [blame] | 153 | |
| 154 | // If the most derived class is marked final, we know that now subclass |
| 155 | // can override this member. |
| 156 | if (base->getBestDynamicClassType()->hasAttr<FinalAttr>()) |
| 157 | callIsNonVirtual = true; |
| 158 | } |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Get the callee. |
| 162 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CE->getDirectCallee()); |
Benjamin Kramer | cb4efc1 | 2014-08-21 10:25:03 +0000 | [diff] [blame] | 163 | if (MD && MD->isVirtual() && !callIsNonVirtual && !MD->hasAttr<FinalAttr>() && |
| 164 | !MD->getParent()->hasAttr<FinalAttr>()) |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 165 | ReportVirtualCall(CE, MD->isPure()); |
| 166 | |
| 167 | Enqueue(CE); |
| 168 | } |
| 169 | |
| 170 | void WalkAST::ReportVirtualCall(const CallExpr *CE, bool isPure) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 171 | SmallString<100> buf; |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 172 | llvm::raw_svector_ostream os(buf); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 174 | os << "Call Path : "; |
| 175 | // Name of current visiting CallExpr. |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 176 | os << *CE->getDirectCallee(); |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 177 | |
| 178 | // Name of the CallExpr whose body is current walking. |
| 179 | if (visitingCallExpr) |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 180 | os << " <-- " << *visitingCallExpr->getDirectCallee(); |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 181 | // Names of FunctionDecls in worklist with state PostVisited. |
| 182 | for (SmallVectorImpl<const CallExpr *>::iterator I = WList.end(), |
| 183 | E = WList.begin(); I != E; --I) { |
| 184 | const FunctionDecl *FD = (*(I-1))->getDirectCallee(); |
| 185 | assert(FD); |
| 186 | if (VisitedFunctions[FD] == PostVisited) |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 187 | os << " <-- " << *FD; |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | PathDiagnosticLocation CELoc = |
| 191 | PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC); |
| 192 | SourceRange R = CE->getCallee()->getSourceRange(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 194 | if (isPure) { |
| 195 | os << "\n" << "Call pure virtual functions during construction or " |
| 196 | << "destruction may leads undefined behaviour"; |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 197 | BR.EmitBasicReport(AC->getDecl(), Checker, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 198 | "Call pure virtual function during construction or " |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 199 | "Destruction", |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 200 | "Cplusplus", os.str(), CELoc, R); |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | else { |
| 204 | os << "\n" << "Call virtual functions during construction or " |
| 205 | << "destruction will never go to a more derived class"; |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 206 | BR.EmitBasicReport(AC->getDecl(), Checker, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 207 | "Call virtual function during construction or " |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 208 | "Destruction", |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 209 | "Cplusplus", os.str(), CELoc, R); |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // VirtualCallChecker |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
| 218 | namespace { |
| 219 | class VirtualCallChecker : public Checker<check::ASTDecl<CXXRecordDecl> > { |
| 220 | public: |
| 221 | void checkASTDecl(const CXXRecordDecl *RD, AnalysisManager& mgr, |
| 222 | BugReporter &BR) const { |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 223 | WalkAST walker(this, BR, mgr.getAnalysisDeclContext(RD)); |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 224 | |
| 225 | // Check the constructors. |
Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 226 | for (const auto *I : RD->ctors()) { |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 227 | if (!I->isCopyOrMoveConstructor()) |
| 228 | if (Stmt *Body = I->getBody()) { |
| 229 | walker.Visit(Body); |
| 230 | walker.Execute(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Check the destructor. |
| 235 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
| 236 | if (Stmt *Body = DD->getBody()) { |
| 237 | walker.Visit(Body); |
| 238 | walker.Execute(); |
| 239 | } |
| 240 | } |
| 241 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 242 | } |
Ted Kremenek | b3512d3 | 2012-01-03 23:18:57 +0000 | [diff] [blame] | 243 | |
| 244 | void ento::registerVirtualCallChecker(CheckerManager &mgr) { |
| 245 | mgr.registerChecker<VirtualCallChecker>(); |
| 246 | } |