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