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