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