blob: bceded02de9ccfb2547ee202386695c729a93ab5 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// 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 Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Jordan Rose852aa0d2012-07-10 22:07:52 +000051 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 Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekb697b102009-02-23 22:44:26 +000056 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000057}
58
Zhongxing Xuc5619d92009-08-06 01:32:16 +000059static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000060GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000061 return N->pred_empty() ? NULL : *(N->pred_begin());
62}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000063
Zhongxing Xuc5619d92009-08-06 01:32:16 +000064static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000065GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000067}
68
Ted Kremenek9c378f72011-08-12 23:37:29 +000069static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000070 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000071 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000072 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekb697b102009-02-23 22:44:26 +000074 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000075}
76
Ted Kremenek9c378f72011-08-12 23:37:29 +000077static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000079 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 // Check if the statement is '?' or '&&'/'||'. These are "merges",
81 // not actual statement points.
82 switch (S->getStmtClass()) {
83 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000084 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 case Stmt::ConditionalOperatorClass: continue;
86 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000087 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
88 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000089 continue;
90 break;
91 }
92 default:
93 break;
94 }
Ted Kremenekb697b102009-02-23 22:44:26 +000095 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000096 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekb697b102009-02-23 22:44:26 +000098 return 0;
99}
100
Ted Kremenek5f85e172009-07-22 22:35:28 +0000101static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000102GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000103 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000104 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekb697b102009-02-23 22:44:26 +0000106 return GetPreviousStmt(N);
107}
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek5f85e172009-07-22 22:35:28 +0000109static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000110GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000111 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000112 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekb697b102009-02-23 22:44:26 +0000114 return GetNextStmt(N);
115}
116
117//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000118// Diagnostic cleanup.
119//===----------------------------------------------------------------------===//
120
Ted Kremenekb85cce02012-10-25 22:07:10 +0000121static PathDiagnosticEventPiece *
122eventsDescribeSameCondition(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 Kremenek38001652012-10-26 16:02:36 +0000141/// 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.
146static void removeRedundantMsgs(PathPieces &path) {
Ted Kremenekb85cce02012-10-25 22:07:10 +0000147 unsigned N = path.size();
148 if (N < 2)
149 return;
Ted Kremenek38001652012-10-26 16:02:36 +0000150 // 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 Kremenekb85cce02012-10-25 22:07:10 +0000154 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 Kremenek38001652012-10-26 16:02:36 +0000160 removeRedundantMsgs(cast<PathDiagnosticCallPiece>(piece)->path);
Ted Kremenekb85cce02012-10-25 22:07:10 +0000161 break;
162 case clang::ento::PathDiagnosticPiece::Macro:
Ted Kremenek38001652012-10-26 16:02:36 +0000163 removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(piece)->subPieces);
Ted Kremenekb85cce02012-10-25 22:07:10 +0000164 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 Kremenekc89f4b02012-02-28 23:06:21 +0000192/// 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 Kremeneka43df952012-09-21 00:09:11 +0000195bool BugReporter::RemoveUneededCalls(PathPieces &pieces, BugReport *R,
Jordan Rose368f3b02012-11-15 02:07:23 +0000196 PathDiagnosticLocation *LastCallLocation) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000197 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 Kremeneka43df952012-09-21 00:09:11 +0000206 // Throw away pieces with invalid locations.
207 if (piece->getKind() != PathDiagnosticPiece::Call &&
208 piece->getLocation().asLocation().isInvalid())
209 continue;
210
Ted Kremenek72516742012-03-01 00:05:06 +0000211 switch (piece->getKind()) {
212 case PathDiagnosticPiece::Call: {
213 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000214 // 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 Rose368f3b02012-11-15 02:07:23 +0000220
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 Kremenek72516742012-03-01 00:05:06 +0000228 // Recursively clean out the subclass. Keep this call around if
229 // it contains any informative diagnostics.
Jordan Rose84e15132012-11-15 20:10:05 +0000230 PathDiagnosticLocation *ThisCallLocation;
Jordan Rose368f3b02012-11-15 02:07:23 +0000231 if (call->callEnterWithin.asLocation().isValid())
Jordan Rose84e15132012-11-15 20:10:05 +0000232 ThisCallLocation = &call->callEnterWithin;
Jordan Rose368f3b02012-11-15 02:07:23 +0000233 else
Jordan Rose84e15132012-11-15 20:10:05 +0000234 ThisCallLocation = &call->callEnter;
Ted Kremeneka43df952012-09-21 00:09:11 +0000235
Jordan Rose84e15132012-11-15 20:10:05 +0000236 assert(ThisCallLocation && "Outermost call has an invalid location");
237 if (!RemoveUneededCalls(call->path, R, ThisCallLocation))
Jordan Rose368f3b02012-11-15 02:07:23 +0000238 continue;
Ted Kremeneka43df952012-09-21 00:09:11 +0000239
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000240 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000241 break;
242 }
243 case PathDiagnosticPiece::Macro: {
244 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000245 if (!RemoveUneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000246 continue;
247 containsSomethingInteresting = true;
248 break;
249 }
250 case PathDiagnosticPiece::Event: {
251 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
Ted Kremeneka43df952012-09-21 00:09:11 +0000252
Ted Kremenek72516742012-03-01 00:05:06 +0000253 // We never throw away an event, but we do throw it away wholesale
254 // as part of a path if we throw the entire path away.
Ted Kremenek22505ef2012-09-08 07:18:18 +0000255 containsSomethingInteresting |= !event->isPrunable();
Ted Kremenek72516742012-03-01 00:05:06 +0000256 break;
257 }
258 case PathDiagnosticPiece::ControlFlow:
259 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000260 }
261
262 pieces.push_back(piece);
263 }
264
265 return containsSomethingInteresting;
266}
267
268//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000269// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000270//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000271
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000272typedef llvm::DenseMap<const ExplodedNode*,
273const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000274
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000275namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000276class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000277 NodeBackMap& M;
278public:
279 NodeMapClosure(NodeBackMap *m) : M(*m) {}
280 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Ted Kremenek9c378f72011-08-12 23:37:29 +0000282 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000283 NodeBackMap::iterator I = M.find(N);
284 return I == M.end() ? 0 : I->second;
285 }
286};
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000288class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000289 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000290 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000291 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000292 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000293public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000294 const LocationContext *LC;
295
Ted Kremenek8966bc12009-05-06 21:39:49 +0000296 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000297 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000298 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000299 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000300 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
301 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Ted Kremenek9c378f72011-08-12 23:37:29 +0000303 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenek9c378f72011-08-12 23:37:29 +0000305 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
306 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Anna Zaks8e6431a2011-08-18 22:37:56 +0000308 BugReport *getBugReport() { return R; }
309
Tom Care212f6d32010-09-16 03:50:38 +0000310 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000311
312 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000314 const Stmt *getParent(const Stmt *S) {
315 return getParentMap().getParent(S);
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek8966bc12009-05-06 21:39:49 +0000318 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000319
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000320 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
David Blaikieef3643f2011-09-26 00:51:36 +0000322 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
323 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000324 }
325
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000326 bool supportsLogicalOpControlFlow() const {
327 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000328 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000329};
330} // end anonymous namespace
331
Ted Kremenek00605e02009-03-27 20:55:39 +0000332PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000333PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000334 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000335 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000336
Anna Zaks0cd59482011-09-16 19:18:30 +0000337 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
338 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000339}
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek00605e02009-03-27 20:55:39 +0000341PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000342PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
343 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000344
Ted Kremenek143ca222008-05-06 18:11:09 +0000345 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000346 if (os.str().empty())
347 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek00605e02009-03-27 20:55:39 +0000349 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek00605e02009-03-27 20:55:39 +0000351 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000352 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000353 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000354 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000355 else {
356 os << "Execution jumps to the end of the ";
357 const Decl *D = N->getLocationContext()->getDecl();
358 if (isa<ObjCMethodDecl>(D))
359 os << "method";
360 else if (isa<FunctionDecl>(D))
361 os << "function";
362 else {
363 assert(isa<BlockDecl>(D));
364 os << "anonymous block";
365 }
366 os << '.';
367 }
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000369 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000370}
371
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000372static bool IsNested(const Stmt *S, ParentMap &PM) {
373 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
374 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000376 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000378 if (Parent)
379 switch (Parent->getStmtClass()) {
380 case Stmt::ForStmtClass:
381 case Stmt::DoStmtClass:
382 case Stmt::WhileStmtClass:
383 return true;
384 default:
385 break;
386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
388 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000389}
390
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000391PathDiagnosticLocation
392PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000393 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000394 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000395 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000396
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000397 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000398 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000400 if (!Parent)
401 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000403 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000404 case Stmt::BinaryOperatorClass: {
405 const BinaryOperator *B = cast<BinaryOperator>(Parent);
406 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000407 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000408 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000409 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000410 case Stmt::CompoundStmtClass:
411 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000412 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000413 case Stmt::ChooseExprClass:
414 // Similar to '?' if we are referring to condition, just have the edge
415 // point to the entire choose expression.
416 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000417 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000418 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000419 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000420 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000421 case Stmt::ConditionalOperatorClass:
422 // For '?', if we are referring to condition, just have the edge point
423 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000424 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000425 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000426 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000427 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000428 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000429 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000430 case Stmt::ForStmtClass:
431 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000432 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000433 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000434 case Stmt::IfStmtClass:
435 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000436 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000437 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000438 case Stmt::ObjCForCollectionStmtClass:
439 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000440 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000441 break;
442 case Stmt::WhileStmtClass:
443 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000444 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000445 break;
446 default:
447 break;
448 }
449
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000450 S = Parent;
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000453 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000454
455 // Special case: DeclStmts can appear in for statement declarations, in which
456 // case the ForStmt is the context.
457 if (isa<DeclStmt>(S)) {
458 if (const Stmt *Parent = P.getParent(S)) {
459 switch (Parent->getStmtClass()) {
460 case Stmt::ForStmtClass:
461 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000462 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000463 default:
464 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000465 }
466 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000467 }
468 else if (isa<BinaryOperator>(S)) {
469 // Special case: the binary operator represents the initialization
470 // code in a for statement (this can happen when the variable being
471 // initialized is an old variable.
472 if (const ForStmt *FS =
473 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
474 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000475 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000476 }
477 }
478
Anna Zaks220ac8c2011-09-15 01:08:34 +0000479 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000480}
481
Ted Kremenekcf118d42009-02-04 23:49:09 +0000482//===----------------------------------------------------------------------===//
Jordan Rosed632d6f2012-09-22 01:24:56 +0000483// "Visitors only" path diagnostic generation algorithm.
484//===----------------------------------------------------------------------===//
485static bool GenerateVisitorsOnlyPathDiagnostic(PathDiagnostic &PD,
486 PathDiagnosticBuilder &PDB,
487 const ExplodedNode *N,
488 ArrayRef<BugReporterVisitor *> visitors) {
489 // All path generation skips the very first node (the error node).
490 // This is because there is special handling for the end-of-path note.
491 N = N->getFirstPred();
492 if (!N)
493 return true;
494
495 BugReport *R = PDB.getBugReport();
496 while (const ExplodedNode *Pred = N->getFirstPred()) {
497 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
498 E = visitors.end();
499 I != E; ++I) {
500 // Visit all the node pairs, but throw the path pieces away.
501 PathDiagnosticPiece *Piece = (*I)->VisitNode(N, Pred, PDB, *R);
502 delete Piece;
503 }
504
505 N = Pred;
506 }
507
508 return R->isValid();
509}
510
511//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000512// "Minimal" path diagnostic generation algorithm.
513//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000514typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
515typedef SmallVector<StackDiagPair, 6> StackDiagVector;
516
Anna Zaks368a0d52012-03-15 21:13:02 +0000517static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000518 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000519 // If the piece contains a special message, add it to all the call
520 // pieces on the active stack.
521 if (PathDiagnosticEventPiece *ep =
522 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000523
Anna Zaks56a938f2012-03-16 23:24:20 +0000524 if (ep->hasCallStackHint())
525 for (StackDiagVector::iterator I = CallStack.begin(),
526 E = CallStack.end(); I != E; ++I) {
527 PathDiagnosticCallPiece *CP = I->first;
528 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000529 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000530
Anna Zaks368a0d52012-03-15 21:13:02 +0000531 // The last message on the path to final bug is the most important
532 // one. Since we traverse the path backwards, do not add the message
533 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000534 if (!CP->hasCallStackMessage())
535 CP->setCallStackMessage(stackMsg);
536 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000537 }
538}
Ted Kremenek31061982009-03-31 23:00:32 +0000539
Ted Kremenek77d09442012-03-02 01:27:31 +0000540static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000541
Jordan Rose8347d3d2012-09-22 01:24:53 +0000542static bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
Ted Kremenek31061982009-03-31 23:00:32 +0000543 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000544 const ExplodedNode *N,
545 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000548 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000549 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000550 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000551
Anna Zaks56a938f2012-03-16 23:24:20 +0000552 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000553
Ted Kremenek31061982009-03-31 23:00:32 +0000554 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000555 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000556 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000557 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek31061982009-03-31 23:00:32 +0000559 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000560
Anna Zaks80de4872012-08-29 21:22:37 +0000561 do {
562 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
563 PathDiagnosticCallPiece *C =
564 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
565 GRBugReporter& BR = PDB.getBugReporter();
566 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
567 PD.getActivePath().push_front(C);
568 PD.pushActivePath(&C->path);
569 CallStack.push_back(StackDiagPair(C, N));
570 break;
Anna Zaks93739372012-03-14 18:58:28 +0000571 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000572
Anna Zaks80de4872012-08-29 21:22:37 +0000573 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
574 // Flush all locations, and pop the active path.
575 bool VisitedEntireCall = PD.isWithinCall();
576 PD.popActivePath();
577
578 // Either we just added a bunch of stuff to the top-level path, or
579 // we have a previous CallExitEnd. If the former, it means that the
580 // path terminated within a function call. We must then take the
581 // current contents of the active path and place it within
582 // a new PathDiagnosticCallPiece.
583 PathDiagnosticCallPiece *C;
584 if (VisitedEntireCall) {
585 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
586 } else {
587 const Decl *Caller = CE->getLocationContext()->getDecl();
588 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
589 GRBugReporter& BR = PDB.getBugReporter();
590 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
591 }
592
593 C->setCallee(*CE, SMgr);
594 if (!CallStack.empty()) {
595 assert(CallStack.back().first == C);
596 CallStack.pop_back();
597 }
598 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Anna Zaks80de4872012-08-29 21:22:37 +0000601 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
602 const CFGBlock *Src = BE->getSrc();
603 const CFGBlock *Dst = BE->getDst();
604 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Anna Zaks80de4872012-08-29 21:22:37 +0000606 if (!T)
607 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Anna Zaks80de4872012-08-29 21:22:37 +0000609 PathDiagnosticLocation Start =
610 PathDiagnosticLocation::createBegin(T, SMgr,
611 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Anna Zaks80de4872012-08-29 21:22:37 +0000613 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000614 default:
615 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek31061982009-03-31 23:00:32 +0000617 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000618 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000619 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Ted Kremenek31061982009-03-31 23:00:32 +0000621 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000622 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Ted Kremenek31061982009-03-31 23:00:32 +0000624 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000625 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000626 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Ted Kremenek31061982009-03-31 23:00:32 +0000628 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000629 << End.asLocation().getExpansionLineNumber();
630 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
631 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000632 break;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
635 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000636 // Figure out what case arm we took.
637 std::string sbuf;
638 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek9c378f72011-08-12 23:37:29 +0000640 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000641 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek31061982009-03-31 23:00:32 +0000643 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000644 default:
645 os << "No cases match in the switch statement. "
646 "Control jumps to line "
647 << End.asLocation().getExpansionLineNumber();
648 break;
649 case Stmt::DefaultStmtClass:
650 os << "Control jumps to the 'default' case at line "
651 << End.asLocation().getExpansionLineNumber();
652 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anna Zaks80de4872012-08-29 21:22:37 +0000654 case Stmt::CaseStmtClass: {
655 os << "Control jumps to 'case ";
656 const CaseStmt *Case = cast<CaseStmt>(S);
657 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Anna Zaks80de4872012-08-29 21:22:37 +0000659 // Determine if it is an enum.
660 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anna Zaks80de4872012-08-29 21:22:37 +0000662 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
663 // FIXME: Maybe this should be an assertion. Are there cases
664 // were it is not an EnumConstantDecl?
665 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000666 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Anna Zaks80de4872012-08-29 21:22:37 +0000668 if (D) {
669 GetRawInt = false;
670 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000671 }
Ted Kremenek31061982009-03-31 23:00:32 +0000672 }
Anna Zaks80de4872012-08-29 21:22:37 +0000673
674 if (GetRawInt)
675 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
676
677 os << ":' at line "
678 << End.asLocation().getExpansionLineNumber();
679 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000680 }
Anna Zaks80de4872012-08-29 21:22:37 +0000681 }
682 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
683 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000684 }
685 else {
686 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000687 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000688 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
689 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenek31061982009-03-31 23:00:32 +0000692 break;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Ted Kremenek31061982009-03-31 23:00:32 +0000695 case Stmt::BreakStmtClass:
696 case Stmt::ContinueStmtClass: {
697 std::string sbuf;
698 llvm::raw_string_ostream os(sbuf);
699 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000700 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
701 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000702 break;
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Anna Zaks80de4872012-08-29 21:22:37 +0000705 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000706 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000707 case Stmt::ConditionalOperatorClass: {
708 std::string sbuf;
709 llvm::raw_string_ostream os(sbuf);
710 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek31061982009-03-31 23:00:32 +0000712 if (*(Src->succ_begin()+1) == Dst)
713 os << "false";
714 else
715 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenek31061982009-03-31 23:00:32 +0000717 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 if (const Stmt *S = End.asStmt())
720 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Anna Zaks80de4872012-08-29 21:22:37 +0000722 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
723 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000724 break;
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Anna Zaks80de4872012-08-29 21:22:37 +0000727 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000728 case Stmt::BinaryOperatorClass: {
729 if (!PDB.supportsLogicalOpControlFlow())
730 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000732 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000733 std::string sbuf;
734 llvm::raw_string_ostream os(sbuf);
735 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000736
John McCall2de56d12010-08-25 11:45:40 +0000737 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000738 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek31061982009-03-31 23:00:32 +0000740 if (*(Src->succ_begin()+1) == Dst) {
741 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000742 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000743 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000744 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
745 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
746 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000747 }
Ted Kremenek31061982009-03-31 23:00:32 +0000748 else {
749 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000750 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000751 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000752 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
753 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000754 }
Ted Kremenek31061982009-03-31 23:00:32 +0000755 }
756 else {
John McCall2de56d12010-08-25 11:45:40 +0000757 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000758 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek31061982009-03-31 23:00:32 +0000760 if (*(Src->succ_begin()+1) == Dst) {
761 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000762 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000763 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000764 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
765 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000766 }
767 else {
768 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000769 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000770 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000771 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
772 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
773 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000774 }
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek31061982009-03-31 23:00:32 +0000777 break;
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
780 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000781 if (*(Src->succ_begin()) == Dst) {
782 std::string sbuf;
783 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek31061982009-03-31 23:00:32 +0000785 os << "Loop condition is true. ";
786 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenek31061982009-03-31 23:00:32 +0000788 if (const Stmt *S = End.asStmt())
789 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Anna Zaks80de4872012-08-29 21:22:37 +0000791 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
792 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000793 }
794 else {
795 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek31061982009-03-31 23:00:32 +0000797 if (const Stmt *S = End.asStmt())
798 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Anna Zaks80de4872012-08-29 21:22:37 +0000800 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
801 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek31061982009-03-31 23:00:32 +0000804 break;
805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek31061982009-03-31 23:00:32 +0000807 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000808 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000809 if (*(Src->succ_begin()+1) == Dst) {
810 std::string sbuf;
811 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremenek31061982009-03-31 23:00:32 +0000813 os << "Loop condition is false. ";
814 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
815 if (const Stmt *S = End.asStmt())
816 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Anna Zaks80de4872012-08-29 21:22:37 +0000818 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
819 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000820 }
821 else {
822 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
823 if (const Stmt *S = End.asStmt())
824 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Anna Zaks80de4872012-08-29 21:22:37 +0000826 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
827 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek31061982009-03-31 23:00:32 +0000830 break;
831 }
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek31061982009-03-31 23:00:32 +0000833 case Stmt::IfStmtClass: {
834 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek31061982009-03-31 23:00:32 +0000836 if (const Stmt *S = End.asStmt())
837 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek31061982009-03-31 23:00:32 +0000839 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000840 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
841 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000842 else
Anna Zaks80de4872012-08-29 21:22:37 +0000843 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
844 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek31061982009-03-31 23:00:32 +0000846 break;
847 }
Anna Zaks80de4872012-08-29 21:22:37 +0000848 }
Ted Kremenek31061982009-03-31 23:00:32 +0000849 }
Anna Zaks80de4872012-08-29 21:22:37 +0000850 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000852 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000853 // Add diagnostic pieces from custom visitors.
854 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000855 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
856 E = visitors.end();
857 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000858 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000859 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000860 updateStackPiecesWithMessage(p, CallStack);
861 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000862 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000863 }
Ted Kremenek31061982009-03-31 23:00:32 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Jordan Rose8347d3d2012-09-22 01:24:53 +0000866 if (!PDB.getBugReport()->isValid())
867 return false;
868
Ted Kremenek14856d72009-04-06 23:06:54 +0000869 // After constructing the full PathDiagnostic, do a pass over it to compact
870 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000871 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Jordan Rose8347d3d2012-09-22 01:24:53 +0000872 return true;
Ted Kremenek31061982009-03-31 23:00:32 +0000873}
874
875//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000876// "Extensive" PathDiagnostic generation.
877//===----------------------------------------------------------------------===//
878
879static bool IsControlFlowExpr(const Stmt *S) {
880 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000882 if (!E)
883 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
885 E = E->IgnoreParenCasts();
886
John McCall56ca35d2011-02-17 10:25:35 +0000887 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000888 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000890 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
891 if (B->isLogicalOp())
892 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
894 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000895}
896
Ted Kremenek14856d72009-04-06 23:06:54 +0000897namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000898class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000899 bool IsDead;
900public:
901 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
902 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000903
904 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000905 bool isDead() const { return IsDead; }
906};
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000908class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000909 std::vector<ContextLocation> CLocs;
910 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000911 PathDiagnostic &PD;
912 PathDiagnosticBuilder &PDB;
913 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000915 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenek14856d72009-04-06 23:06:54 +0000917 bool containsLocation(const PathDiagnosticLocation &Container,
918 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Ted Kremenek14856d72009-04-06 23:06:54 +0000920 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Ted Kremenek9650cf32009-05-11 21:42:34 +0000922 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
923 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000924 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000925 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000926 while (1) {
927 // Adjust the location for some expressions that are best referenced
928 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000929 switch (S->getStmtClass()) {
930 default:
931 break;
932 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000933 case Stmt::GenericSelectionExprClass:
934 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000935 firstCharOnly = true;
936 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000937 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000938 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000939 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000940 firstCharOnly = true;
941 continue;
942 case Stmt::ChooseExprClass:
943 S = cast<ChooseExpr>(S)->getCond();
944 firstCharOnly = true;
945 continue;
946 case Stmt::BinaryOperatorClass:
947 S = cast<BinaryOperator>(S)->getLHS();
948 firstCharOnly = true;
949 continue;
950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Ted Kremenek9650cf32009-05-11 21:42:34 +0000952 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000953 }
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Ted Kremenek9650cf32009-05-11 21:42:34 +0000955 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000956 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Ted Kremenek9650cf32009-05-11 21:42:34 +0000959 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000960 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000961
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000962 return L;
963 }
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Ted Kremenek14856d72009-04-06 23:06:54 +0000965 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000966 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000967 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000968 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000969 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000970 CLocs.pop_back();
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek14856d72009-04-06 23:06:54 +0000973public:
974 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
975 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremeneka301a672009-04-22 18:16:20 +0000977 // If the PathDiagnostic already has pieces, add the enclosing statement
978 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000979 if (!PD.path.empty()) {
980 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Ted Kremenek14856d72009-04-06 23:06:54 +0000982 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000983 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 }
985 }
986
987 ~EdgeBuilder() {
988 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000989
Ted Kremeneka301a672009-04-22 18:16:20 +0000990 // Finally, add an initial edge from the start location of the first
991 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000992 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000993 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000994 PDB.getSourceManager());
995 if (L.isValid())
996 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000997 }
998
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000999 void flushLocations() {
1000 while (!CLocs.empty())
1001 popLocation();
1002 PrevLoc = PathDiagnosticLocation();
1003 }
1004
Ted Kremenek14856d72009-04-06 23:06:54 +00001005 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001007 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Ted Kremenek14856d72009-04-06 23:06:54 +00001009 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001010 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001011 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +00001012};
Ted Kremenek14856d72009-04-06 23:06:54 +00001013} // end anonymous namespace
1014
1015
1016PathDiagnosticLocation
1017EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1018 if (const Stmt *S = L.asStmt()) {
1019 if (IsControlFlowExpr(S))
1020 return L;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
1022 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +00001023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 return L;
1026}
1027
1028bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1029 const PathDiagnosticLocation &Containee) {
1030
1031 if (Container == Containee)
1032 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Ted Kremenek14856d72009-04-06 23:06:54 +00001034 if (Container.asDecl())
1035 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Ted Kremenek14856d72009-04-06 23:06:54 +00001037 if (const Stmt *S = Containee.asStmt())
1038 if (const Stmt *ContainerS = Container.asStmt()) {
1039 while (S) {
1040 if (S == ContainerS)
1041 return true;
1042 S = PDB.getParent(S);
1043 }
1044 return false;
1045 }
1046
1047 // Less accurate: compare using source ranges.
1048 SourceRange ContainerR = Container.asRange();
1049 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Ted Kremenek14856d72009-04-06 23:06:54 +00001051 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001052 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1053 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1054 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1055 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chandler Carruth64211622011-07-25 21:09:52 +00001057 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1058 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1059 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1060 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek14856d72009-04-06 23:06:54 +00001062 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001063 assert(ContaineeBegLine <= ContaineeEndLine);
1064
Ted Kremenek14856d72009-04-06 23:06:54 +00001065 return (ContainerBegLine <= ContaineeBegLine &&
1066 ContainerEndLine >= ContaineeEndLine &&
1067 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001068 SM.getExpansionColumnNumber(ContainerRBeg) <=
1069 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001070 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001071 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +00001072 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001073}
1074
Ted Kremenek14856d72009-04-06 23:06:54 +00001075void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1076 if (!PrevLoc.isValid()) {
1077 PrevLoc = NewLoc;
1078 return;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001081 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1082 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Ted Kremeneka43df952012-09-21 00:09:11 +00001084 if (PrevLocClean.asLocation().isInvalid()) {
1085 PrevLoc = NewLoc;
1086 return;
1087 }
1088
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001089 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001090 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremenek14856d72009-04-06 23:06:54 +00001092 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001093 if (NewLocClean.asLocation().getExpansionLoc() ==
1094 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001095 return;
1096
Ted Kremenek2042fc12012-02-24 06:00:00 +00001097 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001098 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001099}
1100
1101void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Ted Kremeneka301a672009-04-22 18:16:20 +00001103 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1104 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremenek14856d72009-04-06 23:06:54 +00001106 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1107
1108 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001109 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Ted Kremenek14856d72009-04-06 23:06:54 +00001111 // Is the top location context the same as the one for the new location?
1112 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001113 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001114 if (IsConsumedExpr(TopContextLoc) &&
1115 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001116 TopContextLoc.markDead();
1117
Ted Kremenek14856d72009-04-06 23:06:54 +00001118 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001119 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001120
1121 return;
1122 }
1123
1124 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001125 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001126 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001128 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001129 CLocs.push_back(ContextLocation(CLoc, true));
1130 return;
1131 }
1132 }
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Ted Kremenek14856d72009-04-06 23:06:54 +00001134 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001135 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001136 }
1137
1138 // Context does not contain the location. Flush it.
1139 popLocation();
1140 }
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001142 // If we reach here, there is no enclosing context. Just add the edge.
1143 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001144}
1145
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001146bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1147 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1148 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001150 return false;
1151}
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Ted Kremeneke1baed32009-05-05 23:13:38 +00001153void EdgeBuilder::addExtendedContext(const Stmt *S) {
1154 if (!S)
1155 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001156
1157 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001158 while (Parent) {
1159 if (isa<CompoundStmt>(Parent))
1160 Parent = PDB.getParent(Parent);
1161 else
1162 break;
1163 }
1164
1165 if (Parent) {
1166 switch (Parent->getStmtClass()) {
1167 case Stmt::DoStmtClass:
1168 case Stmt::ObjCAtSynchronizedStmtClass:
1169 addContext(Parent);
1170 default:
1171 break;
1172 }
1173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremeneke1baed32009-05-05 23:13:38 +00001175 addContext(S);
1176}
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Ted Kremenek14856d72009-04-06 23:06:54 +00001178void EdgeBuilder::addContext(const Stmt *S) {
1179 if (!S)
1180 return;
1181
Ted Kremenek59950d32012-02-24 07:12:52 +00001182 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001183 addContext(L);
1184}
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Jordan Rose183ba8e2012-07-26 20:04:05 +00001186void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001187 while (!CLocs.empty()) {
1188 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1189
1190 // Is the top location context the same as the one for the new location?
1191 if (TopContextLoc == L)
1192 return;
1193
1194 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001195 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001196 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001197 }
1198
1199 // Context does not contain the location. Flush it.
1200 popLocation();
1201 }
1202
1203 CLocs.push_back(L);
1204}
1205
Ted Kremenek11abcec2012-05-02 00:31:29 +00001206// Cone-of-influence: support the reverse propagation of "interesting" symbols
1207// and values by tracing interesting calculations backwards through evaluated
1208// expressions along a path. This is probably overly complicated, but the idea
1209// is that if an expression computed an "interesting" value, the child
1210// expressions are are also likely to be "interesting" as well (which then
1211// propagates to the values they in turn compute). This reverse propagation
1212// is needed to track interesting correlations across function call boundaries,
1213// where formal arguments bind to actual arguments, etc. This is also needed
1214// because the constraint solver sometimes simplifies certain symbolic values
1215// into constants when appropriate, and this complicates reasoning about
1216// interesting values.
1217typedef llvm::DenseSet<const Expr *> InterestingExprs;
1218
1219static void reversePropagateIntererstingSymbols(BugReport &R,
1220 InterestingExprs &IE,
1221 const ProgramState *State,
1222 const Expr *Ex,
1223 const LocationContext *LCtx) {
1224 SVal V = State->getSVal(Ex, LCtx);
1225 if (!(R.isInteresting(V) || IE.count(Ex)))
1226 return;
1227
1228 switch (Ex->getStmtClass()) {
1229 default:
1230 if (!isa<CastExpr>(Ex))
1231 break;
1232 // Fall through.
1233 case Stmt::BinaryOperatorClass:
1234 case Stmt::UnaryOperatorClass: {
1235 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1236 CE = Ex->child_end();
1237 CI != CE; ++CI) {
1238 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1239 IE.insert(child);
1240 SVal ChildV = State->getSVal(child, LCtx);
1241 R.markInteresting(ChildV);
1242 }
1243 break;
1244 }
1245 }
1246 }
1247
1248 R.markInteresting(V);
1249}
1250
1251static void reversePropagateInterestingSymbols(BugReport &R,
1252 InterestingExprs &IE,
1253 const ProgramState *State,
1254 const LocationContext *CalleeCtx,
1255 const LocationContext *CallerCtx)
1256{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001257 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001258 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1259 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001260 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001261 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1262 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1263 PE = FD->param_end();
1264 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1265 for (; AI != AE && PI != PE; ++AI, ++PI) {
1266 if (const Expr *ArgE = *AI) {
1267 if (const ParmVarDecl *PD = *PI) {
1268 Loc LV = State->getLValue(PD, CalleeCtx);
1269 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1270 IE.insert(ArgE);
1271 }
1272 }
1273 }
1274 }
1275 }
1276}
1277
Jordan Rose8347d3d2012-09-22 01:24:53 +00001278static bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek14856d72009-04-06 23:06:54 +00001279 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001280 const ExplodedNode *N,
1281 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001282 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001283 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001284 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001285 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001286
Ted Kremenek9c378f72011-08-12 23:37:29 +00001287 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001288 while (NextNode) {
1289 N = NextNode;
1290 NextNode = GetPredecessorNode(N);
1291 ProgramPoint P = N->getLocation();
1292
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001293 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001294 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1295 if (const Expr *Ex = PS->getStmtAs<Expr>())
1296 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1297 N->getState().getPtr(), Ex,
1298 N->getLocationContext());
1299 }
1300
Anna Zaks0b3ade82012-04-20 21:59:08 +00001301 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001302 const Stmt *S = CE->getCalleeContext()->getCallSite();
1303 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001304 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1305 N->getState().getPtr(), Ex,
1306 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001307 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001308
1309 PathDiagnosticCallPiece *C =
1310 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001311 GRBugReporter& BR = PDB.getBugReporter();
1312 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001313
1314 EB.addEdge(C->callReturn, true);
1315 EB.flushLocations();
1316
1317 PD.getActivePath().push_front(C);
1318 PD.pushActivePath(&C->path);
1319 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001320 break;
1321 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001322
Ted Kremenek2042fc12012-02-24 06:00:00 +00001323 // Pop the call hierarchy if we are done walking the contents
1324 // of a function call.
1325 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001326 // Add an edge to the start of the function.
1327 const Decl *D = CE->getCalleeContext()->getDecl();
1328 PathDiagnosticLocation pos =
1329 PathDiagnosticLocation::createBegin(D, SM);
1330 EB.addEdge(pos);
1331
1332 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001333 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001334 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001335 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001336 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001337
Jordan Rose183ba8e2012-07-26 20:04:05 +00001338 // Either we just added a bunch of stuff to the top-level path, or
1339 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001340 // path terminated within a function call. We must then take the
1341 // current contents of the active path and place it within
1342 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001343 PathDiagnosticCallPiece *C;
1344 if (VisitedEntireCall) {
1345 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1346 } else {
1347 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001348 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001349 GRBugReporter& BR = PDB.getBugReporter();
1350 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001351 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001352
Jordan Rose183ba8e2012-07-26 20:04:05 +00001353 C->setCallee(*CE, SM);
1354 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001355
1356 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001357 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001358 CallStack.pop_back();
1359 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001360 break;
1361 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001362
1363 // Note that is important that we update the LocationContext
1364 // after looking at CallExits. CallExit basically adds an
1365 // edge in the *caller*, so we don't want to update the LocationContext
1366 // too soon.
1367 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001368
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001369 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001370 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1371 // Does this represent entering a call? If so, look at propagating
1372 // interesting symbols across call boundaries.
1373 if (NextNode) {
1374 const LocationContext *CallerCtx = NextNode->getLocationContext();
1375 const LocationContext *CalleeCtx = PDB.LC;
1376 if (CallerCtx != CalleeCtx) {
1377 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1378 N->getState().getPtr(),
1379 CalleeCtx, CallerCtx);
1380 }
1381 }
1382
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001383 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001384 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001385 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001386 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001388 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1389 CS = dyn_cast<CompoundStmt>(FS->getBody());
1390 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1391 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001393 PathDiagnosticEventPiece *p =
1394 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001395 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001396 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001398 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001399 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001401 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001402 PathDiagnosticLocation BL =
1403 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001404 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001405 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001406 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001407
1408 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001409 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001411 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001412 }
1413
Mike Stump1eb44332009-09-09 15:08:12 +00001414 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Jordan Rose1a7bcc42012-09-12 22:48:08 +00001415 CFGElement First = BE->getFirstElement();
1416 if (const CFGStmt *S = First.getAs<CFGStmt>()) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001417 const Stmt *stmt = S->getStmt();
1418 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001419 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001420 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001421 }
1422 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001423 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001424 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001425
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001426 break;
1427 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001428
1429
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001430 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001432 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001433 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Anna Zaks8e6431a2011-08-18 22:37:56 +00001435 // Add pieces from custom visitors.
1436 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001437 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1438 E = visitors.end();
1439 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001440 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001441 const PathDiagnosticLocation &Loc = p->getLocation();
1442 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001443 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001444 updateStackPiecesWithMessage(p, CallStack);
1445
Ted Kremenek8966bc12009-05-06 21:39:49 +00001446 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001447 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001448 }
Mike Stump1eb44332009-09-09 15:08:12 +00001449 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001450 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00001451
1452 return PDB.getBugReport()->isValid();
Ted Kremenek14856d72009-04-06 23:06:54 +00001453}
1454
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001455//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001456// Methods for BugType and subclasses.
1457//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001458BugType::~BugType() { }
1459
Ted Kremenekcf118d42009-02-04 23:49:09 +00001460void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001461
David Blaikie99ba9e32011-12-20 02:48:34 +00001462void BuiltinBug::anchor() {}
1463
Ted Kremenekcf118d42009-02-04 23:49:09 +00001464//===----------------------------------------------------------------------===//
1465// Methods for BugReport and subclasses.
1466//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001467
David Blaikie99ba9e32011-12-20 02:48:34 +00001468void BugReport::NodeResolver::anchor() {}
1469
Anna Zaks8e6431a2011-08-18 22:37:56 +00001470void BugReport::addVisitor(BugReporterVisitor* visitor) {
1471 if (!visitor)
1472 return;
1473
1474 llvm::FoldingSetNodeID ID;
1475 visitor->Profile(ID);
1476 void *InsertPos;
1477
1478 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1479 delete visitor;
1480 return;
1481 }
1482
1483 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001484 Callbacks.push_back(visitor);
1485 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001486}
1487
1488BugReport::~BugReport() {
1489 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001490 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001491 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001492 while (!interestingSymbols.empty()) {
1493 popInterestingSymbolsAndRegions();
1494 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001495}
Anna Zakse172e8b2011-08-17 23:00:25 +00001496
Ted Kremenek07189522012-04-04 18:11:35 +00001497const Decl *BugReport::getDeclWithIssue() const {
1498 if (DeclWithIssue)
1499 return DeclWithIssue;
1500
1501 const ExplodedNode *N = getErrorNode();
1502 if (!N)
1503 return 0;
1504
1505 const LocationContext *LC = N->getLocationContext();
1506 return LC->getCurrentStackFrame()->getDecl();
1507}
1508
Anna Zakse172e8b2011-08-17 23:00:25 +00001509void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1510 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001511 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001512 if (UniqueingLocation.isValid()) {
1513 UniqueingLocation.Profile(hash);
1514 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001515 Location.Profile(hash);
1516 } else {
1517 assert(ErrorNode);
1518 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1519 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001520
1521 for (SmallVectorImpl<SourceRange>::const_iterator I =
1522 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1523 const SourceRange range = *I;
1524 if (!range.isValid())
1525 continue;
1526 hash.AddInteger(range.getBegin().getRawEncoding());
1527 hash.AddInteger(range.getEnd().getRawEncoding());
1528 }
1529}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001530
Ted Kremenek76aadc32012-03-09 01:13:14 +00001531void BugReport::markInteresting(SymbolRef sym) {
1532 if (!sym)
1533 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001534
1535 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001536 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001537 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001538
1539 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001540 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001541}
1542
1543void BugReport::markInteresting(const MemRegion *R) {
1544 if (!R)
1545 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001546
1547 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001548 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001549 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001550 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001551
Ted Kremenek76aadc32012-03-09 01:13:14 +00001552 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001553 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001554}
1555
1556void BugReport::markInteresting(SVal V) {
1557 markInteresting(V.getAsRegion());
1558 markInteresting(V.getAsSymbol());
1559}
1560
Anna Zaks80de4872012-08-29 21:22:37 +00001561void BugReport::markInteresting(const LocationContext *LC) {
1562 if (!LC)
1563 return;
1564 InterestingLocationContexts.insert(LC);
1565}
1566
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001567bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001568 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1569}
1570
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001571bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001572 if (!sym)
1573 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001574 // We don't currently consider metadata symbols to be interesting
1575 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001576 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001577}
1578
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001579bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001580 if (!R)
1581 return false;
1582 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001583 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001584 if (b)
1585 return true;
1586 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001587 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001588 return false;
1589}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001590
Anna Zaks80de4872012-08-29 21:22:37 +00001591bool BugReport::isInteresting(const LocationContext *LC) {
1592 if (!LC)
1593 return false;
1594 return InterestingLocationContexts.count(LC);
1595}
1596
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001597void BugReport::lazyInitializeInterestingSets() {
1598 if (interestingSymbols.empty()) {
1599 interestingSymbols.push_back(new Symbols());
1600 interestingRegions.push_back(new Regions());
1601 }
1602}
1603
1604BugReport::Symbols &BugReport::getInterestingSymbols() {
1605 lazyInitializeInterestingSets();
1606 return *interestingSymbols.back();
1607}
1608
1609BugReport::Regions &BugReport::getInterestingRegions() {
1610 lazyInitializeInterestingSets();
1611 return *interestingRegions.back();
1612}
1613
1614void BugReport::pushInterestingSymbolsAndRegions() {
1615 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1616 interestingRegions.push_back(new Regions(getInterestingRegions()));
1617}
1618
1619void BugReport::popInterestingSymbolsAndRegions() {
1620 delete interestingSymbols.back();
1621 interestingSymbols.pop_back();
1622 delete interestingRegions.back();
1623 interestingRegions.pop_back();
1624}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001625
Ted Kremenek9c378f72011-08-12 23:37:29 +00001626const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001627 if (!ErrorNode)
1628 return 0;
1629
Tom Care212f6d32010-09-16 03:50:38 +00001630 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001631 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Ted Kremenek9c378f72011-08-12 23:37:29 +00001633 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001634 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001635 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001636 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001637 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001638 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001639 S = GetStmt(ProgP);
1640
1641 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001642}
1643
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001644std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001645BugReport::getRanges() {
1646 // If no custom ranges, add the range of the statement corresponding to
1647 // the error node.
1648 if (Ranges.empty()) {
1649 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1650 addRange(E->getSourceRange());
1651 else
1652 return std::make_pair(ranges_iterator(), ranges_iterator());
1653 }
1654
Anna Zaks14924262011-08-24 20:31:06 +00001655 // User-specified absence of range info.
1656 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1657 return std::make_pair(ranges_iterator(), ranges_iterator());
1658
Anna Zakse172e8b2011-08-17 23:00:25 +00001659 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001660}
1661
Anna Zaks590dd8e2011-09-20 21:38:35 +00001662PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001663 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001664 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001665 "Either Location or ErrorNode should be specified but not both.");
1666
Ted Kremenek9c378f72011-08-12 23:37:29 +00001667 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001668 const LocationContext *LC = ErrorNode->getLocationContext();
1669
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001670 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001671 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001672 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001673 // For binary operators, return the location of the operator.
1674 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001675 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001676
Jordan Rose63bc1862012-11-15 19:11:43 +00001677 if (isa<PostStmtPurgeDeadSymbols>(ErrorNode->getLocation()))
1678 return PathDiagnosticLocation::createEnd(S, SM, LC);
1679
Anna Zaks590dd8e2011-09-20 21:38:35 +00001680 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001681 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001682 } else {
1683 assert(Location.isValid());
1684 return Location;
1685 }
1686
Anna Zaks590dd8e2011-09-20 21:38:35 +00001687 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001688}
1689
Ted Kremenekcf118d42009-02-04 23:49:09 +00001690//===----------------------------------------------------------------------===//
1691// Methods for BugReporter and subclasses.
1692//===----------------------------------------------------------------------===//
1693
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001694BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001695GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001696BugReporterData::~BugReporterData() {}
1697
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001698ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001699
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001700ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001701GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1702
Anna Zaks3b030a22011-08-19 01:57:09 +00001703BugReporter::~BugReporter() {
1704 FlushReports();
1705
1706 // Free the bug reports we are tracking.
1707 typedef std::vector<BugReportEquivClass *> ContTy;
1708 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1709 I != E; ++I) {
1710 delete *I;
1711 }
1712}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001713
1714void BugReporter::FlushReports() {
1715 if (BugTypes.isEmpty())
1716 return;
1717
1718 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001719 // warnings and new BugTypes.
1720 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1721 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001722 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001723 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001724 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001725 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001726 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001727 const_cast<BugType*>(*I)->FlushReports(*this);
1728
Anna Zaksd015f4f2012-08-02 23:41:05 +00001729 // We need to flush reports in deterministic order to ensure the order
1730 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001731 typedef std::vector<BugReportEquivClass *> ContVecTy;
1732 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1733 EI != EE; ++EI){
1734 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001735 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001736 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001737
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001738 // BugReporter owns and deletes only BugTypes created implicitly through
1739 // EmitBasicReport.
1740 // FIXME: There are leaks from checkers that assume that the BugTypes they
1741 // create will be destroyed by the BugReporter.
1742 for (llvm::StringMap<BugType*>::iterator
1743 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1744 delete I->second;
1745
Ted Kremenekcf118d42009-02-04 23:49:09 +00001746 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001747 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001748}
1749
1750//===----------------------------------------------------------------------===//
1751// PathDiagnostics generation.
1752//===----------------------------------------------------------------------===//
1753
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001754static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001755 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001756MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001757 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Ted Kremenekcf118d42009-02-04 23:49:09 +00001759 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001760 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001761 // error node unless there are two or more error nodes with the same minimum
1762 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001763 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001764 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001765
1766 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001767 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1768 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Ted Kremenekcf118d42009-02-04 23:49:09 +00001770 // Create owning pointers for GTrim and NMap just to ensure that they are
1771 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001772 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1773 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Ted Kremenekcf118d42009-02-04 23:49:09 +00001775 // Find the (first) error node in the trimmed graph. We just need to consult
1776 // the node map (NMap) which maps from nodes in the original graph to nodes
1777 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001778
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001779 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001780 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001781 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001782
Ted Kremenek40406fe2010-12-03 06:52:30 +00001783 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1784 const ExplodedNode *originalNode = nodes[nodeIndex];
1785 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001786 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001787 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001788 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Ted Kremenek938332c2009-05-16 01:11:58 +00001791 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001792
1793 // Create a new (third!) graph with a single path. This is the graph
1794 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001795 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Ted Kremenek10aa5542009-03-12 23:41:59 +00001797 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001798 // to the root node, and then construct a new graph that contains only
1799 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001800 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001802 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001803 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001805 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001806 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001807 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001809 if (Visited.find(Node) != Visited.end())
1810 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001812 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001814 if (Node->pred_empty()) {
1815 Root = Node;
1816 break;
1817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001819 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001820 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001821 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001822 }
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenek938332c2009-05-16 01:11:58 +00001824 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Ted Kremenek10aa5542009-03-12 23:41:59 +00001826 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001827 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001828 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001829 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001830 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001832 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001833 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001834 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001835 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001837 // Create the equivalent node in the new graph with the same state
1838 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001839 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001841 // Store the mapping to the original node.
1842 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1843 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001844 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001846 // Link up the new node with the previous node.
1847 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001848 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001850 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001852 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001853 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001854 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001855 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001856 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001857 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001858 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001859 }
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001861 // Find the next successor node. We choose the node that is marked
1862 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001863 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1864 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001865 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001867 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001869 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001871 if (I == Visited.end())
1872 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001874 if (!N || I->second < MinVal) {
1875 N = *SI;
1876 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001877 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001878 }
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Ted Kremenek938332c2009-05-16 01:11:58 +00001880 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001881 }
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Ted Kremenek938332c2009-05-16 01:11:58 +00001883 assert(First);
1884
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001885 return std::make_pair(std::make_pair(GNew, BM),
1886 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001887}
1888
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001889/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1890/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001891static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001892 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1893 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001895 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001896 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001898 MacroStackTy MacroStack;
1899 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Ted Kremenek77d09442012-03-02 01:27:31 +00001901 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001902 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001903
1904 PathDiagnosticPiece *piece = I->getPtr();
1905
1906 // Recursively compact calls.
1907 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1908 CompactPathDiagnostic(call->path, SM);
1909 }
1910
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001911 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001912 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001914 // Determine the instantiation location, which is the location we group
1915 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001916 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001917 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001918 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001920 if (Loc.isFileID()) {
1921 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001922 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001923 continue;
1924 }
1925
1926 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001928 // Is the PathDiagnosticPiece within the same macro group?
1929 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001930 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001931 continue;
1932 }
1933
1934 // We aren't in the same group. Are we descending into a new macro
1935 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001936 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001937
1938 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001939 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001940 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001942 // Walk the entire macro stack.
1943 while (!MacroStack.empty()) {
1944 if (InstantiationLoc == MacroStack.back().second) {
1945 MacroGroup = MacroStack.back().first;
1946 break;
1947 }
Mike Stump1eb44332009-09-09 15:08:12 +00001948
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001949 if (ParentInstantiationLoc == MacroStack.back().second) {
1950 MacroGroup = MacroStack.back().first;
1951 break;
1952 }
Mike Stump1eb44332009-09-09 15:08:12 +00001953
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001954 MacroStack.pop_back();
1955 }
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001957 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1958 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001959 PathDiagnosticMacroPiece *NewGroup =
1960 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001961 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001962
1963 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001964 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001965 else {
1966 assert(InstantiationLoc.isFileID());
1967 Pieces.push_back(NewGroup);
1968 }
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001970 MacroGroup = NewGroup;
1971 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1972 }
1973
1974 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001975 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001976 }
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001978 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001979 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Ted Kremenek77d09442012-03-02 01:27:31 +00001981 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1982 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001983}
1984
Jordan Rose8347d3d2012-09-22 01:24:53 +00001985bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001986 PathDiagnosticConsumer &PC,
1987 ArrayRef<BugReport *> &bugReports) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001988 assert(!bugReports.empty());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001989
1990 bool HasValid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001991 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001992 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1993 E = bugReports.end(); I != E; ++I) {
Jordan Rose8347d3d2012-09-22 01:24:53 +00001994 if ((*I)->isValid()) {
1995 HasValid = true;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001996 errorNodes.push_back((*I)->getErrorNode());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001997 } else {
1998 errorNodes.push_back(0);
1999 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00002000 }
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Jordan Rose8347d3d2012-09-22 01:24:53 +00002002 // If all the reports have been marked invalid, we're done.
2003 if (!HasValid)
2004 return false;
2005
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00002006 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00002007 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00002008 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002009 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00002010 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Ted Kremenekcf118d42009-02-04 23:49:09 +00002012 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002013 assert(GPair.second.second < bugReports.size());
2014 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00002015 assert(R && "No original report found for sliced graph.");
Jordan Rose8347d3d2012-09-22 01:24:53 +00002016 assert(R->isValid() && "Report selected from trimmed graph marked invalid.");
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002018 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
2019 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002020 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00002021
2022 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002023 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Anna Zaks8e6431a2011-08-18 22:37:56 +00002025 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00002026 R->addVisitor(new NilReceiverBRVisitor());
2027 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002029 BugReport::VisitorList visitors;
2030 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00002031
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002032 // While generating diagnostics, it's possible the visitors will decide
2033 // new symbols and regions are interesting, or add other visitors based on
2034 // the information they find. If they do, we need to regenerate the path
2035 // based on our new report configuration.
2036 do {
2037 // Get a clean copy of all the visitors.
2038 for (BugReport::visitor_iterator I = R->visitor_begin(),
2039 E = R->visitor_end(); I != E; ++I)
2040 visitors.push_back((*I)->clone());
2041
2042 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002043 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002044 originalReportConfigToken = R->getConfigurationChangeToken();
2045
2046 // Generate the very last diagnostic piece - the piece is visible before
2047 // the trace is expanded.
Jordan Rosed632d6f2012-09-22 01:24:56 +00002048 if (PDB.getGenerationScheme() != PathDiagnosticConsumer::None) {
2049 PathDiagnosticPiece *LastPiece = 0;
2050 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
2051 I != E; ++I) {
2052 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
2053 assert (!LastPiece &&
2054 "There can only be one final piece in a diagnostic.");
2055 LastPiece = Piece;
2056 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002057 }
Jordan Rosed632d6f2012-09-22 01:24:56 +00002058 if (!LastPiece)
2059 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
2060 if (LastPiece)
2061 PD.setEndOfPath(LastPiece);
2062 else
2063 return false;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002064 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002065
2066 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00002067 case PathDiagnosticConsumer::Extensive:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002068 if (!GenerateExtensivePathDiagnostic(PD, PDB, N, visitors)) {
2069 assert(!R->isValid() && "Failed on valid report");
2070 // Try again. We'll filter out the bad report when we trim the graph.
2071 // FIXME: It would be more efficient to use the same intermediate
2072 // trimmed graph, and just repeat the shortest-path search.
2073 return generatePathDiagnostic(PD, PC, bugReports);
2074 }
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00002075 break;
David Blaikieef3643f2011-09-26 00:51:36 +00002076 case PathDiagnosticConsumer::Minimal:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002077 if (!GenerateMinimalPathDiagnostic(PD, PDB, N, visitors)) {
2078 assert(!R->isValid() && "Failed on valid report");
2079 // Try again. We'll filter out the bad report when we trim the graph.
2080 return generatePathDiagnostic(PD, PC, bugReports);
2081 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00002082 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002083 case PathDiagnosticConsumer::None:
Jordan Rosed632d6f2012-09-22 01:24:56 +00002084 if (!GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors)) {
2085 assert(!R->isValid() && "Failed on valid report");
2086 // Try again. We'll filter out the bad report when we trim the graph.
2087 return generatePathDiagnostic(PD, PC, bugReports);
2088 }
2089 break;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002090 }
2091
2092 // Clean up the visitors we used.
2093 llvm::DeleteContainerPointers(visitors);
2094
2095 // Did anything change while generating this path?
2096 finalReportConfigToken = R->getConfigurationChangeToken();
2097 } while(finalReportConfigToken != originalReportConfigToken);
2098
Ted Kremenekc89f4b02012-02-28 23:06:21 +00002099 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremenekb85cce02012-10-25 22:07:10 +00002100 if (!PD.path.empty()) {
2101 // Remove messages that are basically the same.
Ted Kremenek38001652012-10-26 16:02:36 +00002102 removeRedundantMsgs(PD.getMutablePieces());
Ted Kremenekb85cce02012-10-25 22:07:10 +00002103
2104 if (R->shouldPrunePath()) {
2105 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(),
2106 R);
2107 assert(hasSomethingInteresting);
2108 (void) hasSomethingInteresting;
2109 }
Ted Kremeneked7948b2012-05-31 06:03:17 +00002110 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00002111
2112 return true;
Ted Kremenek7dc86642009-03-31 20:22:36 +00002113}
2114
Ted Kremenekcf118d42009-02-04 23:49:09 +00002115void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00002116 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00002117}
2118
Jordan Rose785950e2012-11-02 01:53:40 +00002119void BugReporter::emitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00002120 // Compute the bug report's hash to determine its equivalence class.
2121 llvm::FoldingSetNodeID ID;
2122 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002123
2124 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00002125 BugType& BT = R->getBugType();
2126 Register(&BT);
2127 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002128 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Ted Kremenekcf118d42009-02-04 23:49:09 +00002130 if (!EQ) {
2131 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002132 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00002133 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002134 }
2135 else
2136 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00002137}
2138
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002139
2140//===----------------------------------------------------------------------===//
2141// Emitting reports in equivalence classes.
2142//===----------------------------------------------------------------------===//
2143
2144namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002145struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002146 const ExplodedNode *N;
2147 ExplodedNode::const_succ_iterator I, E;
2148
2149 FRIEC_WLItem(const ExplodedNode *n)
2150 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2151};
2152}
2153
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002154static BugReport *
2155FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002156 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002157
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002158 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2159 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002160 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002161
Ted Kremenek40406fe2010-12-03 06:52:30 +00002162 // If we don't need to suppress any of the nodes because they are
2163 // post-dominated by a sink, simply add all the nodes in the equivalence class
2164 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002165 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002166 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002167 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002168 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002169 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002170 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002171 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002172 }
2173 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002174 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002175 }
2176
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002177 // For bug reports that should be suppressed when all paths are post-dominated
2178 // by a sink node, iterate through the reports in the equivalence class
2179 // until we find one that isn't post-dominated (if one exists). We use a
2180 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2181 // this as a recursive function, but we don't want to risk blowing out the
2182 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002183 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002184
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002185 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002186 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002187
Ted Kremenek40406fe2010-12-03 06:52:30 +00002188 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002189 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002190 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002191 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002192 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002193 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002194 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002195 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002196 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002197 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002198 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002199 continue;
2200 }
2201
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002202 // At this point we know that 'N' is not a sink and it has at least one
2203 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2204 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002205 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002206 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2207
2208 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002209 WL.push_back(errorNode);
2210 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002211
2212 while (!WL.empty()) {
2213 WLItem &WI = WL.back();
2214 assert(!WI.N->succ_empty());
2215
2216 for (; WI.I != WI.E; ++WI.I) {
2217 const ExplodedNode *Succ = *WI.I;
2218 // End-of-path node?
2219 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002220 // If we found an end-of-path node that is not a sink.
2221 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002222 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002223 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002224 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002225 WL.clear();
2226 break;
2227 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002228 // Found a sink? Continue on to the next successor.
2229 continue;
2230 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002231 // Mark the successor as visited. If it hasn't been explored,
2232 // enqueue it to the DFS worklist.
2233 unsigned &mark = Visited[Succ];
2234 if (!mark) {
2235 mark = 1;
2236 WL.push_back(Succ);
2237 break;
2238 }
2239 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002240
2241 // The worklist may have been cleared at this point. First
2242 // check if it is empty before checking the last item.
2243 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002244 WL.pop_back();
2245 }
2246 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002247
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002248 // ExampleReport will be NULL if all the nodes in the equivalence class
2249 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002250 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002251}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002252
Ted Kremenekcf118d42009-02-04 23:49:09 +00002253void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002254 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002255 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002256 if (exampleReport) {
2257 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2258 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2259 E=C.end(); I != E; ++I) {
2260 FlushReport(exampleReport, **I, bugReports);
2261 }
2262 }
2263}
2264
2265void BugReporter::FlushReport(BugReport *exampleReport,
2266 PathDiagnosticConsumer &PD,
2267 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Ted Kremenekcf118d42009-02-04 23:49:09 +00002269 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002270 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002271 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002272
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002273 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002274 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2275 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002276 exampleReport->getDescription(),
2277 exampleReport->getShortDescription(/*Fallback=*/false),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002278 BT.getCategory()));
2279
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002280 // Generate the full path diagnostic, using the generation scheme
Jordan Rosed632d6f2012-09-22 01:24:56 +00002281 // specified by the PathDiagnosticConsumer. Note that we have to generate
2282 // path diagnostics even for consumers which do not support paths, because
2283 // the BugReporterVisitors may mark this bug as a false positive.
2284 if (!bugReports.empty())
2285 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2286 return;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002287
2288 // If the path is empty, generate a single step path with the location
2289 // of the issue.
2290 if (D->path.empty()) {
2291 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2292 PathDiagnosticPiece *piece =
2293 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2294 BugReport::ranges_iterator Beg, End;
2295 llvm::tie(Beg, End) = exampleReport->getRanges();
2296 for ( ; Beg != End; ++Beg)
2297 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002298 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002299 }
2300
Ted Kremenek072192b2008-04-30 23:47:44 +00002301 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002302 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002303 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2304 e = Meta.end(); i != e; ++i) {
2305 D->addMeta(*i);
2306 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002307
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002308 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002309}
Ted Kremenek57202072008-07-14 17:40:50 +00002310
Ted Kremenek07189522012-04-04 18:11:35 +00002311void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002312 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002313 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002314 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002315 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002317 // 'BT' is owned by BugReporter.
2318 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002319 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002320 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002321 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
Jordan Rose785950e2012-11-02 01:53:40 +00002322 emitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002323}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002324
Chris Lattner5f9e2722011-07-23 10:55:15 +00002325BugType *BugReporter::getBugTypeForName(StringRef name,
2326 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002327 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002328 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2329 llvm::StringMapEntry<BugType *> &
2330 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2331 BugType *BT = entry.getValue();
2332 if (!BT) {
2333 BT = new BugType(name, category);
2334 entry.setValue(BT);
2335 }
2336 return BT;
2337}