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 |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 11 | // PathDiagnostics. |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 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/AST/ASTContext.h" |
| 18 | #include "clang/AST/CFG.h" |
| 19 | #include "clang/AST/Expr.h" |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtObjC.h" |
| 22 | #include "clang/Basic/SourceManager.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 | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 33 | BugReporterVisitor::~BugReporterVisitor() {} |
| 34 | BugReporterContext::~BugReporterContext() { |
| 35 | for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) |
| 36 | if ((*I)->isOwnedByReporterContext()) delete *I; |
| 37 | } |
| 38 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 40 | // Helper routines for walking the ExplodedGraph and fetching statements. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
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 | static inline Stmt* GetStmt(ProgramPoint P) { |
| 44 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 45 | return PS->getStmt(); |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 46 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 47 | return BE->getSrc()->getTerminator(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 49 | return 0; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 52 | static inline const ExplodedNode<GRState>* |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 53 | GetPredecessorNode(const ExplodedNode<GRState>* N) { |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 54 | return N->pred_empty() ? NULL : *(N->pred_begin()); |
| 55 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 57 | static inline const ExplodedNode<GRState>* |
| 58 | GetSuccessorNode(const ExplodedNode<GRState>* N) { |
| 59 | return N->succ_empty() ? NULL : *(N->succ_begin()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 62 | static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) { |
| 63 | for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N)) |
| 64 | if (Stmt *S = GetStmt(N->getLocation())) |
| 65 | return S; |
| 66 | |
| 67 | return 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 70 | static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) { |
| 71 | for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N)) |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 72 | if (Stmt *S = GetStmt(N->getLocation())) { |
| 73 | // Check if the statement is '?' or '&&'/'||'. These are "merges", |
| 74 | // not actual statement points. |
| 75 | switch (S->getStmtClass()) { |
| 76 | case Stmt::ChooseExprClass: |
| 77 | case Stmt::ConditionalOperatorClass: continue; |
| 78 | case Stmt::BinaryOperatorClass: { |
| 79 | BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode(); |
| 80 | if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr) |
| 81 | continue; |
| 82 | break; |
| 83 | } |
| 84 | default: |
| 85 | break; |
| 86 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 87 | return S; |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 88 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) { |
| 94 | if (Stmt *S = GetStmt(N->getLocation())) |
| 95 | return S; |
| 96 | |
| 97 | return GetPreviousStmt(N); |
| 98 | } |
| 99 | |
| 100 | static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) { |
| 101 | if (Stmt *S = GetStmt(N->getLocation())) |
| 102 | return S; |
| 103 | |
| 104 | return GetNextStmt(N); |
| 105 | } |
| 106 | |
| 107 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 108 | // PathDiagnosticBuilder and its associated routines and helper objects. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 111 | typedef llvm::DenseMap<const ExplodedNode<GRState>*, |
| 112 | const ExplodedNode<GRState>*> NodeBackMap; |
| 113 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 114 | namespace { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 115 | class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver { |
| 116 | NodeBackMap& M; |
| 117 | public: |
| 118 | NodeMapClosure(NodeBackMap *m) : M(*m) {} |
| 119 | ~NodeMapClosure() {} |
| 120 | |
| 121 | const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) { |
| 122 | NodeBackMap::iterator I = M.find(N); |
| 123 | return I == M.end() ? 0 : I->second; |
| 124 | } |
| 125 | }; |
| 126 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 127 | class VISIBILITY_HIDDEN PathDiagnosticBuilder : public BugReporterContext { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 128 | BugReport *R; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 129 | PathDiagnosticClient *PDC; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 130 | llvm::OwningPtr<ParentMap> PM; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 131 | NodeMapClosure NMC; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 132 | public: |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 133 | PathDiagnosticBuilder(GRBugReporter &br, |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 134 | BugReport *r, NodeBackMap *Backmap, |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 135 | PathDiagnosticClient *pdc) |
| 136 | : BugReporterContext(br), |
| 137 | R(r), PDC(pdc), NMC(Backmap) |
| 138 | { |
| 139 | addVisitor(R); |
| 140 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 142 | PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N); |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 144 | PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os, |
| 145 | const ExplodedNode<GRState>* N); |
| 146 | |
| 147 | ParentMap& getParentMap() { |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 148 | if (PM.get() == 0) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame^] | 149 | PM.reset(new ParentMap(getCodeDecl().getBody())); |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 150 | return *PM.get(); |
| 151 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 153 | const Stmt *getParent(const Stmt *S) { |
| 154 | return getParentMap().getParent(S); |
| 155 | } |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 156 | |
| 157 | virtual NodeMapClosure& getNodeResolver() { return NMC; } |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 158 | BugReport& getReport() { return *R; } |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 160 | PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); |
| 161 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 162 | PathDiagnosticLocation |
| 163 | getEnclosingStmtLocation(const PathDiagnosticLocation &L) { |
| 164 | if (const Stmt *S = L.asStmt()) |
| 165 | return getEnclosingStmtLocation(S); |
| 166 | |
| 167 | return L; |
| 168 | } |
| 169 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 170 | PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const { |
| 171 | return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive; |
| 172 | } |
| 173 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 174 | bool supportsLogicalOpControlFlow() const { |
| 175 | return PDC ? PDC->supportsLogicalOpControlFlow() : true; |
| 176 | } |
| 177 | }; |
| 178 | } // end anonymous namespace |
| 179 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 180 | PathDiagnosticLocation |
| 181 | PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) { |
| 182 | if (Stmt *S = GetNextStmt(N)) |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 183 | return PathDiagnosticLocation(S, getSourceManager()); |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 184 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame^] | 185 | return FullSourceLoc(getCodeDecl().getBodyRBrace(), getSourceManager()); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 188 | PathDiagnosticLocation |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 189 | PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os, |
| 190 | const ExplodedNode<GRState>* N) { |
| 191 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 192 | // Slow, but probably doesn't matter. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 193 | if (os.str().empty()) |
| 194 | os << ' '; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 196 | const PathDiagnosticLocation &Loc = ExecutionContinues(N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 198 | if (Loc.asStmt()) |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 199 | os << "Execution continues on line " |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 200 | << getSourceManager().getInstantiationLineNumber(Loc.asLocation()) |
| 201 | << '.'; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 202 | else |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 203 | os << "Execution jumps to the end of the " |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 204 | << (isa<ObjCMethodDecl>(getCodeDecl()) ? "method" : "function") << '.'; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 205 | |
| 206 | return Loc; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Ted Kremenek | ddb7bab | 2009-05-15 01:50:15 +0000 | [diff] [blame] | 209 | static bool IsNested(const Stmt *S, ParentMap &PM) { |
| 210 | if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S))) |
| 211 | return true; |
| 212 | |
| 213 | const Stmt *Parent = PM.getParentIgnoreParens(S); |
| 214 | |
| 215 | if (Parent) |
| 216 | switch (Parent->getStmtClass()) { |
| 217 | case Stmt::ForStmtClass: |
| 218 | case Stmt::DoStmtClass: |
| 219 | case Stmt::WhileStmtClass: |
| 220 | return true; |
| 221 | default: |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | return false; |
| 226 | } |
| 227 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 228 | PathDiagnosticLocation |
| 229 | PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { |
| 230 | assert(S && "Null Stmt* passed to getEnclosingStmtLocation"); |
Ted Kremenek | c42e07e | 2009-05-05 22:19:17 +0000 | [diff] [blame] | 231 | ParentMap &P = getParentMap(); |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 232 | SourceManager &SMgr = getSourceManager(); |
Ted Kremenek | e88a170 | 2009-05-11 22:19:32 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | ddb7bab | 2009-05-15 01:50:15 +0000 | [diff] [blame] | 234 | while (IsNested(S, P)) { |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 235 | const Stmt *Parent = P.getParentIgnoreParens(S); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 237 | if (!Parent) |
| 238 | break; |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 240 | switch (Parent->getStmtClass()) { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 241 | case Stmt::BinaryOperatorClass: { |
| 242 | const BinaryOperator *B = cast<BinaryOperator>(Parent); |
| 243 | if (B->isLogicalOp()) |
| 244 | return PathDiagnosticLocation(S, SMgr); |
| 245 | break; |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 246 | } |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 247 | case Stmt::CompoundStmtClass: |
| 248 | case Stmt::StmtExprClass: |
Ted Kremenek | 1d9a23a | 2009-03-28 04:08:14 +0000 | [diff] [blame] | 249 | return PathDiagnosticLocation(S, SMgr); |
| 250 | case Stmt::ChooseExprClass: |
| 251 | // Similar to '?' if we are referring to condition, just have the edge |
| 252 | // point to the entire choose expression. |
| 253 | if (cast<ChooseExpr>(Parent)->getCond() == S) |
| 254 | return PathDiagnosticLocation(Parent, SMgr); |
| 255 | else |
| 256 | return PathDiagnosticLocation(S, SMgr); |
| 257 | case Stmt::ConditionalOperatorClass: |
| 258 | // For '?', if we are referring to condition, just have the edge point |
| 259 | // to the entire '?' expression. |
| 260 | if (cast<ConditionalOperator>(Parent)->getCond() == S) |
| 261 | return PathDiagnosticLocation(Parent, SMgr); |
| 262 | else |
| 263 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 264 | case Stmt::DoStmtClass: |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 265 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 266 | case Stmt::ForStmtClass: |
| 267 | if (cast<ForStmt>(Parent)->getBody() == S) |
| 268 | return PathDiagnosticLocation(S, SMgr); |
| 269 | break; |
| 270 | case Stmt::IfStmtClass: |
| 271 | if (cast<IfStmt>(Parent)->getCond() != S) |
| 272 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 273 | break; |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 274 | case Stmt::ObjCForCollectionStmtClass: |
| 275 | if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) |
| 276 | return PathDiagnosticLocation(S, SMgr); |
| 277 | break; |
| 278 | case Stmt::WhileStmtClass: |
| 279 | if (cast<WhileStmt>(Parent)->getCond() != S) |
| 280 | return PathDiagnosticLocation(S, SMgr); |
| 281 | break; |
| 282 | default: |
| 283 | break; |
| 284 | } |
| 285 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 286 | S = Parent; |
| 287 | } |
| 288 | |
| 289 | assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); |
Ted Kremenek | e88a170 | 2009-05-11 22:19:32 +0000 | [diff] [blame] | 290 | |
| 291 | // Special case: DeclStmts can appear in for statement declarations, in which |
| 292 | // case the ForStmt is the context. |
| 293 | if (isa<DeclStmt>(S)) { |
| 294 | if (const Stmt *Parent = P.getParent(S)) { |
| 295 | switch (Parent->getStmtClass()) { |
| 296 | case Stmt::ForStmtClass: |
| 297 | case Stmt::ObjCForCollectionStmtClass: |
| 298 | return PathDiagnosticLocation(Parent, SMgr); |
| 299 | default: |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | else if (isa<BinaryOperator>(S)) { |
| 305 | // Special case: the binary operator represents the initialization |
| 306 | // code in a for statement (this can happen when the variable being |
| 307 | // initialized is an old variable. |
| 308 | if (const ForStmt *FS = |
| 309 | dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) { |
| 310 | if (FS->getInit() == S) |
| 311 | return PathDiagnosticLocation(FS, SMgr); |
| 312 | } |
| 313 | } |
| 314 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 315 | return PathDiagnosticLocation(S, SMgr); |
| 316 | } |
| 317 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 318 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 319 | // ScanNotableSymbols: closure-like callback for scanning Store bindings. |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
| 322 | static const VarDecl* |
| 323 | GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N, |
| 324 | GRStateManager& VMgr, SVal X) { |
| 325 | |
| 326 | for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { |
| 327 | |
| 328 | ProgramPoint P = N->getLocation(); |
| 329 | |
| 330 | if (!isa<PostStmt>(P)) |
| 331 | continue; |
| 332 | |
| 333 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); |
| 334 | |
| 335 | if (!DR) |
| 336 | continue; |
| 337 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 338 | SVal Y = N->getState()->getSVal(DR); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 339 | |
| 340 | if (X != Y) |
| 341 | continue; |
| 342 | |
| 343 | VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 344 | |
| 345 | if (!VD) |
| 346 | continue; |
| 347 | |
| 348 | return VD; |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | namespace { |
| 355 | class VISIBILITY_HIDDEN NotableSymbolHandler |
| 356 | : public StoreManager::BindingsHandler { |
| 357 | |
| 358 | SymbolRef Sym; |
| 359 | const GRState* PrevSt; |
| 360 | const Stmt* S; |
| 361 | GRStateManager& VMgr; |
| 362 | const ExplodedNode<GRState>* Pred; |
| 363 | PathDiagnostic& PD; |
| 364 | BugReporter& BR; |
| 365 | |
| 366 | public: |
| 367 | |
| 368 | NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s, |
| 369 | GRStateManager& vmgr, const ExplodedNode<GRState>* pred, |
| 370 | PathDiagnostic& pd, BugReporter& br) |
| 371 | : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {} |
| 372 | |
| 373 | bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, |
| 374 | SVal V) { |
| 375 | |
| 376 | SymbolRef ScanSym = V.getAsSymbol(); |
| 377 | |
| 378 | if (ScanSym != Sym) |
| 379 | return true; |
| 380 | |
| 381 | // Check if the previous state has this binding. |
Ted Kremenek | dbc2afc | 2009-06-23 17:55:07 +0000 | [diff] [blame] | 382 | SVal X = PrevSt->getSVal(loc::MemRegionVal(R)); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 383 | |
| 384 | if (X == V) // Same binding? |
| 385 | return true; |
| 386 | |
| 387 | // Different binding. Only handle assignments for now. We don't pull |
| 388 | // this check out of the loop because we will eventually handle other |
| 389 | // cases. |
| 390 | |
| 391 | VarDecl *VD = 0; |
| 392 | |
| 393 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 394 | if (!B->isAssignmentOp()) |
| 395 | return true; |
| 396 | |
| 397 | // What variable did we assign to? |
| 398 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); |
| 399 | |
| 400 | if (!DR) |
| 401 | return true; |
| 402 | |
| 403 | VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 404 | } |
| 405 | else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
| 406 | // FIXME: Eventually CFGs won't have DeclStmts. Right now we |
| 407 | // assume that each DeclStmt has a single Decl. This invariant |
| 408 | // holds by contruction in the CFG. |
| 409 | VD = dyn_cast<VarDecl>(*DS->decl_begin()); |
| 410 | } |
| 411 | |
| 412 | if (!VD) |
| 413 | return true; |
| 414 | |
| 415 | // What is the most recently referenced variable with this binding? |
| 416 | const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); |
| 417 | |
| 418 | if (!MostRecent) |
| 419 | return true; |
| 420 | |
| 421 | // Create the diagnostic. |
| 422 | FullSourceLoc L(S->getLocStart(), BR.getSourceManager()); |
| 423 | |
| 424 | if (Loc::IsLocType(VD->getType())) { |
| 425 | std::string msg = "'" + std::string(VD->getNameAsString()) + |
| 426 | "' now aliases '" + MostRecent->getNameAsString() + "'"; |
| 427 | |
| 428 | PD.push_front(new PathDiagnosticEventPiece(L, msg)); |
| 429 | } |
| 430 | |
| 431 | return true; |
| 432 | } |
| 433 | }; |
| 434 | } |
| 435 | |
| 436 | static void HandleNotableSymbol(const ExplodedNode<GRState>* N, |
| 437 | const Stmt* S, |
| 438 | SymbolRef Sym, BugReporter& BR, |
| 439 | PathDiagnostic& PD) { |
| 440 | |
| 441 | const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin(); |
| 442 | const GRState* PrevSt = Pred ? Pred->getState() : 0; |
| 443 | |
| 444 | if (!PrevSt) |
| 445 | return; |
| 446 | |
| 447 | // Look at the region bindings of the current state that map to the |
| 448 | // specified symbol. Are any of them not in the previous state? |
| 449 | GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); |
| 450 | NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); |
| 451 | cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); |
| 452 | } |
| 453 | |
| 454 | namespace { |
| 455 | class VISIBILITY_HIDDEN ScanNotableSymbols |
| 456 | : public StoreManager::BindingsHandler { |
| 457 | |
| 458 | llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; |
| 459 | const ExplodedNode<GRState>* N; |
| 460 | Stmt* S; |
| 461 | GRBugReporter& BR; |
| 462 | PathDiagnostic& PD; |
| 463 | |
| 464 | public: |
| 465 | ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br, |
| 466 | PathDiagnostic& pd) |
| 467 | : N(n), S(s), BR(br), PD(pd) {} |
| 468 | |
| 469 | bool HandleBinding(StoreManager& SMgr, Store store, |
| 470 | const MemRegion* R, SVal V) { |
| 471 | |
| 472 | SymbolRef ScanSym = V.getAsSymbol(); |
| 473 | |
| 474 | if (!ScanSym) |
| 475 | return true; |
| 476 | |
| 477 | if (!BR.isNotable(ScanSym)) |
| 478 | return true; |
| 479 | |
| 480 | if (AlreadyProcessed.count(ScanSym)) |
| 481 | return true; |
| 482 | |
| 483 | AlreadyProcessed.insert(ScanSym); |
| 484 | |
| 485 | HandleNotableSymbol(N, S, ScanSym, BR, PD); |
| 486 | return true; |
| 487 | } |
| 488 | }; |
| 489 | } // end anonymous namespace |
| 490 | |
| 491 | //===----------------------------------------------------------------------===// |
| 492 | // "Minimal" path diagnostic generation algorithm. |
| 493 | //===----------------------------------------------------------------------===// |
| 494 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 495 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM); |
| 496 | |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 497 | static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, |
| 498 | PathDiagnosticBuilder &PDB, |
| 499 | const ExplodedNode<GRState> *N) { |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 501 | SourceManager& SMgr = PDB.getSourceManager(); |
| 502 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 503 | ? NULL : *(N->pred_begin()); |
| 504 | while (NextNode) { |
| 505 | N = NextNode; |
| 506 | NextNode = GetPredecessorNode(N); |
| 507 | |
| 508 | ProgramPoint P = N->getLocation(); |
| 509 | |
| 510 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 511 | CFGBlock* Src = BE->getSrc(); |
| 512 | CFGBlock* Dst = BE->getDst(); |
| 513 | Stmt* T = Src->getTerminator(); |
| 514 | |
| 515 | if (!T) |
| 516 | continue; |
| 517 | |
| 518 | FullSourceLoc Start(T->getLocStart(), SMgr); |
| 519 | |
| 520 | switch (T->getStmtClass()) { |
| 521 | default: |
| 522 | break; |
| 523 | |
| 524 | case Stmt::GotoStmtClass: |
| 525 | case Stmt::IndirectGotoStmtClass: { |
| 526 | Stmt* S = GetNextStmt(N); |
| 527 | |
| 528 | if (!S) |
| 529 | continue; |
| 530 | |
| 531 | std::string sbuf; |
| 532 | llvm::raw_string_ostream os(sbuf); |
| 533 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
| 534 | |
| 535 | os << "Control jumps to line " |
| 536 | << End.asLocation().getInstantiationLineNumber(); |
| 537 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 538 | os.str())); |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | case Stmt::SwitchStmtClass: { |
| 543 | // Figure out what case arm we took. |
| 544 | std::string sbuf; |
| 545 | llvm::raw_string_ostream os(sbuf); |
| 546 | |
| 547 | if (Stmt* S = Dst->getLabel()) { |
| 548 | PathDiagnosticLocation End(S, SMgr); |
| 549 | |
| 550 | switch (S->getStmtClass()) { |
| 551 | default: |
| 552 | os << "No cases match in the switch statement. " |
| 553 | "Control jumps to line " |
| 554 | << End.asLocation().getInstantiationLineNumber(); |
| 555 | break; |
| 556 | case Stmt::DefaultStmtClass: |
| 557 | os << "Control jumps to the 'default' case at line " |
| 558 | << End.asLocation().getInstantiationLineNumber(); |
| 559 | break; |
| 560 | |
| 561 | case Stmt::CaseStmtClass: { |
| 562 | os << "Control jumps to 'case "; |
| 563 | CaseStmt* Case = cast<CaseStmt>(S); |
| 564 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
| 565 | |
| 566 | // Determine if it is an enum. |
| 567 | bool GetRawInt = true; |
| 568 | |
| 569 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 570 | // FIXME: Maybe this should be an assertion. Are there cases |
| 571 | // were it is not an EnumConstantDecl? |
| 572 | EnumConstantDecl* D = |
| 573 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 574 | |
| 575 | if (D) { |
| 576 | GetRawInt = false; |
| 577 | os << D->getNameAsString(); |
| 578 | } |
| 579 | } |
Eli Friedman | 9ec64d6 | 2009-04-26 19:04:51 +0000 | [diff] [blame] | 580 | |
| 581 | if (GetRawInt) |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 582 | os << LHS->EvaluateAsInt(PDB.getASTContext()); |
Eli Friedman | 9ec64d6 | 2009-04-26 19:04:51 +0000 | [diff] [blame] | 583 | |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 584 | os << ":' at line " |
| 585 | << End.asLocation().getInstantiationLineNumber(); |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 590 | os.str())); |
| 591 | } |
| 592 | else { |
| 593 | os << "'Default' branch taken. "; |
| 594 | const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); |
| 595 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 596 | os.str())); |
| 597 | } |
| 598 | |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | case Stmt::BreakStmtClass: |
| 603 | case Stmt::ContinueStmtClass: { |
| 604 | std::string sbuf; |
| 605 | llvm::raw_string_ostream os(sbuf); |
| 606 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 607 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 608 | os.str())); |
| 609 | break; |
| 610 | } |
| 611 | |
| 612 | // Determine control-flow for ternary '?'. |
| 613 | case Stmt::ConditionalOperatorClass: { |
| 614 | std::string sbuf; |
| 615 | llvm::raw_string_ostream os(sbuf); |
| 616 | os << "'?' condition is "; |
| 617 | |
| 618 | if (*(Src->succ_begin()+1) == Dst) |
| 619 | os << "false"; |
| 620 | else |
| 621 | os << "true"; |
| 622 | |
| 623 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 624 | |
| 625 | if (const Stmt *S = End.asStmt()) |
| 626 | End = PDB.getEnclosingStmtLocation(S); |
| 627 | |
| 628 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 629 | os.str())); |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | // Determine control-flow for short-circuited '&&' and '||'. |
| 634 | case Stmt::BinaryOperatorClass: { |
| 635 | if (!PDB.supportsLogicalOpControlFlow()) |
| 636 | break; |
| 637 | |
| 638 | BinaryOperator *B = cast<BinaryOperator>(T); |
| 639 | std::string sbuf; |
| 640 | llvm::raw_string_ostream os(sbuf); |
| 641 | os << "Left side of '"; |
| 642 | |
| 643 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 644 | os << "&&" << "' is "; |
| 645 | |
| 646 | if (*(Src->succ_begin()+1) == Dst) { |
| 647 | os << "false"; |
| 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 | else { |
| 654 | os << "true"; |
| 655 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 656 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 657 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 658 | os.str())); |
| 659 | } |
| 660 | } |
| 661 | else { |
| 662 | assert(B->getOpcode() == BinaryOperator::LOr); |
| 663 | os << "||" << "' is "; |
| 664 | |
| 665 | if (*(Src->succ_begin()+1) == Dst) { |
| 666 | os << "false"; |
| 667 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 668 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 669 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 670 | os.str())); |
| 671 | } |
| 672 | else { |
| 673 | os << "true"; |
| 674 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 675 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 676 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 677 | os.str())); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | case Stmt::DoStmtClass: { |
| 685 | if (*(Src->succ_begin()) == Dst) { |
| 686 | std::string sbuf; |
| 687 | llvm::raw_string_ostream os(sbuf); |
| 688 | |
| 689 | os << "Loop condition is true. "; |
| 690 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 691 | |
| 692 | if (const Stmt *S = End.asStmt()) |
| 693 | End = PDB.getEnclosingStmtLocation(S); |
| 694 | |
| 695 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 696 | os.str())); |
| 697 | } |
| 698 | else { |
| 699 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 700 | |
| 701 | if (const Stmt *S = End.asStmt()) |
| 702 | End = PDB.getEnclosingStmtLocation(S); |
| 703 | |
| 704 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 705 | "Loop condition is false. Exiting loop")); |
| 706 | } |
| 707 | |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | case Stmt::WhileStmtClass: |
| 712 | case Stmt::ForStmtClass: { |
| 713 | if (*(Src->succ_begin()+1) == Dst) { |
| 714 | std::string sbuf; |
| 715 | llvm::raw_string_ostream os(sbuf); |
| 716 | |
| 717 | os << "Loop condition is false. "; |
| 718 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 719 | if (const Stmt *S = End.asStmt()) |
| 720 | End = PDB.getEnclosingStmtLocation(S); |
| 721 | |
| 722 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 723 | os.str())); |
| 724 | } |
| 725 | else { |
| 726 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 727 | if (const Stmt *S = End.asStmt()) |
| 728 | End = PDB.getEnclosingStmtLocation(S); |
| 729 | |
| 730 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 731 | "Loop condition is true. Entering loop body")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | case Stmt::IfStmtClass: { |
| 738 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 739 | |
| 740 | if (const Stmt *S = End.asStmt()) |
| 741 | End = PDB.getEnclosingStmtLocation(S); |
| 742 | |
| 743 | if (*(Src->succ_begin()+1) == Dst) |
| 744 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 745 | "Taking false branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 746 | else |
| 747 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 748 | "Taking true branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 749 | |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 755 | if (NextNode) { |
| 756 | for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(), |
| 757 | E = PDB.visitor_end(); I!=E; ++I) { |
| 758 | if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) |
| 759 | PD.push_front(p); |
| 760 | } |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 761 | } |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 762 | |
| 763 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 764 | // Scan the region bindings, and see if a "notable" symbol has a new |
| 765 | // lval binding. |
| 766 | ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD); |
| 767 | PDB.getStateManager().iterBindings(N->getState(), SNS); |
| 768 | } |
| 769 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 770 | |
| 771 | // After constructing the full PathDiagnostic, do a pass over it to compact |
| 772 | // PathDiagnosticPieces that occur within a macro. |
| 773 | CompactPathDiagnostic(PD, PDB.getSourceManager()); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 777 | // "Extensive" PathDiagnostic generation. |
| 778 | //===----------------------------------------------------------------------===// |
| 779 | |
| 780 | static bool IsControlFlowExpr(const Stmt *S) { |
| 781 | const Expr *E = dyn_cast<Expr>(S); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 782 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 783 | if (!E) |
| 784 | return false; |
| 785 | |
| 786 | E = E->IgnoreParenCasts(); |
| 787 | |
| 788 | if (isa<ConditionalOperator>(E)) |
| 789 | return true; |
| 790 | |
| 791 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E)) |
| 792 | if (B->isLogicalOp()) |
| 793 | return true; |
| 794 | |
| 795 | return false; |
| 796 | } |
| 797 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 798 | namespace { |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 799 | class VISIBILITY_HIDDEN ContextLocation : public PathDiagnosticLocation { |
| 800 | bool IsDead; |
| 801 | public: |
| 802 | ContextLocation(const PathDiagnosticLocation &L, bool isdead = false) |
| 803 | : PathDiagnosticLocation(L), IsDead(isdead) {} |
| 804 | |
| 805 | void markDead() { IsDead = true; } |
| 806 | bool isDead() const { return IsDead; } |
| 807 | }; |
| 808 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 809 | class VISIBILITY_HIDDEN EdgeBuilder { |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 810 | std::vector<ContextLocation> CLocs; |
| 811 | typedef std::vector<ContextLocation>::iterator iterator; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 812 | PathDiagnostic &PD; |
| 813 | PathDiagnosticBuilder &PDB; |
| 814 | PathDiagnosticLocation PrevLoc; |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 815 | |
| 816 | bool IsConsumedExpr(const PathDiagnosticLocation &L); |
| 817 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 818 | bool containsLocation(const PathDiagnosticLocation &Container, |
| 819 | const PathDiagnosticLocation &Containee); |
| 820 | |
| 821 | PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 822 | |
Ted Kremenek | 9650cf3 | 2009-05-11 21:42:34 +0000 | [diff] [blame] | 823 | PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L, |
| 824 | bool firstCharOnly = false) { |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 825 | if (const Stmt *S = L.asStmt()) { |
Ted Kremenek | 9650cf3 | 2009-05-11 21:42:34 +0000 | [diff] [blame] | 826 | const Stmt *Original = S; |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 827 | while (1) { |
| 828 | // Adjust the location for some expressions that are best referenced |
| 829 | // by one of their subexpressions. |
Ted Kremenek | 9650cf3 | 2009-05-11 21:42:34 +0000 | [diff] [blame] | 830 | switch (S->getStmtClass()) { |
| 831 | default: |
| 832 | break; |
| 833 | case Stmt::ParenExprClass: |
| 834 | S = cast<ParenExpr>(S)->IgnoreParens(); |
| 835 | firstCharOnly = true; |
| 836 | continue; |
| 837 | case Stmt::ConditionalOperatorClass: |
| 838 | S = cast<ConditionalOperator>(S)->getCond(); |
| 839 | firstCharOnly = true; |
| 840 | continue; |
| 841 | case Stmt::ChooseExprClass: |
| 842 | S = cast<ChooseExpr>(S)->getCond(); |
| 843 | firstCharOnly = true; |
| 844 | continue; |
| 845 | case Stmt::BinaryOperatorClass: |
| 846 | S = cast<BinaryOperator>(S)->getLHS(); |
| 847 | firstCharOnly = true; |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | break; |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Ted Kremenek | 9650cf3 | 2009-05-11 21:42:34 +0000 | [diff] [blame] | 854 | if (S != Original) |
| 855 | L = PathDiagnosticLocation(S, L.getManager()); |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Ted Kremenek | 9650cf3 | 2009-05-11 21:42:34 +0000 | [diff] [blame] | 858 | if (firstCharOnly) |
| 859 | L = PathDiagnosticLocation(L.asLocation()); |
| 860 | |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 861 | return L; |
| 862 | } |
| 863 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 864 | void popLocation() { |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 865 | if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) { |
Ted Kremenek | 5c7168c | 2009-04-22 20:36:26 +0000 | [diff] [blame] | 866 | // For contexts, we only one the first character as the range. |
Ted Kremenek | 07c015c | 2009-05-15 02:46:13 +0000 | [diff] [blame] | 867 | rawAddEdge(cleanUpLocation(CLocs.back(), true)); |
Ted Kremenek | 5c7168c | 2009-04-22 20:36:26 +0000 | [diff] [blame] | 868 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 869 | CLocs.pop_back(); |
| 870 | } |
| 871 | |
| 872 | PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L); |
| 873 | |
| 874 | public: |
| 875 | EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb) |
| 876 | : PD(pd), PDB(pdb) { |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 877 | |
| 878 | // If the PathDiagnostic already has pieces, add the enclosing statement |
| 879 | // of the first piece as a context as well. |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 880 | if (!PD.empty()) { |
| 881 | PrevLoc = PD.begin()->getLocation(); |
| 882 | |
| 883 | if (const Stmt *S = PrevLoc.asStmt()) |
Ted Kremenek | e1baed3 | 2009-05-05 23:13:38 +0000 | [diff] [blame] | 884 | addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
| 888 | ~EdgeBuilder() { |
| 889 | while (!CLocs.empty()) popLocation(); |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 890 | |
| 891 | // Finally, add an initial edge from the start location of the first |
| 892 | // statement (if it doesn't already exist). |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 893 | // FIXME: Should handle CXXTryStmt if analyser starts supporting C++. |
| 894 | if (const CompoundStmt *CS = |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame^] | 895 | PDB.getCodeDecl().getCompoundBody()) |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 896 | if (!CS->body_empty()) { |
| 897 | SourceLocation Loc = (*CS->body_begin())->getLocStart(); |
| 898 | rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager())); |
| 899 | } |
| 900 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false); |
| 904 | |
| 905 | void addEdge(const Stmt *S, bool alwaysAdd = false) { |
| 906 | addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd); |
| 907 | } |
| 908 | |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 909 | void rawAddEdge(PathDiagnosticLocation NewLoc); |
| 910 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 911 | void addContext(const Stmt *S); |
Ted Kremenek | e1baed3 | 2009-05-05 23:13:38 +0000 | [diff] [blame] | 912 | void addExtendedContext(const Stmt *S); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 913 | }; |
| 914 | } // end anonymous namespace |
| 915 | |
| 916 | |
| 917 | PathDiagnosticLocation |
| 918 | EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) { |
| 919 | if (const Stmt *S = L.asStmt()) { |
| 920 | if (IsControlFlowExpr(S)) |
| 921 | return L; |
| 922 | |
| 923 | return PDB.getEnclosingStmtLocation(S); |
| 924 | } |
| 925 | |
| 926 | return L; |
| 927 | } |
| 928 | |
| 929 | bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container, |
| 930 | const PathDiagnosticLocation &Containee) { |
| 931 | |
| 932 | if (Container == Containee) |
| 933 | return true; |
| 934 | |
| 935 | if (Container.asDecl()) |
| 936 | return true; |
| 937 | |
| 938 | if (const Stmt *S = Containee.asStmt()) |
| 939 | if (const Stmt *ContainerS = Container.asStmt()) { |
| 940 | while (S) { |
| 941 | if (S == ContainerS) |
| 942 | return true; |
| 943 | S = PDB.getParent(S); |
| 944 | } |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | // Less accurate: compare using source ranges. |
| 949 | SourceRange ContainerR = Container.asRange(); |
| 950 | SourceRange ContaineeR = Containee.asRange(); |
| 951 | |
| 952 | SourceManager &SM = PDB.getSourceManager(); |
| 953 | SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin()); |
| 954 | SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd()); |
| 955 | SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin()); |
| 956 | SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd()); |
| 957 | |
| 958 | unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg); |
| 959 | unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd); |
| 960 | unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg); |
| 961 | unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd); |
| 962 | |
| 963 | assert(ContainerBegLine <= ContainerEndLine); |
| 964 | assert(ContaineeBegLine <= ContaineeEndLine); |
| 965 | |
| 966 | return (ContainerBegLine <= ContaineeBegLine && |
| 967 | ContainerEndLine >= ContaineeEndLine && |
| 968 | (ContainerBegLine != ContaineeBegLine || |
| 969 | SM.getInstantiationColumnNumber(ContainerRBeg) <= |
| 970 | SM.getInstantiationColumnNumber(ContaineeRBeg)) && |
| 971 | (ContainerEndLine != ContaineeEndLine || |
| 972 | SM.getInstantiationColumnNumber(ContainerREnd) >= |
| 973 | SM.getInstantiationColumnNumber(ContainerREnd))); |
| 974 | } |
| 975 | |
| 976 | PathDiagnosticLocation |
| 977 | EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) { |
| 978 | if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt())) |
| 979 | return PathDiagnosticLocation(E->IgnoreParenCasts(), |
| 980 | PDB.getSourceManager()); |
| 981 | return L; |
| 982 | } |
| 983 | |
| 984 | void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) { |
| 985 | if (!PrevLoc.isValid()) { |
| 986 | PrevLoc = NewLoc; |
| 987 | return; |
| 988 | } |
| 989 | |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 990 | const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc); |
| 991 | const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc); |
| 992 | |
| 993 | if (NewLocClean.asLocation() == PrevLocClean.asLocation()) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 994 | return; |
| 995 | |
| 996 | // FIXME: Ignore intra-macro edges for now. |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 997 | if (NewLocClean.asLocation().getInstantiationLoc() == |
| 998 | PrevLocClean.asLocation().getInstantiationLoc()) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 999 | return; |
| 1000 | |
Ted Kremenek | 8c8b0ad | 2009-05-11 19:50:47 +0000 | [diff] [blame] | 1001 | PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean)); |
| 1002 | PrevLoc = NewLoc; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) { |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 1006 | |
| 1007 | if (!alwaysAdd && NewLoc.asLocation().isMacroID()) |
| 1008 | return; |
| 1009 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1010 | const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc); |
| 1011 | |
| 1012 | while (!CLocs.empty()) { |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1013 | ContextLocation &TopContextLoc = CLocs.back(); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1014 | |
| 1015 | // Is the top location context the same as the one for the new location? |
| 1016 | if (TopContextLoc == CLoc) { |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1017 | if (alwaysAdd) { |
Ted Kremenek | 4c6f8d3 | 2009-05-04 18:15:17 +0000 | [diff] [blame] | 1018 | if (IsConsumedExpr(TopContextLoc) && |
| 1019 | !IsControlFlowExpr(TopContextLoc.asStmt())) |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1020 | TopContextLoc.markDead(); |
| 1021 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1022 | rawAddEdge(NewLoc); |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1023 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1024 | |
| 1025 | return; |
| 1026 | } |
| 1027 | |
| 1028 | if (containsLocation(TopContextLoc, CLoc)) { |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1029 | if (alwaysAdd) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1030 | rawAddEdge(NewLoc); |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1031 | |
Ted Kremenek | 4c6f8d3 | 2009-05-04 18:15:17 +0000 | [diff] [blame] | 1032 | if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) { |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1033 | CLocs.push_back(ContextLocation(CLoc, true)); |
| 1034 | return; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1038 | CLocs.push_back(CLoc); |
| 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | // Context does not contain the location. Flush it. |
| 1043 | popLocation(); |
| 1044 | } |
Ted Kremenek | 5c7168c | 2009-04-22 20:36:26 +0000 | [diff] [blame] | 1045 | |
| 1046 | // If we reach here, there is no enclosing context. Just add the edge. |
| 1047 | rawAddEdge(NewLoc); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Ted Kremenek | 8f9b1b3 | 2009-05-01 16:08:09 +0000 | [diff] [blame] | 1050 | bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) { |
| 1051 | if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt())) |
| 1052 | return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X); |
| 1053 | |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
Ted Kremenek | e1baed3 | 2009-05-05 23:13:38 +0000 | [diff] [blame] | 1057 | void EdgeBuilder::addExtendedContext(const Stmt *S) { |
| 1058 | if (!S) |
| 1059 | return; |
| 1060 | |
| 1061 | const Stmt *Parent = PDB.getParent(S); |
| 1062 | while (Parent) { |
| 1063 | if (isa<CompoundStmt>(Parent)) |
| 1064 | Parent = PDB.getParent(Parent); |
| 1065 | else |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | if (Parent) { |
| 1070 | switch (Parent->getStmtClass()) { |
| 1071 | case Stmt::DoStmtClass: |
| 1072 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 1073 | addContext(Parent); |
| 1074 | default: |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | addContext(S); |
| 1080 | } |
| 1081 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1082 | void EdgeBuilder::addContext(const Stmt *S) { |
| 1083 | if (!S) |
| 1084 | return; |
| 1085 | |
| 1086 | PathDiagnosticLocation L(S, PDB.getSourceManager()); |
| 1087 | |
| 1088 | while (!CLocs.empty()) { |
| 1089 | const PathDiagnosticLocation &TopContextLoc = CLocs.back(); |
| 1090 | |
| 1091 | // Is the top location context the same as the one for the new location? |
| 1092 | if (TopContextLoc == L) |
| 1093 | return; |
| 1094 | |
| 1095 | if (containsLocation(TopContextLoc, L)) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1096 | CLocs.push_back(L); |
| 1097 | return; |
| 1098 | } |
| 1099 | |
| 1100 | // Context does not contain the location. Flush it. |
| 1101 | popLocation(); |
| 1102 | } |
| 1103 | |
| 1104 | CLocs.push_back(L); |
| 1105 | } |
| 1106 | |
| 1107 | static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD, |
| 1108 | PathDiagnosticBuilder &PDB, |
| 1109 | const ExplodedNode<GRState> *N) { |
| 1110 | |
| 1111 | |
| 1112 | EdgeBuilder EB(PD, PDB); |
| 1113 | |
| 1114 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 1115 | ? NULL : *(N->pred_begin()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1116 | while (NextNode) { |
| 1117 | N = NextNode; |
| 1118 | NextNode = GetPredecessorNode(N); |
| 1119 | ProgramPoint P = N->getLocation(); |
| 1120 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1121 | do { |
| 1122 | // Block edges. |
| 1123 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 1124 | const CFGBlock &Blk = *BE->getSrc(); |
| 1125 | const Stmt *Term = Blk.getTerminator(); |
| 1126 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1127 | // Are we jumping to the head of a loop? Add a special diagnostic. |
Ted Kremenek | ddb7bab | 2009-05-15 01:50:15 +0000 | [diff] [blame] | 1128 | if (const Stmt *Loop = BE->getDst()->getLoopTarget()) { |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1129 | PathDiagnosticLocation L(Loop, PDB.getSourceManager()); |
Ted Kremenek | ddb7bab | 2009-05-15 01:50:15 +0000 | [diff] [blame] | 1130 | const CompoundStmt *CS = NULL; |
| 1131 | |
| 1132 | if (!Term) { |
| 1133 | if (const ForStmt *FS = dyn_cast<ForStmt>(Loop)) |
| 1134 | CS = dyn_cast<CompoundStmt>(FS->getBody()); |
| 1135 | else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop)) |
| 1136 | CS = dyn_cast<CompoundStmt>(WS->getBody()); |
| 1137 | } |
| 1138 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1139 | PathDiagnosticEventPiece *p = |
| 1140 | new PathDiagnosticEventPiece(L, |
Ted Kremenek | 07c015c | 2009-05-15 02:46:13 +0000 | [diff] [blame] | 1141 | "Looping back to the head of the loop"); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1142 | |
| 1143 | EB.addEdge(p->getLocation(), true); |
| 1144 | PD.push_front(p); |
| 1145 | |
Ted Kremenek | ddb7bab | 2009-05-15 01:50:15 +0000 | [diff] [blame] | 1146 | if (CS) { |
Ted Kremenek | 07c015c | 2009-05-15 02:46:13 +0000 | [diff] [blame] | 1147 | PathDiagnosticLocation BL(CS->getRBracLoc(), |
| 1148 | PDB.getSourceManager()); |
| 1149 | BL = PathDiagnosticLocation(BL.asLocation()); |
| 1150 | EB.addEdge(BL); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1151 | } |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 1152 | } |
Ted Kremenek | ddb7bab | 2009-05-15 01:50:15 +0000 | [diff] [blame] | 1153 | |
| 1154 | if (Term) |
| 1155 | EB.addContext(Term); |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1156 | |
| 1157 | break; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1160 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 1161 | if (const Stmt* S = BE->getFirstStmt()) { |
| 1162 | if (IsControlFlowExpr(S)) { |
| 1163 | // Add the proper context for '&&', '||', and '?'. |
| 1164 | EB.addContext(S); |
| 1165 | } |
| 1166 | else |
| 1167 | EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt()); |
| 1168 | } |
| 1169 | |
| 1170 | break; |
| 1171 | } |
| 1172 | } while (0); |
| 1173 | |
| 1174 | if (!NextNode) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1175 | continue; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1176 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1177 | for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(), |
| 1178 | E = PDB.visitor_end(); I!=E; ++I) { |
| 1179 | if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) { |
| 1180 | const PathDiagnosticLocation &Loc = p->getLocation(); |
| 1181 | EB.addEdge(Loc, true); |
| 1182 | PD.push_front(p); |
| 1183 | if (const Stmt *S = Loc.asStmt()) |
| 1184 | EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt()); |
| 1185 | } |
| 1186 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1190 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1191 | // Methods for BugType and subclasses. |
| 1192 | //===----------------------------------------------------------------------===// |
| 1193 | BugType::~BugType() {} |
| 1194 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1195 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1196 | //===----------------------------------------------------------------------===// |
| 1197 | // Methods for BugReport and subclasses. |
| 1198 | //===----------------------------------------------------------------------===// |
| 1199 | BugReport::~BugReport() {} |
| 1200 | RangedBugReport::~RangedBugReport() {} |
| 1201 | |
| 1202 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 1203 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1204 | Stmt *S = NULL; |
| 1205 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1206 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 1207 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1208 | } |
| 1209 | if (!S) S = GetStmt(ProgP); |
| 1210 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1211 | return S; |
| 1212 | } |
| 1213 | |
| 1214 | PathDiagnosticPiece* |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1215 | BugReport::getEndPath(BugReporterContext& BRC, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1216 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1217 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1218 | Stmt* S = getStmt(BRC.getBugReporter()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1219 | |
| 1220 | if (!S) |
| 1221 | return NULL; |
Ted Kremenek | 3ef538d | 2009-05-11 23:50:59 +0000 | [diff] [blame] | 1222 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 1223 | const SourceRange *Beg, *End; |
Ted Kremenek | 3ef538d | 2009-05-11 23:50:59 +0000 | [diff] [blame] | 1224 | getRanges(BRC.getBugReporter(), Beg, End); |
| 1225 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1226 | |
Ted Kremenek | 3ef538d | 2009-05-11 23:50:59 +0000 | [diff] [blame] | 1227 | // Only add the statement itself as a range if we didn't specify any |
| 1228 | // special ranges for this report. |
| 1229 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(), |
| 1230 | Beg == End); |
| 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, |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1266 | BugReporterContext &BRC) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 1267 | return NULL; |
| 1268 | } |
| 1269 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1270 | //===----------------------------------------------------------------------===// |
| 1271 | // Methods for BugReporter and subclasses. |
| 1272 | //===----------------------------------------------------------------------===// |
| 1273 | |
| 1274 | BugReportEquivClass::~BugReportEquivClass() { |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1275 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 1279 | BugReporterData::~BugReporterData() {} |
| 1280 | |
| 1281 | ExplodedGraph<GRState>& |
| 1282 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 1283 | |
| 1284 | GRStateManager& |
| 1285 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 1286 | |
| 1287 | BugReporter::~BugReporter() { FlushReports(); } |
| 1288 | |
| 1289 | void BugReporter::FlushReports() { |
| 1290 | if (BugTypes.isEmpty()) |
| 1291 | return; |
| 1292 | |
| 1293 | // First flush the warnings for each BugType. This may end up creating new |
| 1294 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 1295 | // structure, we do not need to worry about the iterators being invalidated. |
| 1296 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 1297 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 1298 | |
| 1299 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 1300 | // with new BugType objects and new warnings. |
| 1301 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 1302 | BugType *BT = const_cast<BugType*>(*I); |
| 1303 | |
| 1304 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 1305 | SetTy& EQClasses = BT->EQClasses; |
| 1306 | |
| 1307 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 1308 | BugReportEquivClass& EQ = *EI; |
| 1309 | FlushReport(EQ); |
| 1310 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1311 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1312 | // Delete the BugType object. This will also delete the equivalence |
| 1313 | // classes. |
| 1314 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1315 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Remove all references to the BugType objects. |
| 1318 | BugTypes = F.GetEmptySet(); |
| 1319 | } |
| 1320 | |
| 1321 | //===----------------------------------------------------------------------===// |
| 1322 | // PathDiagnostics generation. |
| 1323 | //===----------------------------------------------------------------------===// |
| 1324 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1325 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1326 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 1327 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 1328 | const ExplodedNode<GRState>** NStart, |
| 1329 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1330 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1331 | // Create the trimmed graph. It will contain the shortest paths from the |
| 1332 | // error nodes to the root. In the new graph we should only have one |
| 1333 | // error node unless there are two or more error nodes with the same minimum |
| 1334 | // path length. |
| 1335 | ExplodedGraph<GRState>* GTrim; |
| 1336 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1337 | |
| 1338 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 1339 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1340 | |
| 1341 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 1342 | // released when this function exists. |
| 1343 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 1344 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 1345 | |
| 1346 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 1347 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 1348 | // in the new graph. |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1349 | |
| 1350 | std::queue<const ExplodedNode<GRState>*> WS; |
| 1351 | typedef llvm::DenseMap<const ExplodedNode<GRState>*,unsigned> IndexMapTy; |
| 1352 | IndexMapTy IndexMap; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1353 | |
| 1354 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1355 | if (const ExplodedNode<GRState> *N = NMap->getMappedNode(*I)) { |
| 1356 | unsigned NodeIndex = (I - NStart) / sizeof(*I); |
| 1357 | WS.push(N); |
| 1358 | IndexMap[*I] = NodeIndex; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1361 | assert(!WS.empty() && "No error node found in the trimmed graph."); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1362 | |
| 1363 | // Create a new (third!) graph with a single path. This is the graph |
| 1364 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1365 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1366 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 1367 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1368 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1369 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1370 | // to the root node, and then construct a new graph that contains only |
| 1371 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1372 | llvm::DenseMap<const void*,unsigned> Visited; |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1373 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1374 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1375 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1376 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1377 | while (!WS.empty()) { |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1378 | const ExplodedNode<GRState>* Node = WS.front(); |
| 1379 | WS.pop(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (Visited.find(Node) != Visited.end()) |
| 1382 | continue; |
| 1383 | |
| 1384 | Visited[Node] = cnt++; |
| 1385 | |
| 1386 | if (Node->pred_empty()) { |
| 1387 | Root = Node; |
| 1388 | break; |
| 1389 | } |
| 1390 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1391 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1392 | E=Node->pred_end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1393 | WS.push(*I); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1394 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1395 | |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1396 | assert(Root); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1397 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1398 | // 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] | 1399 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1400 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1401 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1402 | unsigned NodeIndex = 0; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1403 | |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1404 | for ( const ExplodedNode<GRState> *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 | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1407 | assert(I != Visited.end()); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 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? |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1426 | IndexMapTy::iterator IMI = |
| 1427 | IndexMap.find((const ExplodedNode<GRState>*)(IMitr->second)); |
| 1428 | if (IMI != IndexMap.end()) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1429 | First = NewN; |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1430 | NodeIndex = IMI->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1431 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1432 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1433 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1434 | // Find the next successor node. We choose the node that is marked |
| 1435 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1436 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 1437 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1438 | N = 0; |
| 1439 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1440 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1441 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1442 | I = Visited.find(*SI); |
| 1443 | |
| 1444 | if (I == Visited.end()) |
| 1445 | continue; |
| 1446 | |
| 1447 | if (!N || I->second < MinVal) { |
| 1448 | N = *SI; |
| 1449 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1450 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1451 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1452 | |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1453 | assert(N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1454 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1455 | |
Ted Kremenek | 938332c | 2009-05-16 01:11:58 +0000 | [diff] [blame] | 1456 | assert(First); |
| 1457 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1458 | return std::make_pair(std::make_pair(GNew, BM), |
| 1459 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1462 | /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object |
| 1463 | /// and collapses PathDiagosticPieces that are expanded by macros. |
| 1464 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { |
| 1465 | typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > |
| 1466 | MacroStackTy; |
| 1467 | |
| 1468 | typedef std::vector<PathDiagnosticPiece*> |
| 1469 | PiecesTy; |
| 1470 | |
| 1471 | MacroStackTy MacroStack; |
| 1472 | PiecesTy Pieces; |
| 1473 | |
| 1474 | for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { |
| 1475 | // Get the location of the PathDiagnosticPiece. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1476 | const FullSourceLoc Loc = I->getLocation().asLocation(); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1477 | |
| 1478 | // Determine the instantiation location, which is the location we group |
| 1479 | // related PathDiagnosticPieces. |
| 1480 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
| 1481 | SM.getInstantiationLoc(Loc) : |
| 1482 | SourceLocation(); |
| 1483 | |
| 1484 | if (Loc.isFileID()) { |
| 1485 | MacroStack.clear(); |
| 1486 | Pieces.push_back(&*I); |
| 1487 | continue; |
| 1488 | } |
| 1489 | |
| 1490 | assert(Loc.isMacroID()); |
| 1491 | |
| 1492 | // Is the PathDiagnosticPiece within the same macro group? |
| 1493 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
| 1494 | MacroStack.back().first->push_back(&*I); |
| 1495 | continue; |
| 1496 | } |
| 1497 | |
| 1498 | // We aren't in the same group. Are we descending into a new macro |
| 1499 | // or are part of an old one? |
| 1500 | PathDiagnosticMacroPiece *MacroGroup = 0; |
| 1501 | |
| 1502 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
| 1503 | SM.getInstantiationLoc(Loc) : |
| 1504 | SourceLocation(); |
| 1505 | |
| 1506 | // Walk the entire macro stack. |
| 1507 | while (!MacroStack.empty()) { |
| 1508 | if (InstantiationLoc == MacroStack.back().second) { |
| 1509 | MacroGroup = MacroStack.back().first; |
| 1510 | break; |
| 1511 | } |
| 1512 | |
| 1513 | if (ParentInstantiationLoc == MacroStack.back().second) { |
| 1514 | MacroGroup = MacroStack.back().first; |
| 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | MacroStack.pop_back(); |
| 1519 | } |
| 1520 | |
| 1521 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
| 1522 | // Create a new macro group and add it to the stack. |
| 1523 | PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc); |
| 1524 | |
| 1525 | if (MacroGroup) |
| 1526 | MacroGroup->push_back(NewGroup); |
| 1527 | else { |
| 1528 | assert(InstantiationLoc.isFileID()); |
| 1529 | Pieces.push_back(NewGroup); |
| 1530 | } |
| 1531 | |
| 1532 | MacroGroup = NewGroup; |
| 1533 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
| 1534 | } |
| 1535 | |
| 1536 | // Finally, add the PathDiagnosticPiece to the group. |
| 1537 | MacroGroup->push_back(&*I); |
| 1538 | } |
| 1539 | |
| 1540 | // Now take the pieces and construct a new PathDiagnostic. |
| 1541 | PD.resetPath(false); |
| 1542 | |
| 1543 | for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { |
| 1544 | if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 1545 | if (!MP->containsEvent()) { |
| 1546 | delete MP; |
| 1547 | continue; |
| 1548 | } |
| 1549 | |
| 1550 | PD.push_back(*I); |
| 1551 | } |
| 1552 | } |
| 1553 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1554 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1555 | BugReportEquivClass& EQ) { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1556 | |
| 1557 | std::vector<const ExplodedNode<GRState>*> Nodes; |
| 1558 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1559 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 1560 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 1561 | if (N) Nodes.push_back(N); |
| 1562 | } |
| 1563 | |
| 1564 | if (Nodes.empty()) |
| 1565 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1566 | |
| 1567 | // 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] | 1568 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1569 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1570 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 1571 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1572 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1573 | // Find the BugReport with the original location. |
| 1574 | BugReport *R = 0; |
| 1575 | unsigned i = 0; |
| 1576 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 1577 | if (i == GPair.second.second) { R = *I; break; } |
| 1578 | |
| 1579 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1580 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1581 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 1582 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1583 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1584 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1585 | // Start building the path diagnostic... |
| 1586 | PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient()); |
| 1587 | |
| 1588 | if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1589 | PD.push_back(Piece); |
| 1590 | else |
| 1591 | return; |
Ted Kremenek | dd986cc | 2009-05-07 00:45:33 +0000 | [diff] [blame] | 1592 | |
| 1593 | R->registerInitialVisitors(PDB, N); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1594 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1595 | switch (PDB.getGenerationScheme()) { |
| 1596 | case PathDiagnosticClient::Extensive: |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1597 | GenerateExtensivePathDiagnostic(PD, PDB, N); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1598 | break; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1599 | case PathDiagnosticClient::Minimal: |
| 1600 | GenerateMinimalPathDiagnostic(PD, PDB, N); |
| 1601 | break; |
| 1602 | } |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1605 | void BugReporter::Register(BugType *BT) { |
| 1606 | BugTypes = F.Add(BugTypes, BT); |
Ted Kremenek | 76d90c8 | 2008-05-16 18:33:14 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1609 | void BugReporter::EmitReport(BugReport* R) { |
| 1610 | // Compute the bug report's hash to determine its equivalence class. |
| 1611 | llvm::FoldingSetNodeID ID; |
| 1612 | R->Profile(ID); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1613 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1614 | // Lookup the equivance class. If there isn't one, create it. |
| 1615 | BugType& BT = R->getBugType(); |
| 1616 | Register(&BT); |
| 1617 | void *InsertPos; |
| 1618 | BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
| 1619 | |
| 1620 | if (!EQ) { |
| 1621 | EQ = new BugReportEquivClass(R); |
| 1622 | BT.EQClasses.InsertNode(EQ, InsertPos); |
| 1623 | } |
| 1624 | else |
| 1625 | EQ->AddReport(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1628 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
| 1629 | assert(!EQ.Reports.empty()); |
| 1630 | BugReport &R = **EQ.begin(); |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 1631 | PathDiagnosticClient* PD = getPathDiagnosticClient(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1632 | |
| 1633 | // FIXME: Make sure we use the 'R' for the path that was actually used. |
| 1634 | // Probably doesn't make a difference in practice. |
| 1635 | BugType& BT = R.getBugType(); |
| 1636 | |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 1637 | llvm::OwningPtr<PathDiagnostic> |
| 1638 | D(new PathDiagnostic(R.getBugType().getName(), |
Ted Kremenek | da0e842 | 2009-04-29 22:05:03 +0000 | [diff] [blame] | 1639 | !PD || PD->useVerboseDescription() |
Ted Kremenek | d49967f | 2009-04-29 21:58:13 +0000 | [diff] [blame] | 1640 | ? R.getDescription() : R.getShortDescription(), |
| 1641 | BT.getCategory())); |
| 1642 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1643 | GeneratePathDiagnostic(*D.get(), EQ); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1644 | |
| 1645 | // Get the meta data. |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1646 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1647 | 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] | 1648 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1649 | // Emit a summary diagnostic to the regular Diagnostics engine. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1650 | const SourceRange *Beg = 0, *End = 0; |
| 1651 | R.getRanges(*this, Beg, End); |
| 1652 | Diagnostic& Diag = getDiagnostic(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1653 | FullSourceLoc L(R.getLocation(), getSourceManager()); |
Ted Kremenek | d90e708 | 2009-02-07 22:36:41 +0000 | [diff] [blame] | 1654 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 1655 | R.getShortDescription().c_str()); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1656 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1657 | switch (End-Beg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1658 | default: assert(0 && "Don't handle this many ranges yet!"); |
| 1659 | case 0: Diag.Report(L, ErrorDiag); break; |
| 1660 | case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break; |
| 1661 | case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break; |
| 1662 | 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] | 1663 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1664 | |
| 1665 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
| 1666 | if (!PD) |
| 1667 | return; |
| 1668 | |
| 1669 | if (D->empty()) { |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1670 | PathDiagnosticPiece* piece = |
| 1671 | new PathDiagnosticEventPiece(L, R.getDescription()); |
| 1672 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1673 | for ( ; Beg != End; ++Beg) piece->addRange(*Beg); |
| 1674 | D->push_back(piece); |
| 1675 | } |
| 1676 | |
| 1677 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1678 | } |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1679 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1680 | void BugReporter::EmitBasicReport(const char* name, const char* str, |
| 1681 | SourceLocation Loc, |
| 1682 | SourceRange* RBeg, unsigned NumRanges) { |
| 1683 | EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); |
| 1684 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1685 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1686 | void BugReporter::EmitBasicReport(const char* name, const char* category, |
| 1687 | const char* str, SourceLocation Loc, |
| 1688 | SourceRange* RBeg, unsigned NumRanges) { |
| 1689 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1690 | // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'. |
| 1691 | BugType *BT = new BugType(name, category); |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1692 | FullSourceLoc L = getContext().getFullLoc(Loc); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1693 | RangedBugReport *R = new DiagBugReport(*BT, str, L); |
| 1694 | for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); |
| 1695 | EmitReport(R); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1696 | } |