blob: 84535d5b1e0d555bfce3cd0719eefd66abd65d25 [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"
Ted Kremenek61f3e052008-04-03 04:42:52 +000020#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000021#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
23#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000024#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000025#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000027#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000028#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000029#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000030#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000031
32using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000033using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
Ted Kremenek8966bc12009-05-06 21:39:49 +000035BugReporterVisitor::~BugReporterVisitor() {}
36BugReporterContext::~BugReporterContext() {
37 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I)
38 if ((*I)->isOwnedByReporterContext()) delete *I;
39}
40
Ted Kremenek1b431022010-03-20 18:01:57 +000041void BugReporterContext::addVisitor(BugReporterVisitor* visitor) {
42 if (!visitor)
43 return;
44
45 llvm::FoldingSetNodeID ID;
46 visitor->Profile(ID);
47 void *InsertPos;
48
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000049 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
50 delete visitor;
Ted Kremenek1b431022010-03-20 18:01:57 +000051 return;
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000052 }
Ted Kremenek1b431022010-03-20 18:01:57 +000053
54 CallbacksSet.InsertNode(visitor, InsertPos);
Ted Kremenek3baf6722010-11-24 00:54:37 +000055 Callbacks = F.add(visitor, Callbacks);
Ted Kremenek1b431022010-03-20 18:01:57 +000056}
57
Ted Kremenekcf118d42009-02-04 23:49:09 +000058//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000059// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000060//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000061
Zhongxing Xu7f330852010-04-06 03:01:56 +000062static inline const Stmt* GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000063 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
64 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000065 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000066 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenekb697b102009-02-23 22:44:26 +000068 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000069}
70
Zhongxing Xuc5619d92009-08-06 01:32:16 +000071static inline const ExplodedNode*
72GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000073 return N->pred_empty() ? NULL : *(N->pred_begin());
74}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000075
Zhongxing Xuc5619d92009-08-06 01:32:16 +000076static inline const ExplodedNode*
77GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000079}
80
Zhongxing Xuc5619d92009-08-06 01:32:16 +000081static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000082 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000083 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000084 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000085
Ted Kremenekb697b102009-02-23 22:44:26 +000086 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000087}
88
Zhongxing Xuc5619d92009-08-06 01:32:16 +000089static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000090 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000091 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000092 // Check if the statement is '?' or '&&'/'||'. These are "merges",
93 // not actual statement points.
94 switch (S->getStmtClass()) {
95 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000096 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000097 case Stmt::ConditionalOperatorClass: continue;
98 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000099 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
100 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000101 continue;
102 break;
103 }
104 default:
105 break;
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenekb7c51522009-07-28 00:07:15 +0000108 // Some expressions don't have locations.
109 if (S->getLocStart().isInvalid())
110 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenekb697b102009-02-23 22:44:26 +0000112 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenekb697b102009-02-23 22:44:26 +0000115 return 0;
116}
117
Ted Kremenek5f85e172009-07-22 22:35:28 +0000118static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000119GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000120 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000121 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremenekb697b102009-02-23 22:44:26 +0000123 return GetPreviousStmt(N);
124}
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenek5f85e172009-07-22 22:35:28 +0000126static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000127GetCurrentOrNextStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000128 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000129 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenekb697b102009-02-23 22:44:26 +0000131 return GetNextStmt(N);
132}
133
134//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000135// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000136//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000137
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000138typedef llvm::DenseMap<const ExplodedNode*,
139const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000140
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000141namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000142class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000143 NodeBackMap& M;
144public:
145 NodeMapClosure(NodeBackMap *m) : M(*m) {}
146 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000148 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000149 NodeBackMap::iterator I = M.find(N);
150 return I == M.end() ? 0 : I->second;
151 }
152};
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000154class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000155 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000156 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000157 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000158 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000159public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000160 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000161 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000162 PathDiagnosticClient *pdc)
163 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000164 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000165 addVisitor(R);
166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000168 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremenek00605e02009-03-27 20:55:39 +0000170 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000171 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Tom Care212f6d32010-09-16 03:50:38 +0000173 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000174
Tom Care212f6d32010-09-16 03:50:38 +0000175 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000177 const Stmt *getParent(const Stmt *S) {
178 return getParentMap().getParent(S);
179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek8966bc12009-05-06 21:39:49 +0000181 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000182
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000183 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenek7dc86642009-03-31 20:22:36 +0000185 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
186 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
187 }
188
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000189 bool supportsLogicalOpControlFlow() const {
190 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000191 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000192};
193} // end anonymous namespace
194
Ted Kremenek00605e02009-03-27 20:55:39 +0000195PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000196PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000197 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000198 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000199
Mike Stump1eb44332009-09-09 15:08:12 +0000200 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000201 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000202}
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek00605e02009-03-27 20:55:39 +0000204PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000205PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000206 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000207
Ted Kremenek143ca222008-05-06 18:11:09 +0000208 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000209 if (os.str().empty())
210 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek00605e02009-03-27 20:55:39 +0000212 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek00605e02009-03-27 20:55:39 +0000214 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000215 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000216 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000217 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000218 else {
219 os << "Execution jumps to the end of the ";
220 const Decl *D = N->getLocationContext()->getDecl();
221 if (isa<ObjCMethodDecl>(D))
222 os << "method";
223 else if (isa<FunctionDecl>(D))
224 os << "function";
225 else {
226 assert(isa<BlockDecl>(D));
227 os << "anonymous block";
228 }
229 os << '.';
230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000232 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000233}
234
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000235static bool IsNested(const Stmt *S, ParentMap &PM) {
236 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
237 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000239 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000241 if (Parent)
242 switch (Parent->getStmtClass()) {
243 case Stmt::ForStmtClass:
244 case Stmt::DoStmtClass:
245 case Stmt::WhileStmtClass:
246 return true;
247 default:
248 break;
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
251 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000252}
253
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000254PathDiagnosticLocation
255PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
256 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000257 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000258 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000259
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000260 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000261 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000263 if (!Parent)
264 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000266 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000267 case Stmt::BinaryOperatorClass: {
268 const BinaryOperator *B = cast<BinaryOperator>(Parent);
269 if (B->isLogicalOp())
270 return PathDiagnosticLocation(S, SMgr);
271 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000272 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000273 case Stmt::CompoundStmtClass:
274 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000275 return PathDiagnosticLocation(S, SMgr);
276 case Stmt::ChooseExprClass:
277 // Similar to '?' if we are referring to condition, just have the edge
278 // point to the entire choose expression.
279 if (cast<ChooseExpr>(Parent)->getCond() == S)
280 return PathDiagnosticLocation(Parent, SMgr);
281 else
Mike Stump1eb44332009-09-09 15:08:12 +0000282 return PathDiagnosticLocation(S, SMgr);
John McCall56ca35d2011-02-17 10:25:35 +0000283 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000284 case Stmt::ConditionalOperatorClass:
285 // For '?', if we are referring to condition, just have the edge point
286 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000287 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000288 return PathDiagnosticLocation(Parent, SMgr);
289 else
Mike Stump1eb44332009-09-09 15:08:12 +0000290 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000291 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000292 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000293 case Stmt::ForStmtClass:
294 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000295 return PathDiagnosticLocation(S, SMgr);
296 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000297 case Stmt::IfStmtClass:
298 if (cast<IfStmt>(Parent)->getCond() != S)
299 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000300 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000301 case Stmt::ObjCForCollectionStmtClass:
302 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
303 return PathDiagnosticLocation(S, SMgr);
304 break;
305 case Stmt::WhileStmtClass:
306 if (cast<WhileStmt>(Parent)->getCond() != S)
307 return PathDiagnosticLocation(S, SMgr);
308 break;
309 default:
310 break;
311 }
312
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000313 S = Parent;
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000316 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000317
318 // Special case: DeclStmts can appear in for statement declarations, in which
319 // case the ForStmt is the context.
320 if (isa<DeclStmt>(S)) {
321 if (const Stmt *Parent = P.getParent(S)) {
322 switch (Parent->getStmtClass()) {
323 case Stmt::ForStmtClass:
324 case Stmt::ObjCForCollectionStmtClass:
325 return PathDiagnosticLocation(Parent, SMgr);
326 default:
327 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000328 }
329 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000330 }
331 else if (isa<BinaryOperator>(S)) {
332 // Special case: the binary operator represents the initialization
333 // code in a for statement (this can happen when the variable being
334 // initialized is an old variable.
335 if (const ForStmt *FS =
336 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
337 if (FS->getInit() == S)
338 return PathDiagnosticLocation(FS, SMgr);
339 }
340 }
341
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000342 return PathDiagnosticLocation(S, SMgr);
343}
344
Ted Kremenekcf118d42009-02-04 23:49:09 +0000345//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000346// ScanNotableSymbols: closure-like callback for scanning Store bindings.
347//===----------------------------------------------------------------------===//
348
349static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000350GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000351 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek31061982009-03-31 23:00:32 +0000353 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek31061982009-03-31 23:00:32 +0000355 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek31061982009-03-31 23:00:32 +0000357 if (!isa<PostStmt>(P))
358 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek5f85e172009-07-22 22:35:28 +0000360 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek31061982009-03-31 23:00:32 +0000362 if (!DR)
363 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek13976632010-02-08 16:18:51 +0000365 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek31061982009-03-31 23:00:32 +0000367 if (X != Y)
368 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek5f85e172009-07-22 22:35:28 +0000370 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek31061982009-03-31 23:00:32 +0000372 if (!VD)
373 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek31061982009-03-31 23:00:32 +0000375 return VD;
376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek31061982009-03-31 23:00:32 +0000378 return 0;
379}
380
381namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000382class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000383: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek31061982009-03-31 23:00:32 +0000385 SymbolRef Sym;
386 const GRState* PrevSt;
387 const Stmt* S;
388 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000389 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000390 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000391 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek31061982009-03-31 23:00:32 +0000393public:
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek31061982009-03-31 23:00:32 +0000395 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000396 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000397 PathDiagnostic& pd, BugReporter& br)
398 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek31061982009-03-31 23:00:32 +0000400 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
401 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek31061982009-03-31 23:00:32 +0000405 if (ScanSym != Sym)
406 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
408 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000409 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek31061982009-03-31 23:00:32 +0000411 if (X == V) // Same binding?
412 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremenek31061982009-03-31 23:00:32 +0000414 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000415 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000416 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek31061982009-03-31 23:00:32 +0000418 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek31061982009-03-31 23:00:32 +0000420 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
421 if (!B->isAssignmentOp())
422 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek31061982009-03-31 23:00:32 +0000424 // What variable did we assign to?
425 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 if (!DR)
428 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek31061982009-03-31 23:00:32 +0000430 VD = dyn_cast<VarDecl>(DR->getDecl());
431 }
432 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
433 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
434 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000435 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000436 VD = dyn_cast<VarDecl>(*DS->decl_begin());
437 }
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek31061982009-03-31 23:00:32 +0000439 if (!VD)
440 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek31061982009-03-31 23:00:32 +0000442 // What is the most recently referenced variable with this binding?
443 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek31061982009-03-31 23:00:32 +0000445 if (!MostRecent)
446 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek31061982009-03-31 23:00:32 +0000448 // Create the diagnostic.
449 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000451 if (Loc::isLocType(VD->getType())) {
Ted Kremenek31061982009-03-31 23:00:32 +0000452 std::string msg = "'" + std::string(VD->getNameAsString()) +
453 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek31061982009-03-31 23:00:32 +0000455 PD.push_front(new PathDiagnosticEventPiece(L, msg));
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek31061982009-03-31 23:00:32 +0000458 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000459 }
Ted Kremenek31061982009-03-31 23:00:32 +0000460};
461}
462
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000463static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000464 const Stmt* S,
465 SymbolRef Sym, BugReporter& BR,
466 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000468 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000469 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek31061982009-03-31 23:00:32 +0000471 if (!PrevSt)
472 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek31061982009-03-31 23:00:32 +0000474 // Look at the region bindings of the current state that map to the
475 // specified symbol. Are any of them not in the previous state?
476 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
477 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
478 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
479}
480
481namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000482class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000483: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek31061982009-03-31 23:00:32 +0000485 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000486 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000487 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000488 GRBugReporter& BR;
489 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek31061982009-03-31 23:00:32 +0000491public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000492 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000493 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000494 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 bool HandleBinding(StoreManager& SMgr, Store store,
497 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Ted Kremenek31061982009-03-31 23:00:32 +0000499 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501 if (!ScanSym)
502 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek31061982009-03-31 23:00:32 +0000504 if (!BR.isNotable(ScanSym))
505 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek31061982009-03-31 23:00:32 +0000507 if (AlreadyProcessed.count(ScanSym))
508 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenek31061982009-03-31 23:00:32 +0000510 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek31061982009-03-31 23:00:32 +0000512 HandleNotableSymbol(N, S, ScanSym, BR, PD);
513 return true;
514 }
515};
516} // end anonymous namespace
517
518//===----------------------------------------------------------------------===//
519// "Minimal" path diagnostic generation algorithm.
520//===----------------------------------------------------------------------===//
521
Ted Kremenek14856d72009-04-06 23:06:54 +0000522static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
523
Ted Kremenek31061982009-03-31 23:00:32 +0000524static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
525 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000526 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000527
Ted Kremenek31061982009-03-31 23:00:32 +0000528 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000529 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000530 ? NULL : *(N->pred_begin());
531 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000532 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000533 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Ted Kremenek31061982009-03-31 23:00:32 +0000535 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Ted Kremenek31061982009-03-31 23:00:32 +0000537 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000538 const CFGBlock* Src = BE->getSrc();
539 const CFGBlock* Dst = BE->getDst();
540 const Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenek31061982009-03-31 23:00:32 +0000542 if (!T)
543 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 switch (T->getStmtClass()) {
548 default:
549 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek31061982009-03-31 23:00:32 +0000551 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000552 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000553 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek31061982009-03-31 23:00:32 +0000555 if (!S)
556 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek31061982009-03-31 23:00:32 +0000558 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000559 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000560 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Ted Kremenek31061982009-03-31 23:00:32 +0000562 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000563 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000564 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
565 os.str()));
566 break;
567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
569 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000570 // Figure out what case arm we took.
571 std::string sbuf;
572 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000574 if (const Stmt* S = Dst->getLabel()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000575 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenek31061982009-03-31 23:00:32 +0000577 switch (S->getStmtClass()) {
578 default:
579 os << "No cases match in the switch statement. "
580 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000581 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000582 break;
583 case Stmt::DefaultStmtClass:
584 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000585 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000586 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek31061982009-03-31 23:00:32 +0000588 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000589 os << "Control jumps to 'case ";
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000590 const CaseStmt* Case = cast<CaseStmt>(S);
591 const Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000592
593 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000594 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000596 if (const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000597 // FIXME: Maybe this should be an assertion. Are there cases
598 // were it is not an EnumConstantDecl?
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000599 const EnumConstantDecl* D =
600 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Ted Kremenek31061982009-03-31 23:00:32 +0000602 if (D) {
603 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000604 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000605 }
606 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000607
608 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000609 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000610
Ted Kremenek31061982009-03-31 23:00:32 +0000611 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000612 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000613 break;
614 }
615 }
616 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
617 os.str()));
618 }
619 else {
620 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000621 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000622 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
623 os.str()));
624 }
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek31061982009-03-31 23:00:32 +0000626 break;
627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek31061982009-03-31 23:00:32 +0000629 case Stmt::BreakStmtClass:
630 case Stmt::ContinueStmtClass: {
631 std::string sbuf;
632 llvm::raw_string_ostream os(sbuf);
633 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
634 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
635 os.str()));
636 break;
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000640 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000641 case Stmt::ConditionalOperatorClass: {
642 std::string sbuf;
643 llvm::raw_string_ostream os(sbuf);
644 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek31061982009-03-31 23:00:32 +0000646 if (*(Src->succ_begin()+1) == Dst)
647 os << "false";
648 else
649 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek31061982009-03-31 23:00:32 +0000651 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 if (const Stmt *S = End.asStmt())
654 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek31061982009-03-31 23:00:32 +0000656 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
657 os.str()));
658 break;
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek31061982009-03-31 23:00:32 +0000661 // Determine control-flow for short-circuited '&&' and '||'.
662 case Stmt::BinaryOperatorClass: {
663 if (!PDB.supportsLogicalOpControlFlow())
664 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000666 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000667 std::string sbuf;
668 llvm::raw_string_ostream os(sbuf);
669 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000670
John McCall2de56d12010-08-25 11:45:40 +0000671 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000672 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenek31061982009-03-31 23:00:32 +0000674 if (*(Src->succ_begin()+1) == Dst) {
675 os << "false";
676 PathDiagnosticLocation End(B->getLHS(), SMgr);
677 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
678 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
679 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000680 }
Ted Kremenek31061982009-03-31 23:00:32 +0000681 else {
682 os << "true";
683 PathDiagnosticLocation Start(B->getLHS(), SMgr);
684 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
685 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
686 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000687 }
Ted Kremenek31061982009-03-31 23:00:32 +0000688 }
689 else {
John McCall2de56d12010-08-25 11:45:40 +0000690 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000691 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Ted Kremenek31061982009-03-31 23:00:32 +0000693 if (*(Src->succ_begin()+1) == Dst) {
694 os << "false";
695 PathDiagnosticLocation Start(B->getLHS(), SMgr);
696 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
697 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000698 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000699 }
700 else {
701 os << "true";
702 PathDiagnosticLocation End(B->getLHS(), SMgr);
703 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
704 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000705 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000706 }
707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenek31061982009-03-31 23:00:32 +0000709 break;
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
712 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000713 if (*(Src->succ_begin()) == Dst) {
714 std::string sbuf;
715 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenek31061982009-03-31 23:00:32 +0000717 os << "Loop condition is true. ";
718 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Ted Kremenek31061982009-03-31 23:00:32 +0000720 if (const Stmt *S = End.asStmt())
721 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek31061982009-03-31 23:00:32 +0000723 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
724 os.str()));
725 }
726 else {
727 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek31061982009-03-31 23:00:32 +0000729 if (const Stmt *S = End.asStmt())
730 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek31061982009-03-31 23:00:32 +0000732 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
733 "Loop condition is false. Exiting loop"));
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Ted Kremenek31061982009-03-31 23:00:32 +0000736 break;
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek31061982009-03-31 23:00:32 +0000739 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000740 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000741 if (*(Src->succ_begin()+1) == Dst) {
742 std::string sbuf;
743 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek31061982009-03-31 23:00:32 +0000745 os << "Loop condition is false. ";
746 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
747 if (const Stmt *S = End.asStmt())
748 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremenek31061982009-03-31 23:00:32 +0000750 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
751 os.str()));
752 }
753 else {
754 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
755 if (const Stmt *S = End.asStmt())
756 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek31061982009-03-31 23:00:32 +0000758 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000759 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek31061982009-03-31 23:00:32 +0000762 break;
763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek31061982009-03-31 23:00:32 +0000765 case Stmt::IfStmtClass: {
766 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek31061982009-03-31 23:00:32 +0000768 if (const Stmt *S = End.asStmt())
769 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Ted Kremenek31061982009-03-31 23:00:32 +0000771 if (*(Src->succ_begin()+1) == Dst)
772 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000773 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000774 else
Ted Kremenek31061982009-03-31 23:00:32 +0000775 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000776 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek31061982009-03-31 23:00:32 +0000778 break;
779 }
780 }
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000783 if (NextNode) {
784 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
785 E = PDB.visitor_end(); I!=E; ++I) {
786 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
787 PD.push_front(p);
788 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
791 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000792 // Scan the region bindings, and see if a "notable" symbol has a new
793 // lval binding.
794 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
795 PDB.getStateManager().iterBindings(N->getState(), SNS);
796 }
797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek14856d72009-04-06 23:06:54 +0000799 // After constructing the full PathDiagnostic, do a pass over it to compact
800 // PathDiagnosticPieces that occur within a macro.
801 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000802}
803
804//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000805// "Extensive" PathDiagnostic generation.
806//===----------------------------------------------------------------------===//
807
808static bool IsControlFlowExpr(const Stmt *S) {
809 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000811 if (!E)
812 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
814 E = E->IgnoreParenCasts();
815
John McCall56ca35d2011-02-17 10:25:35 +0000816 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000817 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000819 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
820 if (B->isLogicalOp())
821 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
823 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000824}
825
Ted Kremenek14856d72009-04-06 23:06:54 +0000826namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000827class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000828 bool IsDead;
829public:
830 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
831 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000832
833 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000834 bool isDead() const { return IsDead; }
835};
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000837class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000838 std::vector<ContextLocation> CLocs;
839 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000840 PathDiagnostic &PD;
841 PathDiagnosticBuilder &PDB;
842 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000844 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek14856d72009-04-06 23:06:54 +0000846 bool containsLocation(const PathDiagnosticLocation &Container,
847 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenek14856d72009-04-06 23:06:54 +0000849 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek9650cf32009-05-11 21:42:34 +0000851 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
852 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000853 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000854 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000855 while (1) {
856 // Adjust the location for some expressions that are best referenced
857 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000858 switch (S->getStmtClass()) {
859 default:
860 break;
861 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000862 case Stmt::GenericSelectionExprClass:
863 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000864 firstCharOnly = true;
865 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000866 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000867 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000868 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000869 firstCharOnly = true;
870 continue;
871 case Stmt::ChooseExprClass:
872 S = cast<ChooseExpr>(S)->getCond();
873 firstCharOnly = true;
874 continue;
875 case Stmt::BinaryOperatorClass:
876 S = cast<BinaryOperator>(S)->getLHS();
877 firstCharOnly = true;
878 continue;
879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek9650cf32009-05-11 21:42:34 +0000881 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek9650cf32009-05-11 21:42:34 +0000884 if (S != Original)
885 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Ted Kremenek9650cf32009-05-11 21:42:34 +0000888 if (firstCharOnly)
889 L = PathDiagnosticLocation(L.asLocation());
890
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000891 return L;
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek14856d72009-04-06 23:06:54 +0000894 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000895 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000896 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000897 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000898 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000899 CLocs.pop_back();
900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Ted Kremenek14856d72009-04-06 23:06:54 +0000902public:
903 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
904 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremeneka301a672009-04-22 18:16:20 +0000906 // If the PathDiagnostic already has pieces, add the enclosing statement
907 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000908 if (!PD.empty()) {
909 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek14856d72009-04-06 23:06:54 +0000911 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000912 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000913 }
914 }
915
916 ~EdgeBuilder() {
917 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Ted Kremeneka301a672009-04-22 18:16:20 +0000919 // Finally, add an initial edge from the start location of the first
920 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000921 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
922 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000923 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000924 if (!CS->body_empty()) {
925 SourceLocation Loc = (*CS->body_begin())->getLocStart();
926 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremenek14856d72009-04-06 23:06:54 +0000929 }
930
931 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000933 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Ted Kremenek14856d72009-04-06 23:06:54 +0000935 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000936 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000937};
Ted Kremenek14856d72009-04-06 23:06:54 +0000938} // end anonymous namespace
939
940
941PathDiagnosticLocation
942EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
943 if (const Stmt *S = L.asStmt()) {
944 if (IsControlFlowExpr(S))
945 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000946
947 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000948 }
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Ted Kremenek14856d72009-04-06 23:06:54 +0000950 return L;
951}
952
953bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
954 const PathDiagnosticLocation &Containee) {
955
956 if (Container == Containee)
957 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Ted Kremenek14856d72009-04-06 23:06:54 +0000959 if (Container.asDecl())
960 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Ted Kremenek14856d72009-04-06 23:06:54 +0000962 if (const Stmt *S = Containee.asStmt())
963 if (const Stmt *ContainerS = Container.asStmt()) {
964 while (S) {
965 if (S == ContainerS)
966 return true;
967 S = PDB.getParent(S);
968 }
969 return false;
970 }
971
972 // Less accurate: compare using source ranges.
973 SourceRange ContainerR = Container.asRange();
974 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Ted Kremenek14856d72009-04-06 23:06:54 +0000976 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000977 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
978 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
979 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
980 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chandler Carruth64211622011-07-25 21:09:52 +0000982 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
983 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
984 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
985 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenek14856d72009-04-06 23:06:54 +0000987 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000988 assert(ContaineeBegLine <= ContaineeEndLine);
989
Ted Kremenek14856d72009-04-06 23:06:54 +0000990 return (ContainerBegLine <= ContaineeBegLine &&
991 ContainerEndLine >= ContaineeEndLine &&
992 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000993 SM.getExpansionColumnNumber(ContainerRBeg) <=
994 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000995 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000996 SM.getExpansionColumnNumber(ContainerREnd) >=
997 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000998}
999
Ted Kremenek14856d72009-04-06 23:06:54 +00001000void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1001 if (!PrevLoc.isValid()) {
1002 PrevLoc = NewLoc;
1003 return;
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001006 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1007 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001009 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001010 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Ted Kremenek14856d72009-04-06 23:06:54 +00001012 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001013 if (NewLocClean.asLocation().getExpansionLoc() ==
1014 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001015 return;
1016
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001017 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1018 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001019}
1020
1021void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremeneka301a672009-04-22 18:16:20 +00001023 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1024 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek14856d72009-04-06 23:06:54 +00001026 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1027
1028 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001029 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Ted Kremenek14856d72009-04-06 23:06:54 +00001031 // Is the top location context the same as the one for the new location?
1032 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001033 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001034 if (IsConsumedExpr(TopContextLoc) &&
1035 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001036 TopContextLoc.markDead();
1037
Ted Kremenek14856d72009-04-06 23:06:54 +00001038 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001040
1041 return;
1042 }
1043
1044 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001045 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001046 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001048 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001049 CLocs.push_back(ContextLocation(CLoc, true));
1050 return;
1051 }
1052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Ted Kremenek14856d72009-04-06 23:06:54 +00001054 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001055 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001056 }
1057
1058 // Context does not contain the location. Flush it.
1059 popLocation();
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001062 // If we reach here, there is no enclosing context. Just add the edge.
1063 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001064}
1065
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001066bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1067 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1068 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001070 return false;
1071}
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Ted Kremeneke1baed32009-05-05 23:13:38 +00001073void EdgeBuilder::addExtendedContext(const Stmt *S) {
1074 if (!S)
1075 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
1077 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001078 while (Parent) {
1079 if (isa<CompoundStmt>(Parent))
1080 Parent = PDB.getParent(Parent);
1081 else
1082 break;
1083 }
1084
1085 if (Parent) {
1086 switch (Parent->getStmtClass()) {
1087 case Stmt::DoStmtClass:
1088 case Stmt::ObjCAtSynchronizedStmtClass:
1089 addContext(Parent);
1090 default:
1091 break;
1092 }
1093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremeneke1baed32009-05-05 23:13:38 +00001095 addContext(S);
1096}
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Ted Kremenek14856d72009-04-06 23:06:54 +00001098void EdgeBuilder::addContext(const Stmt *S) {
1099 if (!S)
1100 return;
1101
1102 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Ted Kremenek14856d72009-04-06 23:06:54 +00001104 while (!CLocs.empty()) {
1105 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1106
1107 // Is the top location context the same as the one for the new location?
1108 if (TopContextLoc == L)
1109 return;
1110
1111 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001112 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001113 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001114 }
1115
1116 // Context does not contain the location. Flush it.
1117 popLocation();
1118 }
1119
1120 CLocs.push_back(L);
1121}
1122
1123static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1124 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001125 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001126 EdgeBuilder EB(PD, PDB);
1127
Zhongxing Xu41ca1342010-03-23 05:13:26 +00001128 const ExplodedNode* NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001129 while (NextNode) {
1130 N = NextNode;
1131 NextNode = GetPredecessorNode(N);
1132 ProgramPoint P = N->getLocation();
1133
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001134 do {
1135 // Block edges.
1136 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1137 const CFGBlock &Blk = *BE->getSrc();
1138 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001140 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001141 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001142 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001143 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001145 if (!Term) {
1146 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1147 CS = dyn_cast<CompoundStmt>(FS->getBody());
1148 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001149 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001152 PathDiagnosticEventPiece *p =
1153 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001154 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001156 EB.addEdge(p->getLocation(), true);
1157 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001159 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001160 PathDiagnosticLocation BL(CS->getRBracLoc(),
1161 PDB.getSourceManager());
1162 BL = PathDiagnosticLocation(BL.asLocation());
1163 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001164 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001165 }
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001167 if (Term)
1168 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001170 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001171 }
1172
Mike Stump1eb44332009-09-09 15:08:12 +00001173 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001174 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1175 const Stmt *stmt = S->getStmt();
1176 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001177 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001178 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001179 }
1180 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001181 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001182 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001183
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001184 break;
1185 }
1186 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001188 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001189 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremenek8966bc12009-05-06 21:39:49 +00001191 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1192 E = PDB.visitor_end(); I!=E; ++I) {
1193 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1194 const PathDiagnosticLocation &Loc = p->getLocation();
1195 EB.addEdge(Loc, true);
1196 PD.push_front(p);
1197 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001198 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001201 }
1202}
1203
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001204//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001205// Methods for BugType and subclasses.
1206//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001207BugType::~BugType() { }
1208
Ted Kremenekcf118d42009-02-04 23:49:09 +00001209void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001210
Ted Kremenekcf118d42009-02-04 23:49:09 +00001211//===----------------------------------------------------------------------===//
1212// Methods for BugReport and subclasses.
1213//===----------------------------------------------------------------------===//
1214BugReport::~BugReport() {}
1215RangedBugReport::~RangedBugReport() {}
1216
Mike Stump1eb44332009-09-09 15:08:12 +00001217const Stmt* BugReport::getStmt() const {
Tom Care212f6d32010-09-16 03:50:38 +00001218 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001219 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenekcf118d42009-02-04 23:49:09 +00001221 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001222 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001223 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001224 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001225 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001226 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001227 S = GetStmt(ProgP);
1228
1229 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001230}
1231
1232PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001233BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001234 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001236 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Ted Kremenek61f3e052008-04-03 04:42:52 +00001238 if (!S)
1239 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001240
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001241 BugReport::ranges_iterator Beg, End;
1242 llvm::tie(Beg, End) = getRanges();
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001243 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001245 // Only add the statement itself as a range if we didn't specify any
1246 // special ranges for this report.
1247 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1248 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001250 for (; Beg != End; ++Beg)
1251 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenek61f3e052008-04-03 04:42:52 +00001253 return P;
1254}
1255
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001256std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
1257BugReport::getRanges() const {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001258 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001259 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001260 assert(R.isValid());
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001261 return std::make_pair(&R, &R+1);
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001262 }
1263 else
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001264 return std::make_pair(ranges_iterator(), ranges_iterator());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001265}
1266
Mike Stump1eb44332009-09-09 15:08:12 +00001267SourceLocation BugReport::getLocation() const {
Tom Care212f6d32010-09-16 03:50:38 +00001268 if (ErrorNode)
1269 if (const Stmt* S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001270 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001271 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001272 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001273 // For binary operators, return the location of the operator.
1274 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1275 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001276
Ted Kremenekcf118d42009-02-04 23:49:09 +00001277 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001278 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001279
1280 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001281}
1282
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001283PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1284 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001285 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001286 return NULL;
1287}
1288
Ted Kremenekcf118d42009-02-04 23:49:09 +00001289//===----------------------------------------------------------------------===//
1290// Methods for BugReporter and subclasses.
1291//===----------------------------------------------------------------------===//
1292
1293BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001294 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001295}
1296
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001297GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001298BugReporterData::~BugReporterData() {}
1299
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001300ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001301
1302GRStateManager&
1303GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1304
1305BugReporter::~BugReporter() { FlushReports(); }
1306
1307void BugReporter::FlushReports() {
1308 if (BugTypes.isEmpty())
1309 return;
1310
1311 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001312 // warnings and new BugTypes.
1313 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1314 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001315 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001316 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001317 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001318 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001319 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001320 const_cast<BugType*>(*I)->FlushReports(*this);
1321
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001322 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1323 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1324 BugReportEquivClass& EQ = *EI;
1325 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001326 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001327
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001328 // BugReporter owns and deletes only BugTypes created implicitly through
1329 // EmitBasicReport.
1330 // FIXME: There are leaks from checkers that assume that the BugTypes they
1331 // create will be destroyed by the BugReporter.
1332 for (llvm::StringMap<BugType*>::iterator
1333 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1334 delete I->second;
1335
Ted Kremenekcf118d42009-02-04 23:49:09 +00001336 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001337 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001338}
1339
1340//===----------------------------------------------------------------------===//
1341// PathDiagnostics generation.
1342//===----------------------------------------------------------------------===//
1343
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001344static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001345 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001346MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001347 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Ted Kremenekcf118d42009-02-04 23:49:09 +00001349 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001350 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001351 // error node unless there are two or more error nodes with the same minimum
1352 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001353 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001354 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001355
1356 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001357 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1358 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Ted Kremenekcf118d42009-02-04 23:49:09 +00001360 // Create owning pointers for GTrim and NMap just to ensure that they are
1361 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001362 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001363 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenekcf118d42009-02-04 23:49:09 +00001365 // Find the (first) error node in the trimmed graph. We just need to consult
1366 // the node map (NMap) which maps from nodes in the original graph to nodes
1367 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001368
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001369 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001370 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001371 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001372
Ted Kremenek40406fe2010-12-03 06:52:30 +00001373 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1374 const ExplodedNode *originalNode = nodes[nodeIndex];
1375 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001376 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001377 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001378 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenek938332c2009-05-16 01:11:58 +00001381 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001382
1383 // Create a new (third!) graph with a single path. This is the graph
1384 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001385 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenek10aa5542009-03-12 23:41:59 +00001387 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001388 // to the root node, and then construct a new graph that contains only
1389 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001390 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001392 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001393 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001395 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001396 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001397 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001399 if (Visited.find(Node) != Visited.end())
1400 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001402 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001404 if (Node->pred_empty()) {
1405 Root = Node;
1406 break;
1407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001409 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001410 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001411 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Ted Kremenek938332c2009-05-16 01:11:58 +00001414 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenek10aa5542009-03-12 23:41:59 +00001416 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001417 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001418 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001419 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001420 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001422 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001423 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001424 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001425 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001427 // Create the equivalent node in the new graph with the same state
1428 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001429 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001431 // Store the mapping to the original node.
1432 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1433 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001434 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001436 // Link up the new node with the previous node.
1437 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001438 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001440 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001442 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001443 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001444 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001445 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001447 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001448 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001451 // Find the next successor node. We choose the node that is marked
1452 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001453 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1454 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001455 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001457 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001459 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001461 if (I == Visited.end())
1462 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001464 if (!N || I->second < MinVal) {
1465 N = *SI;
1466 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001467 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001468 }
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek938332c2009-05-16 01:11:58 +00001470 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenek938332c2009-05-16 01:11:58 +00001473 assert(First);
1474
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001475 return std::make_pair(std::make_pair(GNew, BM),
1476 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001477}
1478
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001479/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1480/// and collapses PathDiagosticPieces that are expanded by macros.
1481static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1482 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1483 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001485 typedef std::vector<PathDiagnosticPiece*>
1486 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001488 MacroStackTy MacroStack;
1489 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001491 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1492 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001493 const FullSourceLoc Loc = I->getLocation().asLocation();
1494
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001495 // Determine the instantiation location, which is the location we group
1496 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001497 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001498 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001499 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001501 if (Loc.isFileID()) {
1502 MacroStack.clear();
1503 Pieces.push_back(&*I);
1504 continue;
1505 }
1506
1507 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001509 // Is the PathDiagnosticPiece within the same macro group?
1510 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1511 MacroStack.back().first->push_back(&*I);
1512 continue;
1513 }
1514
1515 // We aren't in the same group. Are we descending into a new macro
1516 // or are part of an old one?
1517 PathDiagnosticMacroPiece *MacroGroup = 0;
1518
1519 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001520 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001521 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001523 // Walk the entire macro stack.
1524 while (!MacroStack.empty()) {
1525 if (InstantiationLoc == MacroStack.back().second) {
1526 MacroGroup = MacroStack.back().first;
1527 break;
1528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001530 if (ParentInstantiationLoc == MacroStack.back().second) {
1531 MacroGroup = MacroStack.back().first;
1532 break;
1533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001535 MacroStack.pop_back();
1536 }
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001538 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1539 // Create a new macro group and add it to the stack.
1540 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1541
1542 if (MacroGroup)
1543 MacroGroup->push_back(NewGroup);
1544 else {
1545 assert(InstantiationLoc.isFileID());
1546 Pieces.push_back(NewGroup);
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001549 MacroGroup = NewGroup;
1550 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1551 }
1552
1553 // Finally, add the PathDiagnosticPiece to the group.
1554 MacroGroup->push_back(&*I);
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001557 // Now take the pieces and construct a new PathDiagnostic.
1558 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001560 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1561 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1562 if (!MP->containsEvent()) {
1563 delete MP;
1564 continue;
1565 }
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001567 PD.push_back(*I);
1568 }
1569}
1570
Ted Kremenek7dc86642009-03-31 20:22:36 +00001571void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001572 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Ted Kremenek40406fe2010-12-03 06:52:30 +00001574 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001575 SmallVector<const ExplodedNode *, 10> errorNodes;
1576 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001577 E = bugReports.end(); I != E; ++I) {
1578 errorNodes.push_back((*I)->getErrorNode());
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001581 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001582 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001583 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001584 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001585 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Ted Kremenekcf118d42009-02-04 23:49:09 +00001587 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001588 assert(GPair.second.second < bugReports.size());
1589 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001590 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001592 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001593 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001594 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
1596 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001597 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Ted Kremenek8966bc12009-05-06 21:39:49 +00001599 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001600 PD.push_back(Piece);
1601 else
1602 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Ted Kremenekff7f7362010-03-20 18:02:01 +00001604 // Register node visitors.
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001605 R->registerInitialVisitors(PDB, N);
Ted Kremenekff7f7362010-03-20 18:02:01 +00001606 bugreporter::registerNilReceiverVisitor(PDB);
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Ted Kremenek7dc86642009-03-31 20:22:36 +00001608 switch (PDB.getGenerationScheme()) {
1609 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001610 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001611 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001612 case PathDiagnosticClient::Minimal:
1613 GenerateMinimalPathDiagnostic(PD, PDB, N);
1614 break;
1615 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001616}
1617
Ted Kremenekcf118d42009-02-04 23:49:09 +00001618void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001619 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001620}
1621
Mike Stump1eb44332009-09-09 15:08:12 +00001622void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001623 // Compute the bug report's hash to determine its equivalence class.
1624 llvm::FoldingSetNodeID ID;
1625 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001626
1627 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001628 BugType& BT = R->getBugType();
1629 Register(&BT);
1630 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001631 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Ted Kremenekcf118d42009-02-04 23:49:09 +00001633 if (!EQ) {
1634 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001635 EQClasses.InsertNode(EQ, InsertPos);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001636 }
1637 else
1638 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001639}
1640
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001641
1642//===----------------------------------------------------------------------===//
1643// Emitting reports in equivalence classes.
1644//===----------------------------------------------------------------------===//
1645
1646namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001647struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001648 const ExplodedNode *N;
1649 ExplodedNode::const_succ_iterator I, E;
1650
1651 FRIEC_WLItem(const ExplodedNode *n)
1652 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1653};
1654}
1655
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001656static BugReport *
1657FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001658 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001659
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001660 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1661 assert(I != E);
1662 BugReport *R = *I;
1663 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001664
Ted Kremenek40406fe2010-12-03 06:52:30 +00001665 // If we don't need to suppress any of the nodes because they are
1666 // post-dominated by a sink, simply add all the nodes in the equivalence class
1667 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001668 if (!BT.isSuppressOnSink()) {
1669 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Tom Care212f6d32010-09-16 03:50:38 +00001670 const ExplodedNode* N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001671 if (N) {
1672 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001673 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001674 }
1675 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001676 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001677 }
1678
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001679 // For bug reports that should be suppressed when all paths are post-dominated
1680 // by a sink node, iterate through the reports in the equivalence class
1681 // until we find one that isn't post-dominated (if one exists). We use a
1682 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1683 // this as a recursive function, but we don't want to risk blowing out the
1684 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001685 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001686
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001687 for (; I != E; ++I) {
1688 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001689 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001690
Ted Kremenek40406fe2010-12-03 06:52:30 +00001691 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001692 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001693 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001694 assert(false &&
1695 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001696 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001697 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001698 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001699 if (errorNode->succ_empty()) {
1700 bugReports.push_back(R);
1701 if (!exampleReport)
1702 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001703 continue;
1704 }
1705
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001706 // At this point we know that 'N' is not a sink and it has at least one
1707 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1708 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001709 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001710 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1711
1712 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001713 WL.push_back(errorNode);
1714 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001715
1716 while (!WL.empty()) {
1717 WLItem &WI = WL.back();
1718 assert(!WI.N->succ_empty());
1719
1720 for (; WI.I != WI.E; ++WI.I) {
1721 const ExplodedNode *Succ = *WI.I;
1722 // End-of-path node?
1723 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001724 // If we found an end-of-path node that is not a sink.
1725 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001726 bugReports.push_back(R);
1727 if (!exampleReport)
1728 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001729 WL.clear();
1730 break;
1731 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001732 // Found a sink? Continue on to the next successor.
1733 continue;
1734 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001735 // Mark the successor as visited. If it hasn't been explored,
1736 // enqueue it to the DFS worklist.
1737 unsigned &mark = Visited[Succ];
1738 if (!mark) {
1739 mark = 1;
1740 WL.push_back(Succ);
1741 break;
1742 }
1743 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001744
1745 // The worklist may have been cleared at this point. First
1746 // check if it is empty before checking the last item.
1747 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001748 WL.pop_back();
1749 }
1750 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001751
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001752 // ExampleReport will be NULL if all the nodes in the equivalence class
1753 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001754 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001755}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001756
1757//===----------------------------------------------------------------------===//
1758// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1759// uses global state, which eventually should go elsewhere.
1760//===----------------------------------------------------------------------===//
1761namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001762class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001763 llvm::FoldingSetNodeID ID;
1764public:
1765 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1766 ID.AddString(R->getBugType().getName());
1767 ID.AddString(R->getBugType().getCategory());
1768 ID.AddString(R->getDescription());
1769 ID.AddInteger(R->getLocation().getRawEncoding());
1770 PD->Profile(ID);
1771 }
1772
1773 void Profile(llvm::FoldingSetNodeID &id) {
1774 id = ID;
1775 }
1776
1777 llvm::FoldingSetNodeID &getID() { return ID; }
1778};
1779}
1780
1781static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1782 // FIXME: Eventually this diagnostic cache should reside in something
1783 // like AnalysisManager instead of being a static variable. This is
1784 // really unsafe in the long term.
1785 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1786 static DiagnosticCache DC;
1787
1788 void *InsertPos;
1789 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1790
1791 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1792 delete Item;
1793 return true;
1794 }
1795
1796 DC.InsertNode(Item, InsertPos);
1797 return false;
1798}
1799
Ted Kremenekcf118d42009-02-04 23:49:09 +00001800void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001801 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001802 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1803 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001804 return;
1805
Ted Kremenekd49967f2009-04-29 21:58:13 +00001806 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Ted Kremenekcf118d42009-02-04 23:49:09 +00001808 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001809 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001810 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenekd49967f2009-04-29 21:58:13 +00001812 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001813 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001814 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001815 ? exampleReport->getDescription()
1816 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001817 BT.getCategory()));
1818
Ted Kremenek40406fe2010-12-03 06:52:30 +00001819 if (!bugReports.empty())
1820 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Ted Kremenek40406fe2010-12-03 06:52:30 +00001822 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001823 return;
1824
Ted Kremenek072192b2008-04-30 23:47:44 +00001825 // Get the meta data.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001826 std::pair<const char**, const char**> Meta =
1827 exampleReport->getExtraDescriptiveText();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001828 for (const char** s = Meta.first; s != Meta.second; ++s)
1829 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001830
Ted Kremenek3148eb42009-01-24 00:55:43 +00001831 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001832 BugReport::ranges_iterator Beg, End;
1833 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001834 Diagnostic &Diag = getDiagnostic();
1835 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001836
1837 // Search the description for '%', as that will be interpretted as a
1838 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001839 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001840 unsigned ErrorDiag;
1841 {
1842 llvm::SmallString<512> TmpStr;
1843 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001844 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001845 if (*I == '%')
1846 Out << "%%";
1847 else
1848 Out << *I;
1849
1850 Out.flush();
1851 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1852 }
Ted Kremenek57202072008-07-14 17:40:50 +00001853
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001854 {
1855 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001856 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001857 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001858 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001859
1860 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1861 if (!PD)
1862 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001863
1864 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001865 PathDiagnosticPiece* piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001866 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001867
Ted Kremenek3148eb42009-01-24 00:55:43 +00001868 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1869 D->push_back(piece);
1870 }
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Ted Kremenek3148eb42009-01-24 00:55:43 +00001872 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001873}
Ted Kremenek57202072008-07-14 17:40:50 +00001874
Chris Lattner5f9e2722011-07-23 10:55:15 +00001875void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001876 SourceLocation Loc,
1877 SourceRange* RBeg, unsigned NumRanges) {
1878 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1879}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001880
Chris Lattner5f9e2722011-07-23 10:55:15 +00001881void BugReporter::EmitBasicReport(StringRef name,
1882 StringRef category,
1883 StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001884 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001886 // 'BT' is owned by BugReporter.
1887 BugType *BT = getBugTypeForName(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001888 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001889 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1890 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1891 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001892}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001893
Chris Lattner5f9e2722011-07-23 10:55:15 +00001894BugType *BugReporter::getBugTypeForName(StringRef name,
1895 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001896 llvm::SmallString<136> fullDesc;
1897 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1898 llvm::StringMapEntry<BugType *> &
1899 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1900 BugType *BT = entry.getValue();
1901 if (!BT) {
1902 BT = new BugType(name, category);
1903 entry.setValue(BT);
1904 }
1905 return BT;
1906}