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