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