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