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