blob: 6b94ac81a328f8c4cd84e684a50e9bb7c9d1246d [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//===----------------------------------------------------------------------===//
Jordan Rosed632d6f2012-09-22 01:24:56 +0000405// "Visitors only" path diagnostic generation algorithm.
406//===----------------------------------------------------------------------===//
407static bool GenerateVisitorsOnlyPathDiagnostic(PathDiagnostic &PD,
408 PathDiagnosticBuilder &PDB,
409 const ExplodedNode *N,
410 ArrayRef<BugReporterVisitor *> visitors) {
411 // All path generation skips the very first node (the error node).
412 // This is because there is special handling for the end-of-path note.
413 N = N->getFirstPred();
414 if (!N)
415 return true;
416
417 BugReport *R = PDB.getBugReport();
418 while (const ExplodedNode *Pred = N->getFirstPred()) {
419 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
420 E = visitors.end();
421 I != E; ++I) {
422 // Visit all the node pairs, but throw the path pieces away.
423 PathDiagnosticPiece *Piece = (*I)->VisitNode(N, Pred, PDB, *R);
424 delete Piece;
425 }
426
427 N = Pred;
428 }
429
430 return R->isValid();
431}
432
433//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000434// "Minimal" path diagnostic generation algorithm.
435//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000436typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
437typedef SmallVector<StackDiagPair, 6> StackDiagVector;
438
Anna Zaks368a0d52012-03-15 21:13:02 +0000439static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000440 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000441 // If the piece contains a special message, add it to all the call
442 // pieces on the active stack.
443 if (PathDiagnosticEventPiece *ep =
444 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000445
Anna Zaks56a938f2012-03-16 23:24:20 +0000446 if (ep->hasCallStackHint())
447 for (StackDiagVector::iterator I = CallStack.begin(),
448 E = CallStack.end(); I != E; ++I) {
449 PathDiagnosticCallPiece *CP = I->first;
450 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000451 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000452
Anna Zaks368a0d52012-03-15 21:13:02 +0000453 // The last message on the path to final bug is the most important
454 // one. Since we traverse the path backwards, do not add the message
455 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000456 if (!CP->hasCallStackMessage())
457 CP->setCallStackMessage(stackMsg);
458 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000459 }
460}
Ted Kremenek31061982009-03-31 23:00:32 +0000461
Ted Kremenek77d09442012-03-02 01:27:31 +0000462static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000463
Jordan Rose8347d3d2012-09-22 01:24:53 +0000464static bool GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
Ted Kremenek31061982009-03-31 23:00:32 +0000465 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000466 const ExplodedNode *N,
467 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000468
Ted Kremenek31061982009-03-31 23:00:32 +0000469 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000470 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000471 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000472 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000473
Anna Zaks56a938f2012-03-16 23:24:20 +0000474 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000475
Ted Kremenek31061982009-03-31 23:00:32 +0000476 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000477 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000478 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000479 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek31061982009-03-31 23:00:32 +0000481 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000482
Anna Zaks80de4872012-08-29 21:22:37 +0000483 do {
484 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
485 PathDiagnosticCallPiece *C =
486 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
487 GRBugReporter& BR = PDB.getBugReporter();
488 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
489 PD.getActivePath().push_front(C);
490 PD.pushActivePath(&C->path);
491 CallStack.push_back(StackDiagPair(C, N));
492 break;
Anna Zaks93739372012-03-14 18:58:28 +0000493 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000494
Anna Zaks80de4872012-08-29 21:22:37 +0000495 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
496 // Flush all locations, and pop the active path.
497 bool VisitedEntireCall = PD.isWithinCall();
498 PD.popActivePath();
499
500 // Either we just added a bunch of stuff to the top-level path, or
501 // we have a previous CallExitEnd. If the former, it means that the
502 // path terminated within a function call. We must then take the
503 // current contents of the active path and place it within
504 // a new PathDiagnosticCallPiece.
505 PathDiagnosticCallPiece *C;
506 if (VisitedEntireCall) {
507 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
508 } else {
509 const Decl *Caller = CE->getLocationContext()->getDecl();
510 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
511 GRBugReporter& BR = PDB.getBugReporter();
512 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
513 }
514
515 C->setCallee(*CE, SMgr);
516 if (!CallStack.empty()) {
517 assert(CallStack.back().first == C);
518 CallStack.pop_back();
519 }
520 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Anna Zaks80de4872012-08-29 21:22:37 +0000523 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
524 const CFGBlock *Src = BE->getSrc();
525 const CFGBlock *Dst = BE->getDst();
526 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anna Zaks80de4872012-08-29 21:22:37 +0000528 if (!T)
529 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anna Zaks80de4872012-08-29 21:22:37 +0000531 PathDiagnosticLocation Start =
532 PathDiagnosticLocation::createBegin(T, SMgr,
533 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Anna Zaks80de4872012-08-29 21:22:37 +0000535 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000536 default:
537 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek31061982009-03-31 23:00:32 +0000539 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000540 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000541 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek31061982009-03-31 23:00:32 +0000543 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000544 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Ted Kremenek31061982009-03-31 23:00:32 +0000546 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000547 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000548 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek31061982009-03-31 23:00:32 +0000550 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000551 << End.asLocation().getExpansionLineNumber();
552 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
553 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000554 break;
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
557 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000558 // Figure out what case arm we took.
559 std::string sbuf;
560 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Ted Kremenek9c378f72011-08-12 23:37:29 +0000562 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000563 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenek31061982009-03-31 23:00:32 +0000565 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000566 default:
567 os << "No cases match in the switch statement. "
568 "Control jumps to line "
569 << End.asLocation().getExpansionLineNumber();
570 break;
571 case Stmt::DefaultStmtClass:
572 os << "Control jumps to the 'default' case at line "
573 << End.asLocation().getExpansionLineNumber();
574 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Anna Zaks80de4872012-08-29 21:22:37 +0000576 case Stmt::CaseStmtClass: {
577 os << "Control jumps to 'case ";
578 const CaseStmt *Case = cast<CaseStmt>(S);
579 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Anna Zaks80de4872012-08-29 21:22:37 +0000581 // Determine if it is an enum.
582 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Anna Zaks80de4872012-08-29 21:22:37 +0000584 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
585 // FIXME: Maybe this should be an assertion. Are there cases
586 // were it is not an EnumConstantDecl?
587 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000588 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Anna Zaks80de4872012-08-29 21:22:37 +0000590 if (D) {
591 GetRawInt = false;
592 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000593 }
Ted Kremenek31061982009-03-31 23:00:32 +0000594 }
Anna Zaks80de4872012-08-29 21:22:37 +0000595
596 if (GetRawInt)
597 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
598
599 os << ":' at line "
600 << End.asLocation().getExpansionLineNumber();
601 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000602 }
Anna Zaks80de4872012-08-29 21:22:37 +0000603 }
604 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
605 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000606 }
607 else {
608 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000609 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000610 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
611 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremenek31061982009-03-31 23:00:32 +0000614 break;
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek31061982009-03-31 23:00:32 +0000617 case Stmt::BreakStmtClass:
618 case Stmt::ContinueStmtClass: {
619 std::string sbuf;
620 llvm::raw_string_ostream os(sbuf);
621 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000622 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
623 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000624 break;
625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Anna Zaks80de4872012-08-29 21:22:37 +0000627 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000628 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000629 case Stmt::ConditionalOperatorClass: {
630 std::string sbuf;
631 llvm::raw_string_ostream os(sbuf);
632 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Ted Kremenek31061982009-03-31 23:00:32 +0000634 if (*(Src->succ_begin()+1) == Dst)
635 os << "false";
636 else
637 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek31061982009-03-31 23:00:32 +0000641 if (const Stmt *S = End.asStmt())
642 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Anna Zaks80de4872012-08-29 21:22:37 +0000644 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
645 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000646 break;
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Anna Zaks80de4872012-08-29 21:22:37 +0000649 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000650 case Stmt::BinaryOperatorClass: {
651 if (!PDB.supportsLogicalOpControlFlow())
652 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000654 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000655 std::string sbuf;
656 llvm::raw_string_ostream os(sbuf);
657 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000658
John McCall2de56d12010-08-25 11:45:40 +0000659 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000660 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek31061982009-03-31 23:00:32 +0000662 if (*(Src->succ_begin()+1) == Dst) {
663 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000664 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000665 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000666 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
667 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
668 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000669 }
Ted Kremenek31061982009-03-31 23:00:32 +0000670 else {
671 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000672 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000673 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000674 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
675 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000676 }
Ted Kremenek31061982009-03-31 23:00:32 +0000677 }
678 else {
John McCall2de56d12010-08-25 11:45:40 +0000679 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000680 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Ted Kremenek31061982009-03-31 23:00:32 +0000682 if (*(Src->succ_begin()+1) == Dst) {
683 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000684 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000685 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000686 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
687 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000688 }
689 else {
690 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000691 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000692 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000693 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
694 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
695 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000696 }
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Ted Kremenek31061982009-03-31 23:00:32 +0000699 break;
700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
702 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000703 if (*(Src->succ_begin()) == Dst) {
704 std::string sbuf;
705 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 os << "Loop condition is true. ";
708 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek31061982009-03-31 23:00:32 +0000710 if (const Stmt *S = End.asStmt())
711 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Anna Zaks80de4872012-08-29 21:22:37 +0000713 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
714 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000715 }
716 else {
717 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 if (const Stmt *S = End.asStmt())
720 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Anna Zaks80de4872012-08-29 21:22:37 +0000722 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
723 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek31061982009-03-31 23:00:32 +0000726 break;
727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek31061982009-03-31 23:00:32 +0000729 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000730 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000731 if (*(Src->succ_begin()+1) == Dst) {
732 std::string sbuf;
733 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek31061982009-03-31 23:00:32 +0000735 os << "Loop condition is false. ";
736 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
737 if (const Stmt *S = End.asStmt())
738 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Anna Zaks80de4872012-08-29 21:22:37 +0000740 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
741 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000742 }
743 else {
744 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
745 if (const Stmt *S = End.asStmt())
746 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Anna Zaks80de4872012-08-29 21:22:37 +0000748 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
749 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Ted Kremenek31061982009-03-31 23:00:32 +0000752 break;
753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek31061982009-03-31 23:00:32 +0000755 case Stmt::IfStmtClass: {
756 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek31061982009-03-31 23:00:32 +0000758 if (const Stmt *S = End.asStmt())
759 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek31061982009-03-31 23:00:32 +0000761 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000762 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
763 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000764 else
Anna Zaks80de4872012-08-29 21:22:37 +0000765 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
766 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek31061982009-03-31 23:00:32 +0000768 break;
769 }
Anna Zaks80de4872012-08-29 21:22:37 +0000770 }
Ted Kremenek31061982009-03-31 23:00:32 +0000771 }
Anna Zaks80de4872012-08-29 21:22:37 +0000772 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000774 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000775 // Add diagnostic pieces from custom visitors.
776 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000777 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
778 E = visitors.end();
779 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000780 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000781 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000782 updateStackPiecesWithMessage(p, CallStack);
783 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000784 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000785 }
Ted Kremenek31061982009-03-31 23:00:32 +0000786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Jordan Rose8347d3d2012-09-22 01:24:53 +0000788 if (!PDB.getBugReport()->isValid())
789 return false;
790
Ted Kremenek14856d72009-04-06 23:06:54 +0000791 // After constructing the full PathDiagnostic, do a pass over it to compact
792 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000793 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Jordan Rose8347d3d2012-09-22 01:24:53 +0000794 return true;
Ted Kremenek31061982009-03-31 23:00:32 +0000795}
796
797//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000798// "Extensive" PathDiagnostic generation.
799//===----------------------------------------------------------------------===//
800
801static bool IsControlFlowExpr(const Stmt *S) {
802 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000804 if (!E)
805 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
807 E = E->IgnoreParenCasts();
808
John McCall56ca35d2011-02-17 10:25:35 +0000809 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000810 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000812 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
813 if (B->isLogicalOp())
814 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
816 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000817}
818
Ted Kremenek14856d72009-04-06 23:06:54 +0000819namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000820class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000821 bool IsDead;
822public:
823 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
824 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000825
826 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000827 bool isDead() const { return IsDead; }
828};
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000830class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000831 std::vector<ContextLocation> CLocs;
832 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000833 PathDiagnostic &PD;
834 PathDiagnosticBuilder &PDB;
835 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000837 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek14856d72009-04-06 23:06:54 +0000839 bool containsLocation(const PathDiagnosticLocation &Container,
840 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek14856d72009-04-06 23:06:54 +0000842 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek9650cf32009-05-11 21:42:34 +0000844 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
845 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000846 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000847 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000848 while (1) {
849 // Adjust the location for some expressions that are best referenced
850 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000851 switch (S->getStmtClass()) {
852 default:
853 break;
854 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000855 case Stmt::GenericSelectionExprClass:
856 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000857 firstCharOnly = true;
858 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000859 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000860 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000861 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000862 firstCharOnly = true;
863 continue;
864 case Stmt::ChooseExprClass:
865 S = cast<ChooseExpr>(S)->getCond();
866 firstCharOnly = true;
867 continue;
868 case Stmt::BinaryOperatorClass:
869 S = cast<BinaryOperator>(S)->getLHS();
870 firstCharOnly = true;
871 continue;
872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Ted Kremenek9650cf32009-05-11 21:42:34 +0000874 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Ted Kremenek9650cf32009-05-11 21:42:34 +0000877 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000878 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek9650cf32009-05-11 21:42:34 +0000881 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000882 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000883
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000884 return L;
885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Ted Kremenek14856d72009-04-06 23:06:54 +0000887 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000888 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000889 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000890 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000891 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000892 CLocs.pop_back();
893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Ted Kremenek14856d72009-04-06 23:06:54 +0000895public:
896 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
897 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Ted Kremeneka301a672009-04-22 18:16:20 +0000899 // If the PathDiagnostic already has pieces, add the enclosing statement
900 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000901 if (!PD.path.empty()) {
902 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Ted Kremenek14856d72009-04-06 23:06:54 +0000904 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000905 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000906 }
907 }
908
909 ~EdgeBuilder() {
910 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000911
Ted Kremeneka301a672009-04-22 18:16:20 +0000912 // Finally, add an initial edge from the start location of the first
913 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000914 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000915 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000916 PDB.getSourceManager());
917 if (L.isValid())
918 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000919 }
920
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000921 void flushLocations() {
922 while (!CLocs.empty())
923 popLocation();
924 PrevLoc = PathDiagnosticLocation();
925 }
926
Ted Kremenek14856d72009-04-06 23:06:54 +0000927 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000929 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek14856d72009-04-06 23:06:54 +0000931 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +0000932 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000933 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000934};
Ted Kremenek14856d72009-04-06 23:06:54 +0000935} // end anonymous namespace
936
937
938PathDiagnosticLocation
939EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
940 if (const Stmt *S = L.asStmt()) {
941 if (IsControlFlowExpr(S))
942 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
944 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000945 }
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Ted Kremenek14856d72009-04-06 23:06:54 +0000947 return L;
948}
949
950bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
951 const PathDiagnosticLocation &Containee) {
952
953 if (Container == Containee)
954 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek14856d72009-04-06 23:06:54 +0000956 if (Container.asDecl())
957 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Ted Kremenek14856d72009-04-06 23:06:54 +0000959 if (const Stmt *S = Containee.asStmt())
960 if (const Stmt *ContainerS = Container.asStmt()) {
961 while (S) {
962 if (S == ContainerS)
963 return true;
964 S = PDB.getParent(S);
965 }
966 return false;
967 }
968
969 // Less accurate: compare using source ranges.
970 SourceRange ContainerR = Container.asRange();
971 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek14856d72009-04-06 23:06:54 +0000973 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000974 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
975 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
976 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
977 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chandler Carruth64211622011-07-25 21:09:52 +0000979 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
980 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
981 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
982 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000985 assert(ContaineeBegLine <= ContaineeEndLine);
986
Ted Kremenek14856d72009-04-06 23:06:54 +0000987 return (ContainerBegLine <= ContaineeBegLine &&
988 ContainerEndLine >= ContaineeEndLine &&
989 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000990 SM.getExpansionColumnNumber(ContainerRBeg) <=
991 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000992 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000993 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000994 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000995}
996
Ted Kremenek14856d72009-04-06 23:06:54 +0000997void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
998 if (!PrevLoc.isValid()) {
999 PrevLoc = NewLoc;
1000 return;
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001003 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1004 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremeneka43df952012-09-21 00:09:11 +00001006 if (PrevLocClean.asLocation().isInvalid()) {
1007 PrevLoc = NewLoc;
1008 return;
1009 }
1010
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001011 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001012 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Ted Kremenek14856d72009-04-06 23:06:54 +00001014 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001015 if (NewLocClean.asLocation().getExpansionLoc() ==
1016 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001017 return;
1018
Ted Kremenek2042fc12012-02-24 06:00:00 +00001019 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001020 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001021}
1022
1023void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremeneka301a672009-04-22 18:16:20 +00001025 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1026 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Ted Kremenek14856d72009-04-06 23:06:54 +00001028 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1029
1030 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001031 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Ted Kremenek14856d72009-04-06 23:06:54 +00001033 // Is the top location context the same as the one for the new location?
1034 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001035 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001036 if (IsConsumedExpr(TopContextLoc) &&
1037 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001038 TopContextLoc.markDead();
1039
Ted Kremenek14856d72009-04-06 23:06:54 +00001040 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001041 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001042
1043 return;
1044 }
1045
1046 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001047 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001048 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001050 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001051 CLocs.push_back(ContextLocation(CLoc, true));
1052 return;
1053 }
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Ted Kremenek14856d72009-04-06 23:06:54 +00001056 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001057 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001058 }
1059
1060 // Context does not contain the location. Flush it.
1061 popLocation();
1062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001064 // If we reach here, there is no enclosing context. Just add the edge.
1065 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001066}
1067
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001068bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1069 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1070 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001072 return false;
1073}
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Ted Kremeneke1baed32009-05-05 23:13:38 +00001075void EdgeBuilder::addExtendedContext(const Stmt *S) {
1076 if (!S)
1077 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001078
1079 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001080 while (Parent) {
1081 if (isa<CompoundStmt>(Parent))
1082 Parent = PDB.getParent(Parent);
1083 else
1084 break;
1085 }
1086
1087 if (Parent) {
1088 switch (Parent->getStmtClass()) {
1089 case Stmt::DoStmtClass:
1090 case Stmt::ObjCAtSynchronizedStmtClass:
1091 addContext(Parent);
1092 default:
1093 break;
1094 }
1095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Ted Kremeneke1baed32009-05-05 23:13:38 +00001097 addContext(S);
1098}
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Ted Kremenek14856d72009-04-06 23:06:54 +00001100void EdgeBuilder::addContext(const Stmt *S) {
1101 if (!S)
1102 return;
1103
Ted Kremenek59950d32012-02-24 07:12:52 +00001104 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001105 addContext(L);
1106}
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Jordan Rose183ba8e2012-07-26 20:04:05 +00001108void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001109 while (!CLocs.empty()) {
1110 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1111
1112 // Is the top location context the same as the one for the new location?
1113 if (TopContextLoc == L)
1114 return;
1115
1116 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001117 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001118 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001119 }
1120
1121 // Context does not contain the location. Flush it.
1122 popLocation();
1123 }
1124
1125 CLocs.push_back(L);
1126}
1127
Ted Kremenek11abcec2012-05-02 00:31:29 +00001128// Cone-of-influence: support the reverse propagation of "interesting" symbols
1129// and values by tracing interesting calculations backwards through evaluated
1130// expressions along a path. This is probably overly complicated, but the idea
1131// is that if an expression computed an "interesting" value, the child
1132// expressions are are also likely to be "interesting" as well (which then
1133// propagates to the values they in turn compute). This reverse propagation
1134// is needed to track interesting correlations across function call boundaries,
1135// where formal arguments bind to actual arguments, etc. This is also needed
1136// because the constraint solver sometimes simplifies certain symbolic values
1137// into constants when appropriate, and this complicates reasoning about
1138// interesting values.
1139typedef llvm::DenseSet<const Expr *> InterestingExprs;
1140
1141static void reversePropagateIntererstingSymbols(BugReport &R,
1142 InterestingExprs &IE,
1143 const ProgramState *State,
1144 const Expr *Ex,
1145 const LocationContext *LCtx) {
1146 SVal V = State->getSVal(Ex, LCtx);
1147 if (!(R.isInteresting(V) || IE.count(Ex)))
1148 return;
1149
1150 switch (Ex->getStmtClass()) {
1151 default:
1152 if (!isa<CastExpr>(Ex))
1153 break;
1154 // Fall through.
1155 case Stmt::BinaryOperatorClass:
1156 case Stmt::UnaryOperatorClass: {
1157 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1158 CE = Ex->child_end();
1159 CI != CE; ++CI) {
1160 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1161 IE.insert(child);
1162 SVal ChildV = State->getSVal(child, LCtx);
1163 R.markInteresting(ChildV);
1164 }
1165 break;
1166 }
1167 }
1168 }
1169
1170 R.markInteresting(V);
1171}
1172
1173static void reversePropagateInterestingSymbols(BugReport &R,
1174 InterestingExprs &IE,
1175 const ProgramState *State,
1176 const LocationContext *CalleeCtx,
1177 const LocationContext *CallerCtx)
1178{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001179 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001180 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1181 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001182 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001183 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1184 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1185 PE = FD->param_end();
1186 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1187 for (; AI != AE && PI != PE; ++AI, ++PI) {
1188 if (const Expr *ArgE = *AI) {
1189 if (const ParmVarDecl *PD = *PI) {
1190 Loc LV = State->getLValue(PD, CalleeCtx);
1191 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1192 IE.insert(ArgE);
1193 }
1194 }
1195 }
1196 }
1197 }
1198}
1199
Jordan Rose8347d3d2012-09-22 01:24:53 +00001200static bool GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek14856d72009-04-06 23:06:54 +00001201 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001202 const ExplodedNode *N,
1203 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001204 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001205 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001206 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001207 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001208
Ted Kremenek9c378f72011-08-12 23:37:29 +00001209 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001210 while (NextNode) {
1211 N = NextNode;
1212 NextNode = GetPredecessorNode(N);
1213 ProgramPoint P = N->getLocation();
1214
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001215 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001216 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1217 if (const Expr *Ex = PS->getStmtAs<Expr>())
1218 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1219 N->getState().getPtr(), Ex,
1220 N->getLocationContext());
1221 }
1222
Anna Zaks0b3ade82012-04-20 21:59:08 +00001223 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001224 const Stmt *S = CE->getCalleeContext()->getCallSite();
1225 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001226 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1227 N->getState().getPtr(), Ex,
1228 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001229 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001230
1231 PathDiagnosticCallPiece *C =
1232 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001233 GRBugReporter& BR = PDB.getBugReporter();
1234 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001235
1236 EB.addEdge(C->callReturn, true);
1237 EB.flushLocations();
1238
1239 PD.getActivePath().push_front(C);
1240 PD.pushActivePath(&C->path);
1241 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001242 break;
1243 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001244
Ted Kremenek2042fc12012-02-24 06:00:00 +00001245 // Pop the call hierarchy if we are done walking the contents
1246 // of a function call.
1247 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001248 // Add an edge to the start of the function.
1249 const Decl *D = CE->getCalleeContext()->getDecl();
1250 PathDiagnosticLocation pos =
1251 PathDiagnosticLocation::createBegin(D, SM);
1252 EB.addEdge(pos);
1253
1254 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001255 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001256 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001257 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001258 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001259
Jordan Rose183ba8e2012-07-26 20:04:05 +00001260 // Either we just added a bunch of stuff to the top-level path, or
1261 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001262 // path terminated within a function call. We must then take the
1263 // current contents of the active path and place it within
1264 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001265 PathDiagnosticCallPiece *C;
1266 if (VisitedEntireCall) {
1267 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1268 } else {
1269 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001270 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001271 GRBugReporter& BR = PDB.getBugReporter();
1272 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001273 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001274
Jordan Rose183ba8e2012-07-26 20:04:05 +00001275 C->setCallee(*CE, SM);
1276 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001277
1278 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001279 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001280 CallStack.pop_back();
1281 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001282 break;
1283 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001284
1285 // Note that is important that we update the LocationContext
1286 // after looking at CallExits. CallExit basically adds an
1287 // edge in the *caller*, so we don't want to update the LocationContext
1288 // too soon.
1289 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001290
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001291 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001292 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1293 // Does this represent entering a call? If so, look at propagating
1294 // interesting symbols across call boundaries.
1295 if (NextNode) {
1296 const LocationContext *CallerCtx = NextNode->getLocationContext();
1297 const LocationContext *CalleeCtx = PDB.LC;
1298 if (CallerCtx != CalleeCtx) {
1299 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1300 N->getState().getPtr(),
1301 CalleeCtx, CallerCtx);
1302 }
1303 }
1304
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001305 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001306 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001307 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001308 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001310 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1311 CS = dyn_cast<CompoundStmt>(FS->getBody());
1312 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1313 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001315 PathDiagnosticEventPiece *p =
1316 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001317 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001318 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001320 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001321 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001323 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001324 PathDiagnosticLocation BL =
1325 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001326 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001327 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001328 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001329
1330 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001331 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001333 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001334 }
1335
Mike Stump1eb44332009-09-09 15:08:12 +00001336 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Jordan Rose1a7bcc42012-09-12 22:48:08 +00001337 CFGElement First = BE->getFirstElement();
1338 if (const CFGStmt *S = First.getAs<CFGStmt>()) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001339 const Stmt *stmt = S->getStmt();
1340 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001341 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001342 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001343 }
1344 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001345 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001346 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001347
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001348 break;
1349 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001350
1351
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001352 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001354 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001355 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Anna Zaks8e6431a2011-08-18 22:37:56 +00001357 // Add pieces from custom visitors.
1358 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001359 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1360 E = visitors.end();
1361 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001362 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001363 const PathDiagnosticLocation &Loc = p->getLocation();
1364 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001365 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001366 updateStackPiecesWithMessage(p, CallStack);
1367
Ted Kremenek8966bc12009-05-06 21:39:49 +00001368 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001369 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001370 }
Mike Stump1eb44332009-09-09 15:08:12 +00001371 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001372 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00001373
1374 return PDB.getBugReport()->isValid();
Ted Kremenek14856d72009-04-06 23:06:54 +00001375}
1376
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001377//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001378// Methods for BugType and subclasses.
1379//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001380BugType::~BugType() { }
1381
Ted Kremenekcf118d42009-02-04 23:49:09 +00001382void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001383
David Blaikie99ba9e32011-12-20 02:48:34 +00001384void BuiltinBug::anchor() {}
1385
Ted Kremenekcf118d42009-02-04 23:49:09 +00001386//===----------------------------------------------------------------------===//
1387// Methods for BugReport and subclasses.
1388//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001389
David Blaikie99ba9e32011-12-20 02:48:34 +00001390void BugReport::NodeResolver::anchor() {}
1391
Anna Zaks8e6431a2011-08-18 22:37:56 +00001392void BugReport::addVisitor(BugReporterVisitor* visitor) {
1393 if (!visitor)
1394 return;
1395
1396 llvm::FoldingSetNodeID ID;
1397 visitor->Profile(ID);
1398 void *InsertPos;
1399
1400 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1401 delete visitor;
1402 return;
1403 }
1404
1405 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001406 Callbacks.push_back(visitor);
1407 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001408}
1409
1410BugReport::~BugReport() {
1411 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001412 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001413 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001414 while (!interestingSymbols.empty()) {
1415 popInterestingSymbolsAndRegions();
1416 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001417}
Anna Zakse172e8b2011-08-17 23:00:25 +00001418
Ted Kremenek07189522012-04-04 18:11:35 +00001419const Decl *BugReport::getDeclWithIssue() const {
1420 if (DeclWithIssue)
1421 return DeclWithIssue;
1422
1423 const ExplodedNode *N = getErrorNode();
1424 if (!N)
1425 return 0;
1426
1427 const LocationContext *LC = N->getLocationContext();
1428 return LC->getCurrentStackFrame()->getDecl();
1429}
1430
Anna Zakse172e8b2011-08-17 23:00:25 +00001431void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1432 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001433 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001434 if (UniqueingLocation.isValid()) {
1435 UniqueingLocation.Profile(hash);
1436 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001437 Location.Profile(hash);
1438 } else {
1439 assert(ErrorNode);
1440 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1441 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001442
1443 for (SmallVectorImpl<SourceRange>::const_iterator I =
1444 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1445 const SourceRange range = *I;
1446 if (!range.isValid())
1447 continue;
1448 hash.AddInteger(range.getBegin().getRawEncoding());
1449 hash.AddInteger(range.getEnd().getRawEncoding());
1450 }
1451}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001452
Ted Kremenek76aadc32012-03-09 01:13:14 +00001453void BugReport::markInteresting(SymbolRef sym) {
1454 if (!sym)
1455 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001456
1457 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001458 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001459 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001460
1461 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001462 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001463}
1464
1465void BugReport::markInteresting(const MemRegion *R) {
1466 if (!R)
1467 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001468
1469 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001470 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001471 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001472 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001473
Ted Kremenek76aadc32012-03-09 01:13:14 +00001474 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001475 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001476}
1477
1478void BugReport::markInteresting(SVal V) {
1479 markInteresting(V.getAsRegion());
1480 markInteresting(V.getAsSymbol());
1481}
1482
Anna Zaks80de4872012-08-29 21:22:37 +00001483void BugReport::markInteresting(const LocationContext *LC) {
1484 if (!LC)
1485 return;
1486 InterestingLocationContexts.insert(LC);
1487}
1488
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001489bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001490 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1491}
1492
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001493bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001494 if (!sym)
1495 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001496 // We don't currently consider metadata symbols to be interesting
1497 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001498 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001499}
1500
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001501bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001502 if (!R)
1503 return false;
1504 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001505 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001506 if (b)
1507 return true;
1508 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001509 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001510 return false;
1511}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001512
Anna Zaks80de4872012-08-29 21:22:37 +00001513bool BugReport::isInteresting(const LocationContext *LC) {
1514 if (!LC)
1515 return false;
1516 return InterestingLocationContexts.count(LC);
1517}
1518
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001519void BugReport::lazyInitializeInterestingSets() {
1520 if (interestingSymbols.empty()) {
1521 interestingSymbols.push_back(new Symbols());
1522 interestingRegions.push_back(new Regions());
1523 }
1524}
1525
1526BugReport::Symbols &BugReport::getInterestingSymbols() {
1527 lazyInitializeInterestingSets();
1528 return *interestingSymbols.back();
1529}
1530
1531BugReport::Regions &BugReport::getInterestingRegions() {
1532 lazyInitializeInterestingSets();
1533 return *interestingRegions.back();
1534}
1535
1536void BugReport::pushInterestingSymbolsAndRegions() {
1537 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1538 interestingRegions.push_back(new Regions(getInterestingRegions()));
1539}
1540
1541void BugReport::popInterestingSymbolsAndRegions() {
1542 delete interestingSymbols.back();
1543 interestingSymbols.pop_back();
1544 delete interestingRegions.back();
1545 interestingRegions.pop_back();
1546}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001547
Ted Kremenek9c378f72011-08-12 23:37:29 +00001548const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001549 if (!ErrorNode)
1550 return 0;
1551
Tom Care212f6d32010-09-16 03:50:38 +00001552 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001553 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek9c378f72011-08-12 23:37:29 +00001555 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001556 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001557 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001558 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001559 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001560 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001561 S = GetStmt(ProgP);
1562
1563 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001564}
1565
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001566std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001567BugReport::getRanges() {
1568 // If no custom ranges, add the range of the statement corresponding to
1569 // the error node.
1570 if (Ranges.empty()) {
1571 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1572 addRange(E->getSourceRange());
1573 else
1574 return std::make_pair(ranges_iterator(), ranges_iterator());
1575 }
1576
Anna Zaks14924262011-08-24 20:31:06 +00001577 // User-specified absence of range info.
1578 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1579 return std::make_pair(ranges_iterator(), ranges_iterator());
1580
Anna Zakse172e8b2011-08-17 23:00:25 +00001581 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001582}
1583
Anna Zaks590dd8e2011-09-20 21:38:35 +00001584PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001585 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001586 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001587 "Either Location or ErrorNode should be specified but not both.");
1588
Ted Kremenek9c378f72011-08-12 23:37:29 +00001589 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001590 const LocationContext *LC = ErrorNode->getLocationContext();
1591
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001592 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001593 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001594 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001595 // For binary operators, return the location of the operator.
1596 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001597 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001598
Anna Zaks590dd8e2011-09-20 21:38:35 +00001599 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001600 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001601 } else {
1602 assert(Location.isValid());
1603 return Location;
1604 }
1605
Anna Zaks590dd8e2011-09-20 21:38:35 +00001606 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001607}
1608
Ted Kremenekcf118d42009-02-04 23:49:09 +00001609//===----------------------------------------------------------------------===//
1610// Methods for BugReporter and subclasses.
1611//===----------------------------------------------------------------------===//
1612
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001613BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001614GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001615BugReporterData::~BugReporterData() {}
1616
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001617ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001618
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001619ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001620GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1621
Anna Zaks3b030a22011-08-19 01:57:09 +00001622BugReporter::~BugReporter() {
1623 FlushReports();
1624
1625 // Free the bug reports we are tracking.
1626 typedef std::vector<BugReportEquivClass *> ContTy;
1627 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1628 I != E; ++I) {
1629 delete *I;
1630 }
1631}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001632
1633void BugReporter::FlushReports() {
1634 if (BugTypes.isEmpty())
1635 return;
1636
1637 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001638 // warnings and new BugTypes.
1639 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1640 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001641 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001642 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001643 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001644 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001645 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001646 const_cast<BugType*>(*I)->FlushReports(*this);
1647
Anna Zaksd015f4f2012-08-02 23:41:05 +00001648 // We need to flush reports in deterministic order to ensure the order
1649 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001650 typedef std::vector<BugReportEquivClass *> ContVecTy;
1651 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1652 EI != EE; ++EI){
1653 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001654 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001655 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001656
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001657 // BugReporter owns and deletes only BugTypes created implicitly through
1658 // EmitBasicReport.
1659 // FIXME: There are leaks from checkers that assume that the BugTypes they
1660 // create will be destroyed by the BugReporter.
1661 for (llvm::StringMap<BugType*>::iterator
1662 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1663 delete I->second;
1664
Ted Kremenekcf118d42009-02-04 23:49:09 +00001665 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001666 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001667}
1668
1669//===----------------------------------------------------------------------===//
1670// PathDiagnostics generation.
1671//===----------------------------------------------------------------------===//
1672
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001673static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001674 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001675MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001676 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Ted Kremenekcf118d42009-02-04 23:49:09 +00001678 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001679 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001680 // error node unless there are two or more error nodes with the same minimum
1681 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001682 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001683 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001684
1685 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001686 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1687 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Ted Kremenekcf118d42009-02-04 23:49:09 +00001689 // Create owning pointers for GTrim and NMap just to ensure that they are
1690 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001691 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1692 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Ted Kremenekcf118d42009-02-04 23:49:09 +00001694 // Find the (first) error node in the trimmed graph. We just need to consult
1695 // the node map (NMap) which maps from nodes in the original graph to nodes
1696 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001697
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001698 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001699 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001700 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001701
Ted Kremenek40406fe2010-12-03 06:52:30 +00001702 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1703 const ExplodedNode *originalNode = nodes[nodeIndex];
1704 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001705 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001706 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001707 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Ted Kremenek938332c2009-05-16 01:11:58 +00001710 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001711
1712 // Create a new (third!) graph with a single path. This is the graph
1713 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001714 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Ted Kremenek10aa5542009-03-12 23:41:59 +00001716 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001717 // to the root node, and then construct a new graph that contains only
1718 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001719 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001721 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001722 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001724 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001725 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001726 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001728 if (Visited.find(Node) != Visited.end())
1729 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001731 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001733 if (Node->pred_empty()) {
1734 Root = Node;
1735 break;
1736 }
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001738 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001739 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001740 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001741 }
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Ted Kremenek938332c2009-05-16 01:11:58 +00001743 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Ted Kremenek10aa5542009-03-12 23:41:59 +00001745 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001746 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001747 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001748 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001749 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001751 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001752 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001753 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001754 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001756 // Create the equivalent node in the new graph with the same state
1757 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001758 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001760 // Store the mapping to the original node.
1761 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1762 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001763 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001765 // Link up the new node with the previous node.
1766 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001767 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001769 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001771 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001772 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001773 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001774 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001775 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001776 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001777 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001778 }
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001780 // Find the next successor node. We choose the node that is marked
1781 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001782 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1783 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001784 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001786 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001788 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001790 if (I == Visited.end())
1791 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001793 if (!N || I->second < MinVal) {
1794 N = *SI;
1795 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001796 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001797 }
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Ted Kremenek938332c2009-05-16 01:11:58 +00001799 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001800 }
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Ted Kremenek938332c2009-05-16 01:11:58 +00001802 assert(First);
1803
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001804 return std::make_pair(std::make_pair(GNew, BM),
1805 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001806}
1807
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001808/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1809/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001810static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001811 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1812 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001814 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001815 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001817 MacroStackTy MacroStack;
1818 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Ted Kremenek77d09442012-03-02 01:27:31 +00001820 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001821 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001822
1823 PathDiagnosticPiece *piece = I->getPtr();
1824
1825 // Recursively compact calls.
1826 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1827 CompactPathDiagnostic(call->path, SM);
1828 }
1829
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001830 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001831 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001833 // Determine the instantiation location, which is the location we group
1834 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001835 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001836 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001837 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001839 if (Loc.isFileID()) {
1840 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001841 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001842 continue;
1843 }
1844
1845 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001847 // Is the PathDiagnosticPiece within the same macro group?
1848 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001849 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001850 continue;
1851 }
1852
1853 // We aren't in the same group. Are we descending into a new macro
1854 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001855 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001856
1857 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001858 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001859 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001861 // Walk the entire macro stack.
1862 while (!MacroStack.empty()) {
1863 if (InstantiationLoc == MacroStack.back().second) {
1864 MacroGroup = MacroStack.back().first;
1865 break;
1866 }
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001868 if (ParentInstantiationLoc == MacroStack.back().second) {
1869 MacroGroup = MacroStack.back().first;
1870 break;
1871 }
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001873 MacroStack.pop_back();
1874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001876 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1877 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001878 PathDiagnosticMacroPiece *NewGroup =
1879 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001880 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001881
1882 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001883 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001884 else {
1885 assert(InstantiationLoc.isFileID());
1886 Pieces.push_back(NewGroup);
1887 }
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001889 MacroGroup = NewGroup;
1890 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1891 }
1892
1893 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001894 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001895 }
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001897 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001898 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Ted Kremenek77d09442012-03-02 01:27:31 +00001900 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1901 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001902}
1903
Jordan Rose8347d3d2012-09-22 01:24:53 +00001904bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001905 PathDiagnosticConsumer &PC,
1906 ArrayRef<BugReport *> &bugReports) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001907 assert(!bugReports.empty());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001908
1909 bool HasValid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001910 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001911 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1912 E = bugReports.end(); I != E; ++I) {
Jordan Rose8347d3d2012-09-22 01:24:53 +00001913 if ((*I)->isValid()) {
1914 HasValid = true;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001915 errorNodes.push_back((*I)->getErrorNode());
Jordan Rose8347d3d2012-09-22 01:24:53 +00001916 } else {
1917 errorNodes.push_back(0);
1918 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001919 }
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Jordan Rose8347d3d2012-09-22 01:24:53 +00001921 // If all the reports have been marked invalid, we're done.
1922 if (!HasValid)
1923 return false;
1924
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001925 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001926 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001927 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001928 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001929 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Ted Kremenekcf118d42009-02-04 23:49:09 +00001931 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001932 assert(GPair.second.second < bugReports.size());
1933 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001934 assert(R && "No original report found for sliced graph.");
Jordan Rose8347d3d2012-09-22 01:24:53 +00001935 assert(R->isValid() && "Report selected from trimmed graph marked invalid.");
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001937 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1938 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001939 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001940
1941 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001942 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Anna Zaks8e6431a2011-08-18 22:37:56 +00001944 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001945 R->addVisitor(new NilReceiverBRVisitor());
1946 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001948 BugReport::VisitorList visitors;
1949 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001950
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001951 // While generating diagnostics, it's possible the visitors will decide
1952 // new symbols and regions are interesting, or add other visitors based on
1953 // the information they find. If they do, we need to regenerate the path
1954 // based on our new report configuration.
1955 do {
1956 // Get a clean copy of all the visitors.
1957 for (BugReport::visitor_iterator I = R->visitor_begin(),
1958 E = R->visitor_end(); I != E; ++I)
1959 visitors.push_back((*I)->clone());
1960
1961 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00001962 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001963 originalReportConfigToken = R->getConfigurationChangeToken();
1964
1965 // Generate the very last diagnostic piece - the piece is visible before
1966 // the trace is expanded.
Jordan Rosed632d6f2012-09-22 01:24:56 +00001967 if (PDB.getGenerationScheme() != PathDiagnosticConsumer::None) {
1968 PathDiagnosticPiece *LastPiece = 0;
1969 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1970 I != E; ++I) {
1971 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1972 assert (!LastPiece &&
1973 "There can only be one final piece in a diagnostic.");
1974 LastPiece = Piece;
1975 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001976 }
Jordan Rosed632d6f2012-09-22 01:24:56 +00001977 if (!LastPiece)
1978 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1979 if (LastPiece)
1980 PD.setEndOfPath(LastPiece);
1981 else
1982 return false;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001983 }
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001984
1985 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001986 case PathDiagnosticConsumer::Extensive:
Jordan Rose8347d3d2012-09-22 01:24:53 +00001987 if (!GenerateExtensivePathDiagnostic(PD, PDB, N, visitors)) {
1988 assert(!R->isValid() && "Failed on valid report");
1989 // Try again. We'll filter out the bad report when we trim the graph.
1990 // FIXME: It would be more efficient to use the same intermediate
1991 // trimmed graph, and just repeat the shortest-path search.
1992 return generatePathDiagnostic(PD, PC, bugReports);
1993 }
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001994 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001995 case PathDiagnosticConsumer::Minimal:
Jordan Rose8347d3d2012-09-22 01:24:53 +00001996 if (!GenerateMinimalPathDiagnostic(PD, PDB, N, visitors)) {
1997 assert(!R->isValid() && "Failed on valid report");
1998 // Try again. We'll filter out the bad report when we trim the graph.
1999 return generatePathDiagnostic(PD, PC, bugReports);
2000 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00002001 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002002 case PathDiagnosticConsumer::None:
Jordan Rosed632d6f2012-09-22 01:24:56 +00002003 if (!GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors)) {
2004 assert(!R->isValid() && "Failed on valid report");
2005 // Try again. We'll filter out the bad report when we trim the graph.
2006 return generatePathDiagnostic(PD, PC, bugReports);
2007 }
2008 break;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00002009 }
2010
2011 // Clean up the visitors we used.
2012 llvm::DeleteContainerPointers(visitors);
2013
2014 // Did anything change while generating this path?
2015 finalReportConfigToken = R->getConfigurationChangeToken();
2016 } while(finalReportConfigToken != originalReportConfigToken);
2017
Ted Kremenekc89f4b02012-02-28 23:06:21 +00002018 // Finally, prune the diagnostic path of uninteresting stuff.
Jordan Rosed632d6f2012-09-22 01:24:56 +00002019 if (!PD.path.empty() && R->shouldPrunePath()) {
Anna Zaks80de4872012-08-29 21:22:37 +00002020 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(), R);
Ted Kremeneked7948b2012-05-31 06:03:17 +00002021 assert(hasSomethingInteresting);
2022 (void) hasSomethingInteresting;
2023 }
Jordan Rose8347d3d2012-09-22 01:24:53 +00002024
2025 return true;
Ted Kremenek7dc86642009-03-31 20:22:36 +00002026}
2027
Ted Kremenekcf118d42009-02-04 23:49:09 +00002028void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00002029 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00002030}
2031
Mike Stump1eb44332009-09-09 15:08:12 +00002032void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00002033 // Compute the bug report's hash to determine its equivalence class.
2034 llvm::FoldingSetNodeID ID;
2035 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00002036
2037 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00002038 BugType& BT = R->getBugType();
2039 Register(&BT);
2040 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002041 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Ted Kremenekcf118d42009-02-04 23:49:09 +00002043 if (!EQ) {
2044 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002045 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00002046 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002047 }
2048 else
2049 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00002050}
2051
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002052
2053//===----------------------------------------------------------------------===//
2054// Emitting reports in equivalence classes.
2055//===----------------------------------------------------------------------===//
2056
2057namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002058struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002059 const ExplodedNode *N;
2060 ExplodedNode::const_succ_iterator I, E;
2061
2062 FRIEC_WLItem(const ExplodedNode *n)
2063 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2064};
2065}
2066
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002067static BugReport *
2068FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002069 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002070
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002071 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2072 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002073 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002074
Ted Kremenek40406fe2010-12-03 06:52:30 +00002075 // If we don't need to suppress any of the nodes because they are
2076 // post-dominated by a sink, simply add all the nodes in the equivalence class
2077 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002078 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002079 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002080 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002081 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002082 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002083 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002084 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002085 }
2086 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002087 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002088 }
2089
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002090 // For bug reports that should be suppressed when all paths are post-dominated
2091 // by a sink node, iterate through the reports in the equivalence class
2092 // until we find one that isn't post-dominated (if one exists). We use a
2093 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2094 // this as a recursive function, but we don't want to risk blowing out the
2095 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002096 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002097
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002098 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002099 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002100
Ted Kremenek40406fe2010-12-03 06:52:30 +00002101 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002102 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002103 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002104 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002105 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002106 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002107 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002108 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002109 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002110 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002111 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002112 continue;
2113 }
2114
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002115 // At this point we know that 'N' is not a sink and it has at least one
2116 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2117 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002118 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002119 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2120
2121 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002122 WL.push_back(errorNode);
2123 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002124
2125 while (!WL.empty()) {
2126 WLItem &WI = WL.back();
2127 assert(!WI.N->succ_empty());
2128
2129 for (; WI.I != WI.E; ++WI.I) {
2130 const ExplodedNode *Succ = *WI.I;
2131 // End-of-path node?
2132 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002133 // If we found an end-of-path node that is not a sink.
2134 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002135 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002136 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002137 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002138 WL.clear();
2139 break;
2140 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002141 // Found a sink? Continue on to the next successor.
2142 continue;
2143 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002144 // Mark the successor as visited. If it hasn't been explored,
2145 // enqueue it to the DFS worklist.
2146 unsigned &mark = Visited[Succ];
2147 if (!mark) {
2148 mark = 1;
2149 WL.push_back(Succ);
2150 break;
2151 }
2152 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002153
2154 // The worklist may have been cleared at this point. First
2155 // check if it is empty before checking the last item.
2156 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002157 WL.pop_back();
2158 }
2159 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002160
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002161 // ExampleReport will be NULL if all the nodes in the equivalence class
2162 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002163 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002164}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002165
Ted Kremenekcf118d42009-02-04 23:49:09 +00002166void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002167 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002168 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002169 if (exampleReport) {
2170 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2171 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2172 E=C.end(); I != E; ++I) {
2173 FlushReport(exampleReport, **I, bugReports);
2174 }
2175 }
2176}
2177
2178void BugReporter::FlushReport(BugReport *exampleReport,
2179 PathDiagnosticConsumer &PD,
2180 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Ted Kremenekcf118d42009-02-04 23:49:09 +00002182 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002183 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002184 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002186 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002187 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2188 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002189 exampleReport->getDescription(),
2190 exampleReport->getShortDescription(/*Fallback=*/false),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002191 BT.getCategory()));
2192
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002193 // Generate the full path diagnostic, using the generation scheme
Jordan Rosed632d6f2012-09-22 01:24:56 +00002194 // specified by the PathDiagnosticConsumer. Note that we have to generate
2195 // path diagnostics even for consumers which do not support paths, because
2196 // the BugReporterVisitors may mark this bug as a false positive.
2197 if (!bugReports.empty())
2198 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
2199 return;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002200
2201 // If the path is empty, generate a single step path with the location
2202 // of the issue.
2203 if (D->path.empty()) {
2204 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2205 PathDiagnosticPiece *piece =
2206 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2207 BugReport::ranges_iterator Beg, End;
2208 llvm::tie(Beg, End) = exampleReport->getRanges();
2209 for ( ; Beg != End; ++Beg)
2210 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002211 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002212 }
2213
Ted Kremenek072192b2008-04-30 23:47:44 +00002214 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002215 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002216 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2217 e = Meta.end(); i != e; ++i) {
2218 D->addMeta(*i);
2219 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002220
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002221 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002222}
Ted Kremenek57202072008-07-14 17:40:50 +00002223
Ted Kremenek07189522012-04-04 18:11:35 +00002224void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002225 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002226 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002227 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002228 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002230 // 'BT' is owned by BugReporter.
2231 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002232 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002233 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002234 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2235 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002236}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002237
Chris Lattner5f9e2722011-07-23 10:55:15 +00002238BugType *BugReporter::getBugTypeForName(StringRef name,
2239 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002240 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002241 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2242 llvm::StringMapEntry<BugType *> &
2243 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2244 BugType *BT = entry.getValue();
2245 if (!BT) {
2246 BT = new BugType(name, category);
2247 entry.setValue(BT);
2248 }
2249 return BT;
2250}