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 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 459 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM); |
| 460 | |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 461 | static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, |
| 462 | PathDiagnosticBuilder &PDB, |
| 463 | const ExplodedNode<GRState> *N) { |
| 464 | ASTContext& Ctx = PDB.getContext(); |
| 465 | SourceManager& SMgr = PDB.getSourceManager(); |
| 466 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 467 | ? NULL : *(N->pred_begin()); |
| 468 | while (NextNode) { |
| 469 | N = NextNode; |
| 470 | NextNode = GetPredecessorNode(N); |
| 471 | |
| 472 | ProgramPoint P = N->getLocation(); |
| 473 | |
| 474 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 475 | CFGBlock* Src = BE->getSrc(); |
| 476 | CFGBlock* Dst = BE->getDst(); |
| 477 | Stmt* T = Src->getTerminator(); |
| 478 | |
| 479 | if (!T) |
| 480 | continue; |
| 481 | |
| 482 | FullSourceLoc Start(T->getLocStart(), SMgr); |
| 483 | |
| 484 | switch (T->getStmtClass()) { |
| 485 | default: |
| 486 | break; |
| 487 | |
| 488 | case Stmt::GotoStmtClass: |
| 489 | case Stmt::IndirectGotoStmtClass: { |
| 490 | Stmt* S = GetNextStmt(N); |
| 491 | |
| 492 | if (!S) |
| 493 | continue; |
| 494 | |
| 495 | std::string sbuf; |
| 496 | llvm::raw_string_ostream os(sbuf); |
| 497 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
| 498 | |
| 499 | os << "Control jumps to line " |
| 500 | << End.asLocation().getInstantiationLineNumber(); |
| 501 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 502 | os.str())); |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | case Stmt::SwitchStmtClass: { |
| 507 | // Figure out what case arm we took. |
| 508 | std::string sbuf; |
| 509 | llvm::raw_string_ostream os(sbuf); |
| 510 | |
| 511 | if (Stmt* S = Dst->getLabel()) { |
| 512 | PathDiagnosticLocation End(S, SMgr); |
| 513 | |
| 514 | switch (S->getStmtClass()) { |
| 515 | default: |
| 516 | os << "No cases match in the switch statement. " |
| 517 | "Control jumps to line " |
| 518 | << End.asLocation().getInstantiationLineNumber(); |
| 519 | break; |
| 520 | case Stmt::DefaultStmtClass: |
| 521 | os << "Control jumps to the 'default' case at line " |
| 522 | << End.asLocation().getInstantiationLineNumber(); |
| 523 | break; |
| 524 | |
| 525 | case Stmt::CaseStmtClass: { |
| 526 | os << "Control jumps to 'case "; |
| 527 | CaseStmt* Case = cast<CaseStmt>(S); |
| 528 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
| 529 | |
| 530 | // Determine if it is an enum. |
| 531 | bool GetRawInt = true; |
| 532 | |
| 533 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 534 | // FIXME: Maybe this should be an assertion. Are there cases |
| 535 | // were it is not an EnumConstantDecl? |
| 536 | EnumConstantDecl* D = |
| 537 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 538 | |
| 539 | if (D) { |
| 540 | GetRawInt = false; |
| 541 | os << D->getNameAsString(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | if (GetRawInt) { |
| 546 | |
| 547 | // Not an enum. |
| 548 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 549 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 550 | llvm::APSInt V(bits, false); |
| 551 | |
| 552 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 553 | assert (false && "Case condition must be constant."); |
| 554 | continue; |
| 555 | } |
| 556 | |
| 557 | os << V; |
| 558 | } |
| 559 | |
| 560 | os << ":' at line " |
| 561 | << End.asLocation().getInstantiationLineNumber(); |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 566 | os.str())); |
| 567 | } |
| 568 | else { |
| 569 | os << "'Default' branch taken. "; |
| 570 | const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); |
| 571 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 572 | os.str())); |
| 573 | } |
| 574 | |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | case Stmt::BreakStmtClass: |
| 579 | case Stmt::ContinueStmtClass: { |
| 580 | std::string sbuf; |
| 581 | llvm::raw_string_ostream os(sbuf); |
| 582 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 583 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 584 | os.str())); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | // Determine control-flow for ternary '?'. |
| 589 | case Stmt::ConditionalOperatorClass: { |
| 590 | std::string sbuf; |
| 591 | llvm::raw_string_ostream os(sbuf); |
| 592 | os << "'?' condition is "; |
| 593 | |
| 594 | if (*(Src->succ_begin()+1) == Dst) |
| 595 | os << "false"; |
| 596 | else |
| 597 | os << "true"; |
| 598 | |
| 599 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 600 | |
| 601 | if (const Stmt *S = End.asStmt()) |
| 602 | End = PDB.getEnclosingStmtLocation(S); |
| 603 | |
| 604 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 605 | os.str())); |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | // Determine control-flow for short-circuited '&&' and '||'. |
| 610 | case Stmt::BinaryOperatorClass: { |
| 611 | if (!PDB.supportsLogicalOpControlFlow()) |
| 612 | break; |
| 613 | |
| 614 | BinaryOperator *B = cast<BinaryOperator>(T); |
| 615 | std::string sbuf; |
| 616 | llvm::raw_string_ostream os(sbuf); |
| 617 | os << "Left side of '"; |
| 618 | |
| 619 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 620 | os << "&&" << "' is "; |
| 621 | |
| 622 | if (*(Src->succ_begin()+1) == Dst) { |
| 623 | os << "false"; |
| 624 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 625 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 626 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 627 | os.str())); |
| 628 | } |
| 629 | else { |
| 630 | os << "true"; |
| 631 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 632 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 633 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 634 | os.str())); |
| 635 | } |
| 636 | } |
| 637 | else { |
| 638 | assert(B->getOpcode() == BinaryOperator::LOr); |
| 639 | os << "||" << "' is "; |
| 640 | |
| 641 | if (*(Src->succ_begin()+1) == Dst) { |
| 642 | os << "false"; |
| 643 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 644 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 645 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 646 | os.str())); |
| 647 | } |
| 648 | else { |
| 649 | os << "true"; |
| 650 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 651 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 652 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 653 | os.str())); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | case Stmt::DoStmtClass: { |
| 661 | if (*(Src->succ_begin()) == Dst) { |
| 662 | std::string sbuf; |
| 663 | llvm::raw_string_ostream os(sbuf); |
| 664 | |
| 665 | os << "Loop condition is true. "; |
| 666 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 667 | |
| 668 | if (const Stmt *S = End.asStmt()) |
| 669 | End = PDB.getEnclosingStmtLocation(S); |
| 670 | |
| 671 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 672 | os.str())); |
| 673 | } |
| 674 | else { |
| 675 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 676 | |
| 677 | if (const Stmt *S = End.asStmt()) |
| 678 | End = PDB.getEnclosingStmtLocation(S); |
| 679 | |
| 680 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 681 | "Loop condition is false. Exiting loop")); |
| 682 | } |
| 683 | |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | case Stmt::WhileStmtClass: |
| 688 | case Stmt::ForStmtClass: { |
| 689 | if (*(Src->succ_begin()+1) == Dst) { |
| 690 | std::string sbuf; |
| 691 | llvm::raw_string_ostream os(sbuf); |
| 692 | |
| 693 | os << "Loop condition is false. "; |
| 694 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 695 | if (const Stmt *S = End.asStmt()) |
| 696 | End = PDB.getEnclosingStmtLocation(S); |
| 697 | |
| 698 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 699 | os.str())); |
| 700 | } |
| 701 | else { |
| 702 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 703 | if (const Stmt *S = End.asStmt()) |
| 704 | End = PDB.getEnclosingStmtLocation(S); |
| 705 | |
| 706 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 707 | "Loop condition is true. Entering loop body")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | break; |
| 711 | } |
| 712 | |
| 713 | case Stmt::IfStmtClass: { |
| 714 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 715 | |
| 716 | if (const Stmt *S = End.asStmt()) |
| 717 | End = PDB.getEnclosingStmtLocation(S); |
| 718 | |
| 719 | if (*(Src->succ_begin()+1) == Dst) |
| 720 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 721 | "Taking false branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 722 | else |
| 723 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 724 | "Taking true branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 725 | |
| 726 | break; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if (PathDiagnosticPiece* p = |
| 732 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 733 | PDB.getBugReporter(), |
| 734 | PDB.getNodeMapClosure())) { |
| 735 | PD.push_front(p); |
| 736 | } |
| 737 | |
| 738 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 739 | // Scan the region bindings, and see if a "notable" symbol has a new |
| 740 | // lval binding. |
| 741 | ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD); |
| 742 | PDB.getStateManager().iterBindings(N->getState(), SNS); |
| 743 | } |
| 744 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 745 | |
| 746 | // After constructing the full PathDiagnostic, do a pass over it to compact |
| 747 | // PathDiagnosticPieces that occur within a macro. |
| 748 | CompactPathDiagnostic(PD, PDB.getSourceManager()); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 752 | // "Extensive" PathDiagnostic generation. |
| 753 | //===----------------------------------------------------------------------===// |
| 754 | |
| 755 | static bool IsControlFlowExpr(const Stmt *S) { |
| 756 | const Expr *E = dyn_cast<Expr>(S); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 757 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 758 | if (!E) |
| 759 | return false; |
| 760 | |
| 761 | E = E->IgnoreParenCasts(); |
| 762 | |
| 763 | if (isa<ConditionalOperator>(E)) |
| 764 | return true; |
| 765 | |
| 766 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E)) |
| 767 | if (B->isLogicalOp()) |
| 768 | return true; |
| 769 | |
| 770 | return false; |
| 771 | } |
| 772 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 773 | #if 1 |
| 774 | |
| 775 | namespace { |
| 776 | class VISIBILITY_HIDDEN EdgeBuilder { |
| 777 | std::vector<PathDiagnosticLocation> CLocs; |
| 778 | typedef std::vector<PathDiagnosticLocation>::iterator iterator; |
| 779 | PathDiagnostic &PD; |
| 780 | PathDiagnosticBuilder &PDB; |
| 781 | PathDiagnosticLocation PrevLoc; |
| 782 | |
| 783 | bool containsLocation(const PathDiagnosticLocation &Container, |
| 784 | const PathDiagnosticLocation &Containee); |
| 785 | |
| 786 | PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L); |
| 787 | void rawAddEdge(PathDiagnosticLocation NewLoc); |
| 788 | |
| 789 | void popLocation() { |
| 790 | rawAddEdge(CLocs.back()); |
| 791 | CLocs.pop_back(); |
| 792 | } |
| 793 | |
| 794 | PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L); |
| 795 | |
| 796 | public: |
| 797 | EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb) |
| 798 | : PD(pd), PDB(pdb) { |
| 799 | CLocs.push_back(PathDiagnosticLocation(&PDB.getCodeDecl(), |
| 800 | PDB.getSourceManager())); |
| 801 | if (!PD.empty()) { |
| 802 | PrevLoc = PD.begin()->getLocation(); |
| 803 | |
| 804 | if (const Stmt *S = PrevLoc.asStmt()) |
| 805 | addContext(PDB.getEnclosingStmtLocation(S).asStmt()); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | ~EdgeBuilder() { |
| 810 | while (!CLocs.empty()) popLocation(); |
| 811 | } |
| 812 | |
| 813 | void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false); |
| 814 | |
| 815 | void addEdge(const Stmt *S, bool alwaysAdd = false) { |
| 816 | addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd); |
| 817 | } |
| 818 | |
| 819 | void addContext(const Stmt *S); |
| 820 | }; |
| 821 | } // end anonymous namespace |
| 822 | |
| 823 | |
| 824 | PathDiagnosticLocation |
| 825 | EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) { |
| 826 | if (const Stmt *S = L.asStmt()) { |
| 827 | if (IsControlFlowExpr(S)) |
| 828 | return L; |
| 829 | |
| 830 | return PDB.getEnclosingStmtLocation(S); |
| 831 | } |
| 832 | |
| 833 | return L; |
| 834 | } |
| 835 | |
| 836 | bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container, |
| 837 | const PathDiagnosticLocation &Containee) { |
| 838 | |
| 839 | if (Container == Containee) |
| 840 | return true; |
| 841 | |
| 842 | if (Container.asDecl()) |
| 843 | return true; |
| 844 | |
| 845 | if (const Stmt *S = Containee.asStmt()) |
| 846 | if (const Stmt *ContainerS = Container.asStmt()) { |
| 847 | while (S) { |
| 848 | if (S == ContainerS) |
| 849 | return true; |
| 850 | S = PDB.getParent(S); |
| 851 | } |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | // Less accurate: compare using source ranges. |
| 856 | SourceRange ContainerR = Container.asRange(); |
| 857 | SourceRange ContaineeR = Containee.asRange(); |
| 858 | |
| 859 | SourceManager &SM = PDB.getSourceManager(); |
| 860 | SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin()); |
| 861 | SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd()); |
| 862 | SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin()); |
| 863 | SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd()); |
| 864 | |
| 865 | unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg); |
| 866 | unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd); |
| 867 | unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg); |
| 868 | unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd); |
| 869 | |
| 870 | assert(ContainerBegLine <= ContainerEndLine); |
| 871 | assert(ContaineeBegLine <= ContaineeEndLine); |
| 872 | |
| 873 | return (ContainerBegLine <= ContaineeBegLine && |
| 874 | ContainerEndLine >= ContaineeEndLine && |
| 875 | (ContainerBegLine != ContaineeBegLine || |
| 876 | SM.getInstantiationColumnNumber(ContainerRBeg) <= |
| 877 | SM.getInstantiationColumnNumber(ContaineeRBeg)) && |
| 878 | (ContainerEndLine != ContaineeEndLine || |
| 879 | SM.getInstantiationColumnNumber(ContainerREnd) >= |
| 880 | SM.getInstantiationColumnNumber(ContainerREnd))); |
| 881 | } |
| 882 | |
| 883 | PathDiagnosticLocation |
| 884 | EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) { |
| 885 | if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt())) |
| 886 | return PathDiagnosticLocation(E->IgnoreParenCasts(), |
| 887 | PDB.getSourceManager()); |
| 888 | return L; |
| 889 | } |
| 890 | |
| 891 | void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) { |
| 892 | if (!PrevLoc.isValid()) { |
| 893 | PrevLoc = NewLoc; |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | if (NewLoc.asLocation() == PrevLoc.asLocation()) |
| 898 | return; |
| 899 | |
| 900 | // FIXME: Ignore intra-macro edges for now. |
| 901 | if (NewLoc.asLocation().getInstantiationLoc() == |
| 902 | PrevLoc.asLocation().getInstantiationLoc()) |
| 903 | return; |
| 904 | |
| 905 | PD.push_front(new PathDiagnosticControlFlowPiece(NewLoc, PrevLoc)); |
| 906 | PrevLoc = NewLoc; |
| 907 | } |
| 908 | |
| 909 | void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) { |
| 910 | const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc); |
| 911 | |
| 912 | while (!CLocs.empty()) { |
| 913 | const PathDiagnosticLocation &TopContextLoc = CLocs.back(); |
| 914 | |
| 915 | // Is the top location context the same as the one for the new location? |
| 916 | if (TopContextLoc == CLoc) { |
Ted Kremenek | e97386f | 2009-04-07 00:11:40 +0000 | [diff] [blame^] | 917 | if (alwaysAdd) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 918 | rawAddEdge(NewLoc); |
| 919 | |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | if (containsLocation(TopContextLoc, CLoc)) { |
| 924 | if (alwaysAdd) |
| 925 | rawAddEdge(NewLoc); |
| 926 | |
| 927 | CLocs.push_back(CLoc); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | // Context does not contain the location. Flush it. |
| 932 | popLocation(); |
| 933 | } |
| 934 | |
| 935 | assert(0 && "addEdge should never pop the top context"); |
| 936 | } |
| 937 | |
| 938 | void EdgeBuilder::addContext(const Stmt *S) { |
| 939 | if (!S) |
| 940 | return; |
| 941 | |
| 942 | PathDiagnosticLocation L(S, PDB.getSourceManager()); |
| 943 | |
| 944 | while (!CLocs.empty()) { |
| 945 | const PathDiagnosticLocation &TopContextLoc = CLocs.back(); |
| 946 | |
| 947 | // Is the top location context the same as the one for the new location? |
| 948 | if (TopContextLoc == L) |
| 949 | return; |
| 950 | |
| 951 | if (containsLocation(TopContextLoc, L)) { |
Ted Kremenek | e97386f | 2009-04-07 00:11:40 +0000 | [diff] [blame^] | 952 | // / if (const Stmt *S = L.asStmt()) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 953 | // if (isa<Expr>(S)) |
| 954 | // if (const Stmt *P = PDB.getParent(S)) |
| 955 | // addContext(PDB.getEnclosingStmtLocation(P).asStmt()); |
| 956 | |
| 957 | CLocs.push_back(L); |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | // Context does not contain the location. Flush it. |
| 962 | popLocation(); |
| 963 | } |
| 964 | |
| 965 | CLocs.push_back(L); |
| 966 | } |
| 967 | |
| 968 | static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD, |
| 969 | PathDiagnosticBuilder &PDB, |
| 970 | const ExplodedNode<GRState> *N) { |
| 971 | |
| 972 | |
| 973 | EdgeBuilder EB(PD, PDB); |
| 974 | |
| 975 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 976 | ? NULL : *(N->pred_begin()); |
| 977 | |
| 978 | while (NextNode) { |
| 979 | N = NextNode; |
| 980 | NextNode = GetPredecessorNode(N); |
| 981 | ProgramPoint P = N->getLocation(); |
| 982 | |
| 983 | // Block edges. |
| 984 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 985 | const CFGBlock &Blk = *BE->getSrc(); |
| 986 | |
| 987 | if (const Stmt *Term = Blk.getTerminator()) |
| 988 | EB.addContext(Term); |
| 989 | |
| 990 | // Only handle blocks with more than 1 statement here, as the blocks |
| 991 | // with one statement are handled at BlockEntrances. |
| 992 | if (Blk.size() > 1) { |
| 993 | const Stmt *S = *Blk.rbegin(); |
| 994 | EB.addEdge(S); |
| 995 | } |
| 996 | |
| 997 | continue; |
| 998 | } |
| 999 | |
| 1000 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 1001 | if (const Stmt* S = BE->getFirstStmt()) { |
| 1002 | if (IsControlFlowExpr(S)) |
| 1003 | EB.addContext(S); |
| 1004 | else |
| 1005 | EB.addEdge(S); |
| 1006 | } |
| 1007 | |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
| 1011 | PathDiagnosticPiece* p = |
| 1012 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 1013 | PDB.getBugReporter(), PDB.getNodeMapClosure()); |
| 1014 | |
| 1015 | if (p) { |
| 1016 | EB.addEdge(p->getLocation(), true); |
| 1017 | PD.push_front(p); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | #else |
| 1024 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1025 | static void GenExtAddEdge(PathDiagnostic& PD, |
| 1026 | PathDiagnosticBuilder &PDB, |
| 1027 | PathDiagnosticLocation NewLoc, |
| 1028 | PathDiagnosticLocation &PrevLoc, |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1029 | bool allowBlockJump = false) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1030 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1031 | if (const Stmt *S = NewLoc.asStmt()) { |
| 1032 | if (IsControlFlowExpr(S)) |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | if (!PrevLoc.isValid()) { |
| 1038 | PrevLoc = NewLoc; |
| 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | if (NewLoc == PrevLoc) |
| 1043 | return; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1044 | |
Ted Kremenek | 0dc65be | 2009-04-01 19:43:28 +0000 | [diff] [blame] | 1045 | // Are we jumping between statements within the same compound statement? |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1046 | if (!allowBlockJump) |
| 1047 | if (const Stmt *PS = PrevLoc.asStmt()) |
| 1048 | if (const Stmt *NS = NewLoc.asStmt()) { |
| 1049 | const Stmt *parentPS = PDB.getParent(PS); |
Ted Kremenek | 28de78b | 2009-04-02 03:30:55 +0000 | [diff] [blame] | 1050 | if (parentPS && isa<CompoundStmt>(parentPS) && |
| 1051 | parentPS == PDB.getParent(NS)) |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1052 | return; |
| 1053 | } |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1054 | |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1055 | // Add an extra edge when jumping between contexts. |
| 1056 | while (1) { |
| 1057 | if (const Stmt *PS = PrevLoc.asStmt()) |
| 1058 | if (const Stmt *NS = NewLoc.asStmt()) { |
| 1059 | PathDiagnosticLocation X = PDB.getEnclosingStmtLocation(PS); |
| 1060 | // FIXME: We need a version of getParent that ignores '()' and casts. |
| 1061 | const Stmt *parentX = PDB.getParent(X.asStmt()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1062 | |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1063 | const PathDiagnosticLocation &Y = PDB.getEnclosingStmtLocation(NS); |
| 1064 | // FIXME: We need a version of getParent that ignores '()' and casts. |
| 1065 | const Stmt *parentY = PDB.getParent(Y.asStmt()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1066 | |
Ted Kremenek | 0ddaff3 | 2009-04-02 03:44:00 +0000 | [diff] [blame] | 1067 | if (parentX && IsControlFlowExpr(parentX)) { |
| 1068 | if (parentX == parentY) |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1069 | break; |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1070 | else { |
Ted Kremenek | 0ddaff3 | 2009-04-02 03:44:00 +0000 | [diff] [blame] | 1071 | if (const Stmt *grandparentX = PDB.getParent(parentX)) { |
| 1072 | const PathDiagnosticLocation &W = |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1073 | PDB.getEnclosingStmtLocation(grandparentX); |
Ted Kremenek | 0ddaff3 | 2009-04-02 03:44:00 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (W != Y) X = W; |
| 1076 | } |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | if (X != Y && PrevLoc.asLocation() != X.asLocation()) { |
| 1081 | PD.push_front(new PathDiagnosticControlFlowPiece(X, PrevLoc)); |
| 1082 | PrevLoc = X; |
| 1083 | } |
| 1084 | } |
| 1085 | break; |
| 1086 | } |
| 1087 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1088 | PD.push_front(new PathDiagnosticControlFlowPiece(NewLoc, PrevLoc)); |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1089 | PrevLoc = NewLoc; |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | static bool IsNestedDeclStmt(const Stmt *S, ParentMap &PM) { |
| 1093 | const DeclStmt *DS = dyn_cast<DeclStmt>(S); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1094 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1095 | if (!DS) |
| 1096 | return false; |
| 1097 | |
| 1098 | const Stmt *Parent = PM.getParent(DS); |
| 1099 | if (!Parent) |
| 1100 | return false; |
| 1101 | |
| 1102 | if (const ForStmt *FS = dyn_cast<ForStmt>(Parent)) |
| 1103 | return FS->getInit() == DS; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1104 | |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1105 | // FIXME: In the future IfStmt/WhileStmt may contain DeclStmts in their |
| 1106 | // condition. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1107 | |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1111 | static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD, |
| 1112 | PathDiagnosticBuilder &PDB, |
| 1113 | const ExplodedNode<GRState> *N) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1114 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1115 | SourceManager& SMgr = PDB.getSourceManager(); |
| 1116 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1117 | ? NULL : *(N->pred_begin()); |
| 1118 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1119 | PathDiagnosticLocation PrevLoc; |
| 1120 | |
| 1121 | while (NextNode) { |
| 1122 | N = NextNode; |
| 1123 | NextNode = GetPredecessorNode(N); |
| 1124 | ProgramPoint P = N->getLocation(); |
| 1125 | |
| 1126 | // Block edges. |
| 1127 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 1128 | const CFGBlock &Blk = *BE->getSrc(); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1129 | |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 1130 | // Add a special edge for the entrance into the function/method. |
| 1131 | if (&Blk == &PDB.getCFG().getEntry()) { |
| 1132 | FullSourceLoc L = FullSourceLoc(PDB.getCodeDecl().getLocation(), SMgr); |
| 1133 | GenExtAddEdge(PD, PDB, L.getSpellingLoc(), PrevLoc); |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1137 | if (const Stmt *Term = Blk.getTerminator()) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1138 | const Stmt *Cond = Blk.getTerminatorCondition(); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1139 | if (!Cond || !IsControlFlowExpr(Cond)) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1140 | // For terminators that are control-flow expressions like '&&', '?', |
| 1141 | // have the condition be the anchor point for the control-flow edge |
| 1142 | // instead of the terminator. |
| 1143 | const Stmt *X = isa<Expr>(Term) ? (Cond ? Cond : Term) : Term; |
| 1144 | GenExtAddEdge(PD, PDB, PathDiagnosticLocation(X, SMgr), PrevLoc,true); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1145 | continue; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | // Only handle blocks with more than 1 statement here, as the blocks |
| 1150 | // with one statement are handled at BlockEntrances. |
| 1151 | if (Blk.size() > 1) { |
| 1152 | const Stmt *S = *Blk.rbegin(); |
| 1153 | |
| 1154 | // We don't add control-flow edges for DeclStmt's that appear in |
| 1155 | // the condition of if/while/for or are control-flow merge expressions. |
| 1156 | if (!IsControlFlowExpr(S) && !IsNestedDeclStmt(S, PDB.getParentMap())) { |
| 1157 | GenExtAddEdge(PD, PDB, PathDiagnosticLocation(S, SMgr), PrevLoc); |
| 1158 | } |
| 1159 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1160 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1161 | continue; |
| 1162 | } |
| 1163 | |
| 1164 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 1165 | if (const Stmt* S = BE->getFirstStmt()) { |
| 1166 | if (!IsControlFlowExpr(S) && !IsNestedDeclStmt(S, PDB.getParentMap())) { |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1167 | if (PrevLoc.isValid()) { |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 1168 | // Are we jumping within the same enclosing statement? |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1169 | if (PDB.getEnclosingStmtLocation(S) == |
| 1170 | PDB.getEnclosingStmtLocation(PrevLoc)) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1171 | continue; |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | GenExtAddEdge(PD, PDB, PDB.getEnclosingStmtLocation(S), PrevLoc); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | continue; |
| 1179 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1180 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1181 | PathDiagnosticPiece* p = |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1182 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 1183 | PDB.getBugReporter(), PDB.getNodeMapClosure()); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1184 | |
| 1185 | if (p) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1186 | GenExtAddEdge(PD, PDB, p->getLocation(), PrevLoc, true); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1187 | PD.push_front(p); |
| 1188 | } |
| 1189 | } |
| 1190 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1191 | #endif |
| 1192 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1193 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1194 | // Methods for BugType and subclasses. |
| 1195 | //===----------------------------------------------------------------------===// |
| 1196 | BugType::~BugType() {} |
| 1197 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1198 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1199 | //===----------------------------------------------------------------------===// |
| 1200 | // Methods for BugReport and subclasses. |
| 1201 | //===----------------------------------------------------------------------===// |
| 1202 | BugReport::~BugReport() {} |
| 1203 | RangedBugReport::~RangedBugReport() {} |
| 1204 | |
| 1205 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 1206 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1207 | Stmt *S = NULL; |
| 1208 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1209 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 1210 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1211 | } |
| 1212 | if (!S) S = GetStmt(ProgP); |
| 1213 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1214 | return S; |
| 1215 | } |
| 1216 | |
| 1217 | PathDiagnosticPiece* |
| 1218 | BugReport::getEndPath(BugReporter& BR, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1219 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1220 | |
| 1221 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (!S) |
| 1224 | return NULL; |
| 1225 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1226 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1227 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1228 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 1229 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1230 | getRanges(BR, Beg, End); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1231 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1232 | for (; Beg != End; ++Beg) |
| 1233 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1234 | |
| 1235 | return P; |
| 1236 | } |
| 1237 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1238 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 1239 | const SourceRange*& end) { |
| 1240 | |
| 1241 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 1242 | R = E->getSourceRange(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 1243 | assert(R.isValid()); |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1244 | beg = &R; |
| 1245 | end = beg+1; |
| 1246 | } |
| 1247 | else |
| 1248 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1251 | SourceLocation BugReport::getLocation() const { |
| 1252 | if (EndNode) |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 1253 | if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) { |
| 1254 | // For member expressions, return the location of the '.' or '->'. |
| 1255 | if (MemberExpr* ME = dyn_cast<MemberExpr>(S)) |
| 1256 | return ME->getMemberLoc(); |
| 1257 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1258 | return S->getLocStart(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 1259 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1260 | |
| 1261 | return FullSourceLoc(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1264 | PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N, |
| 1265 | const ExplodedNode<GRState>* PrevN, |
| 1266 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1267 | BugReporter& BR, |
| 1268 | NodeResolver &NR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 1269 | return NULL; |
| 1270 | } |
| 1271 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1272 | //===----------------------------------------------------------------------===// |
| 1273 | // Methods for BugReporter and subclasses. |
| 1274 | //===----------------------------------------------------------------------===// |
| 1275 | |
| 1276 | BugReportEquivClass::~BugReportEquivClass() { |
| 1277 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
| 1278 | } |
| 1279 | |
| 1280 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 1281 | BugReporterData::~BugReporterData() {} |
| 1282 | |
| 1283 | ExplodedGraph<GRState>& |
| 1284 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 1285 | |
| 1286 | GRStateManager& |
| 1287 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 1288 | |
| 1289 | BugReporter::~BugReporter() { FlushReports(); } |
| 1290 | |
| 1291 | void BugReporter::FlushReports() { |
| 1292 | if (BugTypes.isEmpty()) |
| 1293 | return; |
| 1294 | |
| 1295 | // First flush the warnings for each BugType. This may end up creating new |
| 1296 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 1297 | // structure, we do not need to worry about the iterators being invalidated. |
| 1298 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 1299 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 1300 | |
| 1301 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 1302 | // with new BugType objects and new warnings. |
| 1303 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 1304 | BugType *BT = const_cast<BugType*>(*I); |
| 1305 | |
| 1306 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 1307 | SetTy& EQClasses = BT->EQClasses; |
| 1308 | |
| 1309 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 1310 | BugReportEquivClass& EQ = *EI; |
| 1311 | FlushReport(EQ); |
| 1312 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1313 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1314 | // Delete the BugType object. This will also delete the equivalence |
| 1315 | // classes. |
| 1316 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1317 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1318 | |
| 1319 | // Remove all references to the BugType objects. |
| 1320 | BugTypes = F.GetEmptySet(); |
| 1321 | } |
| 1322 | |
| 1323 | //===----------------------------------------------------------------------===// |
| 1324 | // PathDiagnostics generation. |
| 1325 | //===----------------------------------------------------------------------===// |
| 1326 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1327 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1328 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 1329 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 1330 | const ExplodedNode<GRState>** NStart, |
| 1331 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1332 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1333 | // Create the trimmed graph. It will contain the shortest paths from the |
| 1334 | // error nodes to the root. In the new graph we should only have one |
| 1335 | // error node unless there are two or more error nodes with the same minimum |
| 1336 | // path length. |
| 1337 | ExplodedGraph<GRState>* GTrim; |
| 1338 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1339 | |
| 1340 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 1341 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 1344 | // released when this function exists. |
| 1345 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 1346 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 1347 | |
| 1348 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 1349 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 1350 | // in the new graph. |
| 1351 | const ExplodedNode<GRState>* N = 0; |
| 1352 | unsigned NodeIndex = 0; |
| 1353 | |
| 1354 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
| 1355 | if ((N = NMap->getMappedNode(*I))) { |
| 1356 | NodeIndex = (I - NStart) / sizeof(*I); |
| 1357 | break; |
| 1358 | } |
| 1359 | |
| 1360 | assert(N && "No error node found in the trimmed graph."); |
| 1361 | |
| 1362 | // Create a new (third!) graph with a single path. This is the graph |
| 1363 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1364 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1365 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 1366 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1367 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1368 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1369 | // to the root node, and then construct a new graph that contains only |
| 1370 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1371 | llvm::DenseMap<const void*,unsigned> Visited; |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1372 | std::queue<const ExplodedNode<GRState>*> WS; |
| 1373 | WS.push(N); |
| 1374 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1375 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1376 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1377 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1378 | while (!WS.empty()) { |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1379 | const ExplodedNode<GRState>* Node = WS.front(); |
| 1380 | WS.pop(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (Visited.find(Node) != Visited.end()) |
| 1383 | continue; |
| 1384 | |
| 1385 | Visited[Node] = cnt++; |
| 1386 | |
| 1387 | if (Node->pred_empty()) { |
| 1388 | Root = Node; |
| 1389 | break; |
| 1390 | } |
| 1391 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1392 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1393 | E=Node->pred_end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1394 | WS.push(*I); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1395 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1396 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1397 | assert (Root); |
| 1398 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1399 | // 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] | 1400 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1401 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1402 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1403 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1404 | for ( N = Root ;;) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1405 | // Lookup the number associated with the current node. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1406 | llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1407 | assert (I != Visited.end()); |
| 1408 | |
| 1409 | // Create the equivalent node in the new graph with the same state |
| 1410 | // and location. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1411 | ExplodedNode<GRState>* NewN = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1412 | GNew->getNode(N->getLocation(), N->getState()); |
| 1413 | |
| 1414 | // Store the mapping to the original node. |
| 1415 | llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); |
| 1416 | assert(IMitr != InverseMap.end() && "No mapping to original node."); |
| 1417 | (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1418 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1419 | // Link up the new node with the previous node. |
| 1420 | if (Last) |
| 1421 | NewN->addPredecessor(Last); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1422 | |
| 1423 | Last = NewN; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1424 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1425 | // Are we at the final node? |
| 1426 | if (I->second == 0) { |
| 1427 | First = NewN; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1428 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1429 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1430 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1431 | // Find the next successor node. We choose the node that is marked |
| 1432 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1433 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 1434 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1435 | N = 0; |
| 1436 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1437 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1438 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1439 | I = Visited.find(*SI); |
| 1440 | |
| 1441 | if (I == Visited.end()) |
| 1442 | continue; |
| 1443 | |
| 1444 | if (!N || I->second < MinVal) { |
| 1445 | N = *SI; |
| 1446 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1447 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1448 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1449 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1450 | assert (N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1451 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1452 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1453 | assert (First); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1454 | return std::make_pair(std::make_pair(GNew, BM), |
| 1455 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1458 | /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object |
| 1459 | /// and collapses PathDiagosticPieces that are expanded by macros. |
| 1460 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { |
| 1461 | typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > |
| 1462 | MacroStackTy; |
| 1463 | |
| 1464 | typedef std::vector<PathDiagnosticPiece*> |
| 1465 | PiecesTy; |
| 1466 | |
| 1467 | MacroStackTy MacroStack; |
| 1468 | PiecesTy Pieces; |
| 1469 | |
| 1470 | for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { |
| 1471 | // Get the location of the PathDiagnosticPiece. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1472 | const FullSourceLoc Loc = I->getLocation().asLocation(); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1473 | |
| 1474 | // Determine the instantiation location, which is the location we group |
| 1475 | // related PathDiagnosticPieces. |
| 1476 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
| 1477 | SM.getInstantiationLoc(Loc) : |
| 1478 | SourceLocation(); |
| 1479 | |
| 1480 | if (Loc.isFileID()) { |
| 1481 | MacroStack.clear(); |
| 1482 | Pieces.push_back(&*I); |
| 1483 | continue; |
| 1484 | } |
| 1485 | |
| 1486 | assert(Loc.isMacroID()); |
| 1487 | |
| 1488 | // Is the PathDiagnosticPiece within the same macro group? |
| 1489 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
| 1490 | MacroStack.back().first->push_back(&*I); |
| 1491 | continue; |
| 1492 | } |
| 1493 | |
| 1494 | // We aren't in the same group. Are we descending into a new macro |
| 1495 | // or are part of an old one? |
| 1496 | PathDiagnosticMacroPiece *MacroGroup = 0; |
| 1497 | |
| 1498 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
| 1499 | SM.getInstantiationLoc(Loc) : |
| 1500 | SourceLocation(); |
| 1501 | |
| 1502 | // Walk the entire macro stack. |
| 1503 | while (!MacroStack.empty()) { |
| 1504 | if (InstantiationLoc == MacroStack.back().second) { |
| 1505 | MacroGroup = MacroStack.back().first; |
| 1506 | break; |
| 1507 | } |
| 1508 | |
| 1509 | if (ParentInstantiationLoc == MacroStack.back().second) { |
| 1510 | MacroGroup = MacroStack.back().first; |
| 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | MacroStack.pop_back(); |
| 1515 | } |
| 1516 | |
| 1517 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
| 1518 | // Create a new macro group and add it to the stack. |
| 1519 | PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc); |
| 1520 | |
| 1521 | if (MacroGroup) |
| 1522 | MacroGroup->push_back(NewGroup); |
| 1523 | else { |
| 1524 | assert(InstantiationLoc.isFileID()); |
| 1525 | Pieces.push_back(NewGroup); |
| 1526 | } |
| 1527 | |
| 1528 | MacroGroup = NewGroup; |
| 1529 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
| 1530 | } |
| 1531 | |
| 1532 | // Finally, add the PathDiagnosticPiece to the group. |
| 1533 | MacroGroup->push_back(&*I); |
| 1534 | } |
| 1535 | |
| 1536 | // Now take the pieces and construct a new PathDiagnostic. |
| 1537 | PD.resetPath(false); |
| 1538 | |
| 1539 | for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { |
| 1540 | if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 1541 | if (!MP->containsEvent()) { |
| 1542 | delete MP; |
| 1543 | continue; |
| 1544 | } |
| 1545 | |
| 1546 | PD.push_back(*I); |
| 1547 | } |
| 1548 | } |
| 1549 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1550 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 1551 | BugReportEquivClass& EQ) { |
| 1552 | |
| 1553 | std::vector<const ExplodedNode<GRState>*> Nodes; |
| 1554 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1555 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 1556 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 1557 | if (N) Nodes.push_back(N); |
| 1558 | } |
| 1559 | |
| 1560 | if (Nodes.empty()) |
| 1561 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1562 | |
| 1563 | // 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] | 1564 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1565 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1566 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 1567 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1568 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1569 | // Find the BugReport with the original location. |
| 1570 | BugReport *R = 0; |
| 1571 | unsigned i = 0; |
| 1572 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 1573 | if (i == GPair.second.second) { R = *I; break; } |
| 1574 | |
| 1575 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1576 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1577 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 1578 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1579 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1580 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1581 | // Start building the path diagnostic... |
| 1582 | if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1583 | PD.push_back(Piece); |
| 1584 | else |
| 1585 | return; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1586 | |
| 1587 | PathDiagnosticBuilder PDB(*this, ReportGraph.get(), R, BackMap.get(), |
| 1588 | getStateManager().getCodeDecl(), |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 1589 | getPathDiagnosticClient()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1590 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1591 | switch (PDB.getGenerationScheme()) { |
| 1592 | case PathDiagnosticClient::Extensive: |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1593 | GenerateExtensivePathDiagnostic(PD,PDB, N); |
| 1594 | break; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1595 | case PathDiagnosticClient::Minimal: |
| 1596 | GenerateMinimalPathDiagnostic(PD, PDB, N); |
| 1597 | break; |
| 1598 | } |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1601 | void BugReporter::Register(BugType *BT) { |
| 1602 | BugTypes = F.Add(BugTypes, BT); |
Ted Kremenek | 76d90c8 | 2008-05-16 18:33:14 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1605 | void BugReporter::EmitReport(BugReport* R) { |
| 1606 | // Compute the bug report's hash to determine its equivalence class. |
| 1607 | llvm::FoldingSetNodeID ID; |
| 1608 | R->Profile(ID); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1609 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1610 | // Lookup the equivance class. If there isn't one, create it. |
| 1611 | BugType& BT = R->getBugType(); |
| 1612 | Register(&BT); |
| 1613 | void *InsertPos; |
| 1614 | BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
| 1615 | |
| 1616 | if (!EQ) { |
| 1617 | EQ = new BugReportEquivClass(R); |
| 1618 | BT.EQClasses.InsertNode(EQ, InsertPos); |
| 1619 | } |
| 1620 | else |
| 1621 | EQ->AddReport(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1624 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
| 1625 | assert(!EQ.Reports.empty()); |
| 1626 | BugReport &R = **EQ.begin(); |
| 1627 | |
| 1628 | // FIXME: Make sure we use the 'R' for the path that was actually used. |
| 1629 | // Probably doesn't make a difference in practice. |
| 1630 | BugType& BT = R.getBugType(); |
| 1631 | |
| 1632 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(), |
| 1633 | R.getDescription(), |
| 1634 | BT.getCategory())); |
| 1635 | GeneratePathDiagnostic(*D.get(), EQ); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1636 | |
| 1637 | // Get the meta data. |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1638 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1639 | 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] | 1640 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1641 | // Emit a summary diagnostic to the regular Diagnostics engine. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 1642 | PathDiagnosticClient* PD = getPathDiagnosticClient(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1643 | const SourceRange *Beg = 0, *End = 0; |
| 1644 | R.getRanges(*this, Beg, End); |
| 1645 | Diagnostic& Diag = getDiagnostic(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1646 | FullSourceLoc L(R.getLocation(), getSourceManager()); |
Ted Kremenek | d90e708 | 2009-02-07 22:36:41 +0000 | [diff] [blame] | 1647 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 1648 | R.getDescription().c_str()); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1649 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1650 | switch (End-Beg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1651 | default: assert(0 && "Don't handle this many ranges yet!"); |
| 1652 | case 0: Diag.Report(L, ErrorDiag); break; |
| 1653 | case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break; |
| 1654 | case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break; |
| 1655 | 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] | 1656 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1657 | |
| 1658 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
| 1659 | if (!PD) |
| 1660 | return; |
| 1661 | |
| 1662 | if (D->empty()) { |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1663 | PathDiagnosticPiece* piece = |
| 1664 | new PathDiagnosticEventPiece(L, R.getDescription()); |
| 1665 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1666 | for ( ; Beg != End; ++Beg) piece->addRange(*Beg); |
| 1667 | D->push_back(piece); |
| 1668 | } |
| 1669 | |
| 1670 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1671 | } |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1672 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1673 | void BugReporter::EmitBasicReport(const char* name, const char* str, |
| 1674 | SourceLocation Loc, |
| 1675 | SourceRange* RBeg, unsigned NumRanges) { |
| 1676 | EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); |
| 1677 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1678 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1679 | void BugReporter::EmitBasicReport(const char* name, const char* category, |
| 1680 | const char* str, SourceLocation Loc, |
| 1681 | SourceRange* RBeg, unsigned NumRanges) { |
| 1682 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1683 | // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'. |
| 1684 | BugType *BT = new BugType(name, category); |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1685 | FullSourceLoc L = getContext().getFullLoc(Loc); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1686 | RangedBugReport *R = new DiagBugReport(*BT, str, L); |
| 1687 | for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); |
| 1688 | EmitReport(R); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1689 | } |