blob: e45d43e3131d10f551024c485242c2e14a8649c8 [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
121/// Recursively scan through a path and prune out calls and macros pieces
122/// that aren't needed. Return true if afterwards the path contains
123/// "interesting stuff" which means it should be pruned from the parent path.
Ted Kremeneka43df952012-09-21 00:09:11 +0000124bool BugReporter::RemoveUneededCalls(PathPieces &pieces, BugReport *R,
125 PathDiagnosticCallPiece *CallWithLoc) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000126 bool containsSomethingInteresting = false;
127 const unsigned N = pieces.size();
128
129 for (unsigned i = 0 ; i < N ; ++i) {
130 // Remove the front piece from the path. If it is still something we
131 // want to keep once we are done, we will push it back on the end.
132 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
133 pieces.pop_front();
134
Ted Kremeneka43df952012-09-21 00:09:11 +0000135 // Throw away pieces with invalid locations.
136 if (piece->getKind() != PathDiagnosticPiece::Call &&
137 piece->getLocation().asLocation().isInvalid())
138 continue;
139
Ted Kremenek72516742012-03-01 00:05:06 +0000140 switch (piece->getKind()) {
141 case PathDiagnosticPiece::Call: {
142 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000143 // Check if the location context is interesting.
144 assert(LocationContextMap.count(call));
145 if (R->isInteresting(LocationContextMap[call])) {
146 containsSomethingInteresting = true;
147 break;
148 }
Ted Kremenek72516742012-03-01 00:05:06 +0000149 // Recursively clean out the subclass. Keep this call around if
150 // it contains any informative diagnostics.
Ted Kremeneka43df952012-09-21 00:09:11 +0000151 PathDiagnosticCallPiece *NewCallWithLoc =
152 call->getLocation().asLocation().isValid()
153 ? call : CallWithLoc;
154
155 if (!RemoveUneededCalls(call->path, R, NewCallWithLoc))
Ted Kremenek72516742012-03-01 00:05:06 +0000156 continue;
Ted Kremeneka43df952012-09-21 00:09:11 +0000157
158 if (NewCallWithLoc == CallWithLoc && CallWithLoc) {
159 call->callEnter = CallWithLoc->callEnter;
160 }
161
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000162 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000163 break;
164 }
165 case PathDiagnosticPiece::Macro: {
166 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000167 if (!RemoveUneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000168 continue;
169 containsSomethingInteresting = true;
170 break;
171 }
172 case PathDiagnosticPiece::Event: {
173 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
Ted Kremeneka43df952012-09-21 00:09:11 +0000174
Ted Kremenek72516742012-03-01 00:05:06 +0000175 // We never throw away an event, but we do throw it away wholesale
176 // as part of a path if we throw the entire path away.
Ted Kremenek22505ef2012-09-08 07:18:18 +0000177 containsSomethingInteresting |= !event->isPrunable();
Ted Kremenek72516742012-03-01 00:05:06 +0000178 break;
179 }
180 case PathDiagnosticPiece::ControlFlow:
181 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000182 }
183
184 pieces.push_back(piece);
185 }
186
187 return containsSomethingInteresting;
188}
189
190//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000191// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000192//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000193
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000194typedef llvm::DenseMap<const ExplodedNode*,
195const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000196
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000197namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000198class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000199 NodeBackMap& M;
200public:
201 NodeMapClosure(NodeBackMap *m) : M(*m) {}
202 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek9c378f72011-08-12 23:37:29 +0000204 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000205 NodeBackMap::iterator I = M.find(N);
206 return I == M.end() ? 0 : I->second;
207 }
208};
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000210class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000211 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000212 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000213 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000214 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000215public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000216 const LocationContext *LC;
217
Ted Kremenek8966bc12009-05-06 21:39:49 +0000218 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000219 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000220 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000221 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000222 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
223 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek9c378f72011-08-12 23:37:29 +0000225 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek9c378f72011-08-12 23:37:29 +0000227 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
228 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Anna Zaks8e6431a2011-08-18 22:37:56 +0000230 BugReport *getBugReport() { return R; }
231
Tom Care212f6d32010-09-16 03:50:38 +0000232 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000233
234 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000236 const Stmt *getParent(const Stmt *S) {
237 return getParentMap().getParent(S);
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenek8966bc12009-05-06 21:39:49 +0000240 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000241
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000242 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
David Blaikieef3643f2011-09-26 00:51:36 +0000244 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
245 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000246 }
247
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000248 bool supportsLogicalOpControlFlow() const {
249 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000250 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000251};
252} // end anonymous namespace
253
Ted Kremenek00605e02009-03-27 20:55:39 +0000254PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000255PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000256 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000257 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000258
Anna Zaks0cd59482011-09-16 19:18:30 +0000259 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
260 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000261}
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenek00605e02009-03-27 20:55:39 +0000263PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000264PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
265 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000266
Ted Kremenek143ca222008-05-06 18:11:09 +0000267 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000268 if (os.str().empty())
269 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek00605e02009-03-27 20:55:39 +0000271 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Ted Kremenek00605e02009-03-27 20:55:39 +0000273 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000274 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000275 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000276 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000277 else {
278 os << "Execution jumps to the end of the ";
279 const Decl *D = N->getLocationContext()->getDecl();
280 if (isa<ObjCMethodDecl>(D))
281 os << "method";
282 else if (isa<FunctionDecl>(D))
283 os << "function";
284 else {
285 assert(isa<BlockDecl>(D));
286 os << "anonymous block";
287 }
288 os << '.';
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000291 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000292}
293
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000294static bool IsNested(const Stmt *S, ParentMap &PM) {
295 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
296 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000298 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000300 if (Parent)
301 switch (Parent->getStmtClass()) {
302 case Stmt::ForStmtClass:
303 case Stmt::DoStmtClass:
304 case Stmt::WhileStmtClass:
305 return true;
306 default:
307 break;
308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
310 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000311}
312
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000313PathDiagnosticLocation
314PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000315 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000316 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000317 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000318
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000319 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000320 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000322 if (!Parent)
323 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000325 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000326 case Stmt::BinaryOperatorClass: {
327 const BinaryOperator *B = cast<BinaryOperator>(Parent);
328 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000329 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000330 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000331 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000332 case Stmt::CompoundStmtClass:
333 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000335 case Stmt::ChooseExprClass:
336 // Similar to '?' if we are referring to condition, just have the edge
337 // point to the entire choose expression.
338 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000339 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000340 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000341 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000342 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000343 case Stmt::ConditionalOperatorClass:
344 // For '?', if we are referring to condition, just have the edge point
345 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000346 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000347 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000348 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000349 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000350 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000351 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000352 case Stmt::ForStmtClass:
353 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000354 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000355 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000356 case Stmt::IfStmtClass:
357 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000358 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000359 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000360 case Stmt::ObjCForCollectionStmtClass:
361 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000362 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000363 break;
364 case Stmt::WhileStmtClass:
365 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000366 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000367 break;
368 default:
369 break;
370 }
371
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000372 S = Parent;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000375 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000376
377 // Special case: DeclStmts can appear in for statement declarations, in which
378 // case the ForStmt is the context.
379 if (isa<DeclStmt>(S)) {
380 if (const Stmt *Parent = P.getParent(S)) {
381 switch (Parent->getStmtClass()) {
382 case Stmt::ForStmtClass:
383 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000384 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000385 default:
386 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000387 }
388 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000389 }
390 else if (isa<BinaryOperator>(S)) {
391 // Special case: the binary operator represents the initialization
392 // code in a for statement (this can happen when the variable being
393 // initialized is an old variable.
394 if (const ForStmt *FS =
395 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
396 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000397 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000398 }
399 }
400
Anna Zaks220ac8c2011-09-15 01:08:34 +0000401 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000402}
403
Ted Kremenekcf118d42009-02-04 23:49:09 +0000404//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000405// "Minimal" path diagnostic generation algorithm.
406//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000407typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
408typedef SmallVector<StackDiagPair, 6> StackDiagVector;
409
Anna Zaks368a0d52012-03-15 21:13:02 +0000410static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000411 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000412 // If the piece contains a special message, add it to all the call
413 // pieces on the active stack.
414 if (PathDiagnosticEventPiece *ep =
415 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000416
Anna Zaks56a938f2012-03-16 23:24:20 +0000417 if (ep->hasCallStackHint())
418 for (StackDiagVector::iterator I = CallStack.begin(),
419 E = CallStack.end(); I != E; ++I) {
420 PathDiagnosticCallPiece *CP = I->first;
421 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000422 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000423
Anna Zaks368a0d52012-03-15 21:13:02 +0000424 // The last message on the path to final bug is the most important
425 // one. Since we traverse the path backwards, do not add the message
426 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000427 if (!CP->hasCallStackMessage())
428 CP->setCallStackMessage(stackMsg);
429 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000430 }
431}
Ted Kremenek31061982009-03-31 23:00:32 +0000432
Ted Kremenek77d09442012-03-02 01:27:31 +0000433static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000434
Jordan Rose8347d3d2012-09-22 01:24:53 +0000435static bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
Ted Kremenek31061982009-03-31 23:00:32 +0000436 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000437 const ExplodedNode *N,
438 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000441 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000442 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000443 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000444
Anna Zaks56a938f2012-03-16 23:24:20 +0000445 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000446
Ted Kremenek31061982009-03-31 23:00:32 +0000447 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000448 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000449 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000450 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek31061982009-03-31 23:00:32 +0000452 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000453
Anna Zaks80de4872012-08-29 21:22:37 +0000454 do {
455 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
456 PathDiagnosticCallPiece *C =
457 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
458 GRBugReporter& BR = PDB.getBugReporter();
459 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
460 PD.getActivePath().push_front(C);
461 PD.pushActivePath(&C->path);
462 CallStack.push_back(StackDiagPair(C, N));
463 break;
Anna Zaks93739372012-03-14 18:58:28 +0000464 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000465
Anna Zaks80de4872012-08-29 21:22:37 +0000466 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
467 // Flush all locations, and pop the active path.
468 bool VisitedEntireCall = PD.isWithinCall();
469 PD.popActivePath();
470
471 // Either we just added a bunch of stuff to the top-level path, or
472 // we have a previous CallExitEnd. If the former, it means that the
473 // path terminated within a function call. We must then take the
474 // current contents of the active path and place it within
475 // a new PathDiagnosticCallPiece.
476 PathDiagnosticCallPiece *C;
477 if (VisitedEntireCall) {
478 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
479 } else {
480 const Decl *Caller = CE->getLocationContext()->getDecl();
481 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
482 GRBugReporter& BR = PDB.getBugReporter();
483 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
484 }
485
486 C->setCallee(*CE, SMgr);
487 if (!CallStack.empty()) {
488 assert(CallStack.back().first == C);
489 CallStack.pop_back();
490 }
491 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anna Zaks80de4872012-08-29 21:22:37 +0000494 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
495 const CFGBlock *Src = BE->getSrc();
496 const CFGBlock *Dst = BE->getDst();
497 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Anna Zaks80de4872012-08-29 21:22:37 +0000499 if (!T)
500 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Anna Zaks80de4872012-08-29 21:22:37 +0000502 PathDiagnosticLocation Start =
503 PathDiagnosticLocation::createBegin(T, SMgr,
504 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Anna Zaks80de4872012-08-29 21:22:37 +0000506 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000507 default:
508 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenek31061982009-03-31 23:00:32 +0000510 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000511 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000512 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek31061982009-03-31 23:00:32 +0000514 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000515 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek31061982009-03-31 23:00:32 +0000517 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000518 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000519 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek31061982009-03-31 23:00:32 +0000521 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000522 << End.asLocation().getExpansionLineNumber();
523 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
524 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000525 break;
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
528 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000529 // Figure out what case arm we took.
530 std::string sbuf;
531 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek9c378f72011-08-12 23:37:29 +0000533 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000534 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek31061982009-03-31 23:00:32 +0000536 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000537 default:
538 os << "No cases match in the switch statement. "
539 "Control jumps to line "
540 << End.asLocation().getExpansionLineNumber();
541 break;
542 case Stmt::DefaultStmtClass:
543 os << "Control jumps to the 'default' case at line "
544 << End.asLocation().getExpansionLineNumber();
545 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Anna Zaks80de4872012-08-29 21:22:37 +0000547 case Stmt::CaseStmtClass: {
548 os << "Control jumps to 'case ";
549 const CaseStmt *Case = cast<CaseStmt>(S);
550 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Anna Zaks80de4872012-08-29 21:22:37 +0000552 // Determine if it is an enum.
553 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Anna Zaks80de4872012-08-29 21:22:37 +0000555 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
556 // FIXME: Maybe this should be an assertion. Are there cases
557 // were it is not an EnumConstantDecl?
558 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000559 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Anna Zaks80de4872012-08-29 21:22:37 +0000561 if (D) {
562 GetRawInt = false;
563 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000564 }
Ted Kremenek31061982009-03-31 23:00:32 +0000565 }
Anna Zaks80de4872012-08-29 21:22:37 +0000566
567 if (GetRawInt)
568 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
569
570 os << ":' at line "
571 << End.asLocation().getExpansionLineNumber();
572 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000573 }
Anna Zaks80de4872012-08-29 21:22:37 +0000574 }
575 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
576 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000577 }
578 else {
579 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000580 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000581 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
582 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 break;
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek31061982009-03-31 23:00:32 +0000588 case Stmt::BreakStmtClass:
589 case Stmt::ContinueStmtClass: {
590 std::string sbuf;
591 llvm::raw_string_ostream os(sbuf);
592 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000593 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
594 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000595 break;
596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Anna Zaks80de4872012-08-29 21:22:37 +0000598 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000599 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000600 case Stmt::ConditionalOperatorClass: {
601 std::string sbuf;
602 llvm::raw_string_ostream os(sbuf);
603 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenek31061982009-03-31 23:00:32 +0000605 if (*(Src->succ_begin()+1) == Dst)
606 os << "false";
607 else
608 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Ted Kremenek31061982009-03-31 23:00:32 +0000610 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenek31061982009-03-31 23:00:32 +0000612 if (const Stmt *S = End.asStmt())
613 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anna Zaks80de4872012-08-29 21:22:37 +0000615 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
616 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000617 break;
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anna Zaks80de4872012-08-29 21:22:37 +0000620 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000621 case Stmt::BinaryOperatorClass: {
622 if (!PDB.supportsLogicalOpControlFlow())
623 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000625 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000626 std::string sbuf;
627 llvm::raw_string_ostream os(sbuf);
628 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000629
John McCall2de56d12010-08-25 11:45:40 +0000630 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000631 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek31061982009-03-31 23:00:32 +0000633 if (*(Src->succ_begin()+1) == Dst) {
634 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000635 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000636 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000637 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
638 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
639 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000640 }
Ted Kremenek31061982009-03-31 23:00:32 +0000641 else {
642 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000643 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000644 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000645 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
646 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000647 }
Ted Kremenek31061982009-03-31 23:00:32 +0000648 }
649 else {
John McCall2de56d12010-08-25 11:45:40 +0000650 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000651 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 if (*(Src->succ_begin()+1) == Dst) {
654 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000655 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000656 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000657 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
658 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000659 }
660 else {
661 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000662 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000663 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000664 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
665 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
666 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000667 }
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 break;
671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
673 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000674 if (*(Src->succ_begin()) == Dst) {
675 std::string sbuf;
676 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Ted Kremenek31061982009-03-31 23:00:32 +0000678 os << "Loop condition is true. ";
679 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Ted Kremenek31061982009-03-31 23:00:32 +0000681 if (const Stmt *S = End.asStmt())
682 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Anna Zaks80de4872012-08-29 21:22:37 +0000684 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
685 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000686 }
687 else {
688 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek31061982009-03-31 23:00:32 +0000690 if (const Stmt *S = End.asStmt())
691 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Anna Zaks80de4872012-08-29 21:22:37 +0000693 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
694 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek31061982009-03-31 23:00:32 +0000697 break;
698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Ted Kremenek31061982009-03-31 23:00:32 +0000700 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000701 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000702 if (*(Src->succ_begin()+1) == Dst) {
703 std::string sbuf;
704 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Ted Kremenek31061982009-03-31 23:00:32 +0000706 os << "Loop condition is false. ";
707 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
708 if (const Stmt *S = End.asStmt())
709 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Anna Zaks80de4872012-08-29 21:22:37 +0000711 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
712 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000713 }
714 else {
715 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
716 if (const Stmt *S = End.asStmt())
717 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Anna Zaks80de4872012-08-29 21:22:37 +0000719 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
720 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek31061982009-03-31 23:00:32 +0000723 break;
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek31061982009-03-31 23:00:32 +0000726 case Stmt::IfStmtClass: {
727 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek31061982009-03-31 23:00:32 +0000729 if (const Stmt *S = End.asStmt())
730 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek31061982009-03-31 23:00:32 +0000732 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000733 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
734 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000735 else
Anna Zaks80de4872012-08-29 21:22:37 +0000736 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
737 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek31061982009-03-31 23:00:32 +0000739 break;
740 }
Anna Zaks80de4872012-08-29 21:22:37 +0000741 }
Ted Kremenek31061982009-03-31 23:00:32 +0000742 }
Anna Zaks80de4872012-08-29 21:22:37 +0000743 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000745 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000746 // Add diagnostic pieces from custom visitors.
747 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000748 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
749 E = visitors.end();
750 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000751 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000752 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000753 updateStackPiecesWithMessage(p, CallStack);
754 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000755 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000756 }
Ted Kremenek31061982009-03-31 23:00:32 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Jordan Rose8347d3d2012-09-22 01:24:53 +0000759 if (!PDB.getBugReport()->isValid())
760 return false;
761
Ted Kremenek14856d72009-04-06 23:06:54 +0000762 // After constructing the full PathDiagnostic, do a pass over it to compact
763 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000764 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Jordan Rose8347d3d2012-09-22 01:24:53 +0000765 return true;
Ted Kremenek31061982009-03-31 23:00:32 +0000766}
767
768//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000769// "Extensive" PathDiagnostic generation.
770//===----------------------------------------------------------------------===//
771
772static bool IsControlFlowExpr(const Stmt *S) {
773 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000775 if (!E)
776 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000777
778 E = E->IgnoreParenCasts();
779
John McCall56ca35d2011-02-17 10:25:35 +0000780 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000781 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000783 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
784 if (B->isLogicalOp())
785 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000786
787 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000788}
789
Ted Kremenek14856d72009-04-06 23:06:54 +0000790namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000791class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000792 bool IsDead;
793public:
794 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
795 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000796
797 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000798 bool isDead() const { return IsDead; }
799};
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000801class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000802 std::vector<ContextLocation> CLocs;
803 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000804 PathDiagnostic &PD;
805 PathDiagnosticBuilder &PDB;
806 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000808 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenek14856d72009-04-06 23:06:54 +0000810 bool containsLocation(const PathDiagnosticLocation &Container,
811 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremenek14856d72009-04-06 23:06:54 +0000813 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek9650cf32009-05-11 21:42:34 +0000815 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
816 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000817 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000818 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000819 while (1) {
820 // Adjust the location for some expressions that are best referenced
821 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000822 switch (S->getStmtClass()) {
823 default:
824 break;
825 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000826 case Stmt::GenericSelectionExprClass:
827 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000828 firstCharOnly = true;
829 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000830 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000831 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000832 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000833 firstCharOnly = true;
834 continue;
835 case Stmt::ChooseExprClass:
836 S = cast<ChooseExpr>(S)->getCond();
837 firstCharOnly = true;
838 continue;
839 case Stmt::BinaryOperatorClass:
840 S = cast<BinaryOperator>(S)->getLHS();
841 firstCharOnly = true;
842 continue;
843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek9650cf32009-05-11 21:42:34 +0000845 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek9650cf32009-05-11 21:42:34 +0000848 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000849 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000850 }
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Ted Kremenek9650cf32009-05-11 21:42:34 +0000852 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000853 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000854
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000855 return L;
856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Ted Kremenek14856d72009-04-06 23:06:54 +0000858 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000859 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000860 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000861 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000862 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000863 CLocs.pop_back();
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Ted Kremenek14856d72009-04-06 23:06:54 +0000866public:
867 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
868 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremeneka301a672009-04-22 18:16:20 +0000870 // If the PathDiagnostic already has pieces, add the enclosing statement
871 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000872 if (!PD.path.empty()) {
873 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek14856d72009-04-06 23:06:54 +0000875 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000876 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000877 }
878 }
879
880 ~EdgeBuilder() {
881 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000882
Ted Kremeneka301a672009-04-22 18:16:20 +0000883 // Finally, add an initial edge from the start location of the first
884 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000885 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000886 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000887 PDB.getSourceManager());
888 if (L.isValid())
889 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000890 }
891
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000892 void flushLocations() {
893 while (!CLocs.empty())
894 popLocation();
895 PrevLoc = PathDiagnosticLocation();
896 }
897
Ted Kremenek14856d72009-04-06 23:06:54 +0000898 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000900 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Ted Kremenek14856d72009-04-06 23:06:54 +0000902 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +0000903 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000904 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000905};
Ted Kremenek14856d72009-04-06 23:06:54 +0000906} // end anonymous namespace
907
908
909PathDiagnosticLocation
910EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
911 if (const Stmt *S = L.asStmt()) {
912 if (IsControlFlowExpr(S))
913 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
915 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremenek14856d72009-04-06 23:06:54 +0000918 return L;
919}
920
921bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
922 const PathDiagnosticLocation &Containee) {
923
924 if (Container == Containee)
925 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenek14856d72009-04-06 23:06:54 +0000927 if (Container.asDecl())
928 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek14856d72009-04-06 23:06:54 +0000930 if (const Stmt *S = Containee.asStmt())
931 if (const Stmt *ContainerS = Container.asStmt()) {
932 while (S) {
933 if (S == ContainerS)
934 return true;
935 S = PDB.getParent(S);
936 }
937 return false;
938 }
939
940 // Less accurate: compare using source ranges.
941 SourceRange ContainerR = Container.asRange();
942 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek14856d72009-04-06 23:06:54 +0000944 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000945 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
946 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
947 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
948 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Chandler Carruth64211622011-07-25 21:09:52 +0000950 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
951 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
952 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
953 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Ted Kremenek14856d72009-04-06 23:06:54 +0000955 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000956 assert(ContaineeBegLine <= ContaineeEndLine);
957
Ted Kremenek14856d72009-04-06 23:06:54 +0000958 return (ContainerBegLine <= ContaineeBegLine &&
959 ContainerEndLine >= ContaineeEndLine &&
960 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000961 SM.getExpansionColumnNumber(ContainerRBeg) <=
962 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000963 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000964 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000965 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000966}
967
Ted Kremenek14856d72009-04-06 23:06:54 +0000968void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
969 if (!PrevLoc.isValid()) {
970 PrevLoc = NewLoc;
971 return;
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000974 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
975 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremeneka43df952012-09-21 00:09:11 +0000977 if (PrevLocClean.asLocation().isInvalid()) {
978 PrevLoc = NewLoc;
979 return;
980 }
981
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000982 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000983 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000986 if (NewLocClean.asLocation().getExpansionLoc() ==
987 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000988 return;
989
Ted Kremenek2042fc12012-02-24 06:00:00 +0000990 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000991 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000992}
993
994void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremeneka301a672009-04-22 18:16:20 +0000996 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
997 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Ted Kremenek14856d72009-04-06 23:06:54 +0000999 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1000
1001 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001002 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 // Is the top location context the same as the one for the new location?
1005 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001006 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001007 if (IsConsumedExpr(TopContextLoc) &&
1008 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001009 TopContextLoc.markDead();
1010
Ted Kremenek14856d72009-04-06 23:06:54 +00001011 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001012 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001013
1014 return;
1015 }
1016
1017 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001018 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001019 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001021 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001022 CLocs.push_back(ContextLocation(CLoc, true));
1023 return;
1024 }
1025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Ted Kremenek14856d72009-04-06 23:06:54 +00001027 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001028 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001029 }
1030
1031 // Context does not contain the location. Flush it.
1032 popLocation();
1033 }
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001035 // If we reach here, there is no enclosing context. Just add the edge.
1036 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001037}
1038
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1040 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1041 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001043 return false;
1044}
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremeneke1baed32009-05-05 23:13:38 +00001046void EdgeBuilder::addExtendedContext(const Stmt *S) {
1047 if (!S)
1048 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001049
1050 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001051 while (Parent) {
1052 if (isa<CompoundStmt>(Parent))
1053 Parent = PDB.getParent(Parent);
1054 else
1055 break;
1056 }
1057
1058 if (Parent) {
1059 switch (Parent->getStmtClass()) {
1060 case Stmt::DoStmtClass:
1061 case Stmt::ObjCAtSynchronizedStmtClass:
1062 addContext(Parent);
1063 default:
1064 break;
1065 }
1066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremeneke1baed32009-05-05 23:13:38 +00001068 addContext(S);
1069}
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremenek14856d72009-04-06 23:06:54 +00001071void EdgeBuilder::addContext(const Stmt *S) {
1072 if (!S)
1073 return;
1074
Ted Kremenek59950d32012-02-24 07:12:52 +00001075 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001076 addContext(L);
1077}
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Jordan Rose183ba8e2012-07-26 20:04:05 +00001079void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001080 while (!CLocs.empty()) {
1081 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1082
1083 // Is the top location context the same as the one for the new location?
1084 if (TopContextLoc == L)
1085 return;
1086
1087 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001088 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001089 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001090 }
1091
1092 // Context does not contain the location. Flush it.
1093 popLocation();
1094 }
1095
1096 CLocs.push_back(L);
1097}
1098
Ted Kremenek11abcec2012-05-02 00:31:29 +00001099// Cone-of-influence: support the reverse propagation of "interesting" symbols
1100// and values by tracing interesting calculations backwards through evaluated
1101// expressions along a path. This is probably overly complicated, but the idea
1102// is that if an expression computed an "interesting" value, the child
1103// expressions are are also likely to be "interesting" as well (which then
1104// propagates to the values they in turn compute). This reverse propagation
1105// is needed to track interesting correlations across function call boundaries,
1106// where formal arguments bind to actual arguments, etc. This is also needed
1107// because the constraint solver sometimes simplifies certain symbolic values
1108// into constants when appropriate, and this complicates reasoning about
1109// interesting values.
1110typedef llvm::DenseSet<const Expr *> InterestingExprs;
1111
1112static void reversePropagateIntererstingSymbols(BugReport &R,
1113 InterestingExprs &IE,
1114 const ProgramState *State,
1115 const Expr *Ex,
1116 const LocationContext *LCtx) {
1117 SVal V = State->getSVal(Ex, LCtx);
1118 if (!(R.isInteresting(V) || IE.count(Ex)))
1119 return;
1120
1121 switch (Ex->getStmtClass()) {
1122 default:
1123 if (!isa<CastExpr>(Ex))
1124 break;
1125 // Fall through.
1126 case Stmt::BinaryOperatorClass:
1127 case Stmt::UnaryOperatorClass: {
1128 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1129 CE = Ex->child_end();
1130 CI != CE; ++CI) {
1131 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1132 IE.insert(child);
1133 SVal ChildV = State->getSVal(child, LCtx);
1134 R.markInteresting(ChildV);
1135 }
1136 break;
1137 }
1138 }
1139 }
1140
1141 R.markInteresting(V);
1142}
1143
1144static void reversePropagateInterestingSymbols(BugReport &R,
1145 InterestingExprs &IE,
1146 const ProgramState *State,
1147 const LocationContext *CalleeCtx,
1148 const LocationContext *CallerCtx)
1149{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001150 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001151 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1152 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001153 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001154 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1155 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1156 PE = FD->param_end();
1157 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1158 for (; AI != AE && PI != PE; ++AI, ++PI) {
1159 if (const Expr *ArgE = *AI) {
1160 if (const ParmVarDecl *PD = *PI) {
1161 Loc LV = State->getLValue(PD, CalleeCtx);
1162 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1163 IE.insert(ArgE);
1164 }
1165 }
1166 }
1167 }
1168 }
1169}
1170
Jordan Rose8347d3d2012-09-22 01:24:53 +00001171static bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek14856d72009-04-06 23:06:54 +00001172 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001173 const ExplodedNode *N,
1174 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001175 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001176 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001177 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001178 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001179
Ted Kremenek9c378f72011-08-12 23:37:29 +00001180 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001181 while (NextNode) {
1182 N = NextNode;
1183 NextNode = GetPredecessorNode(N);
1184 ProgramPoint P = N->getLocation();
1185
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001186 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001187 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1188 if (const Expr *Ex = PS->getStmtAs<Expr>())
1189 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1190 N->getState().getPtr(), Ex,
1191 N->getLocationContext());
1192 }
1193
Anna Zaks0b3ade82012-04-20 21:59:08 +00001194 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001195 const Stmt *S = CE->getCalleeContext()->getCallSite();
1196 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001197 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1198 N->getState().getPtr(), Ex,
1199 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001200 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001201
1202 PathDiagnosticCallPiece *C =
1203 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001204 GRBugReporter& BR = PDB.getBugReporter();
1205 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001206
1207 EB.addEdge(C->callReturn, true);
1208 EB.flushLocations();
1209
1210 PD.getActivePath().push_front(C);
1211 PD.pushActivePath(&C->path);
1212 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001213 break;
1214 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001215
Ted Kremenek2042fc12012-02-24 06:00:00 +00001216 // Pop the call hierarchy if we are done walking the contents
1217 // of a function call.
1218 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001219 // Add an edge to the start of the function.
1220 const Decl *D = CE->getCalleeContext()->getDecl();
1221 PathDiagnosticLocation pos =
1222 PathDiagnosticLocation::createBegin(D, SM);
1223 EB.addEdge(pos);
1224
1225 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001226 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001227 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001228 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001229 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001230
Jordan Rose183ba8e2012-07-26 20:04:05 +00001231 // Either we just added a bunch of stuff to the top-level path, or
1232 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001233 // path terminated within a function call. We must then take the
1234 // current contents of the active path and place it within
1235 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001236 PathDiagnosticCallPiece *C;
1237 if (VisitedEntireCall) {
1238 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1239 } else {
1240 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001241 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001242 GRBugReporter& BR = PDB.getBugReporter();
1243 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001244 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001245
Jordan Rose183ba8e2012-07-26 20:04:05 +00001246 C->setCallee(*CE, SM);
1247 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001248
1249 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001250 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001251 CallStack.pop_back();
1252 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001253 break;
1254 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001255
1256 // Note that is important that we update the LocationContext
1257 // after looking at CallExits. CallExit basically adds an
1258 // edge in the *caller*, so we don't want to update the LocationContext
1259 // too soon.
1260 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001261
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001262 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001263 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1264 // Does this represent entering a call? If so, look at propagating
1265 // interesting symbols across call boundaries.
1266 if (NextNode) {
1267 const LocationContext *CallerCtx = NextNode->getLocationContext();
1268 const LocationContext *CalleeCtx = PDB.LC;
1269 if (CallerCtx != CalleeCtx) {
1270 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1271 N->getState().getPtr(),
1272 CalleeCtx, CallerCtx);
1273 }
1274 }
1275
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001276 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001277 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001278 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001279 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001281 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1282 CS = dyn_cast<CompoundStmt>(FS->getBody());
1283 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1284 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001286 PathDiagnosticEventPiece *p =
1287 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001288 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001289 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001291 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001292 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001294 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001295 PathDiagnosticLocation BL =
1296 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001297 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001298 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001299 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001300
1301 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001302 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001304 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001305 }
1306
Mike Stump1eb44332009-09-09 15:08:12 +00001307 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Jordan Rose1a7bcc42012-09-12 22:48:08 +00001308 CFGElement First = BE->getFirstElement();
1309 if (const CFGStmt *S = First.getAs<CFGStmt>()) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001310 const Stmt *stmt = S->getStmt();
1311 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001312 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001313 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001314 }
1315 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001316 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001317 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001318
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001319 break;
1320 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001321
1322
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001323 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001325 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001326 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Anna Zaks8e6431a2011-08-18 22:37:56 +00001328 // Add pieces from custom visitors.
1329 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001330 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1331 E = visitors.end();
1332 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001333 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001334 const PathDiagnosticLocation &Loc = p->getLocation();
1335 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001336 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001337 updateStackPiecesWithMessage(p, CallStack);
1338
Ted Kremenek8966bc12009-05-06 21:39:49 +00001339 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001340 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001343 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00001344
1345 return PDB.getBugReport()->isValid();
Ted Kremenek14856d72009-04-06 23:06:54 +00001346}
1347
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001348//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001349// Methods for BugType and subclasses.
1350//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001351BugType::~BugType() { }
1352
Ted Kremenekcf118d42009-02-04 23:49:09 +00001353void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001354
David Blaikie99ba9e32011-12-20 02:48:34 +00001355void BuiltinBug::anchor() {}
1356
Ted Kremenekcf118d42009-02-04 23:49:09 +00001357//===----------------------------------------------------------------------===//
1358// Methods for BugReport and subclasses.
1359//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001360
David Blaikie99ba9e32011-12-20 02:48:34 +00001361void BugReport::NodeResolver::anchor() {}
1362
Anna Zaks8e6431a2011-08-18 22:37:56 +00001363void BugReport::addVisitor(BugReporterVisitor* visitor) {
1364 if (!visitor)
1365 return;
1366
1367 llvm::FoldingSetNodeID ID;
1368 visitor->Profile(ID);
1369 void *InsertPos;
1370
1371 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1372 delete visitor;
1373 return;
1374 }
1375
1376 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001377 Callbacks.push_back(visitor);
1378 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001379}
1380
1381BugReport::~BugReport() {
1382 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001383 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001384 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001385 while (!interestingSymbols.empty()) {
1386 popInterestingSymbolsAndRegions();
1387 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001388}
Anna Zakse172e8b2011-08-17 23:00:25 +00001389
Ted Kremenek07189522012-04-04 18:11:35 +00001390const Decl *BugReport::getDeclWithIssue() const {
1391 if (DeclWithIssue)
1392 return DeclWithIssue;
1393
1394 const ExplodedNode *N = getErrorNode();
1395 if (!N)
1396 return 0;
1397
1398 const LocationContext *LC = N->getLocationContext();
1399 return LC->getCurrentStackFrame()->getDecl();
1400}
1401
Anna Zakse172e8b2011-08-17 23:00:25 +00001402void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1403 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001404 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001405 if (UniqueingLocation.isValid()) {
1406 UniqueingLocation.Profile(hash);
1407 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001408 Location.Profile(hash);
1409 } else {
1410 assert(ErrorNode);
1411 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1412 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001413
1414 for (SmallVectorImpl<SourceRange>::const_iterator I =
1415 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1416 const SourceRange range = *I;
1417 if (!range.isValid())
1418 continue;
1419 hash.AddInteger(range.getBegin().getRawEncoding());
1420 hash.AddInteger(range.getEnd().getRawEncoding());
1421 }
1422}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001423
Ted Kremenek76aadc32012-03-09 01:13:14 +00001424void BugReport::markInteresting(SymbolRef sym) {
1425 if (!sym)
1426 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001427
1428 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001429 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001430 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001431
1432 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001433 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001434}
1435
1436void BugReport::markInteresting(const MemRegion *R) {
1437 if (!R)
1438 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001439
1440 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001441 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001442 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001443 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001444
Ted Kremenek76aadc32012-03-09 01:13:14 +00001445 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001446 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001447}
1448
1449void BugReport::markInteresting(SVal V) {
1450 markInteresting(V.getAsRegion());
1451 markInteresting(V.getAsSymbol());
1452}
1453
Anna Zaks80de4872012-08-29 21:22:37 +00001454void BugReport::markInteresting(const LocationContext *LC) {
1455 if (!LC)
1456 return;
1457 InterestingLocationContexts.insert(LC);
1458}
1459
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001460bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001461 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1462}
1463
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001464bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001465 if (!sym)
1466 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001467 // We don't currently consider metadata symbols to be interesting
1468 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001469 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001470}
1471
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001472bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001473 if (!R)
1474 return false;
1475 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001476 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001477 if (b)
1478 return true;
1479 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001480 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001481 return false;
1482}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001483
Anna Zaks80de4872012-08-29 21:22:37 +00001484bool BugReport::isInteresting(const LocationContext *LC) {
1485 if (!LC)
1486 return false;
1487 return InterestingLocationContexts.count(LC);
1488}
1489
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001490void BugReport::lazyInitializeInterestingSets() {
1491 if (interestingSymbols.empty()) {
1492 interestingSymbols.push_back(new Symbols());
1493 interestingRegions.push_back(new Regions());
1494 }
1495}
1496
1497BugReport::Symbols &BugReport::getInterestingSymbols() {
1498 lazyInitializeInterestingSets();
1499 return *interestingSymbols.back();
1500}
1501
1502BugReport::Regions &BugReport::getInterestingRegions() {
1503 lazyInitializeInterestingSets();
1504 return *interestingRegions.back();
1505}
1506
1507void BugReport::pushInterestingSymbolsAndRegions() {
1508 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1509 interestingRegions.push_back(new Regions(getInterestingRegions()));
1510}
1511
1512void BugReport::popInterestingSymbolsAndRegions() {
1513 delete interestingSymbols.back();
1514 interestingSymbols.pop_back();
1515 delete interestingRegions.back();
1516 interestingRegions.pop_back();
1517}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001518
Ted Kremenek9c378f72011-08-12 23:37:29 +00001519const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001520 if (!ErrorNode)
1521 return 0;
1522
Tom Care212f6d32010-09-16 03:50:38 +00001523 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001524 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Ted Kremenek9c378f72011-08-12 23:37:29 +00001526 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001527 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001528 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001529 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001530 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001531 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001532 S = GetStmt(ProgP);
1533
1534 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001535}
1536
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001537std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001538BugReport::getRanges() {
1539 // If no custom ranges, add the range of the statement corresponding to
1540 // the error node.
1541 if (Ranges.empty()) {
1542 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1543 addRange(E->getSourceRange());
1544 else
1545 return std::make_pair(ranges_iterator(), ranges_iterator());
1546 }
1547
Anna Zaks14924262011-08-24 20:31:06 +00001548 // User-specified absence of range info.
1549 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1550 return std::make_pair(ranges_iterator(), ranges_iterator());
1551
Anna Zakse172e8b2011-08-17 23:00:25 +00001552 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001553}
1554
Anna Zaks590dd8e2011-09-20 21:38:35 +00001555PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001556 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001557 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001558 "Either Location or ErrorNode should be specified but not both.");
1559
Ted Kremenek9c378f72011-08-12 23:37:29 +00001560 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001561 const LocationContext *LC = ErrorNode->getLocationContext();
1562
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001563 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001564 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001565 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001566 // For binary operators, return the location of the operator.
1567 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001568 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001569
Anna Zaks590dd8e2011-09-20 21:38:35 +00001570 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001571 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001572 } else {
1573 assert(Location.isValid());
1574 return Location;
1575 }
1576
Anna Zaks590dd8e2011-09-20 21:38:35 +00001577 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001578}
1579
Ted Kremenekcf118d42009-02-04 23:49:09 +00001580//===----------------------------------------------------------------------===//
1581// Methods for BugReporter and subclasses.
1582//===----------------------------------------------------------------------===//
1583
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001584BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001585GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001586BugReporterData::~BugReporterData() {}
1587
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001588ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001589
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001590ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001591GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1592
Anna Zaks3b030a22011-08-19 01:57:09 +00001593BugReporter::~BugReporter() {
1594 FlushReports();
1595
1596 // Free the bug reports we are tracking.
1597 typedef std::vector<BugReportEquivClass *> ContTy;
1598 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1599 I != E; ++I) {
1600 delete *I;
1601 }
1602}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001603
1604void BugReporter::FlushReports() {
1605 if (BugTypes.isEmpty())
1606 return;
1607
1608 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001609 // warnings and new BugTypes.
1610 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1611 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001612 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001613 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001614 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001615 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001616 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001617 const_cast<BugType*>(*I)->FlushReports(*this);
1618
Anna Zaksd015f4f2012-08-02 23:41:05 +00001619 // We need to flush reports in deterministic order to ensure the order
1620 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001621 typedef std::vector<BugReportEquivClass *> ContVecTy;
1622 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1623 EI != EE; ++EI){
1624 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001625 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001626 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001627
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001628 // BugReporter owns and deletes only BugTypes created implicitly through
1629 // EmitBasicReport.
1630 // FIXME: There are leaks from checkers that assume that the BugTypes they
1631 // create will be destroyed by the BugReporter.
1632 for (llvm::StringMap<BugType*>::iterator
1633 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1634 delete I->second;
1635
Ted Kremenekcf118d42009-02-04 23:49:09 +00001636 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001637 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001638}
1639
1640//===----------------------------------------------------------------------===//
1641// PathDiagnostics generation.
1642//===----------------------------------------------------------------------===//
1643
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001644static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001645 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001646MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001647 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Ted Kremenekcf118d42009-02-04 23:49:09 +00001649 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001650 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001651 // error node unless there are two or more error nodes with the same minimum
1652 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001653 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001654 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001655
1656 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001657 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1658 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Ted Kremenekcf118d42009-02-04 23:49:09 +00001660 // Create owning pointers for GTrim and NMap just to ensure that they are
1661 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001662 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1663 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Ted Kremenekcf118d42009-02-04 23:49:09 +00001665 // Find the (first) error node in the trimmed graph. We just need to consult
1666 // the node map (NMap) which maps from nodes in the original graph to nodes
1667 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001668
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001669 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001670 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001671 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001672
Ted Kremenek40406fe2010-12-03 06:52:30 +00001673 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1674 const ExplodedNode *originalNode = nodes[nodeIndex];
1675 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001676 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001677 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001678 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001679 }
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Ted Kremenek938332c2009-05-16 01:11:58 +00001681 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001682
1683 // Create a new (third!) graph with a single path. This is the graph
1684 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001685 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Ted Kremenek10aa5542009-03-12 23:41:59 +00001687 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001688 // to the root node, and then construct a new graph that contains only
1689 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001690 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001692 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001693 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001695 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001696 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001697 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001699 if (Visited.find(Node) != Visited.end())
1700 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001702 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001704 if (Node->pred_empty()) {
1705 Root = Node;
1706 break;
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001709 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001710 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001711 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001712 }
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Ted Kremenek938332c2009-05-16 01:11:58 +00001714 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Ted Kremenek10aa5542009-03-12 23:41:59 +00001716 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001717 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001718 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001719 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001720 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001722 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001723 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001724 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001725 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001727 // Create the equivalent node in the new graph with the same state
1728 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001729 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001731 // Store the mapping to the original node.
1732 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1733 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001734 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001736 // Link up the new node with the previous node.
1737 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001738 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001740 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001742 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001743 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001744 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001745 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001746 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001747 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001748 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001749 }
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001751 // Find the next successor node. We choose the node that is marked
1752 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001753 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1754 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001755 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001757 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001759 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001761 if (I == Visited.end())
1762 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001764 if (!N || I->second < MinVal) {
1765 N = *SI;
1766 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001767 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001768 }
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Ted Kremenek938332c2009-05-16 01:11:58 +00001770 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001771 }
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Ted Kremenek938332c2009-05-16 01:11:58 +00001773 assert(First);
1774
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001775 return std::make_pair(std::make_pair(GNew, BM),
1776 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001777}
1778
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001779/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1780/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001781static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001782 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1783 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001785 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001786 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001788 MacroStackTy MacroStack;
1789 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Ted Kremenek77d09442012-03-02 01:27:31 +00001791 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001792 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001793
1794 PathDiagnosticPiece *piece = I->getPtr();
1795
1796 // Recursively compact calls.
1797 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1798 CompactPathDiagnostic(call->path, SM);
1799 }
1800
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001801 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001802 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001804 // Determine the instantiation location, which is the location we group
1805 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001806 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001807 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001808 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001810 if (Loc.isFileID()) {
1811 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001812 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001813 continue;
1814 }
1815
1816 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001818 // Is the PathDiagnosticPiece within the same macro group?
1819 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001820 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001821 continue;
1822 }
1823
1824 // We aren't in the same group. Are we descending into a new macro
1825 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001826 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001827
1828 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001829 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001830 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001832 // Walk the entire macro stack.
1833 while (!MacroStack.empty()) {
1834 if (InstantiationLoc == MacroStack.back().second) {
1835 MacroGroup = MacroStack.back().first;
1836 break;
1837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001839 if (ParentInstantiationLoc == MacroStack.back().second) {
1840 MacroGroup = MacroStack.back().first;
1841 break;
1842 }
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001844 MacroStack.pop_back();
1845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001847 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1848 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001849 PathDiagnosticMacroPiece *NewGroup =
1850 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001851 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001852
1853 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001854 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001855 else {
1856 assert(InstantiationLoc.isFileID());
1857 Pieces.push_back(NewGroup);
1858 }
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001860 MacroGroup = NewGroup;
1861 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1862 }
1863
1864 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001865 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001866 }
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001868 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001869 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Ted Kremenek77d09442012-03-02 01:27:31 +00001871 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1872 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001873}
1874
Jordan Rose8347d3d2012-09-22 01:24:53 +00001875bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001876 PathDiagnosticConsumer &PC,
1877 ArrayRef<BugReport *> &bugReports) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001878 assert(!bugReports.empty());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001879
1880 bool HasValid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001881 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001882 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1883 E = bugReports.end(); I != E; ++I) {
Jordan Rose8347d3d2012-09-22 01:24:53 +00001884 if ((*I)->isValid()) {
1885 HasValid = true;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001886 errorNodes.push_back((*I)->getErrorNode());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001887 } else {
1888 errorNodes.push_back(0);
1889 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001890 }
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Jordan Rose8347d3d2012-09-22 01:24:53 +00001892 // If all the reports have been marked invalid, we're done.
1893 if (!HasValid)
1894 return false;
1895
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001896 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001897 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001898 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001899 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001900 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Ted Kremenekcf118d42009-02-04 23:49:09 +00001902 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001903 assert(GPair.second.second < bugReports.size());
1904 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001905 assert(R && "No original report found for sliced graph.");
Jordan Rose8347d3d2012-09-22 01:24:53 +00001906 assert(R->isValid() && "Report selected from trimmed graph marked invalid.");
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001908 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1909 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001910 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001911
1912 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001913 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Anna Zaks8e6431a2011-08-18 22:37:56 +00001915 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001916 R->addVisitor(new NilReceiverBRVisitor());
1917 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001919 BugReport::VisitorList visitors;
1920 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001921
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001922 // While generating diagnostics, it's possible the visitors will decide
1923 // new symbols and regions are interesting, or add other visitors based on
1924 // the information they find. If they do, we need to regenerate the path
1925 // based on our new report configuration.
1926 do {
1927 // Get a clean copy of all the visitors.
1928 for (BugReport::visitor_iterator I = R->visitor_begin(),
1929 E = R->visitor_end(); I != E; ++I)
1930 visitors.push_back((*I)->clone());
1931
1932 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00001933 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001934 originalReportConfigToken = R->getConfigurationChangeToken();
1935
1936 // Generate the very last diagnostic piece - the piece is visible before
1937 // the trace is expanded.
1938 PathDiagnosticPiece *LastPiece = 0;
1939 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1940 I != E; ++I) {
1941 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1942 assert (!LastPiece &&
1943 "There can only be one final piece in a diagnostic.");
1944 LastPiece = Piece;
1945 }
1946 }
1947 if (!LastPiece)
1948 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1949 if (LastPiece)
Jordan Rose3a46f5f2012-08-31 00:36:26 +00001950 PD.setEndOfPath(LastPiece);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001951 else
Jordan Rose8347d3d2012-09-22 01:24:53 +00001952 return false;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001953
1954 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001955 case PathDiagnosticConsumer::Extensive:
Jordan Rose8347d3d2012-09-22 01:24:53 +00001956 if (!GenerateExtensivePathDiagnostic(PD, PDB, N, visitors)) {
1957 assert(!R->isValid() && "Failed on valid report");
1958 // Try again. We'll filter out the bad report when we trim the graph.
1959 // FIXME: It would be more efficient to use the same intermediate
1960 // trimmed graph, and just repeat the shortest-path search.
1961 return generatePathDiagnostic(PD, PC, bugReports);
1962 }
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001963 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001964 case PathDiagnosticConsumer::Minimal:
Jordan Rose8347d3d2012-09-22 01:24:53 +00001965 if (!GenerateMinimalPathDiagnostic(PD, PDB, N, visitors)) {
1966 assert(!R->isValid() && "Failed on valid report");
1967 // Try again. We'll filter out the bad report when we trim the graph.
1968 return generatePathDiagnostic(PD, PC, bugReports);
1969 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001970 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001971 case PathDiagnosticConsumer::None:
1972 llvm_unreachable("PathDiagnosticConsumer::None should never appear here");
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001973 }
1974
1975 // Clean up the visitors we used.
1976 llvm::DeleteContainerPointers(visitors);
1977
1978 // Did anything change while generating this path?
1979 finalReportConfigToken = R->getConfigurationChangeToken();
1980 } while(finalReportConfigToken != originalReportConfigToken);
1981
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001982 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremeneked7948b2012-05-31 06:03:17 +00001983 if (R->shouldPrunePath()) {
Anna Zaks80de4872012-08-29 21:22:37 +00001984 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(), R);
Ted Kremeneked7948b2012-05-31 06:03:17 +00001985 assert(hasSomethingInteresting);
1986 (void) hasSomethingInteresting;
1987 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00001988
1989 return true;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001990}
1991
Ted Kremenekcf118d42009-02-04 23:49:09 +00001992void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001993 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001994}
1995
Mike Stump1eb44332009-09-09 15:08:12 +00001996void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001997 // Compute the bug report's hash to determine its equivalence class.
1998 llvm::FoldingSetNodeID ID;
1999 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002000
2001 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00002002 BugType& BT = R->getBugType();
2003 Register(&BT);
2004 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002005 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Ted Kremenekcf118d42009-02-04 23:49:09 +00002007 if (!EQ) {
2008 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002009 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00002010 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002011 }
2012 else
2013 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00002014}
2015
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002016
2017//===----------------------------------------------------------------------===//
2018// Emitting reports in equivalence classes.
2019//===----------------------------------------------------------------------===//
2020
2021namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002022struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002023 const ExplodedNode *N;
2024 ExplodedNode::const_succ_iterator I, E;
2025
2026 FRIEC_WLItem(const ExplodedNode *n)
2027 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2028};
2029}
2030
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002031static BugReport *
2032FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002033 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002034
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002035 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2036 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002037 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002038
Ted Kremenek40406fe2010-12-03 06:52:30 +00002039 // If we don't need to suppress any of the nodes because they are
2040 // post-dominated by a sink, simply add all the nodes in the equivalence class
2041 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002042 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002043 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002044 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002045 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002046 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002047 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002048 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002049 }
2050 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002051 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002052 }
2053
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002054 // For bug reports that should be suppressed when all paths are post-dominated
2055 // by a sink node, iterate through the reports in the equivalence class
2056 // until we find one that isn't post-dominated (if one exists). We use a
2057 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2058 // this as a recursive function, but we don't want to risk blowing out the
2059 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002060 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002061
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002062 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002063 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002064
Ted Kremenek40406fe2010-12-03 06:52:30 +00002065 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002066 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002067 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002068 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002069 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002070 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002071 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002072 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002073 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002074 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002075 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002076 continue;
2077 }
2078
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002079 // At this point we know that 'N' is not a sink and it has at least one
2080 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2081 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002082 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002083 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2084
2085 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002086 WL.push_back(errorNode);
2087 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002088
2089 while (!WL.empty()) {
2090 WLItem &WI = WL.back();
2091 assert(!WI.N->succ_empty());
2092
2093 for (; WI.I != WI.E; ++WI.I) {
2094 const ExplodedNode *Succ = *WI.I;
2095 // End-of-path node?
2096 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002097 // If we found an end-of-path node that is not a sink.
2098 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002099 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002100 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002101 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002102 WL.clear();
2103 break;
2104 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002105 // Found a sink? Continue on to the next successor.
2106 continue;
2107 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002108 // Mark the successor as visited. If it hasn't been explored,
2109 // enqueue it to the DFS worklist.
2110 unsigned &mark = Visited[Succ];
2111 if (!mark) {
2112 mark = 1;
2113 WL.push_back(Succ);
2114 break;
2115 }
2116 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002117
2118 // The worklist may have been cleared at this point. First
2119 // check if it is empty before checking the last item.
2120 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002121 WL.pop_back();
2122 }
2123 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002124
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002125 // ExampleReport will be NULL if all the nodes in the equivalence class
2126 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002127 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002128}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002129
Ted Kremenekcf118d42009-02-04 23:49:09 +00002130void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002131 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002132 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002133 if (exampleReport) {
2134 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2135 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2136 E=C.end(); I != E; ++I) {
2137 FlushReport(exampleReport, **I, bugReports);
2138 }
2139 }
2140}
2141
2142void BugReporter::FlushReport(BugReport *exampleReport,
2143 PathDiagnosticConsumer &PD,
2144 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Ted Kremenekcf118d42009-02-04 23:49:09 +00002146 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002147 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002148 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002150 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002151 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2152 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002153 exampleReport->getDescription(),
2154 exampleReport->getShortDescription(/*Fallback=*/false),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002155 BT.getCategory()));
2156
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002157 // Generate the full path diagnostic, using the generation scheme
2158 // specified by the PathDiagnosticConsumer.
2159 if (PD.getGenerationScheme() != PathDiagnosticConsumer::None) {
2160 if (!bugReports.empty())
Jordan Rose8347d3d2012-09-22 01:24:53 +00002161 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2162 return;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002163 }
2164
2165 // If the path is empty, generate a single step path with the location
2166 // of the issue.
2167 if (D->path.empty()) {
2168 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2169 PathDiagnosticPiece *piece =
2170 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2171 BugReport::ranges_iterator Beg, End;
2172 llvm::tie(Beg, End) = exampleReport->getRanges();
2173 for ( ; Beg != End; ++Beg)
2174 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002175 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002176 }
2177
Ted Kremenek072192b2008-04-30 23:47:44 +00002178 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002179 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002180 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2181 e = Meta.end(); i != e; ++i) {
2182 D->addMeta(*i);
2183 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002184
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002185 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002186}
Ted Kremenek57202072008-07-14 17:40:50 +00002187
Ted Kremenek07189522012-04-04 18:11:35 +00002188void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002189 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002190 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002191 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002192 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002193
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002194 // 'BT' is owned by BugReporter.
2195 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002196 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002197 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002198 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2199 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002200}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002201
Chris Lattner5f9e2722011-07-23 10:55:15 +00002202BugType *BugReporter::getBugTypeForName(StringRef name,
2203 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002204 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002205 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2206 llvm::StringMapEntry<BugType *> &
2207 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2208 BugType *BT = entry.getValue();
2209 if (!BT) {
2210 BT = new BugType(name, category);
2211 entry.setValue(BT);
2212 }
2213 return BT;
2214}