blob: 8e6bc69cc4ab8203051238a99b667bc38d1015c8 [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"
Ted Kremenek61f3e052008-04-03 04:42:52 +000016#include "clang/AST/ASTContext.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000019#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000020#include "clang/AST/StmtObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Analysis/CFG.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000022#include "clang/Analysis/ProgramPoint.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000025#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000026#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000027#include "llvm/ADT/DenseMap.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000028#include "llvm/ADT/IntrusiveRefCntPtr.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000029#include "llvm/ADT/OwningPtr.h"
30#include "llvm/ADT/STLExtras.h"
31#include "llvm/ADT/SmallString.h"
32#include "llvm/Support/raw_ostream.h"
Ted 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
Jordan Roseafa7cae2012-12-07 19:56:29 +0000194/// "interesting stuff" which means it shouldn't be pruned from the parent path.
195bool BugReporter::RemoveUnneededCalls(PathPieces &pieces, BugReport *R) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000196 bool containsSomethingInteresting = false;
197 const unsigned N = pieces.size();
198
199 for (unsigned i = 0 ; i < N ; ++i) {
200 // Remove the front piece from the path. If it is still something we
201 // want to keep once we are done, we will push it back on the end.
202 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
203 pieces.pop_front();
204
Jordan Roseafa7cae2012-12-07 19:56:29 +0000205 // Throw away pieces with invalid locations. Note that we can't throw away
206 // calls just yet because they might have something interesting inside them.
207 // If so, their locations will be adjusted as necessary later.
Ted Kremeneka43df952012-09-21 00:09:11 +0000208 if (piece->getKind() != PathDiagnosticPiece::Call &&
209 piece->getLocation().asLocation().isInvalid())
210 continue;
211
Ted Kremenek72516742012-03-01 00:05:06 +0000212 switch (piece->getKind()) {
213 case PathDiagnosticPiece::Call: {
214 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000215 // Check if the location context is interesting.
216 assert(LocationContextMap.count(call));
217 if (R->isInteresting(LocationContextMap[call])) {
218 containsSomethingInteresting = true;
219 break;
220 }
Jordan Rose368f3b02012-11-15 02:07:23 +0000221
Jordan Roseafa7cae2012-12-07 19:56:29 +0000222 if (!RemoveUnneededCalls(call->path, R))
Jordan Rose368f3b02012-11-15 02:07:23 +0000223 continue;
Ted Kremeneka43df952012-09-21 00:09:11 +0000224
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000225 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000226 break;
227 }
228 case PathDiagnosticPiece::Macro: {
229 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Jordan Roseafa7cae2012-12-07 19:56:29 +0000230 if (!RemoveUnneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000231 continue;
232 containsSomethingInteresting = true;
233 break;
234 }
235 case PathDiagnosticPiece::Event: {
236 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
Ted Kremeneka43df952012-09-21 00:09:11 +0000237
Ted Kremenek72516742012-03-01 00:05:06 +0000238 // We never throw away an event, but we do throw it away wholesale
239 // as part of a path if we throw the entire path away.
Ted Kremenek22505ef2012-09-08 07:18:18 +0000240 containsSomethingInteresting |= !event->isPrunable();
Ted Kremenek72516742012-03-01 00:05:06 +0000241 break;
242 }
243 case PathDiagnosticPiece::ControlFlow:
244 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000245 }
246
247 pieces.push_back(piece);
248 }
249
250 return containsSomethingInteresting;
251}
252
Jordan Roseafa7cae2012-12-07 19:56:29 +0000253/// Recursively scan through a path and make sure that all call pieces have
254/// valid locations. Note that all other pieces with invalid locations should
255/// have already been pruned out.
256static void adjustCallLocations(PathPieces &Pieces,
257 PathDiagnosticLocation *LastCallLocation = 0) {
258 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E; ++I) {
259 PathDiagnosticCallPiece *Call = dyn_cast<PathDiagnosticCallPiece>(*I);
260
261 if (!Call) {
262 assert((*I)->getLocation().asLocation().isValid());
263 continue;
264 }
265
266 if (LastCallLocation) {
267 if (!Call->callEnter.asLocation().isValid())
268 Call->callEnter = *LastCallLocation;
269 if (!Call->callReturn.asLocation().isValid())
270 Call->callReturn = *LastCallLocation;
271 }
272
273 // Recursively clean out the subclass. Keep this call around if
274 // it contains any informative diagnostics.
275 PathDiagnosticLocation *ThisCallLocation;
276 if (Call->callEnterWithin.asLocation().isValid())
277 ThisCallLocation = &Call->callEnterWithin;
278 else
279 ThisCallLocation = &Call->callEnter;
280
281 assert(ThisCallLocation && "Outermost call has an invalid location");
282 adjustCallLocations(Call->path, ThisCallLocation);
283 }
284}
285
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000286//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000287// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000288//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000289
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000290typedef llvm::DenseMap<const ExplodedNode*,
291const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000292
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000293namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000294class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000295 NodeBackMap& M;
296public:
297 NodeMapClosure(NodeBackMap *m) : M(*m) {}
298 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek9c378f72011-08-12 23:37:29 +0000300 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000301 NodeBackMap::iterator I = M.find(N);
302 return I == M.end() ? 0 : I->second;
303 }
304};
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000306class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000307 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000308 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000309 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000310 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000311public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000312 const LocationContext *LC;
313
Ted Kremenek8966bc12009-05-06 21:39:49 +0000314 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000315 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000316 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000317 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000318 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
319 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek9c378f72011-08-12 23:37:29 +0000321 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek9c378f72011-08-12 23:37:29 +0000323 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
324 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Anna Zaks8e6431a2011-08-18 22:37:56 +0000326 BugReport *getBugReport() { return R; }
327
Tom Care212f6d32010-09-16 03:50:38 +0000328 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000329
330 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000332 const Stmt *getParent(const Stmt *S) {
333 return getParentMap().getParent(S);
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek8966bc12009-05-06 21:39:49 +0000336 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000337
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000338 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
David Blaikieef3643f2011-09-26 00:51:36 +0000340 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
341 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000342 }
343
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000344 bool supportsLogicalOpControlFlow() const {
345 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000346 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000347};
348} // end anonymous namespace
349
Ted Kremenek00605e02009-03-27 20:55:39 +0000350PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000351PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000352 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000353 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000354
Anna Zaks0cd59482011-09-16 19:18:30 +0000355 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
356 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000357}
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek00605e02009-03-27 20:55:39 +0000359PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000360PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
361 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000362
Ted Kremenek143ca222008-05-06 18:11:09 +0000363 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000364 if (os.str().empty())
365 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek00605e02009-03-27 20:55:39 +0000367 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek00605e02009-03-27 20:55:39 +0000369 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000370 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000371 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000372 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000373 else {
374 os << "Execution jumps to the end of the ";
375 const Decl *D = N->getLocationContext()->getDecl();
376 if (isa<ObjCMethodDecl>(D))
377 os << "method";
378 else if (isa<FunctionDecl>(D))
379 os << "function";
380 else {
381 assert(isa<BlockDecl>(D));
382 os << "anonymous block";
383 }
384 os << '.';
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000387 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000388}
389
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000390static bool IsNested(const Stmt *S, ParentMap &PM) {
391 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
392 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000394 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000396 if (Parent)
397 switch (Parent->getStmtClass()) {
398 case Stmt::ForStmtClass:
399 case Stmt::DoStmtClass:
400 case Stmt::WhileStmtClass:
401 return true;
402 default:
403 break;
404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
406 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000407}
408
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000409PathDiagnosticLocation
410PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000411 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000412 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000413 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000414
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000415 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000416 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000418 if (!Parent)
419 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000421 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000422 case Stmt::BinaryOperatorClass: {
423 const BinaryOperator *B = cast<BinaryOperator>(Parent);
424 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000425 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000426 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000427 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000428 case Stmt::CompoundStmtClass:
429 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000430 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000431 case Stmt::ChooseExprClass:
432 // Similar to '?' if we are referring to condition, just have the edge
433 // point to the entire choose expression.
434 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000435 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000436 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000437 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000438 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000439 case Stmt::ConditionalOperatorClass:
440 // For '?', if we are referring to condition, just have the edge point
441 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000442 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000443 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000444 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000445 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000446 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000447 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000448 case Stmt::ForStmtClass:
449 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000450 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000451 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000452 case Stmt::IfStmtClass:
453 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000454 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000455 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000456 case Stmt::ObjCForCollectionStmtClass:
457 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000458 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000459 break;
460 case Stmt::WhileStmtClass:
461 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000462 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000463 break;
464 default:
465 break;
466 }
467
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000468 S = Parent;
469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000471 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000472
473 // Special case: DeclStmts can appear in for statement declarations, in which
474 // case the ForStmt is the context.
475 if (isa<DeclStmt>(S)) {
476 if (const Stmt *Parent = P.getParent(S)) {
477 switch (Parent->getStmtClass()) {
478 case Stmt::ForStmtClass:
479 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000480 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000481 default:
482 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000483 }
484 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000485 }
486 else if (isa<BinaryOperator>(S)) {
487 // Special case: the binary operator represents the initialization
488 // code in a for statement (this can happen when the variable being
489 // initialized is an old variable.
490 if (const ForStmt *FS =
491 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
492 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000493 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000494 }
495 }
496
Anna Zaks220ac8c2011-09-15 01:08:34 +0000497 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000498}
499
Ted Kremenekcf118d42009-02-04 23:49:09 +0000500//===----------------------------------------------------------------------===//
Jordan Rosed632d6f2012-09-22 01:24:56 +0000501// "Visitors only" path diagnostic generation algorithm.
502//===----------------------------------------------------------------------===//
503static bool GenerateVisitorsOnlyPathDiagnostic(PathDiagnostic &PD,
504 PathDiagnosticBuilder &PDB,
505 const ExplodedNode *N,
506 ArrayRef<BugReporterVisitor *> visitors) {
507 // All path generation skips the very first node (the error node).
508 // This is because there is special handling for the end-of-path note.
509 N = N->getFirstPred();
510 if (!N)
511 return true;
512
513 BugReport *R = PDB.getBugReport();
514 while (const ExplodedNode *Pred = N->getFirstPred()) {
515 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
516 E = visitors.end();
517 I != E; ++I) {
518 // Visit all the node pairs, but throw the path pieces away.
519 PathDiagnosticPiece *Piece = (*I)->VisitNode(N, Pred, PDB, *R);
520 delete Piece;
521 }
522
523 N = Pred;
524 }
525
526 return R->isValid();
527}
528
529//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000530// "Minimal" path diagnostic generation algorithm.
531//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000532typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
533typedef SmallVector<StackDiagPair, 6> StackDiagVector;
534
Anna Zaks368a0d52012-03-15 21:13:02 +0000535static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000536 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000537 // If the piece contains a special message, add it to all the call
538 // pieces on the active stack.
539 if (PathDiagnosticEventPiece *ep =
540 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000541
Anna Zaks56a938f2012-03-16 23:24:20 +0000542 if (ep->hasCallStackHint())
543 for (StackDiagVector::iterator I = CallStack.begin(),
544 E = CallStack.end(); I != E; ++I) {
545 PathDiagnosticCallPiece *CP = I->first;
546 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000547 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000548
Anna Zaks368a0d52012-03-15 21:13:02 +0000549 // The last message on the path to final bug is the most important
550 // one. Since we traverse the path backwards, do not add the message
551 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000552 if (!CP->hasCallStackMessage())
553 CP->setCallStackMessage(stackMsg);
554 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000555 }
556}
Ted Kremenek31061982009-03-31 23:00:32 +0000557
Ted Kremenek77d09442012-03-02 01:27:31 +0000558static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000559
Jordan Rose8347d3d2012-09-22 01:24:53 +0000560static bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
Ted Kremenek31061982009-03-31 23:00:32 +0000561 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000562 const ExplodedNode *N,
563 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000564
Ted Kremenek31061982009-03-31 23:00:32 +0000565 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000566 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000567 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000568 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000569
Anna Zaks56a938f2012-03-16 23:24:20 +0000570 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000571
Ted Kremenek31061982009-03-31 23:00:32 +0000572 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000573 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000574 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000575 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenek31061982009-03-31 23:00:32 +0000577 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000578
Anna Zaks80de4872012-08-29 21:22:37 +0000579 do {
580 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
581 PathDiagnosticCallPiece *C =
582 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
583 GRBugReporter& BR = PDB.getBugReporter();
584 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
585 PD.getActivePath().push_front(C);
586 PD.pushActivePath(&C->path);
587 CallStack.push_back(StackDiagPair(C, N));
588 break;
Anna Zaks93739372012-03-14 18:58:28 +0000589 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000590
Anna Zaks80de4872012-08-29 21:22:37 +0000591 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
592 // Flush all locations, and pop the active path.
593 bool VisitedEntireCall = PD.isWithinCall();
594 PD.popActivePath();
595
596 // Either we just added a bunch of stuff to the top-level path, or
597 // we have a previous CallExitEnd. If the former, it means that the
598 // path terminated within a function call. We must then take the
599 // current contents of the active path and place it within
600 // a new PathDiagnosticCallPiece.
601 PathDiagnosticCallPiece *C;
602 if (VisitedEntireCall) {
603 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
604 } else {
605 const Decl *Caller = CE->getLocationContext()->getDecl();
606 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
607 GRBugReporter& BR = PDB.getBugReporter();
608 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
609 }
610
611 C->setCallee(*CE, SMgr);
612 if (!CallStack.empty()) {
613 assert(CallStack.back().first == C);
614 CallStack.pop_back();
615 }
616 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Anna Zaks80de4872012-08-29 21:22:37 +0000619 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
620 const CFGBlock *Src = BE->getSrc();
621 const CFGBlock *Dst = BE->getDst();
622 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Anna Zaks80de4872012-08-29 21:22:37 +0000624 if (!T)
625 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Anna Zaks80de4872012-08-29 21:22:37 +0000627 PathDiagnosticLocation Start =
628 PathDiagnosticLocation::createBegin(T, SMgr,
629 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Anna Zaks80de4872012-08-29 21:22:37 +0000631 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000632 default:
633 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek31061982009-03-31 23:00:32 +0000635 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000636 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000637 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000640 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek31061982009-03-31 23:00:32 +0000642 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000643 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000644 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek31061982009-03-31 23:00:32 +0000646 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000647 << End.asLocation().getExpansionLineNumber();
648 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
649 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000650 break;
651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
653 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000654 // Figure out what case arm we took.
655 std::string sbuf;
656 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek9c378f72011-08-12 23:37:29 +0000658 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000659 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek31061982009-03-31 23:00:32 +0000661 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000662 default:
663 os << "No cases match in the switch statement. "
664 "Control jumps to line "
665 << End.asLocation().getExpansionLineNumber();
666 break;
667 case Stmt::DefaultStmtClass:
668 os << "Control jumps to the 'default' case at line "
669 << End.asLocation().getExpansionLineNumber();
670 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Anna Zaks80de4872012-08-29 21:22:37 +0000672 case Stmt::CaseStmtClass: {
673 os << "Control jumps to 'case ";
674 const CaseStmt *Case = cast<CaseStmt>(S);
675 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Anna Zaks80de4872012-08-29 21:22:37 +0000677 // Determine if it is an enum.
678 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Anna Zaks80de4872012-08-29 21:22:37 +0000680 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
681 // FIXME: Maybe this should be an assertion. Are there cases
682 // were it is not an EnumConstantDecl?
683 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000684 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Anna Zaks80de4872012-08-29 21:22:37 +0000686 if (D) {
687 GetRawInt = false;
688 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000689 }
Ted Kremenek31061982009-03-31 23:00:32 +0000690 }
Anna Zaks80de4872012-08-29 21:22:37 +0000691
692 if (GetRawInt)
693 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
694
695 os << ":' at line "
696 << End.asLocation().getExpansionLineNumber();
697 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000698 }
Anna Zaks80de4872012-08-29 21:22:37 +0000699 }
700 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
701 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000702 }
703 else {
704 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000705 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000706 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
707 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek31061982009-03-31 23:00:32 +0000710 break;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek31061982009-03-31 23:00:32 +0000713 case Stmt::BreakStmtClass:
714 case Stmt::ContinueStmtClass: {
715 std::string sbuf;
716 llvm::raw_string_ostream os(sbuf);
717 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000718 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
719 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000720 break;
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Anna Zaks80de4872012-08-29 21:22:37 +0000723 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000724 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000725 case Stmt::ConditionalOperatorClass: {
726 std::string sbuf;
727 llvm::raw_string_ostream os(sbuf);
728 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek31061982009-03-31 23:00:32 +0000730 if (*(Src->succ_begin()+1) == Dst)
731 os << "false";
732 else
733 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek31061982009-03-31 23:00:32 +0000735 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek31061982009-03-31 23:00:32 +0000737 if (const Stmt *S = End.asStmt())
738 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Anna Zaks80de4872012-08-29 21:22:37 +0000740 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
741 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000742 break;
743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Anna Zaks80de4872012-08-29 21:22:37 +0000745 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000746 case Stmt::BinaryOperatorClass: {
747 if (!PDB.supportsLogicalOpControlFlow())
748 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000750 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000751 std::string sbuf;
752 llvm::raw_string_ostream os(sbuf);
753 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000754
John McCall2de56d12010-08-25 11:45:40 +0000755 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000756 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek31061982009-03-31 23:00:32 +0000758 if (*(Src->succ_begin()+1) == Dst) {
759 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000760 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000761 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000762 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
763 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
764 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000765 }
Ted Kremenek31061982009-03-31 23:00:32 +0000766 else {
767 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000768 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000769 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000770 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
771 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000772 }
Ted Kremenek31061982009-03-31 23:00:32 +0000773 }
774 else {
John McCall2de56d12010-08-25 11:45:40 +0000775 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000776 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek31061982009-03-31 23:00:32 +0000778 if (*(Src->succ_begin()+1) == Dst) {
779 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000780 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000781 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000782 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
783 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000784 }
785 else {
786 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000787 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000788 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000789 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
790 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
791 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000792 }
793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek31061982009-03-31 23:00:32 +0000795 break;
796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
798 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000799 if (*(Src->succ_begin()) == Dst) {
800 std::string sbuf;
801 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek31061982009-03-31 23:00:32 +0000803 os << "Loop condition is true. ";
804 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek31061982009-03-31 23:00:32 +0000806 if (const Stmt *S = End.asStmt())
807 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Anna Zaks80de4872012-08-29 21:22:37 +0000809 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
810 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000811 }
812 else {
813 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek31061982009-03-31 23:00:32 +0000815 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, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek31061982009-03-31 23:00:32 +0000822 break;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek31061982009-03-31 23:00:32 +0000825 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000826 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000827 if (*(Src->succ_begin()+1) == Dst) {
828 std::string sbuf;
829 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek31061982009-03-31 23:00:32 +0000831 os << "Loop condition is false. ";
832 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
833 if (const Stmt *S = End.asStmt())
834 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Anna Zaks80de4872012-08-29 21:22:37 +0000836 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
837 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000838 }
839 else {
840 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
841 if (const Stmt *S = End.asStmt())
842 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Anna Zaks80de4872012-08-29 21:22:37 +0000844 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
845 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek31061982009-03-31 23:00:32 +0000848 break;
849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek31061982009-03-31 23:00:32 +0000851 case Stmt::IfStmtClass: {
852 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek31061982009-03-31 23:00:32 +0000854 if (const Stmt *S = End.asStmt())
855 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek31061982009-03-31 23:00:32 +0000857 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000858 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
859 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000860 else
Anna Zaks80de4872012-08-29 21:22:37 +0000861 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
862 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Ted Kremenek31061982009-03-31 23:00:32 +0000864 break;
865 }
Anna Zaks80de4872012-08-29 21:22:37 +0000866 }
Ted Kremenek31061982009-03-31 23:00:32 +0000867 }
Anna Zaks80de4872012-08-29 21:22:37 +0000868 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000870 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000871 // Add diagnostic pieces from custom visitors.
872 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000873 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
874 E = visitors.end();
875 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000876 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000877 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000878 updateStackPiecesWithMessage(p, CallStack);
879 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000880 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000881 }
Ted Kremenek31061982009-03-31 23:00:32 +0000882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Jordan Rose8347d3d2012-09-22 01:24:53 +0000884 if (!PDB.getBugReport()->isValid())
885 return false;
886
Ted Kremenek14856d72009-04-06 23:06:54 +0000887 // After constructing the full PathDiagnostic, do a pass over it to compact
888 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000889 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Jordan Rose8347d3d2012-09-22 01:24:53 +0000890 return true;
Ted Kremenek31061982009-03-31 23:00:32 +0000891}
892
893//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000894// "Extensive" PathDiagnostic generation.
895//===----------------------------------------------------------------------===//
896
897static bool IsControlFlowExpr(const Stmt *S) {
898 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000900 if (!E)
901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
903 E = E->IgnoreParenCasts();
904
John McCall56ca35d2011-02-17 10:25:35 +0000905 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000906 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000908 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
909 if (B->isLogicalOp())
910 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
912 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000913}
914
Ted Kremenek14856d72009-04-06 23:06:54 +0000915namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000916class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000917 bool IsDead;
918public:
919 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
920 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000921
922 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000923 bool isDead() const { return IsDead; }
924};
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000926class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000927 std::vector<ContextLocation> CLocs;
928 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000929 PathDiagnostic &PD;
930 PathDiagnosticBuilder &PDB;
931 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000933 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Ted Kremenek14856d72009-04-06 23:06:54 +0000935 bool containsLocation(const PathDiagnosticLocation &Container,
936 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Ted Kremenek14856d72009-04-06 23:06:54 +0000938 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Ted Kremenek9650cf32009-05-11 21:42:34 +0000940 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
941 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000942 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000943 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000944 while (1) {
945 // Adjust the location for some expressions that are best referenced
946 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000947 switch (S->getStmtClass()) {
948 default:
949 break;
950 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000951 case Stmt::GenericSelectionExprClass:
952 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000953 firstCharOnly = true;
954 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000955 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000956 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000957 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000958 firstCharOnly = true;
959 continue;
960 case Stmt::ChooseExprClass:
961 S = cast<ChooseExpr>(S)->getCond();
962 firstCharOnly = true;
963 continue;
964 case Stmt::BinaryOperatorClass:
965 S = cast<BinaryOperator>(S)->getLHS();
966 firstCharOnly = true;
967 continue;
968 }
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremenek9650cf32009-05-11 21:42:34 +0000970 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek9650cf32009-05-11 21:42:34 +0000973 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000974 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremenek9650cf32009-05-11 21:42:34 +0000977 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000978 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000979
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000980 return L;
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek14856d72009-04-06 23:06:54 +0000983 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000984 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000985 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000986 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000987 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000988 CLocs.pop_back();
989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Ted Kremenek14856d72009-04-06 23:06:54 +0000991public:
992 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
993 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Ted Kremeneka301a672009-04-22 18:16:20 +0000995 // If the PathDiagnostic already has pieces, add the enclosing statement
996 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000997 if (!PD.path.empty()) {
998 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek14856d72009-04-06 23:06:54 +00001000 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +00001001 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +00001002 }
1003 }
1004
1005 ~EdgeBuilder() {
1006 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +00001007
Ted Kremeneka301a672009-04-22 18:16:20 +00001008 // Finally, add an initial edge from the start location of the first
1009 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +00001010 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +00001011 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +00001012 PDB.getSourceManager());
1013 if (L.isValid())
1014 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +00001015 }
1016
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001017 void flushLocations() {
1018 while (!CLocs.empty())
1019 popLocation();
1020 PrevLoc = PathDiagnosticLocation();
1021 }
1022
Ted Kremenek14856d72009-04-06 23:06:54 +00001023 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001025 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Ted Kremenek14856d72009-04-06 23:06:54 +00001027 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001028 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001029 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +00001030};
Ted Kremenek14856d72009-04-06 23:06:54 +00001031} // end anonymous namespace
1032
1033
1034PathDiagnosticLocation
1035EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1036 if (const Stmt *S = L.asStmt()) {
1037 if (IsControlFlowExpr(S))
1038 return L;
Mike Stump1eb44332009-09-09 15:08:12 +00001039
1040 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +00001041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremenek14856d72009-04-06 23:06:54 +00001043 return L;
1044}
1045
1046bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1047 const PathDiagnosticLocation &Containee) {
1048
1049 if (Container == Containee)
1050 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenek14856d72009-04-06 23:06:54 +00001052 if (Container.asDecl())
1053 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 if (const Stmt *S = Containee.asStmt())
1056 if (const Stmt *ContainerS = Container.asStmt()) {
1057 while (S) {
1058 if (S == ContainerS)
1059 return true;
1060 S = PDB.getParent(S);
1061 }
1062 return false;
1063 }
1064
1065 // Less accurate: compare using source ranges.
1066 SourceRange ContainerR = Container.asRange();
1067 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Ted Kremenek14856d72009-04-06 23:06:54 +00001069 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001070 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1071 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1072 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1073 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Chandler Carruth64211622011-07-25 21:09:52 +00001075 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1076 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1077 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1078 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremenek14856d72009-04-06 23:06:54 +00001080 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001081 assert(ContaineeBegLine <= ContaineeEndLine);
1082
Ted Kremenek14856d72009-04-06 23:06:54 +00001083 return (ContainerBegLine <= ContaineeBegLine &&
1084 ContainerEndLine >= ContaineeEndLine &&
1085 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001086 SM.getExpansionColumnNumber(ContainerRBeg) <=
1087 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001088 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001089 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +00001090 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001091}
1092
Ted Kremenek14856d72009-04-06 23:06:54 +00001093void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1094 if (!PrevLoc.isValid()) {
1095 PrevLoc = NewLoc;
1096 return;
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001099 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1100 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremeneka43df952012-09-21 00:09:11 +00001102 if (PrevLocClean.asLocation().isInvalid()) {
1103 PrevLoc = NewLoc;
1104 return;
1105 }
1106
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001107 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001108 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Ted Kremenek14856d72009-04-06 23:06:54 +00001110 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001111 if (NewLocClean.asLocation().getExpansionLoc() ==
1112 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001113 return;
1114
Ted Kremenek2042fc12012-02-24 06:00:00 +00001115 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001116 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001117}
1118
1119void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Ted Kremeneka301a672009-04-22 18:16:20 +00001121 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1122 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Ted Kremenek14856d72009-04-06 23:06:54 +00001124 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1125
1126 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001127 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Ted Kremenek14856d72009-04-06 23:06:54 +00001129 // Is the top location context the same as the one for the new location?
1130 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001131 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001132 if (IsConsumedExpr(TopContextLoc) &&
1133 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001134 TopContextLoc.markDead();
1135
Ted Kremenek14856d72009-04-06 23:06:54 +00001136 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001137 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001138
1139 return;
1140 }
1141
1142 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001143 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001144 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001146 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001147 CLocs.push_back(ContextLocation(CLoc, true));
1148 return;
1149 }
1150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremenek14856d72009-04-06 23:06:54 +00001152 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001153 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001154 }
1155
1156 // Context does not contain the location. Flush it.
1157 popLocation();
1158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001160 // If we reach here, there is no enclosing context. Just add the edge.
1161 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001162}
1163
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001164bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1165 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1166 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001168 return false;
1169}
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremeneke1baed32009-05-05 23:13:38 +00001171void EdgeBuilder::addExtendedContext(const Stmt *S) {
1172 if (!S)
1173 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001174
1175 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001176 while (Parent) {
1177 if (isa<CompoundStmt>(Parent))
1178 Parent = PDB.getParent(Parent);
1179 else
1180 break;
1181 }
1182
1183 if (Parent) {
1184 switch (Parent->getStmtClass()) {
1185 case Stmt::DoStmtClass:
1186 case Stmt::ObjCAtSynchronizedStmtClass:
1187 addContext(Parent);
1188 default:
1189 break;
1190 }
1191 }
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremeneke1baed32009-05-05 23:13:38 +00001193 addContext(S);
1194}
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremenek14856d72009-04-06 23:06:54 +00001196void EdgeBuilder::addContext(const Stmt *S) {
1197 if (!S)
1198 return;
1199
Ted Kremenek59950d32012-02-24 07:12:52 +00001200 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001201 addContext(L);
1202}
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Jordan Rose183ba8e2012-07-26 20:04:05 +00001204void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001205 while (!CLocs.empty()) {
1206 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1207
1208 // Is the top location context the same as the one for the new location?
1209 if (TopContextLoc == L)
1210 return;
1211
1212 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001213 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001214 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001215 }
1216
1217 // Context does not contain the location. Flush it.
1218 popLocation();
1219 }
1220
1221 CLocs.push_back(L);
1222}
1223
Ted Kremenek11abcec2012-05-02 00:31:29 +00001224// Cone-of-influence: support the reverse propagation of "interesting" symbols
1225// and values by tracing interesting calculations backwards through evaluated
1226// expressions along a path. This is probably overly complicated, but the idea
1227// is that if an expression computed an "interesting" value, the child
1228// expressions are are also likely to be "interesting" as well (which then
1229// propagates to the values they in turn compute). This reverse propagation
1230// is needed to track interesting correlations across function call boundaries,
1231// where formal arguments bind to actual arguments, etc. This is also needed
1232// because the constraint solver sometimes simplifies certain symbolic values
1233// into constants when appropriate, and this complicates reasoning about
1234// interesting values.
1235typedef llvm::DenseSet<const Expr *> InterestingExprs;
1236
1237static void reversePropagateIntererstingSymbols(BugReport &R,
1238 InterestingExprs &IE,
1239 const ProgramState *State,
1240 const Expr *Ex,
1241 const LocationContext *LCtx) {
1242 SVal V = State->getSVal(Ex, LCtx);
1243 if (!(R.isInteresting(V) || IE.count(Ex)))
1244 return;
1245
1246 switch (Ex->getStmtClass()) {
1247 default:
1248 if (!isa<CastExpr>(Ex))
1249 break;
1250 // Fall through.
1251 case Stmt::BinaryOperatorClass:
1252 case Stmt::UnaryOperatorClass: {
1253 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1254 CE = Ex->child_end();
1255 CI != CE; ++CI) {
1256 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1257 IE.insert(child);
1258 SVal ChildV = State->getSVal(child, LCtx);
1259 R.markInteresting(ChildV);
1260 }
1261 break;
1262 }
1263 }
1264 }
1265
1266 R.markInteresting(V);
1267}
1268
1269static void reversePropagateInterestingSymbols(BugReport &R,
1270 InterestingExprs &IE,
1271 const ProgramState *State,
1272 const LocationContext *CalleeCtx,
1273 const LocationContext *CallerCtx)
1274{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001275 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001276 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1277 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001278 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001279 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1280 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1281 PE = FD->param_end();
1282 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1283 for (; AI != AE && PI != PE; ++AI, ++PI) {
1284 if (const Expr *ArgE = *AI) {
1285 if (const ParmVarDecl *PD = *PI) {
1286 Loc LV = State->getLValue(PD, CalleeCtx);
1287 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1288 IE.insert(ArgE);
1289 }
1290 }
1291 }
1292 }
1293 }
1294}
1295
Jordan Rose8347d3d2012-09-22 01:24:53 +00001296static bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek14856d72009-04-06 23:06:54 +00001297 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001298 const ExplodedNode *N,
1299 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001300 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001301 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001302 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001303 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001304
Ted Kremenek9c378f72011-08-12 23:37:29 +00001305 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001306 while (NextNode) {
1307 N = NextNode;
1308 NextNode = GetPredecessorNode(N);
1309 ProgramPoint P = N->getLocation();
1310
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001311 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001312 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1313 if (const Expr *Ex = PS->getStmtAs<Expr>())
1314 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1315 N->getState().getPtr(), Ex,
1316 N->getLocationContext());
1317 }
1318
Anna Zaks0b3ade82012-04-20 21:59:08 +00001319 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001320 const Stmt *S = CE->getCalleeContext()->getCallSite();
1321 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001322 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1323 N->getState().getPtr(), Ex,
1324 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001325 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001326
1327 PathDiagnosticCallPiece *C =
1328 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001329 GRBugReporter& BR = PDB.getBugReporter();
1330 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001331
1332 EB.addEdge(C->callReturn, true);
1333 EB.flushLocations();
1334
1335 PD.getActivePath().push_front(C);
1336 PD.pushActivePath(&C->path);
1337 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001338 break;
1339 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001340
Ted Kremenek2042fc12012-02-24 06:00:00 +00001341 // Pop the call hierarchy if we are done walking the contents
1342 // of a function call.
1343 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001344 // Add an edge to the start of the function.
1345 const Decl *D = CE->getCalleeContext()->getDecl();
1346 PathDiagnosticLocation pos =
1347 PathDiagnosticLocation::createBegin(D, SM);
1348 EB.addEdge(pos);
1349
1350 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001351 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001352 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001353 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001354 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001355
Jordan Rose183ba8e2012-07-26 20:04:05 +00001356 // Either we just added a bunch of stuff to the top-level path, or
1357 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001358 // path terminated within a function call. We must then take the
1359 // current contents of the active path and place it within
1360 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001361 PathDiagnosticCallPiece *C;
1362 if (VisitedEntireCall) {
1363 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1364 } else {
1365 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001366 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001367 GRBugReporter& BR = PDB.getBugReporter();
1368 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001369 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001370
Jordan Rose183ba8e2012-07-26 20:04:05 +00001371 C->setCallee(*CE, SM);
1372 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001373
1374 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001375 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001376 CallStack.pop_back();
1377 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001378 break;
1379 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001380
1381 // Note that is important that we update the LocationContext
1382 // after looking at CallExits. CallExit basically adds an
1383 // edge in the *caller*, so we don't want to update the LocationContext
1384 // too soon.
1385 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001386
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001387 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001388 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1389 // Does this represent entering a call? If so, look at propagating
1390 // interesting symbols across call boundaries.
1391 if (NextNode) {
1392 const LocationContext *CallerCtx = NextNode->getLocationContext();
1393 const LocationContext *CalleeCtx = PDB.LC;
1394 if (CallerCtx != CalleeCtx) {
1395 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1396 N->getState().getPtr(),
1397 CalleeCtx, CallerCtx);
1398 }
1399 }
1400
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001401 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001402 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001403 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001404 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001406 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1407 CS = dyn_cast<CompoundStmt>(FS->getBody());
1408 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1409 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001411 PathDiagnosticEventPiece *p =
1412 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001413 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001414 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001416 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001417 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001419 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001420 PathDiagnosticLocation BL =
1421 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001422 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001423 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001424 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001425
1426 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001427 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001429 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001430 }
1431
Mike Stump1eb44332009-09-09 15:08:12 +00001432 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Jordan Rose1a7bcc42012-09-12 22:48:08 +00001433 CFGElement First = BE->getFirstElement();
1434 if (const CFGStmt *S = First.getAs<CFGStmt>()) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001435 const Stmt *stmt = S->getStmt();
1436 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001437 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001438 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001439 }
1440 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001441 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001442 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001443
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001444 break;
1445 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001446
1447
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001448 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001450 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001451 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Anna Zaks8e6431a2011-08-18 22:37:56 +00001453 // Add pieces from custom visitors.
1454 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001455 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1456 E = visitors.end();
1457 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001458 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001459 const PathDiagnosticLocation &Loc = p->getLocation();
1460 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001461 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001462 updateStackPiecesWithMessage(p, CallStack);
1463
Ted Kremenek8966bc12009-05-06 21:39:49 +00001464 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001465 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001466 }
Mike Stump1eb44332009-09-09 15:08:12 +00001467 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001468 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00001469
1470 return PDB.getBugReport()->isValid();
Ted Kremenek14856d72009-04-06 23:06:54 +00001471}
1472
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001473//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001474// Methods for BugType and subclasses.
1475//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001476BugType::~BugType() { }
1477
Ted Kremenekcf118d42009-02-04 23:49:09 +00001478void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001479
David Blaikie99ba9e32011-12-20 02:48:34 +00001480void BuiltinBug::anchor() {}
1481
Ted Kremenekcf118d42009-02-04 23:49:09 +00001482//===----------------------------------------------------------------------===//
1483// Methods for BugReport and subclasses.
1484//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001485
David Blaikie99ba9e32011-12-20 02:48:34 +00001486void BugReport::NodeResolver::anchor() {}
1487
Anna Zaks8e6431a2011-08-18 22:37:56 +00001488void BugReport::addVisitor(BugReporterVisitor* visitor) {
1489 if (!visitor)
1490 return;
1491
1492 llvm::FoldingSetNodeID ID;
1493 visitor->Profile(ID);
1494 void *InsertPos;
1495
1496 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1497 delete visitor;
1498 return;
1499 }
1500
1501 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001502 Callbacks.push_back(visitor);
1503 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001504}
1505
1506BugReport::~BugReport() {
1507 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001508 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001509 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001510 while (!interestingSymbols.empty()) {
1511 popInterestingSymbolsAndRegions();
1512 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001513}
Anna Zakse172e8b2011-08-17 23:00:25 +00001514
Ted Kremenek07189522012-04-04 18:11:35 +00001515const Decl *BugReport::getDeclWithIssue() const {
1516 if (DeclWithIssue)
1517 return DeclWithIssue;
1518
1519 const ExplodedNode *N = getErrorNode();
1520 if (!N)
1521 return 0;
1522
1523 const LocationContext *LC = N->getLocationContext();
1524 return LC->getCurrentStackFrame()->getDecl();
1525}
1526
Anna Zakse172e8b2011-08-17 23:00:25 +00001527void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1528 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001529 hash.AddString(Description);
Anna Zaks97bfb552013-01-08 00:25:29 +00001530 PathDiagnosticLocation UL = getUniqueingLocation();
1531 if (UL.isValid()) {
1532 UL.Profile(hash);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001533 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001534 Location.Profile(hash);
1535 } else {
1536 assert(ErrorNode);
1537 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1538 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001539
1540 for (SmallVectorImpl<SourceRange>::const_iterator I =
1541 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1542 const SourceRange range = *I;
1543 if (!range.isValid())
1544 continue;
1545 hash.AddInteger(range.getBegin().getRawEncoding());
1546 hash.AddInteger(range.getEnd().getRawEncoding());
1547 }
1548}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001549
Ted Kremenek76aadc32012-03-09 01:13:14 +00001550void BugReport::markInteresting(SymbolRef sym) {
1551 if (!sym)
1552 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001553
1554 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001555 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001556 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001557
1558 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001559 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001560}
1561
1562void BugReport::markInteresting(const MemRegion *R) {
1563 if (!R)
1564 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001565
1566 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001567 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001568 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001569 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001570
Ted Kremenek76aadc32012-03-09 01:13:14 +00001571 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001572 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001573}
1574
1575void BugReport::markInteresting(SVal V) {
1576 markInteresting(V.getAsRegion());
1577 markInteresting(V.getAsSymbol());
1578}
1579
Anna Zaks80de4872012-08-29 21:22:37 +00001580void BugReport::markInteresting(const LocationContext *LC) {
1581 if (!LC)
1582 return;
1583 InterestingLocationContexts.insert(LC);
1584}
1585
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001586bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001587 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1588}
1589
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001590bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001591 if (!sym)
1592 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001593 // We don't currently consider metadata symbols to be interesting
1594 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001595 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001596}
1597
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001598bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001599 if (!R)
1600 return false;
1601 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001602 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001603 if (b)
1604 return true;
1605 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001606 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001607 return false;
1608}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001609
Anna Zaks80de4872012-08-29 21:22:37 +00001610bool BugReport::isInteresting(const LocationContext *LC) {
1611 if (!LC)
1612 return false;
1613 return InterestingLocationContexts.count(LC);
1614}
1615
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001616void BugReport::lazyInitializeInterestingSets() {
1617 if (interestingSymbols.empty()) {
1618 interestingSymbols.push_back(new Symbols());
1619 interestingRegions.push_back(new Regions());
1620 }
1621}
1622
1623BugReport::Symbols &BugReport::getInterestingSymbols() {
1624 lazyInitializeInterestingSets();
1625 return *interestingSymbols.back();
1626}
1627
1628BugReport::Regions &BugReport::getInterestingRegions() {
1629 lazyInitializeInterestingSets();
1630 return *interestingRegions.back();
1631}
1632
1633void BugReport::pushInterestingSymbolsAndRegions() {
1634 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1635 interestingRegions.push_back(new Regions(getInterestingRegions()));
1636}
1637
1638void BugReport::popInterestingSymbolsAndRegions() {
1639 delete interestingSymbols.back();
1640 interestingSymbols.pop_back();
1641 delete interestingRegions.back();
1642 interestingRegions.pop_back();
1643}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001644
Ted Kremenek9c378f72011-08-12 23:37:29 +00001645const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001646 if (!ErrorNode)
1647 return 0;
1648
Tom Care212f6d32010-09-16 03:50:38 +00001649 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001650 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Ted Kremenek9c378f72011-08-12 23:37:29 +00001652 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001653 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001654 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001655 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001656 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001657 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001658 S = GetStmt(ProgP);
1659
1660 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001661}
1662
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001663std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001664BugReport::getRanges() {
1665 // If no custom ranges, add the range of the statement corresponding to
1666 // the error node.
1667 if (Ranges.empty()) {
1668 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1669 addRange(E->getSourceRange());
1670 else
1671 return std::make_pair(ranges_iterator(), ranges_iterator());
1672 }
1673
Anna Zaks14924262011-08-24 20:31:06 +00001674 // User-specified absence of range info.
1675 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1676 return std::make_pair(ranges_iterator(), ranges_iterator());
1677
Anna Zakse172e8b2011-08-17 23:00:25 +00001678 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001679}
1680
Anna Zaks590dd8e2011-09-20 21:38:35 +00001681PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001682 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001683 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001684 "Either Location or ErrorNode should be specified but not both.");
1685
Ted Kremenek9c378f72011-08-12 23:37:29 +00001686 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001687 const LocationContext *LC = ErrorNode->getLocationContext();
1688
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001689 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001690 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001691 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001692 // For binary operators, return the location of the operator.
1693 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001694 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001695
Jordan Rose63bc1862012-11-15 19:11:43 +00001696 if (isa<PostStmtPurgeDeadSymbols>(ErrorNode->getLocation()))
1697 return PathDiagnosticLocation::createEnd(S, SM, LC);
1698
Anna Zaks590dd8e2011-09-20 21:38:35 +00001699 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001700 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001701 } else {
1702 assert(Location.isValid());
1703 return Location;
1704 }
1705
Anna Zaks590dd8e2011-09-20 21:38:35 +00001706 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001707}
1708
Ted Kremenekcf118d42009-02-04 23:49:09 +00001709//===----------------------------------------------------------------------===//
1710// Methods for BugReporter and subclasses.
1711//===----------------------------------------------------------------------===//
1712
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001713BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001714GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001715BugReporterData::~BugReporterData() {}
1716
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001717ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001718
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001719ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001720GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1721
Anna Zaks3b030a22011-08-19 01:57:09 +00001722BugReporter::~BugReporter() {
1723 FlushReports();
1724
1725 // Free the bug reports we are tracking.
1726 typedef std::vector<BugReportEquivClass *> ContTy;
1727 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1728 I != E; ++I) {
1729 delete *I;
1730 }
1731}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001732
1733void BugReporter::FlushReports() {
1734 if (BugTypes.isEmpty())
1735 return;
1736
1737 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001738 // warnings and new BugTypes.
1739 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1740 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001741 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001742 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001743 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001744 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001745 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001746 const_cast<BugType*>(*I)->FlushReports(*this);
1747
Anna Zaksd015f4f2012-08-02 23:41:05 +00001748 // We need to flush reports in deterministic order to ensure the order
1749 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001750 typedef std::vector<BugReportEquivClass *> ContVecTy;
1751 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1752 EI != EE; ++EI){
1753 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001754 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001755 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001756
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001757 // BugReporter owns and deletes only BugTypes created implicitly through
1758 // EmitBasicReport.
1759 // FIXME: There are leaks from checkers that assume that the BugTypes they
1760 // create will be destroyed by the BugReporter.
1761 for (llvm::StringMap<BugType*>::iterator
1762 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1763 delete I->second;
1764
Ted Kremenekcf118d42009-02-04 23:49:09 +00001765 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001766 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001767}
1768
1769//===----------------------------------------------------------------------===//
1770// PathDiagnostics generation.
1771//===----------------------------------------------------------------------===//
1772
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001773static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001774 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001775MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001776 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Ted Kremenekcf118d42009-02-04 23:49:09 +00001778 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001779 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001780 // error node unless there are two or more error nodes with the same minimum
1781 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001782 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001783 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001784
1785 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001786 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1787 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Ted Kremenekcf118d42009-02-04 23:49:09 +00001789 // Create owning pointers for GTrim and NMap just to ensure that they are
1790 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001791 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1792 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Ted Kremenekcf118d42009-02-04 23:49:09 +00001794 // Find the (first) error node in the trimmed graph. We just need to consult
1795 // the node map (NMap) which maps from nodes in the original graph to nodes
1796 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001797
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001798 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001799 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001800 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001801
Ted Kremenek40406fe2010-12-03 06:52:30 +00001802 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1803 const ExplodedNode *originalNode = nodes[nodeIndex];
1804 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001805 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001806 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001807 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Ted Kremenek938332c2009-05-16 01:11:58 +00001810 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001811
1812 // Create a new (third!) graph with a single path. This is the graph
1813 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001814 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Ted Kremenek10aa5542009-03-12 23:41:59 +00001816 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001817 // to the root node, and then construct a new graph that contains only
1818 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001819 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001821 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001822 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001824 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001825 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001826 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001828 if (Visited.find(Node) != Visited.end())
1829 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001831 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001833 if (Node->pred_empty()) {
1834 Root = Node;
1835 break;
1836 }
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001838 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001839 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001840 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001841 }
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Ted Kremenek938332c2009-05-16 01:11:58 +00001843 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Ted Kremenek10aa5542009-03-12 23:41:59 +00001845 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001846 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001847 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001848 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001849 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001851 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001852 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001853 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001854 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001856 // Create the equivalent node in the new graph with the same state
1857 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001858 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001860 // Store the mapping to the original node.
1861 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1862 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001863 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001865 // Link up the new node with the previous node.
1866 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001867 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001869 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001871 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001872 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001873 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001874 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001875 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001876 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001877 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001878 }
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001880 // Find the next successor node. We choose the node that is marked
1881 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001882 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1883 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001884 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001886 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001887
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001888 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001890 if (I == Visited.end())
1891 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001893 if (!N || I->second < MinVal) {
1894 N = *SI;
1895 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001896 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001897 }
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Ted Kremenek938332c2009-05-16 01:11:58 +00001899 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Ted Kremenek938332c2009-05-16 01:11:58 +00001902 assert(First);
1903
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001904 return std::make_pair(std::make_pair(GNew, BM),
1905 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001906}
1907
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001908/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1909/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001910static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001911 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1912 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001914 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001915 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001917 MacroStackTy MacroStack;
1918 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Ted Kremenek77d09442012-03-02 01:27:31 +00001920 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001921 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001922
1923 PathDiagnosticPiece *piece = I->getPtr();
1924
1925 // Recursively compact calls.
1926 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1927 CompactPathDiagnostic(call->path, SM);
1928 }
1929
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001930 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001931 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001933 // Determine the instantiation location, which is the location we group
1934 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001935 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001936 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001937 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001939 if (Loc.isFileID()) {
1940 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001941 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001942 continue;
1943 }
1944
1945 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001947 // Is the PathDiagnosticPiece within the same macro group?
1948 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001949 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001950 continue;
1951 }
1952
1953 // We aren't in the same group. Are we descending into a new macro
1954 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001955 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001956
1957 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001958 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001959 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001961 // Walk the entire macro stack.
1962 while (!MacroStack.empty()) {
1963 if (InstantiationLoc == MacroStack.back().second) {
1964 MacroGroup = MacroStack.back().first;
1965 break;
1966 }
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001968 if (ParentInstantiationLoc == MacroStack.back().second) {
1969 MacroGroup = MacroStack.back().first;
1970 break;
1971 }
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001973 MacroStack.pop_back();
1974 }
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001976 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1977 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001978 PathDiagnosticMacroPiece *NewGroup =
1979 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001980 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001981
1982 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001983 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001984 else {
1985 assert(InstantiationLoc.isFileID());
1986 Pieces.push_back(NewGroup);
1987 }
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001989 MacroGroup = NewGroup;
1990 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1991 }
1992
1993 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001994 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001995 }
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001997 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001998 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Ted Kremenek77d09442012-03-02 01:27:31 +00002000 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
2001 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00002002}
2003
Jordan Rose8347d3d2012-09-22 01:24:53 +00002004bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002005 PathDiagnosticConsumer &PC,
2006 ArrayRef<BugReport *> &bugReports) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00002007 assert(!bugReports.empty());
Jordan Rose8347d3d2012-09-22 01:24:53 +00002008
2009 bool HasValid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002010 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002011 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
2012 E = bugReports.end(); I != E; ++I) {
Jordan Rose8347d3d2012-09-22 01:24:53 +00002013 if ((*I)->isValid()) {
2014 HasValid = true;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002015 errorNodes.push_back((*I)->getErrorNode());
Jordan Rose8347d3d2012-09-22 01:24:53 +00002016 } else {
2017 errorNodes.push_back(0);
2018 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00002019 }
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Jordan Rose8347d3d2012-09-22 01:24:53 +00002021 // If all the reports have been marked invalid, we're done.
2022 if (!HasValid)
2023 return false;
2024
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00002025 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00002026 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00002027 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002028 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00002029 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00002030
Ted Kremenekcf118d42009-02-04 23:49:09 +00002031 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002032 assert(GPair.second.second < bugReports.size());
2033 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00002034 assert(R && "No original report found for sliced graph.");
Jordan Rose8347d3d2012-09-22 01:24:53 +00002035 assert(R->isValid() && "Report selected from trimmed graph marked invalid.");
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002037 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
2038 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002039 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00002040
2041 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002042 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Anna Zaks8e6431a2011-08-18 22:37:56 +00002044 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00002045 R->addVisitor(new NilReceiverBRVisitor());
2046 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002048 BugReport::VisitorList visitors;
2049 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00002050
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002051 // While generating diagnostics, it's possible the visitors will decide
2052 // new symbols and regions are interesting, or add other visitors based on
2053 // the information they find. If they do, we need to regenerate the path
2054 // based on our new report configuration.
2055 do {
2056 // Get a clean copy of all the visitors.
2057 for (BugReport::visitor_iterator I = R->visitor_begin(),
2058 E = R->visitor_end(); I != E; ++I)
2059 visitors.push_back((*I)->clone());
2060
2061 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002062 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002063 originalReportConfigToken = R->getConfigurationChangeToken();
2064
2065 // Generate the very last diagnostic piece - the piece is visible before
2066 // the trace is expanded.
Jordan Rosed632d6f2012-09-22 01:24:56 +00002067 if (PDB.getGenerationScheme() != PathDiagnosticConsumer::None) {
2068 PathDiagnosticPiece *LastPiece = 0;
2069 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
2070 I != E; ++I) {
2071 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
2072 assert (!LastPiece &&
2073 "There can only be one final piece in a diagnostic.");
2074 LastPiece = Piece;
2075 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002076 }
Jordan Rosed632d6f2012-09-22 01:24:56 +00002077 if (!LastPiece)
2078 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
2079 if (LastPiece)
2080 PD.setEndOfPath(LastPiece);
2081 else
2082 return false;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002083 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002084
2085 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00002086 case PathDiagnosticConsumer::Extensive:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002087 if (!GenerateExtensivePathDiagnostic(PD, PDB, N, visitors)) {
2088 assert(!R->isValid() && "Failed on valid report");
2089 // Try again. We'll filter out the bad report when we trim the graph.
2090 // FIXME: It would be more efficient to use the same intermediate
2091 // trimmed graph, and just repeat the shortest-path search.
2092 return generatePathDiagnostic(PD, PC, bugReports);
2093 }
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00002094 break;
David Blaikieef3643f2011-09-26 00:51:36 +00002095 case PathDiagnosticConsumer::Minimal:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002096 if (!GenerateMinimalPathDiagnostic(PD, PDB, N, visitors)) {
2097 assert(!R->isValid() && "Failed on valid report");
2098 // Try again. We'll filter out the bad report when we trim the graph.
2099 return generatePathDiagnostic(PD, PC, bugReports);
2100 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00002101 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002102 case PathDiagnosticConsumer::None:
Jordan Rosed632d6f2012-09-22 01:24:56 +00002103 if (!GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors)) {
2104 assert(!R->isValid() && "Failed on valid report");
2105 // Try again. We'll filter out the bad report when we trim the graph.
2106 return generatePathDiagnostic(PD, PC, bugReports);
2107 }
2108 break;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002109 }
2110
2111 // Clean up the visitors we used.
2112 llvm::DeleteContainerPointers(visitors);
2113
2114 // Did anything change while generating this path?
2115 finalReportConfigToken = R->getConfigurationChangeToken();
2116 } while(finalReportConfigToken != originalReportConfigToken);
2117
Ted Kremenekc89f4b02012-02-28 23:06:21 +00002118 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremenekb85cce02012-10-25 22:07:10 +00002119 if (!PD.path.empty()) {
2120 // Remove messages that are basically the same.
Ted Kremenek38001652012-10-26 16:02:36 +00002121 removeRedundantMsgs(PD.getMutablePieces());
Ted Kremenekb85cce02012-10-25 22:07:10 +00002122
2123 if (R->shouldPrunePath()) {
Jordan Roseafa7cae2012-12-07 19:56:29 +00002124 bool hasSomethingInteresting = RemoveUnneededCalls(PD.getMutablePieces(),
2125 R);
Ted Kremenekb85cce02012-10-25 22:07:10 +00002126 assert(hasSomethingInteresting);
2127 (void) hasSomethingInteresting;
2128 }
Jordan Roseafa7cae2012-12-07 19:56:29 +00002129
2130 adjustCallLocations(PD.getMutablePieces());
Ted Kremeneked7948b2012-05-31 06:03:17 +00002131 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00002132
2133 return true;
Ted Kremenek7dc86642009-03-31 20:22:36 +00002134}
2135
Ted Kremenekcf118d42009-02-04 23:49:09 +00002136void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00002137 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00002138}
2139
Jordan Rose785950e2012-11-02 01:53:40 +00002140void BugReporter::emitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00002141 // Compute the bug report's hash to determine its equivalence class.
2142 llvm::FoldingSetNodeID ID;
2143 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002144
2145 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00002146 BugType& BT = R->getBugType();
2147 Register(&BT);
2148 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002149 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Ted Kremenekcf118d42009-02-04 23:49:09 +00002151 if (!EQ) {
2152 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002153 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00002154 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002155 }
2156 else
2157 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00002158}
2159
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002160
2161//===----------------------------------------------------------------------===//
2162// Emitting reports in equivalence classes.
2163//===----------------------------------------------------------------------===//
2164
2165namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002166struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002167 const ExplodedNode *N;
2168 ExplodedNode::const_succ_iterator I, E;
2169
2170 FRIEC_WLItem(const ExplodedNode *n)
2171 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2172};
2173}
2174
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002175static BugReport *
2176FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002177 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002178
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002179 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2180 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002181 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002182
Ted Kremenek40406fe2010-12-03 06:52:30 +00002183 // If we don't need to suppress any of the nodes because they are
2184 // post-dominated by a sink, simply add all the nodes in the equivalence class
2185 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002186 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002187 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002188 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002189 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002190 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002191 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002192 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002193 }
2194 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002195 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002196 }
2197
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002198 // For bug reports that should be suppressed when all paths are post-dominated
2199 // by a sink node, iterate through the reports in the equivalence class
2200 // until we find one that isn't post-dominated (if one exists). We use a
2201 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2202 // this as a recursive function, but we don't want to risk blowing out the
2203 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002204 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002205
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002206 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002207 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002208
Ted Kremenek40406fe2010-12-03 06:52:30 +00002209 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002210 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002211 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002212 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002213 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002214 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002215 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002216 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002217 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002218 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002219 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002220 continue;
2221 }
2222
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002223 // At this point we know that 'N' is not a sink and it has at least one
2224 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2225 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002226 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002227 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2228
2229 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002230 WL.push_back(errorNode);
2231 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002232
2233 while (!WL.empty()) {
2234 WLItem &WI = WL.back();
2235 assert(!WI.N->succ_empty());
2236
2237 for (; WI.I != WI.E; ++WI.I) {
2238 const ExplodedNode *Succ = *WI.I;
2239 // End-of-path node?
2240 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002241 // If we found an end-of-path node that is not a sink.
2242 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002243 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002244 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002245 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002246 WL.clear();
2247 break;
2248 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002249 // Found a sink? Continue on to the next successor.
2250 continue;
2251 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002252 // Mark the successor as visited. If it hasn't been explored,
2253 // enqueue it to the DFS worklist.
2254 unsigned &mark = Visited[Succ];
2255 if (!mark) {
2256 mark = 1;
2257 WL.push_back(Succ);
2258 break;
2259 }
2260 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002261
2262 // The worklist may have been cleared at this point. First
2263 // check if it is empty before checking the last item.
2264 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002265 WL.pop_back();
2266 }
2267 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002268
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002269 // ExampleReport will be NULL if all the nodes in the equivalence class
2270 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002271 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002272}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002273
Ted Kremenekcf118d42009-02-04 23:49:09 +00002274void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002275 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002276 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002277 if (exampleReport) {
2278 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2279 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2280 E=C.end(); I != E; ++I) {
2281 FlushReport(exampleReport, **I, bugReports);
2282 }
2283 }
2284}
2285
2286void BugReporter::FlushReport(BugReport *exampleReport,
2287 PathDiagnosticConsumer &PD,
2288 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Ted Kremenekcf118d42009-02-04 23:49:09 +00002290 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002291 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002292 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002294 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002295 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2296 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002297 exampleReport->getDescription(),
2298 exampleReport->getShortDescription(/*Fallback=*/false),
Anna Zaks97bfb552013-01-08 00:25:29 +00002299 BT.getCategory(),
2300 exampleReport->getUniqueingLocation(),
2301 exampleReport->getUniqueingDecl()));
Ted Kremenekd49967f2009-04-29 21:58:13 +00002302
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002303 // Generate the full path diagnostic, using the generation scheme
Jordan Rosed632d6f2012-09-22 01:24:56 +00002304 // specified by the PathDiagnosticConsumer. Note that we have to generate
2305 // path diagnostics even for consumers which do not support paths, because
2306 // the BugReporterVisitors may mark this bug as a false positive.
2307 if (!bugReports.empty())
2308 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2309 return;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002310
2311 // If the path is empty, generate a single step path with the location
2312 // of the issue.
2313 if (D->path.empty()) {
2314 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2315 PathDiagnosticPiece *piece =
2316 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2317 BugReport::ranges_iterator Beg, End;
2318 llvm::tie(Beg, End) = exampleReport->getRanges();
2319 for ( ; Beg != End; ++Beg)
2320 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002321 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002322 }
2323
Ted Kremenek072192b2008-04-30 23:47:44 +00002324 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002325 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002326 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2327 e = Meta.end(); i != e; ++i) {
2328 D->addMeta(*i);
2329 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002330
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002331 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002332}
Ted Kremenek57202072008-07-14 17:40:50 +00002333
Ted Kremenek07189522012-04-04 18:11:35 +00002334void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002335 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002336 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002337 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002338 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002339
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002340 // 'BT' is owned by BugReporter.
2341 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002342 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002343 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002344 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
Jordan Rose785950e2012-11-02 01:53:40 +00002345 emitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002346}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002347
Chris Lattner5f9e2722011-07-23 10:55:15 +00002348BugType *BugReporter::getBugTypeForName(StringRef name,
2349 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002350 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002351 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2352 llvm::StringMapEntry<BugType *> &
2353 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2354 BugType *BT = entry.getValue();
2355 if (!BT) {
2356 BT = new BugType(name, category);
2357 entry.setValue(BT);
2358 }
2359 return BT;
2360}