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