blob: 2bf701bb9c3199ada63f658e3027b871114ce6c7 [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,
196 PathDiagnosticCallPiece *CallWithLoc) {
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 }
Ted Kremenek72516742012-03-01 00:05:06 +0000220 // Recursively clean out the subclass. Keep this call around if
221 // it contains any informative diagnostics.
Ted Kremeneka43df952012-09-21 00:09:11 +0000222 PathDiagnosticCallPiece *NewCallWithLoc =
223 call->getLocation().asLocation().isValid()
224 ? call : CallWithLoc;
225
226 if (!RemoveUneededCalls(call->path, R, NewCallWithLoc))
Ted Kremenek72516742012-03-01 00:05:06 +0000227 continue;
Ted Kremeneka43df952012-09-21 00:09:11 +0000228
229 if (NewCallWithLoc == CallWithLoc && CallWithLoc) {
230 call->callEnter = CallWithLoc->callEnter;
231 }
232
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000233 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000234 break;
235 }
236 case PathDiagnosticPiece::Macro: {
237 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000238 if (!RemoveUneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000239 continue;
240 containsSomethingInteresting = true;
241 break;
242 }
243 case PathDiagnosticPiece::Event: {
244 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
Ted Kremeneka43df952012-09-21 00:09:11 +0000245
Ted Kremenek72516742012-03-01 00:05:06 +0000246 // We never throw away an event, but we do throw it away wholesale
247 // as part of a path if we throw the entire path away.
Ted Kremenek22505ef2012-09-08 07:18:18 +0000248 containsSomethingInteresting |= !event->isPrunable();
Ted Kremenek72516742012-03-01 00:05:06 +0000249 break;
250 }
251 case PathDiagnosticPiece::ControlFlow:
252 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000253 }
254
255 pieces.push_back(piece);
256 }
257
258 return containsSomethingInteresting;
259}
260
261//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000262// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000263//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000264
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000265typedef llvm::DenseMap<const ExplodedNode*,
266const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000267
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000268namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000269class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000270 NodeBackMap& M;
271public:
272 NodeMapClosure(NodeBackMap *m) : M(*m) {}
273 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek9c378f72011-08-12 23:37:29 +0000275 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000276 NodeBackMap::iterator I = M.find(N);
277 return I == M.end() ? 0 : I->second;
278 }
279};
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000281class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000282 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000283 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000284 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000285 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000286public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000287 const LocationContext *LC;
288
Ted Kremenek8966bc12009-05-06 21:39:49 +0000289 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000290 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000291 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000292 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000293 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
294 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenek9c378f72011-08-12 23:37:29 +0000296 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenek9c378f72011-08-12 23:37:29 +0000298 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
299 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Anna Zaks8e6431a2011-08-18 22:37:56 +0000301 BugReport *getBugReport() { return R; }
302
Tom Care212f6d32010-09-16 03:50:38 +0000303 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000304
305 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000307 const Stmt *getParent(const Stmt *S) {
308 return getParentMap().getParent(S);
309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenek8966bc12009-05-06 21:39:49 +0000311 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000312
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000313 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
David Blaikieef3643f2011-09-26 00:51:36 +0000315 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
316 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000317 }
318
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000319 bool supportsLogicalOpControlFlow() const {
320 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000321 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000322};
323} // end anonymous namespace
324
Ted Kremenek00605e02009-03-27 20:55:39 +0000325PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000326PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000327 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000328 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000329
Anna Zaks0cd59482011-09-16 19:18:30 +0000330 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
331 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000332}
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek00605e02009-03-27 20:55:39 +0000334PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000335PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
336 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000337
Ted Kremenek143ca222008-05-06 18:11:09 +0000338 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000339 if (os.str().empty())
340 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek00605e02009-03-27 20:55:39 +0000342 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek00605e02009-03-27 20:55:39 +0000344 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000345 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000346 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000347 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000348 else {
349 os << "Execution jumps to the end of the ";
350 const Decl *D = N->getLocationContext()->getDecl();
351 if (isa<ObjCMethodDecl>(D))
352 os << "method";
353 else if (isa<FunctionDecl>(D))
354 os << "function";
355 else {
356 assert(isa<BlockDecl>(D));
357 os << "anonymous block";
358 }
359 os << '.';
360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000362 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000363}
364
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000365static bool IsNested(const Stmt *S, ParentMap &PM) {
366 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
367 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000369 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000371 if (Parent)
372 switch (Parent->getStmtClass()) {
373 case Stmt::ForStmtClass:
374 case Stmt::DoStmtClass:
375 case Stmt::WhileStmtClass:
376 return true;
377 default:
378 break;
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
381 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000382}
383
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000384PathDiagnosticLocation
385PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000386 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000387 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000388 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000389
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000390 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000391 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000393 if (!Parent)
394 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000396 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000397 case Stmt::BinaryOperatorClass: {
398 const BinaryOperator *B = cast<BinaryOperator>(Parent);
399 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000400 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000401 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000402 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000403 case Stmt::CompoundStmtClass:
404 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000405 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000406 case Stmt::ChooseExprClass:
407 // Similar to '?' if we are referring to condition, just have the edge
408 // point to the entire choose expression.
409 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000410 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000411 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000412 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000413 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000414 case Stmt::ConditionalOperatorClass:
415 // For '?', if we are referring to condition, just have the edge point
416 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000417 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000418 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000419 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000420 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000421 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000422 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000423 case Stmt::ForStmtClass:
424 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000425 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000426 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000427 case Stmt::IfStmtClass:
428 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000429 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000430 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000431 case Stmt::ObjCForCollectionStmtClass:
432 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000433 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000434 break;
435 case Stmt::WhileStmtClass:
436 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000437 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000438 break;
439 default:
440 break;
441 }
442
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000443 S = Parent;
444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000446 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000447
448 // Special case: DeclStmts can appear in for statement declarations, in which
449 // case the ForStmt is the context.
450 if (isa<DeclStmt>(S)) {
451 if (const Stmt *Parent = P.getParent(S)) {
452 switch (Parent->getStmtClass()) {
453 case Stmt::ForStmtClass:
454 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000455 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000456 default:
457 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000458 }
459 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000460 }
461 else if (isa<BinaryOperator>(S)) {
462 // Special case: the binary operator represents the initialization
463 // code in a for statement (this can happen when the variable being
464 // initialized is an old variable.
465 if (const ForStmt *FS =
466 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
467 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000468 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000469 }
470 }
471
Anna Zaks220ac8c2011-09-15 01:08:34 +0000472 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000473}
474
Ted Kremenekcf118d42009-02-04 23:49:09 +0000475//===----------------------------------------------------------------------===//
Jordan Rosed632d6f2012-09-22 01:24:56 +0000476// "Visitors only" path diagnostic generation algorithm.
477//===----------------------------------------------------------------------===//
478static bool GenerateVisitorsOnlyPathDiagnostic(PathDiagnostic &PD,
479 PathDiagnosticBuilder &PDB,
480 const ExplodedNode *N,
481 ArrayRef<BugReporterVisitor *> visitors) {
482 // All path generation skips the very first node (the error node).
483 // This is because there is special handling for the end-of-path note.
484 N = N->getFirstPred();
485 if (!N)
486 return true;
487
488 BugReport *R = PDB.getBugReport();
489 while (const ExplodedNode *Pred = N->getFirstPred()) {
490 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
491 E = visitors.end();
492 I != E; ++I) {
493 // Visit all the node pairs, but throw the path pieces away.
494 PathDiagnosticPiece *Piece = (*I)->VisitNode(N, Pred, PDB, *R);
495 delete Piece;
496 }
497
498 N = Pred;
499 }
500
501 return R->isValid();
502}
503
504//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000505// "Minimal" path diagnostic generation algorithm.
506//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000507typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
508typedef SmallVector<StackDiagPair, 6> StackDiagVector;
509
Anna Zaks368a0d52012-03-15 21:13:02 +0000510static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000511 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000512 // If the piece contains a special message, add it to all the call
513 // pieces on the active stack.
514 if (PathDiagnosticEventPiece *ep =
515 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000516
Anna Zaks56a938f2012-03-16 23:24:20 +0000517 if (ep->hasCallStackHint())
518 for (StackDiagVector::iterator I = CallStack.begin(),
519 E = CallStack.end(); I != E; ++I) {
520 PathDiagnosticCallPiece *CP = I->first;
521 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000522 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000523
Anna Zaks368a0d52012-03-15 21:13:02 +0000524 // The last message on the path to final bug is the most important
525 // one. Since we traverse the path backwards, do not add the message
526 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000527 if (!CP->hasCallStackMessage())
528 CP->setCallStackMessage(stackMsg);
529 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000530 }
531}
Ted Kremenek31061982009-03-31 23:00:32 +0000532
Ted Kremenek77d09442012-03-02 01:27:31 +0000533static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000534
Jordan Rose8347d3d2012-09-22 01:24:53 +0000535static bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
Ted Kremenek31061982009-03-31 23:00:32 +0000536 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000537 const ExplodedNode *N,
538 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000541 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000542 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000543 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000544
Anna Zaks56a938f2012-03-16 23:24:20 +0000545 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000548 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000549 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000550 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek31061982009-03-31 23:00:32 +0000552 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000553
Anna Zaks80de4872012-08-29 21:22:37 +0000554 do {
555 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
556 PathDiagnosticCallPiece *C =
557 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
558 GRBugReporter& BR = PDB.getBugReporter();
559 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
560 PD.getActivePath().push_front(C);
561 PD.pushActivePath(&C->path);
562 CallStack.push_back(StackDiagPair(C, N));
563 break;
Anna Zaks93739372012-03-14 18:58:28 +0000564 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000565
Anna Zaks80de4872012-08-29 21:22:37 +0000566 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
567 // Flush all locations, and pop the active path.
568 bool VisitedEntireCall = PD.isWithinCall();
569 PD.popActivePath();
570
571 // Either we just added a bunch of stuff to the top-level path, or
572 // we have a previous CallExitEnd. If the former, it means that the
573 // path terminated within a function call. We must then take the
574 // current contents of the active path and place it within
575 // a new PathDiagnosticCallPiece.
576 PathDiagnosticCallPiece *C;
577 if (VisitedEntireCall) {
578 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
579 } else {
580 const Decl *Caller = CE->getLocationContext()->getDecl();
581 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
582 GRBugReporter& BR = PDB.getBugReporter();
583 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
584 }
585
586 C->setCallee(*CE, SMgr);
587 if (!CallStack.empty()) {
588 assert(CallStack.back().first == C);
589 CallStack.pop_back();
590 }
591 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Anna Zaks80de4872012-08-29 21:22:37 +0000594 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
595 const CFGBlock *Src = BE->getSrc();
596 const CFGBlock *Dst = BE->getDst();
597 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anna Zaks80de4872012-08-29 21:22:37 +0000599 if (!T)
600 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Anna Zaks80de4872012-08-29 21:22:37 +0000602 PathDiagnosticLocation Start =
603 PathDiagnosticLocation::createBegin(T, SMgr,
604 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Anna Zaks80de4872012-08-29 21:22:37 +0000606 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000607 default:
608 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Ted Kremenek31061982009-03-31 23:00:32 +0000610 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000611 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000612 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremenek31061982009-03-31 23:00:32 +0000614 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000615 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek31061982009-03-31 23:00:32 +0000617 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000618 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000619 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Ted Kremenek31061982009-03-31 23:00:32 +0000621 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000622 << End.asLocation().getExpansionLineNumber();
623 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
624 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000625 break;
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
628 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000629 // Figure out what case arm we took.
630 std::string sbuf;
631 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek9c378f72011-08-12 23:37:29 +0000633 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000634 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek31061982009-03-31 23:00:32 +0000636 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000637 default:
638 os << "No cases match in the switch statement. "
639 "Control jumps to line "
640 << End.asLocation().getExpansionLineNumber();
641 break;
642 case Stmt::DefaultStmtClass:
643 os << "Control jumps to the 'default' case at line "
644 << End.asLocation().getExpansionLineNumber();
645 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Anna Zaks80de4872012-08-29 21:22:37 +0000647 case Stmt::CaseStmtClass: {
648 os << "Control jumps to 'case ";
649 const CaseStmt *Case = cast<CaseStmt>(S);
650 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Anna Zaks80de4872012-08-29 21:22:37 +0000652 // Determine if it is an enum.
653 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Anna Zaks80de4872012-08-29 21:22:37 +0000655 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
656 // FIXME: Maybe this should be an assertion. Are there cases
657 // were it is not an EnumConstantDecl?
658 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000659 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Anna Zaks80de4872012-08-29 21:22:37 +0000661 if (D) {
662 GetRawInt = false;
663 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000664 }
Ted Kremenek31061982009-03-31 23:00:32 +0000665 }
Anna Zaks80de4872012-08-29 21:22:37 +0000666
667 if (GetRawInt)
668 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
669
670 os << ":' at line "
671 << End.asLocation().getExpansionLineNumber();
672 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000673 }
Anna Zaks80de4872012-08-29 21:22:37 +0000674 }
675 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
676 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000677 }
678 else {
679 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000680 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000681 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
682 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremenek31061982009-03-31 23:00:32 +0000685 break;
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenek31061982009-03-31 23:00:32 +0000688 case Stmt::BreakStmtClass:
689 case Stmt::ContinueStmtClass: {
690 std::string sbuf;
691 llvm::raw_string_ostream os(sbuf);
692 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000693 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
694 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000695 break;
696 }
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Anna Zaks80de4872012-08-29 21:22:37 +0000698 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000699 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000700 case Stmt::ConditionalOperatorClass: {
701 std::string sbuf;
702 llvm::raw_string_ostream os(sbuf);
703 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenek31061982009-03-31 23:00:32 +0000705 if (*(Src->succ_begin()+1) == Dst)
706 os << "false";
707 else
708 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek31061982009-03-31 23:00:32 +0000710 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek31061982009-03-31 23:00:32 +0000712 if (const Stmt *S = End.asStmt())
713 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Anna Zaks80de4872012-08-29 21:22:37 +0000715 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
716 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000717 break;
718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Anna Zaks80de4872012-08-29 21:22:37 +0000720 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000721 case Stmt::BinaryOperatorClass: {
722 if (!PDB.supportsLogicalOpControlFlow())
723 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000725 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000726 std::string sbuf;
727 llvm::raw_string_ostream os(sbuf);
728 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000729
John McCall2de56d12010-08-25 11:45:40 +0000730 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000731 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek31061982009-03-31 23:00:32 +0000733 if (*(Src->succ_begin()+1) == Dst) {
734 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000735 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000736 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000737 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
738 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
739 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000740 }
Ted Kremenek31061982009-03-31 23:00:32 +0000741 else {
742 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000743 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000744 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000745 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
746 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000747 }
Ted Kremenek31061982009-03-31 23:00:32 +0000748 }
749 else {
John McCall2de56d12010-08-25 11:45:40 +0000750 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000751 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenek31061982009-03-31 23:00:32 +0000753 if (*(Src->succ_begin()+1) == Dst) {
754 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000755 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000756 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000757 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
758 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000759 }
760 else {
761 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000762 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000763 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000764 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
765 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
766 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000767 }
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek31061982009-03-31 23:00:32 +0000770 break;
771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772
773 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000774 if (*(Src->succ_begin()) == Dst) {
775 std::string sbuf;
776 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek31061982009-03-31 23:00:32 +0000778 os << "Loop condition is true. ";
779 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Ted Kremenek31061982009-03-31 23:00:32 +0000781 if (const Stmt *S = End.asStmt())
782 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Anna Zaks80de4872012-08-29 21:22:37 +0000784 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
785 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000786 }
787 else {
788 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek31061982009-03-31 23:00:32 +0000790 if (const Stmt *S = End.asStmt())
791 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Anna Zaks80de4872012-08-29 21:22:37 +0000793 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
794 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek31061982009-03-31 23:00:32 +0000797 break;
798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek31061982009-03-31 23:00:32 +0000800 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000801 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000802 if (*(Src->succ_begin()+1) == Dst) {
803 std::string sbuf;
804 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek31061982009-03-31 23:00:32 +0000806 os << "Loop condition is false. ";
807 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
808 if (const Stmt *S = End.asStmt())
809 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Anna Zaks80de4872012-08-29 21:22:37 +0000811 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
812 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000813 }
814 else {
815 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
816 if (const Stmt *S = End.asStmt())
817 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Anna Zaks80de4872012-08-29 21:22:37 +0000819 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
820 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000821 }
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek31061982009-03-31 23:00:32 +0000823 break;
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenek31061982009-03-31 23:00:32 +0000826 case Stmt::IfStmtClass: {
827 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek31061982009-03-31 23:00:32 +0000829 if (const Stmt *S = End.asStmt())
830 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek31061982009-03-31 23:00:32 +0000832 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000833 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
834 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000835 else
Anna Zaks80de4872012-08-29 21:22:37 +0000836 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
837 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek31061982009-03-31 23:00:32 +0000839 break;
840 }
Anna Zaks80de4872012-08-29 21:22:37 +0000841 }
Ted Kremenek31061982009-03-31 23:00:32 +0000842 }
Anna Zaks80de4872012-08-29 21:22:37 +0000843 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000845 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000846 // Add diagnostic pieces from custom visitors.
847 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000848 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
849 E = visitors.end();
850 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000851 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000852 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000853 updateStackPiecesWithMessage(p, CallStack);
854 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000855 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000856 }
Ted Kremenek31061982009-03-31 23:00:32 +0000857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Jordan Rose8347d3d2012-09-22 01:24:53 +0000859 if (!PDB.getBugReport()->isValid())
860 return false;
861
Ted Kremenek14856d72009-04-06 23:06:54 +0000862 // After constructing the full PathDiagnostic, do a pass over it to compact
863 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000864 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Jordan Rose8347d3d2012-09-22 01:24:53 +0000865 return true;
Ted Kremenek31061982009-03-31 23:00:32 +0000866}
867
868//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000869// "Extensive" PathDiagnostic generation.
870//===----------------------------------------------------------------------===//
871
872static bool IsControlFlowExpr(const Stmt *S) {
873 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000875 if (!E)
876 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
878 E = E->IgnoreParenCasts();
879
John McCall56ca35d2011-02-17 10:25:35 +0000880 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000881 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000883 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
884 if (B->isLogicalOp())
885 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000886
887 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000888}
889
Ted Kremenek14856d72009-04-06 23:06:54 +0000890namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000891class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000892 bool IsDead;
893public:
894 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
895 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000896
897 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000898 bool isDead() const { return IsDead; }
899};
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000901class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000902 std::vector<ContextLocation> CLocs;
903 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000904 PathDiagnostic &PD;
905 PathDiagnosticBuilder &PDB;
906 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000908 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremenek14856d72009-04-06 23:06:54 +0000910 bool containsLocation(const PathDiagnosticLocation &Container,
911 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Ted Kremenek14856d72009-04-06 23:06:54 +0000913 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek9650cf32009-05-11 21:42:34 +0000915 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
916 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000917 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000918 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000919 while (1) {
920 // Adjust the location for some expressions that are best referenced
921 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000922 switch (S->getStmtClass()) {
923 default:
924 break;
925 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000926 case Stmt::GenericSelectionExprClass:
927 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000928 firstCharOnly = true;
929 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000930 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000931 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000932 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000933 firstCharOnly = true;
934 continue;
935 case Stmt::ChooseExprClass:
936 S = cast<ChooseExpr>(S)->getCond();
937 firstCharOnly = true;
938 continue;
939 case Stmt::BinaryOperatorClass:
940 S = cast<BinaryOperator>(S)->getLHS();
941 firstCharOnly = true;
942 continue;
943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Ted Kremenek9650cf32009-05-11 21:42:34 +0000945 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Ted Kremenek9650cf32009-05-11 21:42:34 +0000948 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000949 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Ted Kremenek9650cf32009-05-11 21:42:34 +0000952 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000953 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000954
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000955 return L;
956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Ted Kremenek14856d72009-04-06 23:06:54 +0000958 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000959 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000960 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000961 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000962 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000963 CLocs.pop_back();
964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Ted Kremenek14856d72009-04-06 23:06:54 +0000966public:
967 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
968 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremeneka301a672009-04-22 18:16:20 +0000970 // If the PathDiagnostic already has pieces, add the enclosing statement
971 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000972 if (!PD.path.empty()) {
973 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Ted Kremenek14856d72009-04-06 23:06:54 +0000975 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000976 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000977 }
978 }
979
980 ~EdgeBuilder() {
981 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000982
Ted Kremeneka301a672009-04-22 18:16:20 +0000983 // Finally, add an initial edge from the start location of the first
984 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000985 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000986 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000987 PDB.getSourceManager());
988 if (L.isValid())
989 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000990 }
991
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000992 void flushLocations() {
993 while (!CLocs.empty())
994 popLocation();
995 PrevLoc = PathDiagnosticLocation();
996 }
997
Ted Kremenek14856d72009-04-06 23:06:54 +0000998 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001000 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek14856d72009-04-06 23:06:54 +00001002 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001003 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001004 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +00001005};
Ted Kremenek14856d72009-04-06 23:06:54 +00001006} // end anonymous namespace
1007
1008
1009PathDiagnosticLocation
1010EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1011 if (const Stmt *S = L.asStmt()) {
1012 if (IsControlFlowExpr(S))
1013 return L;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
1015 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +00001016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremenek14856d72009-04-06 23:06:54 +00001018 return L;
1019}
1020
1021bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1022 const PathDiagnosticLocation &Containee) {
1023
1024 if (Container == Containee)
1025 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Ted Kremenek14856d72009-04-06 23:06:54 +00001027 if (Container.asDecl())
1028 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremenek14856d72009-04-06 23:06:54 +00001030 if (const Stmt *S = Containee.asStmt())
1031 if (const Stmt *ContainerS = Container.asStmt()) {
1032 while (S) {
1033 if (S == ContainerS)
1034 return true;
1035 S = PDB.getParent(S);
1036 }
1037 return false;
1038 }
1039
1040 // Less accurate: compare using source ranges.
1041 SourceRange ContainerR = Container.asRange();
1042 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Ted Kremenek14856d72009-04-06 23:06:54 +00001044 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001045 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1046 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1047 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1048 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Chandler Carruth64211622011-07-25 21:09:52 +00001050 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1051 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1052 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1053 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001056 assert(ContaineeBegLine <= ContaineeEndLine);
1057
Ted Kremenek14856d72009-04-06 23:06:54 +00001058 return (ContainerBegLine <= ContaineeBegLine &&
1059 ContainerEndLine >= ContaineeEndLine &&
1060 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001061 SM.getExpansionColumnNumber(ContainerRBeg) <=
1062 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001063 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001064 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +00001065 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001066}
1067
Ted Kremenek14856d72009-04-06 23:06:54 +00001068void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1069 if (!PrevLoc.isValid()) {
1070 PrevLoc = NewLoc;
1071 return;
1072 }
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001074 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1075 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Ted Kremeneka43df952012-09-21 00:09:11 +00001077 if (PrevLocClean.asLocation().isInvalid()) {
1078 PrevLoc = NewLoc;
1079 return;
1080 }
1081
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001082 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001083 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Ted Kremenek14856d72009-04-06 23:06:54 +00001085 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001086 if (NewLocClean.asLocation().getExpansionLoc() ==
1087 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001088 return;
1089
Ted Kremenek2042fc12012-02-24 06:00:00 +00001090 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001091 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001092}
1093
1094void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Ted Kremeneka301a672009-04-22 18:16:20 +00001096 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1097 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Ted Kremenek14856d72009-04-06 23:06:54 +00001099 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1100
1101 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001102 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Ted Kremenek14856d72009-04-06 23:06:54 +00001104 // Is the top location context the same as the one for the new location?
1105 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001106 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001107 if (IsConsumedExpr(TopContextLoc) &&
1108 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001109 TopContextLoc.markDead();
1110
Ted Kremenek14856d72009-04-06 23:06:54 +00001111 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001112 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001113
1114 return;
1115 }
1116
1117 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001118 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001119 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001121 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001122 CLocs.push_back(ContextLocation(CLoc, true));
1123 return;
1124 }
1125 }
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Ted Kremenek14856d72009-04-06 23:06:54 +00001127 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001128 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001129 }
1130
1131 // Context does not contain the location. Flush it.
1132 popLocation();
1133 }
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001135 // If we reach here, there is no enclosing context. Just add the edge.
1136 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001137}
1138
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001139bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1140 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1141 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001143 return false;
1144}
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremeneke1baed32009-05-05 23:13:38 +00001146void EdgeBuilder::addExtendedContext(const Stmt *S) {
1147 if (!S)
1148 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001149
1150 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001151 while (Parent) {
1152 if (isa<CompoundStmt>(Parent))
1153 Parent = PDB.getParent(Parent);
1154 else
1155 break;
1156 }
1157
1158 if (Parent) {
1159 switch (Parent->getStmtClass()) {
1160 case Stmt::DoStmtClass:
1161 case Stmt::ObjCAtSynchronizedStmtClass:
1162 addContext(Parent);
1163 default:
1164 break;
1165 }
1166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Ted Kremeneke1baed32009-05-05 23:13:38 +00001168 addContext(S);
1169}
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenek14856d72009-04-06 23:06:54 +00001171void EdgeBuilder::addContext(const Stmt *S) {
1172 if (!S)
1173 return;
1174
Ted Kremenek59950d32012-02-24 07:12:52 +00001175 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001176 addContext(L);
1177}
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Jordan Rose183ba8e2012-07-26 20:04:05 +00001179void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001180 while (!CLocs.empty()) {
1181 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1182
1183 // Is the top location context the same as the one for the new location?
1184 if (TopContextLoc == L)
1185 return;
1186
1187 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001188 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001189 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001190 }
1191
1192 // Context does not contain the location. Flush it.
1193 popLocation();
1194 }
1195
1196 CLocs.push_back(L);
1197}
1198
Ted Kremenek11abcec2012-05-02 00:31:29 +00001199// Cone-of-influence: support the reverse propagation of "interesting" symbols
1200// and values by tracing interesting calculations backwards through evaluated
1201// expressions along a path. This is probably overly complicated, but the idea
1202// is that if an expression computed an "interesting" value, the child
1203// expressions are are also likely to be "interesting" as well (which then
1204// propagates to the values they in turn compute). This reverse propagation
1205// is needed to track interesting correlations across function call boundaries,
1206// where formal arguments bind to actual arguments, etc. This is also needed
1207// because the constraint solver sometimes simplifies certain symbolic values
1208// into constants when appropriate, and this complicates reasoning about
1209// interesting values.
1210typedef llvm::DenseSet<const Expr *> InterestingExprs;
1211
1212static void reversePropagateIntererstingSymbols(BugReport &R,
1213 InterestingExprs &IE,
1214 const ProgramState *State,
1215 const Expr *Ex,
1216 const LocationContext *LCtx) {
1217 SVal V = State->getSVal(Ex, LCtx);
1218 if (!(R.isInteresting(V) || IE.count(Ex)))
1219 return;
1220
1221 switch (Ex->getStmtClass()) {
1222 default:
1223 if (!isa<CastExpr>(Ex))
1224 break;
1225 // Fall through.
1226 case Stmt::BinaryOperatorClass:
1227 case Stmt::UnaryOperatorClass: {
1228 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1229 CE = Ex->child_end();
1230 CI != CE; ++CI) {
1231 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1232 IE.insert(child);
1233 SVal ChildV = State->getSVal(child, LCtx);
1234 R.markInteresting(ChildV);
1235 }
1236 break;
1237 }
1238 }
1239 }
1240
1241 R.markInteresting(V);
1242}
1243
1244static void reversePropagateInterestingSymbols(BugReport &R,
1245 InterestingExprs &IE,
1246 const ProgramState *State,
1247 const LocationContext *CalleeCtx,
1248 const LocationContext *CallerCtx)
1249{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001250 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001251 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1252 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001253 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001254 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1255 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1256 PE = FD->param_end();
1257 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1258 for (; AI != AE && PI != PE; ++AI, ++PI) {
1259 if (const Expr *ArgE = *AI) {
1260 if (const ParmVarDecl *PD = *PI) {
1261 Loc LV = State->getLValue(PD, CalleeCtx);
1262 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1263 IE.insert(ArgE);
1264 }
1265 }
1266 }
1267 }
1268 }
1269}
1270
Jordan Rose8347d3d2012-09-22 01:24:53 +00001271static bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek14856d72009-04-06 23:06:54 +00001272 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001273 const ExplodedNode *N,
1274 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001275 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001276 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001277 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001278 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001279
Ted Kremenek9c378f72011-08-12 23:37:29 +00001280 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001281 while (NextNode) {
1282 N = NextNode;
1283 NextNode = GetPredecessorNode(N);
1284 ProgramPoint P = N->getLocation();
1285
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001286 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001287 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1288 if (const Expr *Ex = PS->getStmtAs<Expr>())
1289 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1290 N->getState().getPtr(), Ex,
1291 N->getLocationContext());
1292 }
1293
Anna Zaks0b3ade82012-04-20 21:59:08 +00001294 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001295 const Stmt *S = CE->getCalleeContext()->getCallSite();
1296 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001297 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1298 N->getState().getPtr(), Ex,
1299 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001300 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001301
1302 PathDiagnosticCallPiece *C =
1303 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001304 GRBugReporter& BR = PDB.getBugReporter();
1305 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001306
1307 EB.addEdge(C->callReturn, true);
1308 EB.flushLocations();
1309
1310 PD.getActivePath().push_front(C);
1311 PD.pushActivePath(&C->path);
1312 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001313 break;
1314 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001315
Ted Kremenek2042fc12012-02-24 06:00:00 +00001316 // Pop the call hierarchy if we are done walking the contents
1317 // of a function call.
1318 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001319 // Add an edge to the start of the function.
1320 const Decl *D = CE->getCalleeContext()->getDecl();
1321 PathDiagnosticLocation pos =
1322 PathDiagnosticLocation::createBegin(D, SM);
1323 EB.addEdge(pos);
1324
1325 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001326 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001327 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001328 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001329 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001330
Jordan Rose183ba8e2012-07-26 20:04:05 +00001331 // Either we just added a bunch of stuff to the top-level path, or
1332 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001333 // path terminated within a function call. We must then take the
1334 // current contents of the active path and place it within
1335 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001336 PathDiagnosticCallPiece *C;
1337 if (VisitedEntireCall) {
1338 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1339 } else {
1340 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001341 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001342 GRBugReporter& BR = PDB.getBugReporter();
1343 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001344 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001345
Jordan Rose183ba8e2012-07-26 20:04:05 +00001346 C->setCallee(*CE, SM);
1347 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001348
1349 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001350 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001351 CallStack.pop_back();
1352 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001353 break;
1354 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001355
1356 // Note that is important that we update the LocationContext
1357 // after looking at CallExits. CallExit basically adds an
1358 // edge in the *caller*, so we don't want to update the LocationContext
1359 // too soon.
1360 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001361
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001362 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001363 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1364 // Does this represent entering a call? If so, look at propagating
1365 // interesting symbols across call boundaries.
1366 if (NextNode) {
1367 const LocationContext *CallerCtx = NextNode->getLocationContext();
1368 const LocationContext *CalleeCtx = PDB.LC;
1369 if (CallerCtx != CalleeCtx) {
1370 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1371 N->getState().getPtr(),
1372 CalleeCtx, CallerCtx);
1373 }
1374 }
1375
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001376 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001377 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001378 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001379 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001381 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1382 CS = dyn_cast<CompoundStmt>(FS->getBody());
1383 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1384 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001386 PathDiagnosticEventPiece *p =
1387 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001388 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001389 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001391 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001392 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001394 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001395 PathDiagnosticLocation BL =
1396 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001397 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001398 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001399 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001400
1401 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001402 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001404 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001405 }
1406
Mike Stump1eb44332009-09-09 15:08:12 +00001407 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Jordan Rose1a7bcc42012-09-12 22:48:08 +00001408 CFGElement First = BE->getFirstElement();
1409 if (const CFGStmt *S = First.getAs<CFGStmt>()) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001410 const Stmt *stmt = S->getStmt();
1411 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001412 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001413 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001414 }
1415 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001416 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001417 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001418
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001419 break;
1420 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001421
1422
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001423 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001425 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001426 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Anna Zaks8e6431a2011-08-18 22:37:56 +00001428 // Add pieces from custom visitors.
1429 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001430 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1431 E = visitors.end();
1432 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001433 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001434 const PathDiagnosticLocation &Loc = p->getLocation();
1435 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001436 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001437 updateStackPiecesWithMessage(p, CallStack);
1438
Ted Kremenek8966bc12009-05-06 21:39:49 +00001439 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001440 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001441 }
Mike Stump1eb44332009-09-09 15:08:12 +00001442 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001443 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00001444
1445 return PDB.getBugReport()->isValid();
Ted Kremenek14856d72009-04-06 23:06:54 +00001446}
1447
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001448//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001449// Methods for BugType and subclasses.
1450//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001451BugType::~BugType() { }
1452
Ted Kremenekcf118d42009-02-04 23:49:09 +00001453void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001454
David Blaikie99ba9e32011-12-20 02:48:34 +00001455void BuiltinBug::anchor() {}
1456
Ted Kremenekcf118d42009-02-04 23:49:09 +00001457//===----------------------------------------------------------------------===//
1458// Methods for BugReport and subclasses.
1459//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001460
David Blaikie99ba9e32011-12-20 02:48:34 +00001461void BugReport::NodeResolver::anchor() {}
1462
Anna Zaks8e6431a2011-08-18 22:37:56 +00001463void BugReport::addVisitor(BugReporterVisitor* visitor) {
1464 if (!visitor)
1465 return;
1466
1467 llvm::FoldingSetNodeID ID;
1468 visitor->Profile(ID);
1469 void *InsertPos;
1470
1471 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1472 delete visitor;
1473 return;
1474 }
1475
1476 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001477 Callbacks.push_back(visitor);
1478 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001479}
1480
1481BugReport::~BugReport() {
1482 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001483 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001484 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001485 while (!interestingSymbols.empty()) {
1486 popInterestingSymbolsAndRegions();
1487 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001488}
Anna Zakse172e8b2011-08-17 23:00:25 +00001489
Ted Kremenek07189522012-04-04 18:11:35 +00001490const Decl *BugReport::getDeclWithIssue() const {
1491 if (DeclWithIssue)
1492 return DeclWithIssue;
1493
1494 const ExplodedNode *N = getErrorNode();
1495 if (!N)
1496 return 0;
1497
1498 const LocationContext *LC = N->getLocationContext();
1499 return LC->getCurrentStackFrame()->getDecl();
1500}
1501
Anna Zakse172e8b2011-08-17 23:00:25 +00001502void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1503 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001504 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001505 if (UniqueingLocation.isValid()) {
1506 UniqueingLocation.Profile(hash);
1507 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001508 Location.Profile(hash);
1509 } else {
1510 assert(ErrorNode);
1511 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1512 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001513
1514 for (SmallVectorImpl<SourceRange>::const_iterator I =
1515 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1516 const SourceRange range = *I;
1517 if (!range.isValid())
1518 continue;
1519 hash.AddInteger(range.getBegin().getRawEncoding());
1520 hash.AddInteger(range.getEnd().getRawEncoding());
1521 }
1522}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001523
Ted Kremenek76aadc32012-03-09 01:13:14 +00001524void BugReport::markInteresting(SymbolRef sym) {
1525 if (!sym)
1526 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001527
1528 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001529 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001530 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001531
1532 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001533 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001534}
1535
1536void BugReport::markInteresting(const MemRegion *R) {
1537 if (!R)
1538 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001539
1540 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001541 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001542 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001543 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001544
Ted Kremenek76aadc32012-03-09 01:13:14 +00001545 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001546 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001547}
1548
1549void BugReport::markInteresting(SVal V) {
1550 markInteresting(V.getAsRegion());
1551 markInteresting(V.getAsSymbol());
1552}
1553
Anna Zaks80de4872012-08-29 21:22:37 +00001554void BugReport::markInteresting(const LocationContext *LC) {
1555 if (!LC)
1556 return;
1557 InterestingLocationContexts.insert(LC);
1558}
1559
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001560bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001561 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1562}
1563
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001564bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001565 if (!sym)
1566 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001567 // We don't currently consider metadata symbols to be interesting
1568 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001569 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001570}
1571
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001572bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001573 if (!R)
1574 return false;
1575 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001576 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001577 if (b)
1578 return true;
1579 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001580 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001581 return false;
1582}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001583
Anna Zaks80de4872012-08-29 21:22:37 +00001584bool BugReport::isInteresting(const LocationContext *LC) {
1585 if (!LC)
1586 return false;
1587 return InterestingLocationContexts.count(LC);
1588}
1589
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001590void BugReport::lazyInitializeInterestingSets() {
1591 if (interestingSymbols.empty()) {
1592 interestingSymbols.push_back(new Symbols());
1593 interestingRegions.push_back(new Regions());
1594 }
1595}
1596
1597BugReport::Symbols &BugReport::getInterestingSymbols() {
1598 lazyInitializeInterestingSets();
1599 return *interestingSymbols.back();
1600}
1601
1602BugReport::Regions &BugReport::getInterestingRegions() {
1603 lazyInitializeInterestingSets();
1604 return *interestingRegions.back();
1605}
1606
1607void BugReport::pushInterestingSymbolsAndRegions() {
1608 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1609 interestingRegions.push_back(new Regions(getInterestingRegions()));
1610}
1611
1612void BugReport::popInterestingSymbolsAndRegions() {
1613 delete interestingSymbols.back();
1614 interestingSymbols.pop_back();
1615 delete interestingRegions.back();
1616 interestingRegions.pop_back();
1617}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001618
Ted Kremenek9c378f72011-08-12 23:37:29 +00001619const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001620 if (!ErrorNode)
1621 return 0;
1622
Tom Care212f6d32010-09-16 03:50:38 +00001623 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001624 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek9c378f72011-08-12 23:37:29 +00001626 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001627 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001628 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001629 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001630 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001631 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001632 S = GetStmt(ProgP);
1633
1634 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001635}
1636
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001637std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001638BugReport::getRanges() {
1639 // If no custom ranges, add the range of the statement corresponding to
1640 // the error node.
1641 if (Ranges.empty()) {
1642 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1643 addRange(E->getSourceRange());
1644 else
1645 return std::make_pair(ranges_iterator(), ranges_iterator());
1646 }
1647
Anna Zaks14924262011-08-24 20:31:06 +00001648 // User-specified absence of range info.
1649 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1650 return std::make_pair(ranges_iterator(), ranges_iterator());
1651
Anna Zakse172e8b2011-08-17 23:00:25 +00001652 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001653}
1654
Anna Zaks590dd8e2011-09-20 21:38:35 +00001655PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001656 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001657 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001658 "Either Location or ErrorNode should be specified but not both.");
1659
Ted Kremenek9c378f72011-08-12 23:37:29 +00001660 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001661 const LocationContext *LC = ErrorNode->getLocationContext();
1662
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001663 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001664 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001665 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001666 // For binary operators, return the location of the operator.
1667 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001668 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001669
Anna Zaks590dd8e2011-09-20 21:38:35 +00001670 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001671 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001672 } else {
1673 assert(Location.isValid());
1674 return Location;
1675 }
1676
Anna Zaks590dd8e2011-09-20 21:38:35 +00001677 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001678}
1679
Ted Kremenekcf118d42009-02-04 23:49:09 +00001680//===----------------------------------------------------------------------===//
1681// Methods for BugReporter and subclasses.
1682//===----------------------------------------------------------------------===//
1683
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001684BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001685GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001686BugReporterData::~BugReporterData() {}
1687
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001688ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001689
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001690ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001691GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1692
Anna Zaks3b030a22011-08-19 01:57:09 +00001693BugReporter::~BugReporter() {
1694 FlushReports();
1695
1696 // Free the bug reports we are tracking.
1697 typedef std::vector<BugReportEquivClass *> ContTy;
1698 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1699 I != E; ++I) {
1700 delete *I;
1701 }
1702}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001703
1704void BugReporter::FlushReports() {
1705 if (BugTypes.isEmpty())
1706 return;
1707
1708 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001709 // warnings and new BugTypes.
1710 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1711 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001712 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001713 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001714 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001715 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001716 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001717 const_cast<BugType*>(*I)->FlushReports(*this);
1718
Anna Zaksd015f4f2012-08-02 23:41:05 +00001719 // We need to flush reports in deterministic order to ensure the order
1720 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001721 typedef std::vector<BugReportEquivClass *> ContVecTy;
1722 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1723 EI != EE; ++EI){
1724 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001725 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001726 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001727
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001728 // BugReporter owns and deletes only BugTypes created implicitly through
1729 // EmitBasicReport.
1730 // FIXME: There are leaks from checkers that assume that the BugTypes they
1731 // create will be destroyed by the BugReporter.
1732 for (llvm::StringMap<BugType*>::iterator
1733 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1734 delete I->second;
1735
Ted Kremenekcf118d42009-02-04 23:49:09 +00001736 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001737 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001738}
1739
1740//===----------------------------------------------------------------------===//
1741// PathDiagnostics generation.
1742//===----------------------------------------------------------------------===//
1743
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001744static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001745 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001746MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001747 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Ted Kremenekcf118d42009-02-04 23:49:09 +00001749 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001750 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001751 // error node unless there are two or more error nodes with the same minimum
1752 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001753 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001754 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001755
1756 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001757 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1758 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Ted Kremenekcf118d42009-02-04 23:49:09 +00001760 // Create owning pointers for GTrim and NMap just to ensure that they are
1761 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001762 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1763 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Ted Kremenekcf118d42009-02-04 23:49:09 +00001765 // Find the (first) error node in the trimmed graph. We just need to consult
1766 // the node map (NMap) which maps from nodes in the original graph to nodes
1767 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001768
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001769 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001770 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001771 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001772
Ted Kremenek40406fe2010-12-03 06:52:30 +00001773 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1774 const ExplodedNode *originalNode = nodes[nodeIndex];
1775 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001776 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001777 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001778 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001779 }
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Ted Kremenek938332c2009-05-16 01:11:58 +00001781 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001782
1783 // Create a new (third!) graph with a single path. This is the graph
1784 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001785 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Ted Kremenek10aa5542009-03-12 23:41:59 +00001787 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001788 // to the root node, and then construct a new graph that contains only
1789 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001790 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001792 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001793 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001795 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001796 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001797 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001799 if (Visited.find(Node) != Visited.end())
1800 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001802 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001804 if (Node->pred_empty()) {
1805 Root = Node;
1806 break;
1807 }
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001809 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001810 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001811 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001812 }
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Ted Kremenek938332c2009-05-16 01:11:58 +00001814 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Ted Kremenek10aa5542009-03-12 23:41:59 +00001816 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001817 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001818 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001819 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001820 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001822 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001823 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001824 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001825 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001827 // Create the equivalent node in the new graph with the same state
1828 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001829 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001831 // Store the mapping to the original node.
1832 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1833 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001834 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001836 // Link up the new node with the previous node.
1837 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001838 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001840 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001842 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001843 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001844 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001845 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001846 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001847 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001848 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001849 }
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001851 // Find the next successor node. We choose the node that is marked
1852 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001853 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1854 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001855 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001857 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001859 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001861 if (I == Visited.end())
1862 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001864 if (!N || I->second < MinVal) {
1865 N = *SI;
1866 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001867 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001868 }
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Ted Kremenek938332c2009-05-16 01:11:58 +00001870 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001871 }
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Ted Kremenek938332c2009-05-16 01:11:58 +00001873 assert(First);
1874
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001875 return std::make_pair(std::make_pair(GNew, BM),
1876 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001877}
1878
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001879/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1880/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001881static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001882 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1883 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001885 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001886 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001887
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001888 MacroStackTy MacroStack;
1889 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Ted Kremenek77d09442012-03-02 01:27:31 +00001891 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001892 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001893
1894 PathDiagnosticPiece *piece = I->getPtr();
1895
1896 // Recursively compact calls.
1897 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1898 CompactPathDiagnostic(call->path, SM);
1899 }
1900
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001901 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001902 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001904 // Determine the instantiation location, which is the location we group
1905 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001906 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001907 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001908 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001910 if (Loc.isFileID()) {
1911 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001912 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001913 continue;
1914 }
1915
1916 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001918 // Is the PathDiagnosticPiece within the same macro group?
1919 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001920 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001921 continue;
1922 }
1923
1924 // We aren't in the same group. Are we descending into a new macro
1925 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001926 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001927
1928 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001929 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001930 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001932 // Walk the entire macro stack.
1933 while (!MacroStack.empty()) {
1934 if (InstantiationLoc == MacroStack.back().second) {
1935 MacroGroup = MacroStack.back().first;
1936 break;
1937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001939 if (ParentInstantiationLoc == MacroStack.back().second) {
1940 MacroGroup = MacroStack.back().first;
1941 break;
1942 }
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001944 MacroStack.pop_back();
1945 }
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001947 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1948 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001949 PathDiagnosticMacroPiece *NewGroup =
1950 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001951 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001952
1953 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001954 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001955 else {
1956 assert(InstantiationLoc.isFileID());
1957 Pieces.push_back(NewGroup);
1958 }
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001960 MacroGroup = NewGroup;
1961 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1962 }
1963
1964 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001965 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001966 }
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001968 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001969 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Ted Kremenek77d09442012-03-02 01:27:31 +00001971 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1972 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001973}
1974
Jordan Rose8347d3d2012-09-22 01:24:53 +00001975bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001976 PathDiagnosticConsumer &PC,
1977 ArrayRef<BugReport *> &bugReports) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001978 assert(!bugReports.empty());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001979
1980 bool HasValid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001981 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001982 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1983 E = bugReports.end(); I != E; ++I) {
Jordan Rose8347d3d2012-09-22 01:24:53 +00001984 if ((*I)->isValid()) {
1985 HasValid = true;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001986 errorNodes.push_back((*I)->getErrorNode());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001987 } else {
1988 errorNodes.push_back(0);
1989 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001990 }
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Jordan Rose8347d3d2012-09-22 01:24:53 +00001992 // If all the reports have been marked invalid, we're done.
1993 if (!HasValid)
1994 return false;
1995
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001996 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001997 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001998 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001999 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00002000 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Ted Kremenekcf118d42009-02-04 23:49:09 +00002002 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002003 assert(GPair.second.second < bugReports.size());
2004 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00002005 assert(R && "No original report found for sliced graph.");
Jordan Rose8347d3d2012-09-22 01:24:53 +00002006 assert(R->isValid() && "Report selected from trimmed graph marked invalid.");
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002008 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
2009 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002010 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
2012 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002013 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Anna Zaks8e6431a2011-08-18 22:37:56 +00002015 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00002016 R->addVisitor(new NilReceiverBRVisitor());
2017 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002019 BugReport::VisitorList visitors;
2020 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00002021
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002022 // While generating diagnostics, it's possible the visitors will decide
2023 // new symbols and regions are interesting, or add other visitors based on
2024 // the information they find. If they do, we need to regenerate the path
2025 // based on our new report configuration.
2026 do {
2027 // Get a clean copy of all the visitors.
2028 for (BugReport::visitor_iterator I = R->visitor_begin(),
2029 E = R->visitor_end(); I != E; ++I)
2030 visitors.push_back((*I)->clone());
2031
2032 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002033 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002034 originalReportConfigToken = R->getConfigurationChangeToken();
2035
2036 // Generate the very last diagnostic piece - the piece is visible before
2037 // the trace is expanded.
Jordan Rosed632d6f2012-09-22 01:24:56 +00002038 if (PDB.getGenerationScheme() != PathDiagnosticConsumer::None) {
2039 PathDiagnosticPiece *LastPiece = 0;
2040 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
2041 I != E; ++I) {
2042 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
2043 assert (!LastPiece &&
2044 "There can only be one final piece in a diagnostic.");
2045 LastPiece = Piece;
2046 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002047 }
Jordan Rosed632d6f2012-09-22 01:24:56 +00002048 if (!LastPiece)
2049 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
2050 if (LastPiece)
2051 PD.setEndOfPath(LastPiece);
2052 else
2053 return false;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002054 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002055
2056 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00002057 case PathDiagnosticConsumer::Extensive:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002058 if (!GenerateExtensivePathDiagnostic(PD, PDB, N, visitors)) {
2059 assert(!R->isValid() && "Failed on valid report");
2060 // Try again. We'll filter out the bad report when we trim the graph.
2061 // FIXME: It would be more efficient to use the same intermediate
2062 // trimmed graph, and just repeat the shortest-path search.
2063 return generatePathDiagnostic(PD, PC, bugReports);
2064 }
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00002065 break;
David Blaikieef3643f2011-09-26 00:51:36 +00002066 case PathDiagnosticConsumer::Minimal:
Jordan Rose8347d3d2012-09-22 01:24:53 +00002067 if (!GenerateMinimalPathDiagnostic(PD, PDB, N, visitors)) {
2068 assert(!R->isValid() && "Failed on valid report");
2069 // Try again. We'll filter out the bad report when we trim the graph.
2070 return generatePathDiagnostic(PD, PC, bugReports);
2071 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00002072 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002073 case PathDiagnosticConsumer::None:
Jordan Rosed632d6f2012-09-22 01:24:56 +00002074 if (!GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors)) {
2075 assert(!R->isValid() && "Failed on valid report");
2076 // Try again. We'll filter out the bad report when we trim the graph.
2077 return generatePathDiagnostic(PD, PC, bugReports);
2078 }
2079 break;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002080 }
2081
2082 // Clean up the visitors we used.
2083 llvm::DeleteContainerPointers(visitors);
2084
2085 // Did anything change while generating this path?
2086 finalReportConfigToken = R->getConfigurationChangeToken();
2087 } while(finalReportConfigToken != originalReportConfigToken);
2088
Ted Kremenekc89f4b02012-02-28 23:06:21 +00002089 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremenekb85cce02012-10-25 22:07:10 +00002090 if (!PD.path.empty()) {
2091 // Remove messages that are basically the same.
Ted Kremenek38001652012-10-26 16:02:36 +00002092 removeRedundantMsgs(PD.getMutablePieces());
Ted Kremenekb85cce02012-10-25 22:07:10 +00002093
2094 if (R->shouldPrunePath()) {
2095 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(),
2096 R);
2097 assert(hasSomethingInteresting);
2098 (void) hasSomethingInteresting;
2099 }
Ted Kremeneked7948b2012-05-31 06:03:17 +00002100 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00002101
2102 return true;
Ted Kremenek7dc86642009-03-31 20:22:36 +00002103}
2104
Ted Kremenekcf118d42009-02-04 23:49:09 +00002105void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00002106 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00002107}
2108
Mike Stump1eb44332009-09-09 15:08:12 +00002109void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00002110 // Compute the bug report's hash to determine its equivalence class.
2111 llvm::FoldingSetNodeID ID;
2112 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002113
2114 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00002115 BugType& BT = R->getBugType();
2116 Register(&BT);
2117 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002118 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Ted Kremenekcf118d42009-02-04 23:49:09 +00002120 if (!EQ) {
2121 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002122 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00002123 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002124 }
2125 else
2126 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00002127}
2128
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002129
2130//===----------------------------------------------------------------------===//
2131// Emitting reports in equivalence classes.
2132//===----------------------------------------------------------------------===//
2133
2134namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002135struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002136 const ExplodedNode *N;
2137 ExplodedNode::const_succ_iterator I, E;
2138
2139 FRIEC_WLItem(const ExplodedNode *n)
2140 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2141};
2142}
2143
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002144static BugReport *
2145FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002146 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002147
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002148 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2149 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002150 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002151
Ted Kremenek40406fe2010-12-03 06:52:30 +00002152 // If we don't need to suppress any of the nodes because they are
2153 // post-dominated by a sink, simply add all the nodes in the equivalence class
2154 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002155 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002156 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002157 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002158 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002159 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002160 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002161 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002162 }
2163 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002164 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002165 }
2166
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002167 // For bug reports that should be suppressed when all paths are post-dominated
2168 // by a sink node, iterate through the reports in the equivalence class
2169 // until we find one that isn't post-dominated (if one exists). We use a
2170 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2171 // this as a recursive function, but we don't want to risk blowing out the
2172 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002173 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002174
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002175 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002176 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002177
Ted Kremenek40406fe2010-12-03 06:52:30 +00002178 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002179 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002180 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002181 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002182 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002183 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002184 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002185 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002186 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002187 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002188 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002189 continue;
2190 }
2191
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002192 // At this point we know that 'N' is not a sink and it has at least one
2193 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2194 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002195 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002196 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2197
2198 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002199 WL.push_back(errorNode);
2200 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002201
2202 while (!WL.empty()) {
2203 WLItem &WI = WL.back();
2204 assert(!WI.N->succ_empty());
2205
2206 for (; WI.I != WI.E; ++WI.I) {
2207 const ExplodedNode *Succ = *WI.I;
2208 // End-of-path node?
2209 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002210 // If we found an end-of-path node that is not a sink.
2211 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002212 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002213 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002214 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002215 WL.clear();
2216 break;
2217 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002218 // Found a sink? Continue on to the next successor.
2219 continue;
2220 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002221 // Mark the successor as visited. If it hasn't been explored,
2222 // enqueue it to the DFS worklist.
2223 unsigned &mark = Visited[Succ];
2224 if (!mark) {
2225 mark = 1;
2226 WL.push_back(Succ);
2227 break;
2228 }
2229 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002230
2231 // The worklist may have been cleared at this point. First
2232 // check if it is empty before checking the last item.
2233 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002234 WL.pop_back();
2235 }
2236 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002237
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002238 // ExampleReport will be NULL if all the nodes in the equivalence class
2239 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002240 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002241}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002242
Ted Kremenekcf118d42009-02-04 23:49:09 +00002243void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002244 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002245 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002246 if (exampleReport) {
2247 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2248 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2249 E=C.end(); I != E; ++I) {
2250 FlushReport(exampleReport, **I, bugReports);
2251 }
2252 }
2253}
2254
2255void BugReporter::FlushReport(BugReport *exampleReport,
2256 PathDiagnosticConsumer &PD,
2257 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Ted Kremenekcf118d42009-02-04 23:49:09 +00002259 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002260 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002261 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002262
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002263 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002264 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2265 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002266 exampleReport->getDescription(),
2267 exampleReport->getShortDescription(/*Fallback=*/false),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002268 BT.getCategory()));
2269
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002270 // Generate the full path diagnostic, using the generation scheme
Jordan Rosed632d6f2012-09-22 01:24:56 +00002271 // specified by the PathDiagnosticConsumer. Note that we have to generate
2272 // path diagnostics even for consumers which do not support paths, because
2273 // the BugReporterVisitors may mark this bug as a false positive.
2274 if (!bugReports.empty())
2275 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2276 return;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002277
2278 // If the path is empty, generate a single step path with the location
2279 // of the issue.
2280 if (D->path.empty()) {
2281 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2282 PathDiagnosticPiece *piece =
2283 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2284 BugReport::ranges_iterator Beg, End;
2285 llvm::tie(Beg, End) = exampleReport->getRanges();
2286 for ( ; Beg != End; ++Beg)
2287 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002288 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002289 }
2290
Ted Kremenek072192b2008-04-30 23:47:44 +00002291 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002292 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002293 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2294 e = Meta.end(); i != e; ++i) {
2295 D->addMeta(*i);
2296 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002297
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002298 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002299}
Ted Kremenek57202072008-07-14 17:40:50 +00002300
Ted Kremenek07189522012-04-04 18:11:35 +00002301void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002302 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002303 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002304 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002305 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002307 // 'BT' is owned by BugReporter.
2308 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002309 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002310 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002311 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2312 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002313}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002314
Chris Lattner5f9e2722011-07-23 10:55:15 +00002315BugType *BugReporter::getBugTypeForName(StringRef name,
2316 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002317 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002318 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2319 llvm::StringMapEntry<BugType *> &
2320 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2321 BugType *BT = entry.getValue();
2322 if (!BT) {
2323 BT = new BugType(name, category);
2324 entry.setValue(BT);
2325 }
2326 return BT;
2327}