blob: 5c14eaf284781e25aee67316699645910cb00af8 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Jordan Rose852aa0d2012-07-10 22:07:52 +000051 else if (const CallEnter *CE = dyn_cast<CallEnter>(&P))
52 return CE->getCallExpr();
53 else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&P))
54 return CEE->getCalleeContext()->getCallSite();
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekb697b102009-02-23 22:44:26 +000056 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000057}
58
Zhongxing Xuc5619d92009-08-06 01:32:16 +000059static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000060GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000061 return N->pred_empty() ? NULL : *(N->pred_begin());
62}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000063
Zhongxing Xuc5619d92009-08-06 01:32:16 +000064static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000065GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000067}
68
Ted Kremenek9c378f72011-08-12 23:37:29 +000069static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000070 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000071 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000072 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekb697b102009-02-23 22:44:26 +000074 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000075}
76
Ted Kremenek9c378f72011-08-12 23:37:29 +000077static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000079 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 // Check if the statement is '?' or '&&'/'||'. These are "merges",
81 // not actual statement points.
82 switch (S->getStmtClass()) {
83 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000084 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 case Stmt::ConditionalOperatorClass: continue;
86 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000087 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
88 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000089 continue;
90 break;
91 }
92 default:
93 break;
94 }
Ted Kremenekb697b102009-02-23 22:44:26 +000095 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000096 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekb697b102009-02-23 22:44:26 +000098 return 0;
99}
100
Ted Kremenek5f85e172009-07-22 22:35:28 +0000101static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000102GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000103 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000104 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekb697b102009-02-23 22:44:26 +0000106 return GetPreviousStmt(N);
107}
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek5f85e172009-07-22 22:35:28 +0000109static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000110GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000111 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000112 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekb697b102009-02-23 22:44:26 +0000114 return GetNextStmt(N);
115}
116
117//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000118// Diagnostic cleanup.
119//===----------------------------------------------------------------------===//
120
Ted Kremenekb85cce02012-10-25 22:07:10 +0000121static PathDiagnosticEventPiece *
122eventsDescribeSameCondition(PathDiagnosticEventPiece *X,
123 PathDiagnosticEventPiece *Y) {
124 // Prefer diagnostics that come from ConditionBRVisitor over
125 // those that came from TrackConstraintBRVisitor.
126 const void *tagPreferred = ConditionBRVisitor::getTag();
127 const void *tagLesser = TrackConstraintBRVisitor::getTag();
128
129 if (X->getLocation() != Y->getLocation())
130 return 0;
131
132 if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
133 return X;
134
135 if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
136 return Y;
137
138 return 0;
139}
140
Ted Kremenek38001652012-10-26 16:02:36 +0000141/// An optimization pass over PathPieces that removes redundant diagnostics
142/// generated by both ConditionBRVisitor and TrackConstraintBRVisitor. Both
143/// BugReporterVisitors use different methods to generate diagnostics, with
144/// one capable of emitting diagnostics in some cases but not in others. This
145/// can lead to redundant diagnostic pieces at the same point in a path.
146static void removeRedundantMsgs(PathPieces &path) {
Ted Kremenekb85cce02012-10-25 22:07:10 +0000147 unsigned N = path.size();
148 if (N < 2)
149 return;
Ted Kremenek38001652012-10-26 16:02:36 +0000150 // NOTE: this loop intentionally is not using an iterator. Instead, we
151 // are streaming the path and modifying it in place. This is done by
152 // grabbing the front, processing it, and if we decide to keep it append
153 // it to the end of the path. The entire path is processed in this way.
Ted Kremenekb85cce02012-10-25 22:07:10 +0000154 for (unsigned i = 0; i < N; ++i) {
155 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(path.front());
156 path.pop_front();
157
158 switch (piece->getKind()) {
159 case clang::ento::PathDiagnosticPiece::Call:
Ted Kremenek38001652012-10-26 16:02:36 +0000160 removeRedundantMsgs(cast<PathDiagnosticCallPiece>(piece)->path);
Ted Kremenekb85cce02012-10-25 22:07:10 +0000161 break;
162 case clang::ento::PathDiagnosticPiece::Macro:
Ted Kremenek38001652012-10-26 16:02:36 +0000163 removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(piece)->subPieces);
Ted Kremenekb85cce02012-10-25 22:07:10 +0000164 break;
165 case clang::ento::PathDiagnosticPiece::ControlFlow:
166 break;
167 case clang::ento::PathDiagnosticPiece::Event: {
168 if (i == N-1)
169 break;
170
171 if (PathDiagnosticEventPiece *nextEvent =
172 dyn_cast<PathDiagnosticEventPiece>(path.front().getPtr())) {
173 PathDiagnosticEventPiece *event =
174 cast<PathDiagnosticEventPiece>(piece);
175 // Check to see if we should keep one of the two pieces. If we
176 // come up with a preference, record which piece to keep, and consume
177 // another piece from the path.
178 if (PathDiagnosticEventPiece *pieceToKeep =
179 eventsDescribeSameCondition(event, nextEvent)) {
180 piece = pieceToKeep;
181 path.pop_front();
182 ++i;
183 }
184 }
185 break;
186 }
187 }
188 path.push_back(piece);
189 }
190}
191
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000192/// Recursively scan through a path and prune out calls and macros pieces
193/// that aren't needed. Return true if afterwards the path contains
194/// "interesting stuff" which means it should be pruned from the parent path.
Ted Kremeneka43df952012-09-21 00:09:11 +0000195bool BugReporter::RemoveUneededCalls(PathPieces &pieces, BugReport *R,
Jordan Rose368f3b02012-11-15 02:07:23 +0000196 PathDiagnosticLocation *LastCallLocation) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000197 bool containsSomethingInteresting = false;
198 const unsigned N = pieces.size();
199
200 for (unsigned i = 0 ; i < N ; ++i) {
201 // Remove the front piece from the path. If it is still something we
202 // want to keep once we are done, we will push it back on the end.
203 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
204 pieces.pop_front();
205
Ted Kremeneka43df952012-09-21 00:09:11 +0000206 // Throw away pieces with invalid locations.
207 if (piece->getKind() != PathDiagnosticPiece::Call &&
208 piece->getLocation().asLocation().isInvalid())
209 continue;
210
Ted Kremenek72516742012-03-01 00:05:06 +0000211 switch (piece->getKind()) {
212 case PathDiagnosticPiece::Call: {
213 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000214 // Check if the location context is interesting.
215 assert(LocationContextMap.count(call));
216 if (R->isInteresting(LocationContextMap[call])) {
217 containsSomethingInteresting = true;
218 break;
219 }
Jordan Rose368f3b02012-11-15 02:07:23 +0000220
221 if (LastCallLocation) {
222 if (!call->callEnter.asLocation().isValid())
223 call->callEnter = *LastCallLocation;
224 if (!call->callReturn.asLocation().isValid())
225 call->callReturn = *LastCallLocation;
226 }
227
Ted Kremenek72516742012-03-01 00:05:06 +0000228 // Recursively clean out the subclass. Keep this call around if
229 // it contains any informative diagnostics.
Jordan Rose368f3b02012-11-15 02:07:23 +0000230 if (call->callEnterWithin.asLocation().isValid())
231 LastCallLocation = &call->callEnterWithin;
232 else
233 LastCallLocation = &call->callEnter;
Ted Kremeneka43df952012-09-21 00:09:11 +0000234
Jordan Rose368f3b02012-11-15 02:07:23 +0000235 assert(LastCallLocation && "Outermost call has an invalid location");
236 if (!RemoveUneededCalls(call->path, R, LastCallLocation))
237 continue;
Ted Kremeneka43df952012-09-21 00:09:11 +0000238
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000239 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000240 break;
241 }
242 case PathDiagnosticPiece::Macro: {
243 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000244 if (!RemoveUneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000245 continue;
246 containsSomethingInteresting = true;
247 break;
248 }
249 case PathDiagnosticPiece::Event: {
250 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
Ted Kremeneka43df952012-09-21 00:09:11 +0000251
Ted Kremenek72516742012-03-01 00:05:06 +0000252 // We never throw away an event, but we do throw it away wholesale
253 // as part of a path if we throw the entire path away.
Ted Kremenek22505ef2012-09-08 07:18:18 +0000254 containsSomethingInteresting |= !event->isPrunable();
Ted Kremenek72516742012-03-01 00:05:06 +0000255 break;
256 }
257 case PathDiagnosticPiece::ControlFlow:
258 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000259 }
260
261 pieces.push_back(piece);
262 }
263
264 return containsSomethingInteresting;
265}
266
267//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000268// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000269//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000270
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000271typedef llvm::DenseMap<const ExplodedNode*,
272const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000273
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000274namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000275class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000276 NodeBackMap& M;
277public:
278 NodeMapClosure(NodeBackMap *m) : M(*m) {}
279 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Ted Kremenek9c378f72011-08-12 23:37:29 +0000281 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000282 NodeBackMap::iterator I = M.find(N);
283 return I == M.end() ? 0 : I->second;
284 }
285};
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000287class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000288 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000289 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000290 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000291 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000292public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000293 const LocationContext *LC;
294
Ted Kremenek8966bc12009-05-06 21:39:49 +0000295 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000296 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000297 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000298 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000299 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
300 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenek9c378f72011-08-12 23:37:29 +0000302 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Ted Kremenek9c378f72011-08-12 23:37:29 +0000304 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
305 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Anna Zaks8e6431a2011-08-18 22:37:56 +0000307 BugReport *getBugReport() { return R; }
308
Tom Care212f6d32010-09-16 03:50:38 +0000309 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000310
311 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000313 const Stmt *getParent(const Stmt *S) {
314 return getParentMap().getParent(S);
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek8966bc12009-05-06 21:39:49 +0000317 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000318
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000319 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
David Blaikieef3643f2011-09-26 00:51:36 +0000321 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
322 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000323 }
324
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000325 bool supportsLogicalOpControlFlow() const {
326 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000327 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000328};
329} // end anonymous namespace
330
Ted Kremenek00605e02009-03-27 20:55:39 +0000331PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000332PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000333 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000334 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000335
Anna Zaks0cd59482011-09-16 19:18:30 +0000336 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
337 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000338}
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek00605e02009-03-27 20:55:39 +0000340PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000341PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
342 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000343
Ted Kremenek143ca222008-05-06 18:11:09 +0000344 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000345 if (os.str().empty())
346 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek00605e02009-03-27 20:55:39 +0000348 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek00605e02009-03-27 20:55:39 +0000350 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000351 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000352 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000353 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000354 else {
355 os << "Execution jumps to the end of the ";
356 const Decl *D = N->getLocationContext()->getDecl();
357 if (isa<ObjCMethodDecl>(D))
358 os << "method";
359 else if (isa<FunctionDecl>(D))
360 os << "function";
361 else {
362 assert(isa<BlockDecl>(D));
363 os << "anonymous block";
364 }
365 os << '.';
366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000368 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000369}
370
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000371static bool IsNested(const Stmt *S, ParentMap &PM) {
372 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
373 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000375 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000377 if (Parent)
378 switch (Parent->getStmtClass()) {
379 case Stmt::ForStmtClass:
380 case Stmt::DoStmtClass:
381 case Stmt::WhileStmtClass:
382 return true;
383 default:
384 break;
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
387 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000388}
389
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000390PathDiagnosticLocation
391PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000392 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000393 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000394 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000395
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000396 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000397 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000399 if (!Parent)
400 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000402 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000403 case Stmt::BinaryOperatorClass: {
404 const BinaryOperator *B = cast<BinaryOperator>(Parent);
405 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000406 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000407 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000408 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000409 case Stmt::CompoundStmtClass:
410 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000411 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000412 case Stmt::ChooseExprClass:
413 // Similar to '?' if we are referring to condition, just have the edge
414 // point to the entire choose expression.
415 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000416 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000417 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000418 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000419 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000420 case Stmt::ConditionalOperatorClass:
421 // For '?', if we are referring to condition, just have the edge point
422 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000423 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000424 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000425 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000426 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000427 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000428 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000429 case Stmt::ForStmtClass:
430 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000431 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000432 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000433 case Stmt::IfStmtClass:
434 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000435 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000436 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000437 case Stmt::ObjCForCollectionStmtClass:
438 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000439 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000440 break;
441 case Stmt::WhileStmtClass:
442 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000443 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000444 break;
445 default:
446 break;
447 }
448
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000449 S = Parent;
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000452 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000453
454 // Special case: DeclStmts can appear in for statement declarations, in which
455 // case the ForStmt is the context.
456 if (isa<DeclStmt>(S)) {
457 if (const Stmt *Parent = P.getParent(S)) {
458 switch (Parent->getStmtClass()) {
459 case Stmt::ForStmtClass:
460 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000461 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000462 default:
463 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000464 }
465 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000466 }
467 else if (isa<BinaryOperator>(S)) {
468 // Special case: the binary operator represents the initialization
469 // code in a for statement (this can happen when the variable being
470 // initialized is an old variable.
471 if (const ForStmt *FS =
472 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
473 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000474 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000475 }
476 }
477
Anna Zaks220ac8c2011-09-15 01:08:34 +0000478 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000479}
480
Ted Kremenekcf118d42009-02-04 23:49:09 +0000481//===----------------------------------------------------------------------===//
Jordan Rosed632d6f2012-09-22 01:24:56 +0000482// "Visitors only" path diagnostic generation algorithm.
483//===----------------------------------------------------------------------===//
484static bool GenerateVisitorsOnlyPathDiagnostic(PathDiagnostic &PD,
485 PathDiagnosticBuilder &PDB,
486 const ExplodedNode *N,
487 ArrayRef<BugReporterVisitor *> visitors) {
488 // All path generation skips the very first node (the error node).
489 // This is because there is special handling for the end-of-path note.
490 N = N->getFirstPred();
491 if (!N)
492 return true;
493
494 BugReport *R = PDB.getBugReport();
495 while (const ExplodedNode *Pred = N->getFirstPred()) {
496 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
497 E = visitors.end();
498 I != E; ++I) {
499 // Visit all the node pairs, but throw the path pieces away.
500 PathDiagnosticPiece *Piece = (*I)->VisitNode(N, Pred, PDB, *R);
501 delete Piece;
502 }
503
504 N = Pred;
505 }
506
507 return R->isValid();
508}
509
510//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000511// "Minimal" path diagnostic generation algorithm.
512//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000513typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
514typedef SmallVector<StackDiagPair, 6> StackDiagVector;
515
Anna Zaks368a0d52012-03-15 21:13:02 +0000516static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000517 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000518 // If the piece contains a special message, add it to all the call
519 // pieces on the active stack.
520 if (PathDiagnosticEventPiece *ep =
521 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000522
Anna Zaks56a938f2012-03-16 23:24:20 +0000523 if (ep->hasCallStackHint())
524 for (StackDiagVector::iterator I = CallStack.begin(),
525 E = CallStack.end(); I != E; ++I) {
526 PathDiagnosticCallPiece *CP = I->first;
527 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000528 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000529
Anna Zaks368a0d52012-03-15 21:13:02 +0000530 // The last message on the path to final bug is the most important
531 // one. Since we traverse the path backwards, do not add the message
532 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000533 if (!CP->hasCallStackMessage())
534 CP->setCallStackMessage(stackMsg);
535 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000536 }
537}
Ted Kremenek31061982009-03-31 23:00:32 +0000538
Ted Kremenek77d09442012-03-02 01:27:31 +0000539static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000540
Jordan Rose8347d3d2012-09-22 01:24:53 +0000541static bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
Ted Kremenek31061982009-03-31 23:00:32 +0000542 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000543 const ExplodedNode *N,
544 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000545
Ted Kremenek31061982009-03-31 23:00:32 +0000546 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000547 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000548 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000549 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000550
Anna Zaks56a938f2012-03-16 23:24:20 +0000551 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000552
Ted Kremenek31061982009-03-31 23:00:32 +0000553 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000554 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000555 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000556 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek31061982009-03-31 23:00:32 +0000558 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000559
Anna Zaks80de4872012-08-29 21:22:37 +0000560 do {
561 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
562 PathDiagnosticCallPiece *C =
563 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
564 GRBugReporter& BR = PDB.getBugReporter();
565 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
566 PD.getActivePath().push_front(C);
567 PD.pushActivePath(&C->path);
568 CallStack.push_back(StackDiagPair(C, N));
569 break;
Anna Zaks93739372012-03-14 18:58:28 +0000570 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000571
Anna Zaks80de4872012-08-29 21:22:37 +0000572 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
573 // Flush all locations, and pop the active path.
574 bool VisitedEntireCall = PD.isWithinCall();
575 PD.popActivePath();
576
577 // Either we just added a bunch of stuff to the top-level path, or
578 // we have a previous CallExitEnd. If the former, it means that the
579 // path terminated within a function call. We must then take the
580 // current contents of the active path and place it within
581 // a new PathDiagnosticCallPiece.
582 PathDiagnosticCallPiece *C;
583 if (VisitedEntireCall) {
584 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
585 } else {
586 const Decl *Caller = CE->getLocationContext()->getDecl();
587 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
588 GRBugReporter& BR = PDB.getBugReporter();
589 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
590 }
591
592 C->setCallee(*CE, SMgr);
593 if (!CallStack.empty()) {
594 assert(CallStack.back().first == C);
595 CallStack.pop_back();
596 }
597 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Anna Zaks80de4872012-08-29 21:22:37 +0000600 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
601 const CFGBlock *Src = BE->getSrc();
602 const CFGBlock *Dst = BE->getDst();
603 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anna Zaks80de4872012-08-29 21:22:37 +0000605 if (!T)
606 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Anna Zaks80de4872012-08-29 21:22:37 +0000608 PathDiagnosticLocation Start =
609 PathDiagnosticLocation::createBegin(T, SMgr,
610 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Anna Zaks80de4872012-08-29 21:22:37 +0000612 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000613 default:
614 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Ted Kremenek31061982009-03-31 23:00:32 +0000616 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000617 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000618 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Ted Kremenek31061982009-03-31 23:00:32 +0000620 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000621 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenek31061982009-03-31 23:00:32 +0000623 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000624 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000625 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Ted Kremenek31061982009-03-31 23:00:32 +0000627 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000628 << End.asLocation().getExpansionLineNumber();
629 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
630 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000631 break;
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
634 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000635 // Figure out what case arm we took.
636 std::string sbuf;
637 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek9c378f72011-08-12 23:37:29 +0000639 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000640 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek31061982009-03-31 23:00:32 +0000642 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000643 default:
644 os << "No cases match in the switch statement. "
645 "Control jumps to line "
646 << End.asLocation().getExpansionLineNumber();
647 break;
648 case Stmt::DefaultStmtClass:
649 os << "Control jumps to the 'default' case at line "
650 << End.asLocation().getExpansionLineNumber();
651 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Anna Zaks80de4872012-08-29 21:22:37 +0000653 case Stmt::CaseStmtClass: {
654 os << "Control jumps to 'case ";
655 const CaseStmt *Case = cast<CaseStmt>(S);
656 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Anna Zaks80de4872012-08-29 21:22:37 +0000658 // Determine if it is an enum.
659 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Anna Zaks80de4872012-08-29 21:22:37 +0000661 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
662 // FIXME: Maybe this should be an assertion. Are there cases
663 // were it is not an EnumConstantDecl?
664 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000665 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Anna Zaks80de4872012-08-29 21:22:37 +0000667 if (D) {
668 GetRawInt = false;
669 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000670 }
Ted Kremenek31061982009-03-31 23:00:32 +0000671 }
Anna Zaks80de4872012-08-29 21:22:37 +0000672
673 if (GetRawInt)
674 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
675
676 os << ":' at line "
677 << End.asLocation().getExpansionLineNumber();
678 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000679 }
Anna Zaks80de4872012-08-29 21:22:37 +0000680 }
681 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
682 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000683 }
684 else {
685 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000686 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000687 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
688 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Ted Kremenek31061982009-03-31 23:00:32 +0000691 break;
692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek31061982009-03-31 23:00:32 +0000694 case Stmt::BreakStmtClass:
695 case Stmt::ContinueStmtClass: {
696 std::string sbuf;
697 llvm::raw_string_ostream os(sbuf);
698 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000699 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
700 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000701 break;
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Anna Zaks80de4872012-08-29 21:22:37 +0000704 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000705 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000706 case Stmt::ConditionalOperatorClass: {
707 std::string sbuf;
708 llvm::raw_string_ostream os(sbuf);
709 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Ted Kremenek31061982009-03-31 23:00:32 +0000711 if (*(Src->succ_begin()+1) == Dst)
712 os << "false";
713 else
714 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremenek31061982009-03-31 23:00:32 +0000716 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek31061982009-03-31 23:00:32 +0000718 if (const Stmt *S = End.asStmt())
719 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Anna Zaks80de4872012-08-29 21:22:37 +0000721 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
722 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000723 break;
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Anna Zaks80de4872012-08-29 21:22:37 +0000726 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000727 case Stmt::BinaryOperatorClass: {
728 if (!PDB.supportsLogicalOpControlFlow())
729 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000731 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000732 std::string sbuf;
733 llvm::raw_string_ostream os(sbuf);
734 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000735
John McCall2de56d12010-08-25 11:45:40 +0000736 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000737 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek31061982009-03-31 23:00:32 +0000739 if (*(Src->succ_begin()+1) == Dst) {
740 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000741 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000742 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000743 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
744 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
745 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000746 }
Ted Kremenek31061982009-03-31 23:00:32 +0000747 else {
748 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000749 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000750 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000751 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
752 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000753 }
Ted Kremenek31061982009-03-31 23:00:32 +0000754 }
755 else {
John McCall2de56d12010-08-25 11:45:40 +0000756 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000757 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Ted Kremenek31061982009-03-31 23:00:32 +0000759 if (*(Src->succ_begin()+1) == Dst) {
760 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000761 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000762 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000763 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
764 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000765 }
766 else {
767 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000768 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000769 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000770 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
771 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
772 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000773 }
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek31061982009-03-31 23:00:32 +0000776 break;
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
779 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000780 if (*(Src->succ_begin()) == Dst) {
781 std::string sbuf;
782 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Ted Kremenek31061982009-03-31 23:00:32 +0000784 os << "Loop condition is true. ";
785 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek31061982009-03-31 23:00:32 +0000787 if (const Stmt *S = End.asStmt())
788 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Anna Zaks80de4872012-08-29 21:22:37 +0000790 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
791 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000792 }
793 else {
794 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek31061982009-03-31 23:00:32 +0000796 if (const Stmt *S = End.asStmt())
797 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Anna Zaks80de4872012-08-29 21:22:37 +0000799 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
800 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek31061982009-03-31 23:00:32 +0000803 break;
804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek31061982009-03-31 23:00:32 +0000806 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000807 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000808 if (*(Src->succ_begin()+1) == Dst) {
809 std::string sbuf;
810 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek31061982009-03-31 23:00:32 +0000812 os << "Loop condition is false. ";
813 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
814 if (const Stmt *S = End.asStmt())
815 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Anna Zaks80de4872012-08-29 21:22:37 +0000817 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
818 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000819 }
820 else {
821 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
822 if (const Stmt *S = End.asStmt())
823 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Anna Zaks80de4872012-08-29 21:22:37 +0000825 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
826 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000827 }
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek31061982009-03-31 23:00:32 +0000829 break;
830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek31061982009-03-31 23:00:32 +0000832 case Stmt::IfStmtClass: {
833 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Ted Kremenek31061982009-03-31 23:00:32 +0000835 if (const Stmt *S = End.asStmt())
836 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek31061982009-03-31 23:00:32 +0000838 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000839 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
840 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000841 else
Anna Zaks80de4872012-08-29 21:22:37 +0000842 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
843 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek31061982009-03-31 23:00:32 +0000845 break;
846 }
Anna Zaks80de4872012-08-29 21:22:37 +0000847 }
Ted Kremenek31061982009-03-31 23:00:32 +0000848 }
Anna Zaks80de4872012-08-29 21:22:37 +0000849 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000851 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000852 // Add diagnostic pieces from custom visitors.
853 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000854 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
855 E = visitors.end();
856 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000857 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000858 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000859 updateStackPiecesWithMessage(p, CallStack);
860 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000861 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000862 }
Ted Kremenek31061982009-03-31 23:00:32 +0000863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Jordan Rose8347d3d2012-09-22 01:24:53 +0000865 if (!PDB.getBugReport()->isValid())
866 return false;
867
Ted Kremenek14856d72009-04-06 23:06:54 +0000868 // After constructing the full PathDiagnostic, do a pass over it to compact
869 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000870 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Jordan Rose8347d3d2012-09-22 01:24:53 +0000871 return true;
Ted Kremenek31061982009-03-31 23:00:32 +0000872}
873
874//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000875// "Extensive" PathDiagnostic generation.
876//===----------------------------------------------------------------------===//
877
878static bool IsControlFlowExpr(const Stmt *S) {
879 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000881 if (!E)
882 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000883
884 E = E->IgnoreParenCasts();
885
John McCall56ca35d2011-02-17 10:25:35 +0000886 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000887 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000889 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
890 if (B->isLogicalOp())
891 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
893 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000894}
895
Ted Kremenek14856d72009-04-06 23:06:54 +0000896namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000897class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000898 bool IsDead;
899public:
900 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
901 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000902
903 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000904 bool isDead() const { return IsDead; }
905};
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000907class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000908 std::vector<ContextLocation> CLocs;
909 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000910 PathDiagnostic &PD;
911 PathDiagnosticBuilder &PDB;
912 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000914 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Ted Kremenek14856d72009-04-06 23:06:54 +0000916 bool containsLocation(const PathDiagnosticLocation &Container,
917 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Ted Kremenek14856d72009-04-06 23:06:54 +0000919 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenek9650cf32009-05-11 21:42:34 +0000921 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
922 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000923 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000924 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000925 while (1) {
926 // Adjust the location for some expressions that are best referenced
927 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000928 switch (S->getStmtClass()) {
929 default:
930 break;
931 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000932 case Stmt::GenericSelectionExprClass:
933 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000934 firstCharOnly = true;
935 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000936 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000937 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000938 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000939 firstCharOnly = true;
940 continue;
941 case Stmt::ChooseExprClass:
942 S = cast<ChooseExpr>(S)->getCond();
943 firstCharOnly = true;
944 continue;
945 case Stmt::BinaryOperatorClass:
946 S = cast<BinaryOperator>(S)->getLHS();
947 firstCharOnly = true;
948 continue;
949 }
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenek9650cf32009-05-11 21:42:34 +0000951 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000952 }
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek9650cf32009-05-11 21:42:34 +0000954 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000955 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Ted Kremenek9650cf32009-05-11 21:42:34 +0000958 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000959 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000960
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000961 return L;
962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Ted Kremenek14856d72009-04-06 23:06:54 +0000964 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000965 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000966 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000967 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000968 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000969 CLocs.pop_back();
970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Ted Kremenek14856d72009-04-06 23:06:54 +0000972public:
973 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
974 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Ted Kremeneka301a672009-04-22 18:16:20 +0000976 // If the PathDiagnostic already has pieces, add the enclosing statement
977 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000978 if (!PD.path.empty()) {
979 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000982 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000983 }
984 }
985
986 ~EdgeBuilder() {
987 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000988
Ted Kremeneka301a672009-04-22 18:16:20 +0000989 // Finally, add an initial edge from the start location of the first
990 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000991 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000992 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000993 PDB.getSourceManager());
994 if (L.isValid())
995 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000996 }
997
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000998 void flushLocations() {
999 while (!CLocs.empty())
1000 popLocation();
1001 PrevLoc = PathDiagnosticLocation();
1002 }
1003
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001006 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek14856d72009-04-06 23:06:54 +00001008 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001009 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001010 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +00001011};
Ted Kremenek14856d72009-04-06 23:06:54 +00001012} // end anonymous namespace
1013
1014
1015PathDiagnosticLocation
1016EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1017 if (const Stmt *S = L.asStmt()) {
1018 if (IsControlFlowExpr(S))
1019 return L;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
1021 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +00001022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Ted Kremenek14856d72009-04-06 23:06:54 +00001024 return L;
1025}
1026
1027bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1028 const PathDiagnosticLocation &Containee) {
1029
1030 if (Container == Containee)
1031 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Ted Kremenek14856d72009-04-06 23:06:54 +00001033 if (Container.asDecl())
1034 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Ted Kremenek14856d72009-04-06 23:06:54 +00001036 if (const Stmt *S = Containee.asStmt())
1037 if (const Stmt *ContainerS = Container.asStmt()) {
1038 while (S) {
1039 if (S == ContainerS)
1040 return true;
1041 S = PDB.getParent(S);
1042 }
1043 return false;
1044 }
1045
1046 // Less accurate: compare using source ranges.
1047 SourceRange ContainerR = Container.asRange();
1048 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek14856d72009-04-06 23:06:54 +00001050 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001051 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1052 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1053 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1054 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chandler Carruth64211622011-07-25 21:09:52 +00001056 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1057 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1058 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1059 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek14856d72009-04-06 23:06:54 +00001061 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001062 assert(ContaineeBegLine <= ContaineeEndLine);
1063
Ted Kremenek14856d72009-04-06 23:06:54 +00001064 return (ContainerBegLine <= ContaineeBegLine &&
1065 ContainerEndLine >= ContaineeEndLine &&
1066 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001067 SM.getExpansionColumnNumber(ContainerRBeg) <=
1068 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001069 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001070 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +00001071 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001072}
1073
Ted Kremenek14856d72009-04-06 23:06:54 +00001074void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1075 if (!PrevLoc.isValid()) {
1076 PrevLoc = NewLoc;
1077 return;
1078 }
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001080 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1081 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Ted Kremeneka43df952012-09-21 00:09:11 +00001083 if (PrevLocClean.asLocation().isInvalid()) {
1084 PrevLoc = NewLoc;
1085 return;
1086 }
1087
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001088 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001089 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Ted Kremenek14856d72009-04-06 23:06:54 +00001091 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001092 if (NewLocClean.asLocation().getExpansionLoc() ==
1093 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001094 return;
1095
Ted Kremenek2042fc12012-02-24 06:00:00 +00001096 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001097 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001098}
1099
1100void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremeneka301a672009-04-22 18:16:20 +00001102 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1103 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Ted Kremenek14856d72009-04-06 23:06:54 +00001105 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1106
1107 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001108 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Ted Kremenek14856d72009-04-06 23:06:54 +00001110 // Is the top location context the same as the one for the new location?
1111 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001112 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001113 if (IsConsumedExpr(TopContextLoc) &&
1114 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001115 TopContextLoc.markDead();
1116
Ted Kremenek14856d72009-04-06 23:06:54 +00001117 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001118 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001119
1120 return;
1121 }
1122
1123 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001124 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001125 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001127 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001128 CLocs.push_back(ContextLocation(CLoc, true));
1129 return;
1130 }
1131 }
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Ted Kremenek14856d72009-04-06 23:06:54 +00001133 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001134 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001135 }
1136
1137 // Context does not contain the location. Flush it.
1138 popLocation();
1139 }
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001141 // If we reach here, there is no enclosing context. Just add the edge.
1142 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001143}
1144
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001145bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1146 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1147 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001149 return false;
1150}
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremeneke1baed32009-05-05 23:13:38 +00001152void EdgeBuilder::addExtendedContext(const Stmt *S) {
1153 if (!S)
1154 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001155
1156 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001157 while (Parent) {
1158 if (isa<CompoundStmt>(Parent))
1159 Parent = PDB.getParent(Parent);
1160 else
1161 break;
1162 }
1163
1164 if (Parent) {
1165 switch (Parent->getStmtClass()) {
1166 case Stmt::DoStmtClass:
1167 case Stmt::ObjCAtSynchronizedStmtClass:
1168 addContext(Parent);
1169 default:
1170 break;
1171 }
1172 }
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Ted Kremeneke1baed32009-05-05 23:13:38 +00001174 addContext(S);
1175}
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Ted Kremenek14856d72009-04-06 23:06:54 +00001177void EdgeBuilder::addContext(const Stmt *S) {
1178 if (!S)
1179 return;
1180
Ted Kremenek59950d32012-02-24 07:12:52 +00001181 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001182 addContext(L);
1183}
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Jordan Rose183ba8e2012-07-26 20:04:05 +00001185void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001186 while (!CLocs.empty()) {
1187 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1188
1189 // Is the top location context the same as the one for the new location?
1190 if (TopContextLoc == L)
1191 return;
1192
1193 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001194 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001195 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001196 }
1197
1198 // Context does not contain the location. Flush it.
1199 popLocation();
1200 }
1201
1202 CLocs.push_back(L);
1203}
1204
Ted Kremenek11abcec2012-05-02 00:31:29 +00001205// Cone-of-influence: support the reverse propagation of "interesting" symbols
1206// and values by tracing interesting calculations backwards through evaluated
1207// expressions along a path. This is probably overly complicated, but the idea
1208// is that if an expression computed an "interesting" value, the child
1209// expressions are are also likely to be "interesting" as well (which then
1210// propagates to the values they in turn compute). This reverse propagation
1211// is needed to track interesting correlations across function call boundaries,
1212// where formal arguments bind to actual arguments, etc. This is also needed
1213// because the constraint solver sometimes simplifies certain symbolic values
1214// into constants when appropriate, and this complicates reasoning about
1215// interesting values.
1216typedef llvm::DenseSet<const Expr *> InterestingExprs;
1217
1218static void reversePropagateIntererstingSymbols(BugReport &R,
1219 InterestingExprs &IE,
1220 const ProgramState *State,
1221 const Expr *Ex,
1222 const LocationContext *LCtx) {
1223 SVal V = State->getSVal(Ex, LCtx);
1224 if (!(R.isInteresting(V) || IE.count(Ex)))
1225 return;
1226
1227 switch (Ex->getStmtClass()) {
1228 default:
1229 if (!isa<CastExpr>(Ex))
1230 break;
1231 // Fall through.
1232 case Stmt::BinaryOperatorClass:
1233 case Stmt::UnaryOperatorClass: {
1234 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1235 CE = Ex->child_end();
1236 CI != CE; ++CI) {
1237 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1238 IE.insert(child);
1239 SVal ChildV = State->getSVal(child, LCtx);
1240 R.markInteresting(ChildV);
1241 }
1242 break;
1243 }
1244 }
1245 }
1246
1247 R.markInteresting(V);
1248}
1249
1250static void reversePropagateInterestingSymbols(BugReport &R,
1251 InterestingExprs &IE,
1252 const ProgramState *State,
1253 const LocationContext *CalleeCtx,
1254 const LocationContext *CallerCtx)
1255{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001256 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001257 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1258 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001259 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001260 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1261 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1262 PE = FD->param_end();
1263 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1264 for (; AI != AE && PI != PE; ++AI, ++PI) {
1265 if (const Expr *ArgE = *AI) {
1266 if (const ParmVarDecl *PD = *PI) {
1267 Loc LV = State->getLValue(PD, CalleeCtx);
1268 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1269 IE.insert(ArgE);
1270 }
1271 }
1272 }
1273 }
1274 }
1275}
1276
Jordan Rose8347d3d2012-09-22 01:24:53 +00001277static bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek14856d72009-04-06 23:06:54 +00001278 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001279 const ExplodedNode *N,
1280 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001281 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001282 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001283 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001284 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001285
Ted Kremenek9c378f72011-08-12 23:37:29 +00001286 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001287 while (NextNode) {
1288 N = NextNode;
1289 NextNode = GetPredecessorNode(N);
1290 ProgramPoint P = N->getLocation();
1291
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001292 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001293 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1294 if (const Expr *Ex = PS->getStmtAs<Expr>())
1295 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1296 N->getState().getPtr(), Ex,
1297 N->getLocationContext());
1298 }
1299
Anna Zaks0b3ade82012-04-20 21:59:08 +00001300 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001301 const Stmt *S = CE->getCalleeContext()->getCallSite();
1302 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001303 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1304 N->getState().getPtr(), Ex,
1305 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001306 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001307
1308 PathDiagnosticCallPiece *C =
1309 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001310 GRBugReporter& BR = PDB.getBugReporter();
1311 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001312
1313 EB.addEdge(C->callReturn, true);
1314 EB.flushLocations();
1315
1316 PD.getActivePath().push_front(C);
1317 PD.pushActivePath(&C->path);
1318 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001319 break;
1320 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001321
Ted Kremenek2042fc12012-02-24 06:00:00 +00001322 // Pop the call hierarchy if we are done walking the contents
1323 // of a function call.
1324 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001325 // Add an edge to the start of the function.
1326 const Decl *D = CE->getCalleeContext()->getDecl();
1327 PathDiagnosticLocation pos =
1328 PathDiagnosticLocation::createBegin(D, SM);
1329 EB.addEdge(pos);
1330
1331 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001332 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001333 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001334 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001335 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001336
Jordan Rose183ba8e2012-07-26 20:04:05 +00001337 // Either we just added a bunch of stuff to the top-level path, or
1338 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001339 // path terminated within a function call. We must then take the
1340 // current contents of the active path and place it within
1341 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001342 PathDiagnosticCallPiece *C;
1343 if (VisitedEntireCall) {
1344 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1345 } else {
1346 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001347 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001348 GRBugReporter& BR = PDB.getBugReporter();
1349 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001350 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001351
Jordan Rose183ba8e2012-07-26 20:04:05 +00001352 C->setCallee(*CE, SM);
1353 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001354
1355 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001356 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001357 CallStack.pop_back();
1358 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001359 break;
1360 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001361
1362 // Note that is important that we update the LocationContext
1363 // after looking at CallExits. CallExit basically adds an
1364 // edge in the *caller*, so we don't want to update the LocationContext
1365 // too soon.
1366 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001367
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001368 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001369 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1370 // Does this represent entering a call? If so, look at propagating
1371 // interesting symbols across call boundaries.
1372 if (NextNode) {
1373 const LocationContext *CallerCtx = NextNode->getLocationContext();
1374 const LocationContext *CalleeCtx = PDB.LC;
1375 if (CallerCtx != CalleeCtx) {
1376 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1377 N->getState().getPtr(),
1378 CalleeCtx, CallerCtx);
1379 }
1380 }
1381
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001382 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001383 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001384 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001385 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001387 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1388 CS = dyn_cast<CompoundStmt>(FS->getBody());
1389 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1390 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001392 PathDiagnosticEventPiece *p =
1393 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001394 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001395 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001397 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001398 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001400 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001401 PathDiagnosticLocation BL =
1402 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001403 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001404 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001405 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001406
1407 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001408 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001410 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001411 }
1412
Mike Stump1eb44332009-09-09 15:08:12 +00001413 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Jordan Rose1a7bcc42012-09-12 22:48:08 +00001414 CFGElement First = BE->getFirstElement();
1415 if (const CFGStmt *S = First.getAs<CFGStmt>()) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001416 const Stmt *stmt = S->getStmt();
1417 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001418 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001419 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001420 }
1421 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001422 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001423 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001424
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001425 break;
1426 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001427
1428
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001429 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001431 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001432 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Anna Zaks8e6431a2011-08-18 22:37:56 +00001434 // Add pieces from custom visitors.
1435 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001436 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1437 E = visitors.end();
1438 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001439 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001440 const PathDiagnosticLocation &Loc = p->getLocation();
1441 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001442 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001443 updateStackPiecesWithMessage(p, CallStack);
1444
Ted Kremenek8966bc12009-05-06 21:39:49 +00001445 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001446 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001449 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00001450
1451 return PDB.getBugReport()->isValid();
Ted Kremenek14856d72009-04-06 23:06:54 +00001452}
1453
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001454//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001455// Methods for BugType and subclasses.
1456//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001457BugType::~BugType() { }
1458
Ted Kremenekcf118d42009-02-04 23:49:09 +00001459void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001460
David Blaikie99ba9e32011-12-20 02:48:34 +00001461void BuiltinBug::anchor() {}
1462
Ted Kremenekcf118d42009-02-04 23:49:09 +00001463//===----------------------------------------------------------------------===//
1464// Methods for BugReport and subclasses.
1465//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001466
David Blaikie99ba9e32011-12-20 02:48:34 +00001467void BugReport::NodeResolver::anchor() {}
1468
Anna Zaks8e6431a2011-08-18 22:37:56 +00001469void BugReport::addVisitor(BugReporterVisitor* visitor) {
1470 if (!visitor)
1471 return;
1472
1473 llvm::FoldingSetNodeID ID;
1474 visitor->Profile(ID);
1475 void *InsertPos;
1476
1477 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1478 delete visitor;
1479 return;
1480 }
1481
1482 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001483 Callbacks.push_back(visitor);
1484 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001485}
1486
1487BugReport::~BugReport() {
1488 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001489 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001490 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001491 while (!interestingSymbols.empty()) {
1492 popInterestingSymbolsAndRegions();
1493 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001494}
Anna Zakse172e8b2011-08-17 23:00:25 +00001495
Ted Kremenek07189522012-04-04 18:11:35 +00001496const Decl *BugReport::getDeclWithIssue() const {
1497 if (DeclWithIssue)
1498 return DeclWithIssue;
1499
1500 const ExplodedNode *N = getErrorNode();
1501 if (!N)
1502 return 0;
1503
1504 const LocationContext *LC = N->getLocationContext();
1505 return LC->getCurrentStackFrame()->getDecl();
1506}
1507
Anna Zakse172e8b2011-08-17 23:00:25 +00001508void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1509 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001510 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001511 if (UniqueingLocation.isValid()) {
1512 UniqueingLocation.Profile(hash);
1513 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001514 Location.Profile(hash);
1515 } else {
1516 assert(ErrorNode);
1517 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1518 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001519
1520 for (SmallVectorImpl<SourceRange>::const_iterator I =
1521 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1522 const SourceRange range = *I;
1523 if (!range.isValid())
1524 continue;
1525 hash.AddInteger(range.getBegin().getRawEncoding());
1526 hash.AddInteger(range.getEnd().getRawEncoding());
1527 }
1528}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001529
Ted Kremenek76aadc32012-03-09 01:13:14 +00001530void BugReport::markInteresting(SymbolRef sym) {
1531 if (!sym)
1532 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001533
1534 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001535 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001536 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001537
1538 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001539 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001540}
1541
1542void BugReport::markInteresting(const MemRegion *R) {
1543 if (!R)
1544 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001545
1546 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001547 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001548 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001549 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001550
Ted Kremenek76aadc32012-03-09 01:13:14 +00001551 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001552 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001553}
1554
1555void BugReport::markInteresting(SVal V) {
1556 markInteresting(V.getAsRegion());
1557 markInteresting(V.getAsSymbol());
1558}
1559
Anna Zaks80de4872012-08-29 21:22:37 +00001560void BugReport::markInteresting(const LocationContext *LC) {
1561 if (!LC)
1562 return;
1563 InterestingLocationContexts.insert(LC);
1564}
1565
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001566bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001567 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1568}
1569
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001570bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001571 if (!sym)
1572 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001573 // We don't currently consider metadata symbols to be interesting
1574 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001575 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001576}
1577
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001578bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001579 if (!R)
1580 return false;
1581 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001582 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001583 if (b)
1584 return true;
1585 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001586 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001587 return false;
1588}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001589
Anna Zaks80de4872012-08-29 21:22:37 +00001590bool BugReport::isInteresting(const LocationContext *LC) {
1591 if (!LC)
1592 return false;
1593 return InterestingLocationContexts.count(LC);
1594}
1595
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001596void BugReport::lazyInitializeInterestingSets() {
1597 if (interestingSymbols.empty()) {
1598 interestingSymbols.push_back(new Symbols());
1599 interestingRegions.push_back(new Regions());
1600 }
1601}
1602
1603BugReport::Symbols &BugReport::getInterestingSymbols() {
1604 lazyInitializeInterestingSets();
1605 return *interestingSymbols.back();
1606}
1607
1608BugReport::Regions &BugReport::getInterestingRegions() {
1609 lazyInitializeInterestingSets();
1610 return *interestingRegions.back();
1611}
1612
1613void BugReport::pushInterestingSymbolsAndRegions() {
1614 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1615 interestingRegions.push_back(new Regions(getInterestingRegions()));
1616}
1617
1618void BugReport::popInterestingSymbolsAndRegions() {
1619 delete interestingSymbols.back();
1620 interestingSymbols.pop_back();
1621 delete interestingRegions.back();
1622 interestingRegions.pop_back();
1623}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001624
Ted Kremenek9c378f72011-08-12 23:37:29 +00001625const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001626 if (!ErrorNode)
1627 return 0;
1628
Tom Care212f6d32010-09-16 03:50:38 +00001629 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001630 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenek9c378f72011-08-12 23:37:29 +00001632 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001633 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001634 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001635 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001636 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001637 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001638 S = GetStmt(ProgP);
1639
1640 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001641}
1642
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001643std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001644BugReport::getRanges() {
1645 // If no custom ranges, add the range of the statement corresponding to
1646 // the error node.
1647 if (Ranges.empty()) {
1648 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1649 addRange(E->getSourceRange());
1650 else
1651 return std::make_pair(ranges_iterator(), ranges_iterator());
1652 }
1653
Anna Zaks14924262011-08-24 20:31:06 +00001654 // User-specified absence of range info.
1655 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1656 return std::make_pair(ranges_iterator(), ranges_iterator());
1657
Anna Zakse172e8b2011-08-17 23:00:25 +00001658 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001659}
1660
Anna Zaks590dd8e2011-09-20 21:38:35 +00001661PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001662 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001663 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001664 "Either Location or ErrorNode should be specified but not both.");
1665
Ted Kremenek9c378f72011-08-12 23:37:29 +00001666 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001667 const LocationContext *LC = ErrorNode->getLocationContext();
1668
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001669 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001670 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001671 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001672 // For binary operators, return the location of the operator.
1673 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001674 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001675
Jordan Rose63bc1862012-11-15 19:11:43 +00001676 if (isa<PostStmtPurgeDeadSymbols>(ErrorNode->getLocation()))
1677 return PathDiagnosticLocation::createEnd(S, SM, LC);
1678
Anna Zaks590dd8e2011-09-20 21:38:35 +00001679 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001680 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001681 } else {
1682 assert(Location.isValid());
1683 return Location;
1684 }
1685
Anna Zaks590dd8e2011-09-20 21:38:35 +00001686 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001687}
1688
Ted Kremenekcf118d42009-02-04 23:49:09 +00001689//===----------------------------------------------------------------------===//
1690// Methods for BugReporter and subclasses.
1691//===----------------------------------------------------------------------===//
1692
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001693BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001694GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001695BugReporterData::~BugReporterData() {}
1696
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001697ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001698
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001699ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001700GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1701
Anna Zaks3b030a22011-08-19 01:57:09 +00001702BugReporter::~BugReporter() {
1703 FlushReports();
1704
1705 // Free the bug reports we are tracking.
1706 typedef std::vector<BugReportEquivClass *> ContTy;
1707 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1708 I != E; ++I) {
1709 delete *I;
1710 }
1711}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001712
1713void BugReporter::FlushReports() {
1714 if (BugTypes.isEmpty())
1715 return;
1716
1717 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001718 // warnings and new BugTypes.
1719 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1720 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001721 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001722 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001723 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001724 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001725 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001726 const_cast<BugType*>(*I)->FlushReports(*this);
1727
Anna Zaksd015f4f2012-08-02 23:41:05 +00001728 // We need to flush reports in deterministic order to ensure the order
1729 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001730 typedef std::vector<BugReportEquivClass *> ContVecTy;
1731 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1732 EI != EE; ++EI){
1733 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001734 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001735 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001736
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001737 // BugReporter owns and deletes only BugTypes created implicitly through
1738 // EmitBasicReport.
1739 // FIXME: There are leaks from checkers that assume that the BugTypes they
1740 // create will be destroyed by the BugReporter.
1741 for (llvm::StringMap<BugType*>::iterator
1742 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1743 delete I->second;
1744
Ted Kremenekcf118d42009-02-04 23:49:09 +00001745 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001746 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001747}
1748
1749//===----------------------------------------------------------------------===//
1750// PathDiagnostics generation.
1751//===----------------------------------------------------------------------===//
1752
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001753static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001754 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001755MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001756 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Ted Kremenekcf118d42009-02-04 23:49:09 +00001758 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001759 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001760 // error node unless there are two or more error nodes with the same minimum
1761 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001762 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001763 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001764
1765 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001766 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1767 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Ted Kremenekcf118d42009-02-04 23:49:09 +00001769 // Create owning pointers for GTrim and NMap just to ensure that they are
1770 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001771 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1772 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Ted Kremenekcf118d42009-02-04 23:49:09 +00001774 // Find the (first) error node in the trimmed graph. We just need to consult
1775 // the node map (NMap) which maps from nodes in the original graph to nodes
1776 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001777
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001778 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001779 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001780 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001781
Ted Kremenek40406fe2010-12-03 06:52:30 +00001782 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1783 const ExplodedNode *originalNode = nodes[nodeIndex];
1784 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001785 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001786 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001787 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Ted Kremenek938332c2009-05-16 01:11:58 +00001790 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001791
1792 // Create a new (third!) graph with a single path. This is the graph
1793 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001794 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Ted Kremenek10aa5542009-03-12 23:41:59 +00001796 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001797 // to the root node, and then construct a new graph that contains only
1798 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001799 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001801 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001802 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001804 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001805 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001806 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001808 if (Visited.find(Node) != Visited.end())
1809 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001811 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001813 if (Node->pred_empty()) {
1814 Root = Node;
1815 break;
1816 }
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001818 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001819 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001820 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001821 }
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Ted Kremenek938332c2009-05-16 01:11:58 +00001823 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Ted Kremenek10aa5542009-03-12 23:41:59 +00001825 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001826 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001827 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001828 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001829 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001831 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001832 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001833 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001834 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001836 // Create the equivalent node in the new graph with the same state
1837 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001838 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001840 // Store the mapping to the original node.
1841 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1842 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001843 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001845 // Link up the new node with the previous node.
1846 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001847 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001849 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001851 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001852 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001853 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001854 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001855 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001856 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001857 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001858 }
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001860 // Find the next successor node. We choose the node that is marked
1861 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001862 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1863 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001864 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001866 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001868 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001870 if (I == Visited.end())
1871 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001873 if (!N || I->second < MinVal) {
1874 N = *SI;
1875 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001876 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001877 }
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Ted Kremenek938332c2009-05-16 01:11:58 +00001879 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001880 }
Mike Stump1eb44332009-09-09 15:08:12 +00001881
Ted Kremenek938332c2009-05-16 01:11:58 +00001882 assert(First);
1883
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001884 return std::make_pair(std::make_pair(GNew, BM),
1885 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001886}
1887
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001888/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1889/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001890static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001891 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1892 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001893
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001894 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001895 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001897 MacroStackTy MacroStack;
1898 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Ted Kremenek77d09442012-03-02 01:27:31 +00001900 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001901 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001902
1903 PathDiagnosticPiece *piece = I->getPtr();
1904
1905 // Recursively compact calls.
1906 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1907 CompactPathDiagnostic(call->path, SM);
1908 }
1909
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001910 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001911 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001913 // Determine the instantiation location, which is the location we group
1914 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001915 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001916 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001917 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001919 if (Loc.isFileID()) {
1920 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001921 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001922 continue;
1923 }
1924
1925 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001927 // Is the PathDiagnosticPiece within the same macro group?
1928 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001929 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001930 continue;
1931 }
1932
1933 // We aren't in the same group. Are we descending into a new macro
1934 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001935 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001936
1937 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001938 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001939 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001941 // Walk the entire macro stack.
1942 while (!MacroStack.empty()) {
1943 if (InstantiationLoc == MacroStack.back().second) {
1944 MacroGroup = MacroStack.back().first;
1945 break;
1946 }
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001948 if (ParentInstantiationLoc == MacroStack.back().second) {
1949 MacroGroup = MacroStack.back().first;
1950 break;
1951 }
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001953 MacroStack.pop_back();
1954 }
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001956 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1957 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001958 PathDiagnosticMacroPiece *NewGroup =
1959 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001960 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001961
1962 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001963 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001964 else {
1965 assert(InstantiationLoc.isFileID());
1966 Pieces.push_back(NewGroup);
1967 }
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001969 MacroGroup = NewGroup;
1970 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1971 }
1972
1973 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001974 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001975 }
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001977 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001978 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Ted Kremenek77d09442012-03-02 01:27:31 +00001980 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1981 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001982}
1983
Jordan Rose8347d3d2012-09-22 01:24:53 +00001984bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001985 PathDiagnosticConsumer &PC,
1986 ArrayRef<BugReport *> &bugReports) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001987 assert(!bugReports.empty());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001988
1989 bool HasValid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001990 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001991 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1992 E = bugReports.end(); I != E; ++I) {
Jordan Rose8347d3d2012-09-22 01:24:53 +00001993 if ((*I)->isValid()) {
1994 HasValid = true;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001995 errorNodes.push_back((*I)->getErrorNode());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001996 } else {
1997 errorNodes.push_back(0);
1998 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001999 }
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Jordan Rose8347d3d2012-09-22 01:24:53 +00002001 // If all the reports have been marked invalid, we're done.
2002 if (!HasValid)
2003 return false;
2004
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00002005 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00002006 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00002007 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002008 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00002009 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Ted Kremenekcf118d42009-02-04 23:49:09 +00002011 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002012 assert(GPair.second.second < bugReports.size());
2013 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00002014 assert(R && "No original report found for sliced graph.");
Jordan Rose8347d3d2012-09-22 01:24:53 +00002015 assert(R->isValid() && "Report selected from trimmed graph marked invalid.");
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002017 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
2018 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002019 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
2021 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002022 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Anna Zaks8e6431a2011-08-18 22:37:56 +00002024 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00002025 R->addVisitor(new NilReceiverBRVisitor());
2026 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00002027
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002028 BugReport::VisitorList visitors;
2029 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00002030
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002031 // While generating diagnostics, it's possible the visitors will decide
2032 // new symbols and regions are interesting, or add other visitors based on
2033 // the information they find. If they do, we need to regenerate the path
2034 // based on our new report configuration.
2035 do {
2036 // Get a clean copy of all the visitors.
2037 for (BugReport::visitor_iterator I = R->visitor_begin(),
2038 E = R->visitor_end(); I != E; ++I)
2039 visitors.push_back((*I)->clone());
2040
2041 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002042 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002043 originalReportConfigToken = R->getConfigurationChangeToken();
2044
2045 // Generate the very last diagnostic piece - the piece is visible before
2046 // the trace is expanded.
Jordan Rosed632d6f2012-09-22 01:24:56 +00002047 if (PDB.getGenerationScheme() != PathDiagnosticConsumer::None) {
2048 PathDiagnosticPiece *LastPiece = 0;
2049 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
2050 I != E; ++I) {
2051 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
2052 assert (!LastPiece &&
2053 "There can only be one final piece in a diagnostic.");
2054 LastPiece = Piece;
2055 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002056 }
Jordan Rosed632d6f2012-09-22 01:24:56 +00002057 if (!LastPiece)
2058 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
2059 if (LastPiece)
2060 PD.setEndOfPath(LastPiece);
2061 else
2062 return false;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002063 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002064
2065 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00002066 case PathDiagnosticConsumer::Extensive:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002067 if (!GenerateExtensivePathDiagnostic(PD, PDB, N, visitors)) {
2068 assert(!R->isValid() && "Failed on valid report");
2069 // Try again. We'll filter out the bad report when we trim the graph.
2070 // FIXME: It would be more efficient to use the same intermediate
2071 // trimmed graph, and just repeat the shortest-path search.
2072 return generatePathDiagnostic(PD, PC, bugReports);
2073 }
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00002074 break;
David Blaikieef3643f2011-09-26 00:51:36 +00002075 case PathDiagnosticConsumer::Minimal:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002076 if (!GenerateMinimalPathDiagnostic(PD, PDB, N, visitors)) {
2077 assert(!R->isValid() && "Failed on valid report");
2078 // Try again. We'll filter out the bad report when we trim the graph.
2079 return generatePathDiagnostic(PD, PC, bugReports);
2080 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00002081 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002082 case PathDiagnosticConsumer::None:
Jordan Rosed632d6f2012-09-22 01:24:56 +00002083 if (!GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors)) {
2084 assert(!R->isValid() && "Failed on valid report");
2085 // Try again. We'll filter out the bad report when we trim the graph.
2086 return generatePathDiagnostic(PD, PC, bugReports);
2087 }
2088 break;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002089 }
2090
2091 // Clean up the visitors we used.
2092 llvm::DeleteContainerPointers(visitors);
2093
2094 // Did anything change while generating this path?
2095 finalReportConfigToken = R->getConfigurationChangeToken();
2096 } while(finalReportConfigToken != originalReportConfigToken);
2097
Ted Kremenekc89f4b02012-02-28 23:06:21 +00002098 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremenekb85cce02012-10-25 22:07:10 +00002099 if (!PD.path.empty()) {
2100 // Remove messages that are basically the same.
Ted Kremenek38001652012-10-26 16:02:36 +00002101 removeRedundantMsgs(PD.getMutablePieces());
Ted Kremenekb85cce02012-10-25 22:07:10 +00002102
2103 if (R->shouldPrunePath()) {
2104 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(),
2105 R);
2106 assert(hasSomethingInteresting);
2107 (void) hasSomethingInteresting;
2108 }
Ted Kremeneked7948b2012-05-31 06:03:17 +00002109 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00002110
2111 return true;
Ted Kremenek7dc86642009-03-31 20:22:36 +00002112}
2113
Ted Kremenekcf118d42009-02-04 23:49:09 +00002114void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00002115 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00002116}
2117
Jordan Rose785950e2012-11-02 01:53:40 +00002118void BugReporter::emitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00002119 // Compute the bug report's hash to determine its equivalence class.
2120 llvm::FoldingSetNodeID ID;
2121 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002122
2123 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00002124 BugType& BT = R->getBugType();
2125 Register(&BT);
2126 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002127 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Ted Kremenekcf118d42009-02-04 23:49:09 +00002129 if (!EQ) {
2130 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002131 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00002132 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002133 }
2134 else
2135 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00002136}
2137
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002138
2139//===----------------------------------------------------------------------===//
2140// Emitting reports in equivalence classes.
2141//===----------------------------------------------------------------------===//
2142
2143namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002144struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002145 const ExplodedNode *N;
2146 ExplodedNode::const_succ_iterator I, E;
2147
2148 FRIEC_WLItem(const ExplodedNode *n)
2149 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2150};
2151}
2152
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002153static BugReport *
2154FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002155 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002156
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002157 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2158 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002159 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002160
Ted Kremenek40406fe2010-12-03 06:52:30 +00002161 // If we don't need to suppress any of the nodes because they are
2162 // post-dominated by a sink, simply add all the nodes in the equivalence class
2163 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002164 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002165 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002166 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002167 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002168 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002169 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002170 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002171 }
2172 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002173 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002174 }
2175
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002176 // For bug reports that should be suppressed when all paths are post-dominated
2177 // by a sink node, iterate through the reports in the equivalence class
2178 // until we find one that isn't post-dominated (if one exists). We use a
2179 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2180 // this as a recursive function, but we don't want to risk blowing out the
2181 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002182 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002183
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002184 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002185 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002186
Ted Kremenek40406fe2010-12-03 06:52:30 +00002187 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002188 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002189 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002190 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002191 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002192 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002193 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002194 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002195 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002196 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002197 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002198 continue;
2199 }
2200
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002201 // At this point we know that 'N' is not a sink and it has at least one
2202 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2203 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002204 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002205 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2206
2207 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002208 WL.push_back(errorNode);
2209 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002210
2211 while (!WL.empty()) {
2212 WLItem &WI = WL.back();
2213 assert(!WI.N->succ_empty());
2214
2215 for (; WI.I != WI.E; ++WI.I) {
2216 const ExplodedNode *Succ = *WI.I;
2217 // End-of-path node?
2218 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002219 // If we found an end-of-path node that is not a sink.
2220 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002221 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002222 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002223 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002224 WL.clear();
2225 break;
2226 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002227 // Found a sink? Continue on to the next successor.
2228 continue;
2229 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002230 // Mark the successor as visited. If it hasn't been explored,
2231 // enqueue it to the DFS worklist.
2232 unsigned &mark = Visited[Succ];
2233 if (!mark) {
2234 mark = 1;
2235 WL.push_back(Succ);
2236 break;
2237 }
2238 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002239
2240 // The worklist may have been cleared at this point. First
2241 // check if it is empty before checking the last item.
2242 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002243 WL.pop_back();
2244 }
2245 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002246
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002247 // ExampleReport will be NULL if all the nodes in the equivalence class
2248 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002249 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002250}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002251
Ted Kremenekcf118d42009-02-04 23:49:09 +00002252void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002253 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002254 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002255 if (exampleReport) {
2256 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2257 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2258 E=C.end(); I != E; ++I) {
2259 FlushReport(exampleReport, **I, bugReports);
2260 }
2261 }
2262}
2263
2264void BugReporter::FlushReport(BugReport *exampleReport,
2265 PathDiagnosticConsumer &PD,
2266 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Ted Kremenekcf118d42009-02-04 23:49:09 +00002268 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002269 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002270 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002272 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002273 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2274 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002275 exampleReport->getDescription(),
2276 exampleReport->getShortDescription(/*Fallback=*/false),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002277 BT.getCategory()));
2278
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002279 // Generate the full path diagnostic, using the generation scheme
Jordan Rosed632d6f2012-09-22 01:24:56 +00002280 // specified by the PathDiagnosticConsumer. Note that we have to generate
2281 // path diagnostics even for consumers which do not support paths, because
2282 // the BugReporterVisitors may mark this bug as a false positive.
2283 if (!bugReports.empty())
2284 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2285 return;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002286
2287 // If the path is empty, generate a single step path with the location
2288 // of the issue.
2289 if (D->path.empty()) {
2290 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2291 PathDiagnosticPiece *piece =
2292 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2293 BugReport::ranges_iterator Beg, End;
2294 llvm::tie(Beg, End) = exampleReport->getRanges();
2295 for ( ; Beg != End; ++Beg)
2296 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002297 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002298 }
2299
Ted Kremenek072192b2008-04-30 23:47:44 +00002300 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002301 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002302 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2303 e = Meta.end(); i != e; ++i) {
2304 D->addMeta(*i);
2305 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002306
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002307 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002308}
Ted Kremenek57202072008-07-14 17:40:50 +00002309
Ted Kremenek07189522012-04-04 18:11:35 +00002310void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002311 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002312 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002313 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002314 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002316 // 'BT' is owned by BugReporter.
2317 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002318 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002319 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002320 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
Jordan Rose785950e2012-11-02 01:53:40 +00002321 emitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002322}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002323
Chris Lattner5f9e2722011-07-23 10:55:15 +00002324BugType *BugReporter::getBugTypeForName(StringRef name,
2325 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002326 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002327 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2328 llvm::StringMapEntry<BugType *> &
2329 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2330 BugType *BT = entry.getValue();
2331 if (!BT) {
2332 BT = new BugType(name, category);
2333 entry.setValue(BT);
2334 }
2335 return BT;
2336}