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