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