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