Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1 | // BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- 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 | // |
| 10 | // This file defines BugReporter, a utility class for generating |
| 11 | // PathDiagnostics for analyses based on GRSimpleVals. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/CFG.h" |
| 21 | #include "clang/AST/Expr.h" |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 22 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/ProgramPoint.h" |
| 24 | #include "clang/Analysis/PathDiagnostic.h" |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 29 | #include <queue> |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 34 | // Helper routines for walking the ExplodedGraph and fetching statements. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 37 | static inline Stmt* GetStmt(ProgramPoint P) { |
| 38 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 39 | return PS->getStmt(); |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 40 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 41 | return BE->getSrc()->getTerminator(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 43 | return 0; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 46 | static inline const ExplodedNode<GRState>* |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 47 | GetPredecessorNode(const ExplodedNode<GRState>* N) { |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 48 | return N->pred_empty() ? NULL : *(N->pred_begin()); |
| 49 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 51 | static inline const ExplodedNode<GRState>* |
| 52 | GetSuccessorNode(const ExplodedNode<GRState>* N) { |
| 53 | return N->succ_empty() ? NULL : *(N->succ_begin()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 56 | static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) { |
| 57 | for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N)) |
| 58 | if (Stmt *S = GetStmt(N->getLocation())) |
| 59 | return S; |
| 60 | |
| 61 | return 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 64 | static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) { |
| 65 | for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N)) |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 66 | if (Stmt *S = GetStmt(N->getLocation())) { |
| 67 | // Check if the statement is '?' or '&&'/'||'. These are "merges", |
| 68 | // not actual statement points. |
| 69 | switch (S->getStmtClass()) { |
| 70 | case Stmt::ChooseExprClass: |
| 71 | case Stmt::ConditionalOperatorClass: continue; |
| 72 | case Stmt::BinaryOperatorClass: { |
| 73 | BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode(); |
| 74 | if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr) |
| 75 | continue; |
| 76 | break; |
| 77 | } |
| 78 | default: |
| 79 | break; |
| 80 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 81 | return S; |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) { |
| 88 | if (Stmt *S = GetStmt(N->getLocation())) |
| 89 | return S; |
| 90 | |
| 91 | return GetPreviousStmt(N); |
| 92 | } |
| 93 | |
| 94 | static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) { |
| 95 | if (Stmt *S = GetStmt(N->getLocation())) |
| 96 | return S; |
| 97 | |
| 98 | return GetNextStmt(N); |
| 99 | } |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 102 | // PathDiagnosticBuilder and its associated routines and helper objects. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 103 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 105 | typedef llvm::DenseMap<const ExplodedNode<GRState>*, |
| 106 | const ExplodedNode<GRState>*> NodeBackMap; |
| 107 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 108 | namespace { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 109 | class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver { |
| 110 | NodeBackMap& M; |
| 111 | public: |
| 112 | NodeMapClosure(NodeBackMap *m) : M(*m) {} |
| 113 | ~NodeMapClosure() {} |
| 114 | |
| 115 | const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) { |
| 116 | NodeBackMap::iterator I = M.find(N); |
| 117 | return I == M.end() ? 0 : I->second; |
| 118 | } |
| 119 | }; |
| 120 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 121 | class VISIBILITY_HIDDEN PathDiagnosticBuilder { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 122 | GRBugReporter &BR; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 123 | SourceManager &SMgr; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 124 | ExplodedGraph<GRState> *ReportGraph; |
| 125 | BugReport *R; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 126 | const Decl& CodeDecl; |
| 127 | PathDiagnosticClient *PDC; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 128 | llvm::OwningPtr<ParentMap> PM; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 129 | NodeMapClosure NMC; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 130 | public: |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 131 | PathDiagnosticBuilder(GRBugReporter &br, ExplodedGraph<GRState> *reportGraph, |
| 132 | BugReport *r, NodeBackMap *Backmap, |
| 133 | const Decl& codedecl, PathDiagnosticClient *pdc) |
| 134 | : BR(br), SMgr(BR.getSourceManager()), ReportGraph(reportGraph), R(r), |
| 135 | CodeDecl(codedecl), PDC(pdc), NMC(Backmap) {} |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 137 | PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N); |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 139 | PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os, |
| 140 | const ExplodedNode<GRState>* N); |
| 141 | |
| 142 | ParentMap& getParentMap() { |
| 143 | if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody())); |
| 144 | return *PM.get(); |
| 145 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 147 | const Stmt *getParent(const Stmt *S) { |
| 148 | return getParentMap().getParent(S); |
| 149 | } |
| 150 | |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 151 | const CFG& getCFG() { |
| 152 | return *BR.getCFG(); |
| 153 | } |
| 154 | |
| 155 | const Decl& getCodeDecl() { |
| 156 | return BR.getStateManager().getCodeDecl(); |
| 157 | } |
| 158 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 159 | ExplodedGraph<GRState>& getGraph() { return *ReportGraph; } |
| 160 | NodeMapClosure& getNodeMapClosure() { return NMC; } |
| 161 | ASTContext& getContext() { return BR.getContext(); } |
| 162 | SourceManager& getSourceManager() { return SMgr; } |
| 163 | BugReport& getReport() { return *R; } |
| 164 | GRBugReporter& getBugReporter() { return BR; } |
| 165 | GRStateManager& getStateManager() { return BR.getStateManager(); } |
| 166 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 167 | PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); |
| 168 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 169 | PathDiagnosticLocation |
| 170 | getEnclosingStmtLocation(const PathDiagnosticLocation &L) { |
| 171 | if (const Stmt *S = L.asStmt()) |
| 172 | return getEnclosingStmtLocation(S); |
| 173 | |
| 174 | return L; |
| 175 | } |
| 176 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 177 | PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const { |
| 178 | return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive; |
| 179 | } |
| 180 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 181 | bool supportsLogicalOpControlFlow() const { |
| 182 | return PDC ? PDC->supportsLogicalOpControlFlow() : true; |
| 183 | } |
| 184 | }; |
| 185 | } // end anonymous namespace |
| 186 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 187 | PathDiagnosticLocation |
| 188 | PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) { |
| 189 | if (Stmt *S = GetNextStmt(N)) |
| 190 | return PathDiagnosticLocation(S, SMgr); |
| 191 | |
| 192 | return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 195 | PathDiagnosticLocation |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 196 | PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os, |
| 197 | const ExplodedNode<GRState>* N) { |
| 198 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 199 | // Slow, but probably doesn't matter. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 200 | if (os.str().empty()) |
| 201 | os << ' '; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 203 | const PathDiagnosticLocation &Loc = ExecutionContinues(N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 205 | if (Loc.asStmt()) |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 206 | os << "Execution continues on line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 207 | << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.'; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 208 | else |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 209 | os << "Execution jumps to the end of the " |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 210 | << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.'; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 211 | |
| 212 | return Loc; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 215 | PathDiagnosticLocation |
| 216 | PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { |
| 217 | assert(S && "Null Stmt* passed to getEnclosingStmtLocation"); |
| 218 | ParentMap &P = getParentMap(); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 219 | |
| 220 | while (isa<DeclStmt>(S) || isa<Expr>(S)) { |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 221 | const Stmt *Parent = P.getParent(S); |
| 222 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 223 | if (!Parent) |
| 224 | break; |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 226 | switch (Parent->getStmtClass()) { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 227 | case Stmt::BinaryOperatorClass: { |
| 228 | const BinaryOperator *B = cast<BinaryOperator>(Parent); |
| 229 | if (B->isLogicalOp()) |
| 230 | return PathDiagnosticLocation(S, SMgr); |
| 231 | break; |
| 232 | } |
| 233 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 234 | case Stmt::CompoundStmtClass: |
| 235 | case Stmt::StmtExprClass: |
Ted Kremenek | 1d9a23a | 2009-03-28 04:08:14 +0000 | [diff] [blame] | 236 | return PathDiagnosticLocation(S, SMgr); |
| 237 | case Stmt::ChooseExprClass: |
| 238 | // Similar to '?' if we are referring to condition, just have the edge |
| 239 | // point to the entire choose expression. |
| 240 | if (cast<ChooseExpr>(Parent)->getCond() == S) |
| 241 | return PathDiagnosticLocation(Parent, SMgr); |
| 242 | else |
| 243 | return PathDiagnosticLocation(S, SMgr); |
| 244 | case Stmt::ConditionalOperatorClass: |
| 245 | // For '?', if we are referring to condition, just have the edge point |
| 246 | // to the entire '?' expression. |
| 247 | if (cast<ConditionalOperator>(Parent)->getCond() == S) |
| 248 | return PathDiagnosticLocation(Parent, SMgr); |
| 249 | else |
| 250 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 251 | case Stmt::DoStmtClass: |
| 252 | if (cast<DoStmt>(Parent)->getCond() != S) |
| 253 | return PathDiagnosticLocation(S, SMgr); |
| 254 | break; |
| 255 | case Stmt::ForStmtClass: |
| 256 | if (cast<ForStmt>(Parent)->getBody() == S) |
| 257 | return PathDiagnosticLocation(S, SMgr); |
| 258 | break; |
| 259 | case Stmt::IfStmtClass: |
| 260 | if (cast<IfStmt>(Parent)->getCond() != S) |
| 261 | return PathDiagnosticLocation(S, SMgr); |
| 262 | break; |
| 263 | case Stmt::ObjCForCollectionStmtClass: |
| 264 | if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) |
| 265 | return PathDiagnosticLocation(S, SMgr); |
| 266 | break; |
| 267 | case Stmt::WhileStmtClass: |
| 268 | if (cast<WhileStmt>(Parent)->getCond() != S) |
| 269 | return PathDiagnosticLocation(S, SMgr); |
| 270 | break; |
| 271 | default: |
| 272 | break; |
| 273 | } |
| 274 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 275 | S = Parent; |
| 276 | } |
| 277 | |
| 278 | assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); |
| 279 | return PathDiagnosticLocation(S, SMgr); |
| 280 | } |
| 281 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 282 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 283 | // ScanNotableSymbols: closure-like callback for scanning Store bindings. |
| 284 | //===----------------------------------------------------------------------===// |
| 285 | |
| 286 | static const VarDecl* |
| 287 | GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N, |
| 288 | GRStateManager& VMgr, SVal X) { |
| 289 | |
| 290 | for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { |
| 291 | |
| 292 | ProgramPoint P = N->getLocation(); |
| 293 | |
| 294 | if (!isa<PostStmt>(P)) |
| 295 | continue; |
| 296 | |
| 297 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); |
| 298 | |
| 299 | if (!DR) |
| 300 | continue; |
| 301 | |
| 302 | SVal Y = VMgr.GetSVal(N->getState(), DR); |
| 303 | |
| 304 | if (X != Y) |
| 305 | continue; |
| 306 | |
| 307 | VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 308 | |
| 309 | if (!VD) |
| 310 | continue; |
| 311 | |
| 312 | return VD; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | namespace { |
| 319 | class VISIBILITY_HIDDEN NotableSymbolHandler |
| 320 | : public StoreManager::BindingsHandler { |
| 321 | |
| 322 | SymbolRef Sym; |
| 323 | const GRState* PrevSt; |
| 324 | const Stmt* S; |
| 325 | GRStateManager& VMgr; |
| 326 | const ExplodedNode<GRState>* Pred; |
| 327 | PathDiagnostic& PD; |
| 328 | BugReporter& BR; |
| 329 | |
| 330 | public: |
| 331 | |
| 332 | NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s, |
| 333 | GRStateManager& vmgr, const ExplodedNode<GRState>* pred, |
| 334 | PathDiagnostic& pd, BugReporter& br) |
| 335 | : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {} |
| 336 | |
| 337 | bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, |
| 338 | SVal V) { |
| 339 | |
| 340 | SymbolRef ScanSym = V.getAsSymbol(); |
| 341 | |
| 342 | if (ScanSym != Sym) |
| 343 | return true; |
| 344 | |
| 345 | // Check if the previous state has this binding. |
| 346 | SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R)); |
| 347 | |
| 348 | if (X == V) // Same binding? |
| 349 | return true; |
| 350 | |
| 351 | // Different binding. Only handle assignments for now. We don't pull |
| 352 | // this check out of the loop because we will eventually handle other |
| 353 | // cases. |
| 354 | |
| 355 | VarDecl *VD = 0; |
| 356 | |
| 357 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 358 | if (!B->isAssignmentOp()) |
| 359 | return true; |
| 360 | |
| 361 | // What variable did we assign to? |
| 362 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); |
| 363 | |
| 364 | if (!DR) |
| 365 | return true; |
| 366 | |
| 367 | VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 368 | } |
| 369 | else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
| 370 | // FIXME: Eventually CFGs won't have DeclStmts. Right now we |
| 371 | // assume that each DeclStmt has a single Decl. This invariant |
| 372 | // holds by contruction in the CFG. |
| 373 | VD = dyn_cast<VarDecl>(*DS->decl_begin()); |
| 374 | } |
| 375 | |
| 376 | if (!VD) |
| 377 | return true; |
| 378 | |
| 379 | // What is the most recently referenced variable with this binding? |
| 380 | const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); |
| 381 | |
| 382 | if (!MostRecent) |
| 383 | return true; |
| 384 | |
| 385 | // Create the diagnostic. |
| 386 | FullSourceLoc L(S->getLocStart(), BR.getSourceManager()); |
| 387 | |
| 388 | if (Loc::IsLocType(VD->getType())) { |
| 389 | std::string msg = "'" + std::string(VD->getNameAsString()) + |
| 390 | "' now aliases '" + MostRecent->getNameAsString() + "'"; |
| 391 | |
| 392 | PD.push_front(new PathDiagnosticEventPiece(L, msg)); |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | }; |
| 398 | } |
| 399 | |
| 400 | static void HandleNotableSymbol(const ExplodedNode<GRState>* N, |
| 401 | const Stmt* S, |
| 402 | SymbolRef Sym, BugReporter& BR, |
| 403 | PathDiagnostic& PD) { |
| 404 | |
| 405 | const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin(); |
| 406 | const GRState* PrevSt = Pred ? Pred->getState() : 0; |
| 407 | |
| 408 | if (!PrevSt) |
| 409 | return; |
| 410 | |
| 411 | // Look at the region bindings of the current state that map to the |
| 412 | // specified symbol. Are any of them not in the previous state? |
| 413 | GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); |
| 414 | NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); |
| 415 | cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); |
| 416 | } |
| 417 | |
| 418 | namespace { |
| 419 | class VISIBILITY_HIDDEN ScanNotableSymbols |
| 420 | : public StoreManager::BindingsHandler { |
| 421 | |
| 422 | llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; |
| 423 | const ExplodedNode<GRState>* N; |
| 424 | Stmt* S; |
| 425 | GRBugReporter& BR; |
| 426 | PathDiagnostic& PD; |
| 427 | |
| 428 | public: |
| 429 | ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br, |
| 430 | PathDiagnostic& pd) |
| 431 | : N(n), S(s), BR(br), PD(pd) {} |
| 432 | |
| 433 | bool HandleBinding(StoreManager& SMgr, Store store, |
| 434 | const MemRegion* R, SVal V) { |
| 435 | |
| 436 | SymbolRef ScanSym = V.getAsSymbol(); |
| 437 | |
| 438 | if (!ScanSym) |
| 439 | return true; |
| 440 | |
| 441 | if (!BR.isNotable(ScanSym)) |
| 442 | return true; |
| 443 | |
| 444 | if (AlreadyProcessed.count(ScanSym)) |
| 445 | return true; |
| 446 | |
| 447 | AlreadyProcessed.insert(ScanSym); |
| 448 | |
| 449 | HandleNotableSymbol(N, S, ScanSym, BR, PD); |
| 450 | return true; |
| 451 | } |
| 452 | }; |
| 453 | } // end anonymous namespace |
| 454 | |
| 455 | //===----------------------------------------------------------------------===// |
| 456 | // "Minimal" path diagnostic generation algorithm. |
| 457 | //===----------------------------------------------------------------------===// |
| 458 | |
| 459 | static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, |
| 460 | PathDiagnosticBuilder &PDB, |
| 461 | const ExplodedNode<GRState> *N) { |
| 462 | ASTContext& Ctx = PDB.getContext(); |
| 463 | SourceManager& SMgr = PDB.getSourceManager(); |
| 464 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 465 | ? NULL : *(N->pred_begin()); |
| 466 | while (NextNode) { |
| 467 | N = NextNode; |
| 468 | NextNode = GetPredecessorNode(N); |
| 469 | |
| 470 | ProgramPoint P = N->getLocation(); |
| 471 | |
| 472 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 473 | CFGBlock* Src = BE->getSrc(); |
| 474 | CFGBlock* Dst = BE->getDst(); |
| 475 | Stmt* T = Src->getTerminator(); |
| 476 | |
| 477 | if (!T) |
| 478 | continue; |
| 479 | |
| 480 | FullSourceLoc Start(T->getLocStart(), SMgr); |
| 481 | |
| 482 | switch (T->getStmtClass()) { |
| 483 | default: |
| 484 | break; |
| 485 | |
| 486 | case Stmt::GotoStmtClass: |
| 487 | case Stmt::IndirectGotoStmtClass: { |
| 488 | Stmt* S = GetNextStmt(N); |
| 489 | |
| 490 | if (!S) |
| 491 | continue; |
| 492 | |
| 493 | std::string sbuf; |
| 494 | llvm::raw_string_ostream os(sbuf); |
| 495 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
| 496 | |
| 497 | os << "Control jumps to line " |
| 498 | << End.asLocation().getInstantiationLineNumber(); |
| 499 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 500 | os.str())); |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | case Stmt::SwitchStmtClass: { |
| 505 | // Figure out what case arm we took. |
| 506 | std::string sbuf; |
| 507 | llvm::raw_string_ostream os(sbuf); |
| 508 | |
| 509 | if (Stmt* S = Dst->getLabel()) { |
| 510 | PathDiagnosticLocation End(S, SMgr); |
| 511 | |
| 512 | switch (S->getStmtClass()) { |
| 513 | default: |
| 514 | os << "No cases match in the switch statement. " |
| 515 | "Control jumps to line " |
| 516 | << End.asLocation().getInstantiationLineNumber(); |
| 517 | break; |
| 518 | case Stmt::DefaultStmtClass: |
| 519 | os << "Control jumps to the 'default' case at line " |
| 520 | << End.asLocation().getInstantiationLineNumber(); |
| 521 | break; |
| 522 | |
| 523 | case Stmt::CaseStmtClass: { |
| 524 | os << "Control jumps to 'case "; |
| 525 | CaseStmt* Case = cast<CaseStmt>(S); |
| 526 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
| 527 | |
| 528 | // Determine if it is an enum. |
| 529 | bool GetRawInt = true; |
| 530 | |
| 531 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 532 | // FIXME: Maybe this should be an assertion. Are there cases |
| 533 | // were it is not an EnumConstantDecl? |
| 534 | EnumConstantDecl* D = |
| 535 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 536 | |
| 537 | if (D) { |
| 538 | GetRawInt = false; |
| 539 | os << D->getNameAsString(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (GetRawInt) { |
| 544 | |
| 545 | // Not an enum. |
| 546 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 547 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 548 | llvm::APSInt V(bits, false); |
| 549 | |
| 550 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 551 | assert (false && "Case condition must be constant."); |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | os << V; |
| 556 | } |
| 557 | |
| 558 | os << ":' at line " |
| 559 | << End.asLocation().getInstantiationLineNumber(); |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 564 | os.str())); |
| 565 | } |
| 566 | else { |
| 567 | os << "'Default' branch taken. "; |
| 568 | const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); |
| 569 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 570 | os.str())); |
| 571 | } |
| 572 | |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | case Stmt::BreakStmtClass: |
| 577 | case Stmt::ContinueStmtClass: { |
| 578 | std::string sbuf; |
| 579 | llvm::raw_string_ostream os(sbuf); |
| 580 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 581 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 582 | os.str())); |
| 583 | break; |
| 584 | } |
| 585 | |
| 586 | // Determine control-flow for ternary '?'. |
| 587 | case Stmt::ConditionalOperatorClass: { |
| 588 | std::string sbuf; |
| 589 | llvm::raw_string_ostream os(sbuf); |
| 590 | os << "'?' condition is "; |
| 591 | |
| 592 | if (*(Src->succ_begin()+1) == Dst) |
| 593 | os << "false"; |
| 594 | else |
| 595 | os << "true"; |
| 596 | |
| 597 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 598 | |
| 599 | if (const Stmt *S = End.asStmt()) |
| 600 | End = PDB.getEnclosingStmtLocation(S); |
| 601 | |
| 602 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 603 | os.str())); |
| 604 | break; |
| 605 | } |
| 606 | |
| 607 | // Determine control-flow for short-circuited '&&' and '||'. |
| 608 | case Stmt::BinaryOperatorClass: { |
| 609 | if (!PDB.supportsLogicalOpControlFlow()) |
| 610 | break; |
| 611 | |
| 612 | BinaryOperator *B = cast<BinaryOperator>(T); |
| 613 | std::string sbuf; |
| 614 | llvm::raw_string_ostream os(sbuf); |
| 615 | os << "Left side of '"; |
| 616 | |
| 617 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 618 | os << "&&" << "' is "; |
| 619 | |
| 620 | if (*(Src->succ_begin()+1) == Dst) { |
| 621 | os << "false"; |
| 622 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 623 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 624 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 625 | os.str())); |
| 626 | } |
| 627 | else { |
| 628 | os << "true"; |
| 629 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 630 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 631 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 632 | os.str())); |
| 633 | } |
| 634 | } |
| 635 | else { |
| 636 | assert(B->getOpcode() == BinaryOperator::LOr); |
| 637 | os << "||" << "' is "; |
| 638 | |
| 639 | if (*(Src->succ_begin()+1) == Dst) { |
| 640 | os << "false"; |
| 641 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 642 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 643 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 644 | os.str())); |
| 645 | } |
| 646 | else { |
| 647 | os << "true"; |
| 648 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 649 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 650 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 651 | os.str())); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | break; |
| 656 | } |
| 657 | |
| 658 | case Stmt::DoStmtClass: { |
| 659 | if (*(Src->succ_begin()) == Dst) { |
| 660 | std::string sbuf; |
| 661 | llvm::raw_string_ostream os(sbuf); |
| 662 | |
| 663 | os << "Loop condition is true. "; |
| 664 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 665 | |
| 666 | if (const Stmt *S = End.asStmt()) |
| 667 | End = PDB.getEnclosingStmtLocation(S); |
| 668 | |
| 669 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 670 | os.str())); |
| 671 | } |
| 672 | else { |
| 673 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 674 | |
| 675 | if (const Stmt *S = End.asStmt()) |
| 676 | End = PDB.getEnclosingStmtLocation(S); |
| 677 | |
| 678 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 679 | "Loop condition is false. Exiting loop")); |
| 680 | } |
| 681 | |
| 682 | break; |
| 683 | } |
| 684 | |
| 685 | case Stmt::WhileStmtClass: |
| 686 | case Stmt::ForStmtClass: { |
| 687 | if (*(Src->succ_begin()+1) == Dst) { |
| 688 | std::string sbuf; |
| 689 | llvm::raw_string_ostream os(sbuf); |
| 690 | |
| 691 | os << "Loop condition is false. "; |
| 692 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 693 | if (const Stmt *S = End.asStmt()) |
| 694 | End = PDB.getEnclosingStmtLocation(S); |
| 695 | |
| 696 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 697 | os.str())); |
| 698 | } |
| 699 | else { |
| 700 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 701 | if (const Stmt *S = End.asStmt()) |
| 702 | End = PDB.getEnclosingStmtLocation(S); |
| 703 | |
| 704 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 705 | "Loop condition is true. Entering loop body")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | case Stmt::IfStmtClass: { |
| 712 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 713 | |
| 714 | if (const Stmt *S = End.asStmt()) |
| 715 | End = PDB.getEnclosingStmtLocation(S); |
| 716 | |
| 717 | if (*(Src->succ_begin()+1) == Dst) |
| 718 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 719 | "Taking false branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 720 | else |
| 721 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 722 | "Taking true branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 723 | |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if (PathDiagnosticPiece* p = |
| 730 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 731 | PDB.getBugReporter(), |
| 732 | PDB.getNodeMapClosure())) { |
| 733 | PD.push_front(p); |
| 734 | } |
| 735 | |
| 736 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 737 | // Scan the region bindings, and see if a "notable" symbol has a new |
| 738 | // lval binding. |
| 739 | ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD); |
| 740 | PDB.getStateManager().iterBindings(N->getState(), SNS); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 746 | // "Extensive" PathDiagnostic generation. |
| 747 | //===----------------------------------------------------------------------===// |
| 748 | |
| 749 | static bool IsControlFlowExpr(const Stmt *S) { |
| 750 | const Expr *E = dyn_cast<Expr>(S); |
| 751 | |
| 752 | if (!E) |
| 753 | return false; |
| 754 | |
| 755 | E = E->IgnoreParenCasts(); |
| 756 | |
| 757 | if (isa<ConditionalOperator>(E)) |
| 758 | return true; |
| 759 | |
| 760 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E)) |
| 761 | if (B->isLogicalOp()) |
| 762 | return true; |
| 763 | |
| 764 | return false; |
| 765 | } |
| 766 | |
| 767 | static void GenExtAddEdge(PathDiagnostic& PD, |
| 768 | PathDiagnosticBuilder &PDB, |
| 769 | PathDiagnosticLocation NewLoc, |
| 770 | PathDiagnosticLocation &PrevLoc, |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 771 | bool allowBlockJump = false) { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 772 | |
| 773 | if (const Stmt *S = NewLoc.asStmt()) { |
| 774 | if (IsControlFlowExpr(S)) |
| 775 | return; |
| 776 | } |
| 777 | |
| 778 | |
| 779 | if (!PrevLoc.isValid()) { |
| 780 | PrevLoc = NewLoc; |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | if (NewLoc == PrevLoc) |
| 785 | return; |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 786 | |
Ted Kremenek | 0dc65be | 2009-04-01 19:43:28 +0000 | [diff] [blame] | 787 | // Are we jumping between statements within the same compound statement? |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 788 | if (!allowBlockJump) |
| 789 | if (const Stmt *PS = PrevLoc.asStmt()) |
| 790 | if (const Stmt *NS = NewLoc.asStmt()) { |
| 791 | const Stmt *parentPS = PDB.getParent(PS); |
| 792 | if (isa<CompoundStmt>(parentPS) && parentPS == PDB.getParent(NS)) |
| 793 | return; |
| 794 | } |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 795 | |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame^] | 796 | // Add an extra edge when jumping between contexts. |
| 797 | while (1) { |
| 798 | if (const Stmt *PS = PrevLoc.asStmt()) |
| 799 | if (const Stmt *NS = NewLoc.asStmt()) { |
| 800 | PathDiagnosticLocation X = PDB.getEnclosingStmtLocation(PS); |
| 801 | // FIXME: We need a version of getParent that ignores '()' and casts. |
| 802 | const Stmt *parentX = PDB.getParent(X.asStmt()); |
| 803 | |
| 804 | const PathDiagnosticLocation &Y = PDB.getEnclosingStmtLocation(NS); |
| 805 | // FIXME: We need a version of getParent that ignores '()' and casts. |
| 806 | const Stmt *parentY = PDB.getParent(Y.asStmt()); |
| 807 | |
| 808 | if (IsControlFlowExpr(parentX)) { |
| 809 | if (IsControlFlowExpr(parentY) && parentX == parentY) { |
| 810 | break; |
| 811 | } |
| 812 | else { |
| 813 | const PathDiagnosticLocation &W = |
| 814 | PDB.getEnclosingStmtLocation(PDB.getParent(parentX)); |
| 815 | |
| 816 | if (W != Y) X = W; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | if (X != Y && PrevLoc.asLocation() != X.asLocation()) { |
| 821 | PD.push_front(new PathDiagnosticControlFlowPiece(X, PrevLoc)); |
| 822 | PrevLoc = X; |
| 823 | } |
| 824 | } |
| 825 | break; |
| 826 | } |
| 827 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 828 | PD.push_front(new PathDiagnosticControlFlowPiece(NewLoc, PrevLoc)); |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 829 | PrevLoc = NewLoc; |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static bool IsNestedDeclStmt(const Stmt *S, ParentMap &PM) { |
| 833 | const DeclStmt *DS = dyn_cast<DeclStmt>(S); |
| 834 | |
| 835 | if (!DS) |
| 836 | return false; |
| 837 | |
| 838 | const Stmt *Parent = PM.getParent(DS); |
| 839 | if (!Parent) |
| 840 | return false; |
| 841 | |
| 842 | if (const ForStmt *FS = dyn_cast<ForStmt>(Parent)) |
| 843 | return FS->getInit() == DS; |
| 844 | |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 845 | // FIXME: In the future IfStmt/WhileStmt may contain DeclStmts in their |
| 846 | // condition. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 847 | |
| 848 | return false; |
| 849 | } |
| 850 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 851 | static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD, |
| 852 | PathDiagnosticBuilder &PDB, |
| 853 | const ExplodedNode<GRState> *N) { |
| 854 | |
| 855 | SourceManager& SMgr = PDB.getSourceManager(); |
| 856 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 857 | ? NULL : *(N->pred_begin()); |
| 858 | |
| 859 | PathDiagnosticLocation PrevLoc; |
| 860 | |
| 861 | while (NextNode) { |
| 862 | N = NextNode; |
| 863 | NextNode = GetPredecessorNode(N); |
| 864 | ProgramPoint P = N->getLocation(); |
| 865 | |
| 866 | // Block edges. |
| 867 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 868 | const CFGBlock &Blk = *BE->getSrc(); |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 869 | |
| 870 | // Add a special edge for the entrance into the function/method. |
| 871 | if (&Blk == &PDB.getCFG().getEntry()) { |
| 872 | FullSourceLoc L = FullSourceLoc(PDB.getCodeDecl().getLocation(), SMgr); |
| 873 | GenExtAddEdge(PD, PDB, L.getSpellingLoc(), PrevLoc); |
| 874 | continue; |
| 875 | } |
| 876 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 877 | if (const Stmt *Term = Blk.getTerminator()) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 878 | const Stmt *Cond = Blk.getTerminatorCondition(); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 879 | if (!Cond || !IsControlFlowExpr(Cond)) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 880 | // For terminators that are control-flow expressions like '&&', '?', |
| 881 | // have the condition be the anchor point for the control-flow edge |
| 882 | // instead of the terminator. |
| 883 | const Stmt *X = isa<Expr>(Term) ? (Cond ? Cond : Term) : Term; |
| 884 | GenExtAddEdge(PD, PDB, PathDiagnosticLocation(X, SMgr), PrevLoc,true); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 885 | continue; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // Only handle blocks with more than 1 statement here, as the blocks |
| 890 | // with one statement are handled at BlockEntrances. |
| 891 | if (Blk.size() > 1) { |
| 892 | const Stmt *S = *Blk.rbegin(); |
| 893 | |
| 894 | // We don't add control-flow edges for DeclStmt's that appear in |
| 895 | // the condition of if/while/for or are control-flow merge expressions. |
| 896 | if (!IsControlFlowExpr(S) && !IsNestedDeclStmt(S, PDB.getParentMap())) { |
| 897 | GenExtAddEdge(PD, PDB, PathDiagnosticLocation(S, SMgr), PrevLoc); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | continue; |
| 902 | } |
| 903 | |
| 904 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 905 | if (const Stmt* S = BE->getFirstStmt()) { |
| 906 | if (!IsControlFlowExpr(S) && !IsNestedDeclStmt(S, PDB.getParentMap())) { |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 907 | if (PrevLoc.isValid()) { |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 908 | // Are we jumping within the same enclosing statement? |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 909 | if (PDB.getEnclosingStmtLocation(S) == |
| 910 | PDB.getEnclosingStmtLocation(PrevLoc)) |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 911 | continue; |
| 912 | } |
| 913 | |
| 914 | GenExtAddEdge(PD, PDB, PDB.getEnclosingStmtLocation(S), PrevLoc); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | continue; |
| 919 | } |
| 920 | |
| 921 | PathDiagnosticPiece* p = |
| 922 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 923 | PDB.getBugReporter(), PDB.getNodeMapClosure()); |
| 924 | |
| 925 | if (p) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 926 | GenExtAddEdge(PD, PDB, p->getLocation(), PrevLoc, true); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 927 | PD.push_front(p); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 933 | // Methods for BugType and subclasses. |
| 934 | //===----------------------------------------------------------------------===// |
| 935 | BugType::~BugType() {} |
| 936 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 937 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 938 | //===----------------------------------------------------------------------===// |
| 939 | // Methods for BugReport and subclasses. |
| 940 | //===----------------------------------------------------------------------===// |
| 941 | BugReport::~BugReport() {} |
| 942 | RangedBugReport::~RangedBugReport() {} |
| 943 | |
| 944 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 945 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 946 | Stmt *S = NULL; |
| 947 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 948 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 949 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 950 | } |
| 951 | if (!S) S = GetStmt(ProgP); |
| 952 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 953 | return S; |
| 954 | } |
| 955 | |
| 956 | PathDiagnosticPiece* |
| 957 | BugReport::getEndPath(BugReporter& BR, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 958 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 959 | |
| 960 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 961 | |
| 962 | if (!S) |
| 963 | return NULL; |
| 964 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 965 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 966 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 967 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 968 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 969 | getRanges(BR, Beg, End); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 970 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 971 | for (; Beg != End; ++Beg) |
| 972 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 973 | |
| 974 | return P; |
| 975 | } |
| 976 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 977 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 978 | const SourceRange*& end) { |
| 979 | |
| 980 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 981 | R = E->getSourceRange(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 982 | assert(R.isValid()); |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 983 | beg = &R; |
| 984 | end = beg+1; |
| 985 | } |
| 986 | else |
| 987 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 990 | SourceLocation BugReport::getLocation() const { |
| 991 | if (EndNode) |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 992 | if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) { |
| 993 | // For member expressions, return the location of the '.' or '->'. |
| 994 | if (MemberExpr* ME = dyn_cast<MemberExpr>(S)) |
| 995 | return ME->getMemberLoc(); |
| 996 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 997 | return S->getLocStart(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 998 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 999 | |
| 1000 | return FullSourceLoc(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1003 | PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N, |
| 1004 | const ExplodedNode<GRState>* PrevN, |
| 1005 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1006 | BugReporter& BR, |
| 1007 | NodeResolver &NR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 1008 | return NULL; |
| 1009 | } |
| 1010 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1011 | //===----------------------------------------------------------------------===// |
| 1012 | // Methods for BugReporter and subclasses. |
| 1013 | //===----------------------------------------------------------------------===// |
| 1014 | |
| 1015 | BugReportEquivClass::~BugReportEquivClass() { |
| 1016 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
| 1017 | } |
| 1018 | |
| 1019 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 1020 | BugReporterData::~BugReporterData() {} |
| 1021 | |
| 1022 | ExplodedGraph<GRState>& |
| 1023 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 1024 | |
| 1025 | GRStateManager& |
| 1026 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 1027 | |
| 1028 | BugReporter::~BugReporter() { FlushReports(); } |
| 1029 | |
| 1030 | void BugReporter::FlushReports() { |
| 1031 | if (BugTypes.isEmpty()) |
| 1032 | return; |
| 1033 | |
| 1034 | // First flush the warnings for each BugType. This may end up creating new |
| 1035 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 1036 | // structure, we do not need to worry about the iterators being invalidated. |
| 1037 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 1038 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 1039 | |
| 1040 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 1041 | // with new BugType objects and new warnings. |
| 1042 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 1043 | BugType *BT = const_cast<BugType*>(*I); |
| 1044 | |
| 1045 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 1046 | SetTy& EQClasses = BT->EQClasses; |
| 1047 | |
| 1048 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 1049 | BugReportEquivClass& EQ = *EI; |
| 1050 | FlushReport(EQ); |
| 1051 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1053 | // Delete the BugType object. This will also delete the equivalence |
| 1054 | // classes. |
| 1055 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1056 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1057 | |
| 1058 | // Remove all references to the BugType objects. |
| 1059 | BugTypes = F.GetEmptySet(); |
| 1060 | } |
| 1061 | |
| 1062 | //===----------------------------------------------------------------------===// |
| 1063 | // PathDiagnostics generation. |
| 1064 | //===----------------------------------------------------------------------===// |
| 1065 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1066 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1067 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 1068 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 1069 | const ExplodedNode<GRState>** NStart, |
| 1070 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1071 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1072 | // Create the trimmed graph. It will contain the shortest paths from the |
| 1073 | // error nodes to the root. In the new graph we should only have one |
| 1074 | // error node unless there are two or more error nodes with the same minimum |
| 1075 | // path length. |
| 1076 | ExplodedGraph<GRState>* GTrim; |
| 1077 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1078 | |
| 1079 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 1080 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1081 | |
| 1082 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 1083 | // released when this function exists. |
| 1084 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 1085 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 1086 | |
| 1087 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 1088 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 1089 | // in the new graph. |
| 1090 | const ExplodedNode<GRState>* N = 0; |
| 1091 | unsigned NodeIndex = 0; |
| 1092 | |
| 1093 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
| 1094 | if ((N = NMap->getMappedNode(*I))) { |
| 1095 | NodeIndex = (I - NStart) / sizeof(*I); |
| 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | assert(N && "No error node found in the trimmed graph."); |
| 1100 | |
| 1101 | // Create a new (third!) graph with a single path. This is the graph |
| 1102 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1103 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1104 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 1105 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1106 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1107 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1108 | // to the root node, and then construct a new graph that contains only |
| 1109 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1110 | llvm::DenseMap<const void*,unsigned> Visited; |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1111 | std::queue<const ExplodedNode<GRState>*> WS; |
| 1112 | WS.push(N); |
| 1113 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1114 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1115 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1116 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1117 | while (!WS.empty()) { |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1118 | const ExplodedNode<GRState>* Node = WS.front(); |
| 1119 | WS.pop(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1120 | |
| 1121 | if (Visited.find(Node) != Visited.end()) |
| 1122 | continue; |
| 1123 | |
| 1124 | Visited[Node] = cnt++; |
| 1125 | |
| 1126 | if (Node->pred_empty()) { |
| 1127 | Root = Node; |
| 1128 | break; |
| 1129 | } |
| 1130 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1131 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1132 | E=Node->pred_end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1133 | WS.push(*I); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1134 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1135 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1136 | assert (Root); |
| 1137 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1138 | // Now walk from the root down the BFS path, always taking the successor |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1139 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1140 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1141 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1142 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1143 | for ( N = Root ;;) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1144 | // Lookup the number associated with the current node. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1145 | llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1146 | assert (I != Visited.end()); |
| 1147 | |
| 1148 | // Create the equivalent node in the new graph with the same state |
| 1149 | // and location. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1150 | ExplodedNode<GRState>* NewN = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1151 | GNew->getNode(N->getLocation(), N->getState()); |
| 1152 | |
| 1153 | // Store the mapping to the original node. |
| 1154 | llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); |
| 1155 | assert(IMitr != InverseMap.end() && "No mapping to original node."); |
| 1156 | (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1157 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1158 | // Link up the new node with the previous node. |
| 1159 | if (Last) |
| 1160 | NewN->addPredecessor(Last); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1161 | |
| 1162 | Last = NewN; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1163 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1164 | // Are we at the final node? |
| 1165 | if (I->second == 0) { |
| 1166 | First = NewN; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1167 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1168 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1169 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1170 | // Find the next successor node. We choose the node that is marked |
| 1171 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1172 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 1173 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1174 | N = 0; |
| 1175 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1176 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1177 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1178 | I = Visited.find(*SI); |
| 1179 | |
| 1180 | if (I == Visited.end()) |
| 1181 | continue; |
| 1182 | |
| 1183 | if (!N || I->second < MinVal) { |
| 1184 | N = *SI; |
| 1185 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1186 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1187 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1188 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1189 | assert (N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1190 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1191 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1192 | assert (First); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1193 | return std::make_pair(std::make_pair(GNew, BM), |
| 1194 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1197 | /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object |
| 1198 | /// and collapses PathDiagosticPieces that are expanded by macros. |
| 1199 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { |
| 1200 | typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > |
| 1201 | MacroStackTy; |
| 1202 | |
| 1203 | typedef std::vector<PathDiagnosticPiece*> |
| 1204 | PiecesTy; |
| 1205 | |
| 1206 | MacroStackTy MacroStack; |
| 1207 | PiecesTy Pieces; |
| 1208 | |
| 1209 | for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { |
| 1210 | // Get the location of the PathDiagnosticPiece. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1211 | const FullSourceLoc Loc = I->getLocation().asLocation(); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1212 | |
| 1213 | // Determine the instantiation location, which is the location we group |
| 1214 | // related PathDiagnosticPieces. |
| 1215 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
| 1216 | SM.getInstantiationLoc(Loc) : |
| 1217 | SourceLocation(); |
| 1218 | |
| 1219 | if (Loc.isFileID()) { |
| 1220 | MacroStack.clear(); |
| 1221 | Pieces.push_back(&*I); |
| 1222 | continue; |
| 1223 | } |
| 1224 | |
| 1225 | assert(Loc.isMacroID()); |
| 1226 | |
| 1227 | // Is the PathDiagnosticPiece within the same macro group? |
| 1228 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
| 1229 | MacroStack.back().first->push_back(&*I); |
| 1230 | continue; |
| 1231 | } |
| 1232 | |
| 1233 | // We aren't in the same group. Are we descending into a new macro |
| 1234 | // or are part of an old one? |
| 1235 | PathDiagnosticMacroPiece *MacroGroup = 0; |
| 1236 | |
| 1237 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
| 1238 | SM.getInstantiationLoc(Loc) : |
| 1239 | SourceLocation(); |
| 1240 | |
| 1241 | // Walk the entire macro stack. |
| 1242 | while (!MacroStack.empty()) { |
| 1243 | if (InstantiationLoc == MacroStack.back().second) { |
| 1244 | MacroGroup = MacroStack.back().first; |
| 1245 | break; |
| 1246 | } |
| 1247 | |
| 1248 | if (ParentInstantiationLoc == MacroStack.back().second) { |
| 1249 | MacroGroup = MacroStack.back().first; |
| 1250 | break; |
| 1251 | } |
| 1252 | |
| 1253 | MacroStack.pop_back(); |
| 1254 | } |
| 1255 | |
| 1256 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
| 1257 | // Create a new macro group and add it to the stack. |
| 1258 | PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc); |
| 1259 | |
| 1260 | if (MacroGroup) |
| 1261 | MacroGroup->push_back(NewGroup); |
| 1262 | else { |
| 1263 | assert(InstantiationLoc.isFileID()); |
| 1264 | Pieces.push_back(NewGroup); |
| 1265 | } |
| 1266 | |
| 1267 | MacroGroup = NewGroup; |
| 1268 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
| 1269 | } |
| 1270 | |
| 1271 | // Finally, add the PathDiagnosticPiece to the group. |
| 1272 | MacroGroup->push_back(&*I); |
| 1273 | } |
| 1274 | |
| 1275 | // Now take the pieces and construct a new PathDiagnostic. |
| 1276 | PD.resetPath(false); |
| 1277 | |
| 1278 | for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { |
| 1279 | if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 1280 | if (!MP->containsEvent()) { |
| 1281 | delete MP; |
| 1282 | continue; |
| 1283 | } |
| 1284 | |
| 1285 | PD.push_back(*I); |
| 1286 | } |
| 1287 | } |
| 1288 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1289 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 1290 | BugReportEquivClass& EQ) { |
| 1291 | |
| 1292 | std::vector<const ExplodedNode<GRState>*> Nodes; |
| 1293 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1294 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 1295 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 1296 | if (N) Nodes.push_back(N); |
| 1297 | } |
| 1298 | |
| 1299 | if (Nodes.empty()) |
| 1300 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1301 | |
| 1302 | // Construct a new graph that contains only a single path from the error |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1303 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1304 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1305 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 1306 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1307 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1308 | // Find the BugReport with the original location. |
| 1309 | BugReport *R = 0; |
| 1310 | unsigned i = 0; |
| 1311 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 1312 | if (i == GPair.second.second) { R = *I; break; } |
| 1313 | |
| 1314 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1315 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1316 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 1317 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1318 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1319 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1320 | // Start building the path diagnostic... |
| 1321 | if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1322 | PD.push_back(Piece); |
| 1323 | else |
| 1324 | return; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1325 | |
| 1326 | PathDiagnosticBuilder PDB(*this, ReportGraph.get(), R, BackMap.get(), |
| 1327 | getStateManager().getCodeDecl(), |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 1328 | getPathDiagnosticClient()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1329 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1330 | switch (PDB.getGenerationScheme()) { |
| 1331 | case PathDiagnosticClient::Extensive: |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1332 | GenerateExtensivePathDiagnostic(PD,PDB, N); |
| 1333 | break; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1334 | case PathDiagnosticClient::Minimal: |
| 1335 | GenerateMinimalPathDiagnostic(PD, PDB, N); |
| 1336 | break; |
| 1337 | } |
| 1338 | |
| 1339 | // After constructing the full PathDiagnostic, do a pass over it to compact |
| 1340 | // PathDiagnosticPieces that occur within a macro. |
| 1341 | CompactPathDiagnostic(PD, PDB.getSourceManager()); |
| 1342 | } |
| 1343 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1344 | void BugReporter::Register(BugType *BT) { |
| 1345 | BugTypes = F.Add(BugTypes, BT); |
Ted Kremenek | 76d90c8 | 2008-05-16 18:33:14 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1348 | void BugReporter::EmitReport(BugReport* R) { |
| 1349 | // Compute the bug report's hash to determine its equivalence class. |
| 1350 | llvm::FoldingSetNodeID ID; |
| 1351 | R->Profile(ID); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1352 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1353 | // Lookup the equivance class. If there isn't one, create it. |
| 1354 | BugType& BT = R->getBugType(); |
| 1355 | Register(&BT); |
| 1356 | void *InsertPos; |
| 1357 | BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
| 1358 | |
| 1359 | if (!EQ) { |
| 1360 | EQ = new BugReportEquivClass(R); |
| 1361 | BT.EQClasses.InsertNode(EQ, InsertPos); |
| 1362 | } |
| 1363 | else |
| 1364 | EQ->AddReport(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1367 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
| 1368 | assert(!EQ.Reports.empty()); |
| 1369 | BugReport &R = **EQ.begin(); |
| 1370 | |
| 1371 | // FIXME: Make sure we use the 'R' for the path that was actually used. |
| 1372 | // Probably doesn't make a difference in practice. |
| 1373 | BugType& BT = R.getBugType(); |
| 1374 | |
| 1375 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(), |
| 1376 | R.getDescription(), |
| 1377 | BT.getCategory())); |
| 1378 | GeneratePathDiagnostic(*D.get(), EQ); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1379 | |
| 1380 | // Get the meta data. |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1381 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1382 | for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 1383 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1384 | // Emit a summary diagnostic to the regular Diagnostics engine. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 1385 | PathDiagnosticClient* PD = getPathDiagnosticClient(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1386 | const SourceRange *Beg = 0, *End = 0; |
| 1387 | R.getRanges(*this, Beg, End); |
| 1388 | Diagnostic& Diag = getDiagnostic(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1389 | FullSourceLoc L(R.getLocation(), getSourceManager()); |
Ted Kremenek | d90e708 | 2009-02-07 22:36:41 +0000 | [diff] [blame] | 1390 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 1391 | R.getDescription().c_str()); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1392 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1393 | switch (End-Beg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1394 | default: assert(0 && "Don't handle this many ranges yet!"); |
| 1395 | case 0: Diag.Report(L, ErrorDiag); break; |
| 1396 | case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break; |
| 1397 | case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break; |
| 1398 | case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break; |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 1399 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
| 1402 | if (!PD) |
| 1403 | return; |
| 1404 | |
| 1405 | if (D->empty()) { |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1406 | PathDiagnosticPiece* piece = |
| 1407 | new PathDiagnosticEventPiece(L, R.getDescription()); |
| 1408 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1409 | for ( ; Beg != End; ++Beg) piece->addRange(*Beg); |
| 1410 | D->push_back(piece); |
| 1411 | } |
| 1412 | |
| 1413 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1414 | } |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1415 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1416 | void BugReporter::EmitBasicReport(const char* name, const char* str, |
| 1417 | SourceLocation Loc, |
| 1418 | SourceRange* RBeg, unsigned NumRanges) { |
| 1419 | EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); |
| 1420 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1421 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1422 | void BugReporter::EmitBasicReport(const char* name, const char* category, |
| 1423 | const char* str, SourceLocation Loc, |
| 1424 | SourceRange* RBeg, unsigned NumRanges) { |
| 1425 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1426 | // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'. |
| 1427 | BugType *BT = new BugType(name, category); |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1428 | FullSourceLoc L = getContext().getFullLoc(Loc); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1429 | RangedBugReport *R = new DiagBugReport(*BT, str, L); |
| 1430 | for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); |
| 1431 | EmitReport(R); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1432 | } |