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