blob: 9a84045ebd976dbef0ffa9cc37ce30bc8cb5c853 [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 "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000216 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
217 << '.';
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
435 // holds by contruction in the CFG.
436 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 "
563 << End.asLocation().getInstantiationLineNumber();
564 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 "
581 << End.asLocation().getInstantiationLineNumber();
582 break;
583 case Stmt::DefaultStmtClass:
584 os << "Control jumps to the 'default' case at line "
585 << End.asLocation().getInstantiationLineNumber();
586 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 "
612 << End.asLocation().getInstantiationLineNumber();
613 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:
862 S = cast<ParenExpr>(S)->IgnoreParens();
863 firstCharOnly = true;
864 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000865 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000866 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000867 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000868 firstCharOnly = true;
869 continue;
870 case Stmt::ChooseExprClass:
871 S = cast<ChooseExpr>(S)->getCond();
872 firstCharOnly = true;
873 continue;
874 case Stmt::BinaryOperatorClass:
875 S = cast<BinaryOperator>(S)->getLHS();
876 firstCharOnly = true;
877 continue;
878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek9650cf32009-05-11 21:42:34 +0000880 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek9650cf32009-05-11 21:42:34 +0000883 if (S != Original)
884 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Ted Kremenek9650cf32009-05-11 21:42:34 +0000887 if (firstCharOnly)
888 L = PathDiagnosticLocation(L.asLocation());
889
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000890 return L;
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek14856d72009-04-06 23:06:54 +0000893 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000894 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000895 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000896 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000897 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000898 CLocs.pop_back();
899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Ted Kremenek14856d72009-04-06 23:06:54 +0000901public:
902 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
903 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Ted Kremeneka301a672009-04-22 18:16:20 +0000905 // If the PathDiagnostic already has pieces, add the enclosing statement
906 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000907 if (!PD.empty()) {
908 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremenek14856d72009-04-06 23:06:54 +0000910 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000911 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000912 }
913 }
914
915 ~EdgeBuilder() {
916 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremeneka301a672009-04-22 18:16:20 +0000918 // Finally, add an initial edge from the start location of the first
919 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000920 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
921 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000922 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000923 if (!CS->body_empty()) {
924 SourceLocation Loc = (*CS->body_begin())->getLocStart();
925 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek14856d72009-04-06 23:06:54 +0000928 }
929
930 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000932 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek14856d72009-04-06 23:06:54 +0000934 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000935 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000936};
Ted Kremenek14856d72009-04-06 23:06:54 +0000937} // end anonymous namespace
938
939
940PathDiagnosticLocation
941EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
942 if (const Stmt *S = L.asStmt()) {
943 if (IsControlFlowExpr(S))
944 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000945
946 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000947 }
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Ted Kremenek14856d72009-04-06 23:06:54 +0000949 return L;
950}
951
952bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
953 const PathDiagnosticLocation &Containee) {
954
955 if (Container == Containee)
956 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Ted Kremenek14856d72009-04-06 23:06:54 +0000958 if (Container.asDecl())
959 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Ted Kremenek14856d72009-04-06 23:06:54 +0000961 if (const Stmt *S = Containee.asStmt())
962 if (const Stmt *ContainerS = Container.asStmt()) {
963 while (S) {
964 if (S == ContainerS)
965 return true;
966 S = PDB.getParent(S);
967 }
968 return false;
969 }
970
971 // Less accurate: compare using source ranges.
972 SourceRange ContainerR = Container.asRange();
973 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Ted Kremenek14856d72009-04-06 23:06:54 +0000975 SourceManager &SM = PDB.getSourceManager();
976 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
977 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
978 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
979 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
982 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
983 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
984 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenek14856d72009-04-06 23:06:54 +0000986 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000987 assert(ContaineeBegLine <= ContaineeEndLine);
988
Ted Kremenek14856d72009-04-06 23:06:54 +0000989 return (ContainerBegLine <= ContaineeBegLine &&
990 ContainerEndLine >= ContaineeEndLine &&
991 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +0000992 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +0000993 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
994 (ContainerEndLine != ContaineeEndLine ||
995 SM.getInstantiationColumnNumber(ContainerREnd) >=
996 SM.getInstantiationColumnNumber(ContainerREnd)));
997}
998
Ted Kremenek14856d72009-04-06 23:06:54 +0000999void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1000 if (!PrevLoc.isValid()) {
1001 PrevLoc = NewLoc;
1002 return;
1003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001005 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1006 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001008 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001009 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Ted Kremenek14856d72009-04-06 23:06:54 +00001011 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001012 if (NewLocClean.asLocation().getInstantiationLoc() ==
1013 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001014 return;
1015
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001016 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1017 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001018}
1019
1020void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Ted Kremeneka301a672009-04-22 18:16:20 +00001022 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1023 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1026
1027 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001028 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremenek14856d72009-04-06 23:06:54 +00001030 // Is the top location context the same as the one for the new location?
1031 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001032 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001033 if (IsConsumedExpr(TopContextLoc) &&
1034 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001035 TopContextLoc.markDead();
1036
Ted Kremenek14856d72009-04-06 23:06:54 +00001037 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001038 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001039
1040 return;
1041 }
1042
1043 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001044 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001045 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001047 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001048 CLocs.push_back(ContextLocation(CLoc, true));
1049 return;
1050 }
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Ted Kremenek14856d72009-04-06 23:06:54 +00001053 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001054 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 }
1056
1057 // Context does not contain the location. Flush it.
1058 popLocation();
1059 }
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001061 // If we reach here, there is no enclosing context. Just add the edge.
1062 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001063}
1064
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001065bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1066 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1067 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001069 return false;
1070}
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremeneke1baed32009-05-05 23:13:38 +00001072void EdgeBuilder::addExtendedContext(const Stmt *S) {
1073 if (!S)
1074 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001075
1076 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001077 while (Parent) {
1078 if (isa<CompoundStmt>(Parent))
1079 Parent = PDB.getParent(Parent);
1080 else
1081 break;
1082 }
1083
1084 if (Parent) {
1085 switch (Parent->getStmtClass()) {
1086 case Stmt::DoStmtClass:
1087 case Stmt::ObjCAtSynchronizedStmtClass:
1088 addContext(Parent);
1089 default:
1090 break;
1091 }
1092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Ted Kremeneke1baed32009-05-05 23:13:38 +00001094 addContext(S);
1095}
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Ted Kremenek14856d72009-04-06 23:06:54 +00001097void EdgeBuilder::addContext(const Stmt *S) {
1098 if (!S)
1099 return;
1100
1101 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Ted Kremenek14856d72009-04-06 23:06:54 +00001103 while (!CLocs.empty()) {
1104 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1105
1106 // Is the top location context the same as the one for the new location?
1107 if (TopContextLoc == L)
1108 return;
1109
1110 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001111 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001112 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001113 }
1114
1115 // Context does not contain the location. Flush it.
1116 popLocation();
1117 }
1118
1119 CLocs.push_back(L);
1120}
1121
1122static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1123 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001124 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001125 EdgeBuilder EB(PD, PDB);
1126
Zhongxing Xu41ca1342010-03-23 05:13:26 +00001127 const ExplodedNode* NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001128 while (NextNode) {
1129 N = NextNode;
1130 NextNode = GetPredecessorNode(N);
1131 ProgramPoint P = N->getLocation();
1132
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001133 do {
1134 // Block edges.
1135 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1136 const CFGBlock &Blk = *BE->getSrc();
1137 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001139 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001140 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001141 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001142 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001144 if (!Term) {
1145 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1146 CS = dyn_cast<CompoundStmt>(FS->getBody());
1147 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001148 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 PathDiagnosticEventPiece *p =
1152 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001153 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001155 EB.addEdge(p->getLocation(), true);
1156 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001158 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001159 PathDiagnosticLocation BL(CS->getRBracLoc(),
1160 PDB.getSourceManager());
1161 BL = PathDiagnosticLocation(BL.asLocation());
1162 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001163 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001164 }
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001166 if (Term)
1167 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001169 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001170 }
1171
Mike Stump1eb44332009-09-09 15:08:12 +00001172 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001173 if (CFGStmt S = BE->getFirstElement().getAs<CFGStmt>()) {
1174 if (IsControlFlowExpr(S)) {
1175 // Add the proper context for '&&', '||', and '?'.
1176 EB.addContext(S);
1177 }
1178 else
1179 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001180 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001181
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001182 break;
1183 }
1184 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001186 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001187 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremenek8966bc12009-05-06 21:39:49 +00001189 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1190 E = PDB.visitor_end(); I!=E; ++I) {
1191 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1192 const PathDiagnosticLocation &Loc = p->getLocation();
1193 EB.addEdge(Loc, true);
1194 PD.push_front(p);
1195 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001196 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001199 }
1200}
1201
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001202//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001203// Methods for BugType and subclasses.
1204//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001205BugType::~BugType() {
1206 // Free up the equivalence class objects. Observe that we get a pointer to
1207 // the object first before incrementing the iterator, as destroying the
1208 // node before doing so means we will read from freed memory.
1209 for (iterator I = begin(), E = end(); I !=E; ) {
1210 BugReportEquivClass *EQ = &*I;
1211 ++I;
1212 delete EQ;
1213 }
1214}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001215void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001216
Ted Kremenekcf118d42009-02-04 23:49:09 +00001217//===----------------------------------------------------------------------===//
1218// Methods for BugReport and subclasses.
1219//===----------------------------------------------------------------------===//
1220BugReport::~BugReport() {}
1221RangedBugReport::~RangedBugReport() {}
1222
Mike Stump1eb44332009-09-09 15:08:12 +00001223const Stmt* BugReport::getStmt() const {
Tom Care212f6d32010-09-16 03:50:38 +00001224 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001225 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Ted Kremenekcf118d42009-02-04 23:49:09 +00001227 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001228 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001229 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001230 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001231 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001232 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001233 S = GetStmt(ProgP);
1234
1235 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001236}
1237
1238PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001239BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001240 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001242 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Ted Kremenek61f3e052008-04-03 04:42:52 +00001244 if (!S)
1245 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001246
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001247 BugReport::ranges_iterator Beg, End;
1248 llvm::tie(Beg, End) = getRanges();
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001249 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001251 // Only add the statement itself as a range if we didn't specify any
1252 // special ranges for this report.
1253 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1254 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001256 for (; Beg != End; ++Beg)
1257 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenek61f3e052008-04-03 04:42:52 +00001259 return P;
1260}
1261
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001262std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
1263BugReport::getRanges() const {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001264 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001265 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001266 assert(R.isValid());
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001267 return std::make_pair(&R, &R+1);
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001268 }
1269 else
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001270 return std::make_pair(ranges_iterator(), ranges_iterator());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001271}
1272
Mike Stump1eb44332009-09-09 15:08:12 +00001273SourceLocation BugReport::getLocation() const {
Tom Care212f6d32010-09-16 03:50:38 +00001274 if (ErrorNode)
1275 if (const Stmt* S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001276 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001277 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001278 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001279 // For binary operators, return the location of the operator.
1280 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1281 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001282
Ted Kremenekcf118d42009-02-04 23:49:09 +00001283 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001284 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001285
1286 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001287}
1288
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001289PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1290 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001291 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001292 return NULL;
1293}
1294
Ted Kremenekcf118d42009-02-04 23:49:09 +00001295//===----------------------------------------------------------------------===//
1296// Methods for BugReporter and subclasses.
1297//===----------------------------------------------------------------------===//
1298
1299BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001300 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001301}
1302
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001303GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001304BugReporterData::~BugReporterData() {}
1305
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001306ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001307
1308GRStateManager&
1309GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1310
1311BugReporter::~BugReporter() { FlushReports(); }
1312
1313void BugReporter::FlushReports() {
1314 if (BugTypes.isEmpty())
1315 return;
1316
1317 // First flush the warnings for each BugType. This may end up creating new
1318 // warnings and new BugTypes. Because ImmutableSet is a functional data
1319 // structure, we do not need to worry about the iterators being invalidated.
1320 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1321 const_cast<BugType*>(*I)->FlushReports(*this);
1322
1323 // Iterate through BugTypes a second time. BugTypes may have been updated
1324 // with new BugType objects and new warnings.
1325 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1326 BugType *BT = const_cast<BugType*>(*I);
1327
1328 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1329 SetTy& EQClasses = BT->EQClasses;
1330
1331 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1332 BugReportEquivClass& EQ = *EI;
1333 FlushReport(EQ);
1334 }
Mike Stump1eb44332009-09-09 15:08:12 +00001335
1336 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001337 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001338 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001339
1340 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001341 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001342}
1343
1344//===----------------------------------------------------------------------===//
1345// PathDiagnostics generation.
1346//===----------------------------------------------------------------------===//
1347
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001348static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001349 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001350MakeReportGraph(const ExplodedGraph* G,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001351 llvm::SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Ted Kremenekcf118d42009-02-04 23:49:09 +00001353 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001354 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001355 // error node unless there are two or more error nodes with the same minimum
1356 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001357 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001358 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001359
1360 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001361 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1362 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Ted Kremenekcf118d42009-02-04 23:49:09 +00001364 // Create owning pointers for GTrim and NMap just to ensure that they are
1365 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001366 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001367 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Ted Kremenekcf118d42009-02-04 23:49:09 +00001369 // Find the (first) error node in the trimmed graph. We just need to consult
1370 // the node map (NMap) which maps from nodes in the original graph to nodes
1371 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001372
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001373 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001374 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001375 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376
Ted Kremenek40406fe2010-12-03 06:52:30 +00001377 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1378 const ExplodedNode *originalNode = nodes[nodeIndex];
1379 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001380 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001381 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001382 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001383 }
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Ted Kremenek938332c2009-05-16 01:11:58 +00001385 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001386
1387 // Create a new (third!) graph with a single path. This is the graph
1388 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001389 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Ted Kremenek10aa5542009-03-12 23:41:59 +00001391 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001392 // to the root node, and then construct a new graph that contains only
1393 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001394 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001396 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001397 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001399 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001400 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001401 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001403 if (Visited.find(Node) != Visited.end())
1404 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001406 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001408 if (Node->pred_empty()) {
1409 Root = Node;
1410 break;
1411 }
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001413 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001414 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001415 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001416 }
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Ted Kremenek938332c2009-05-16 01:11:58 +00001418 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek10aa5542009-03-12 23:41:59 +00001420 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001421 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001422 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001423 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001424 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001426 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001427 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001428 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001429 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001431 // Create the equivalent node in the new graph with the same state
1432 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001433 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001435 // Store the mapping to the original node.
1436 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1437 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001438 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001440 // Link up the new node with the previous node.
1441 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001442 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001444 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001447 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001448 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001449 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001450 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001451 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001452 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001453 }
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001455 // Find the next successor node. We choose the node that is marked
1456 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001457 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1458 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001459 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001461 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001463 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 if (I == Visited.end())
1466 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001468 if (!N || I->second < MinVal) {
1469 N = *SI;
1470 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001471 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek938332c2009-05-16 01:11:58 +00001474 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001475 }
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Ted Kremenek938332c2009-05-16 01:11:58 +00001477 assert(First);
1478
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001479 return std::make_pair(std::make_pair(GNew, BM),
1480 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001481}
1482
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001483/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1484/// and collapses PathDiagosticPieces that are expanded by macros.
1485static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1486 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1487 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001489 typedef std::vector<PathDiagnosticPiece*>
1490 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001492 MacroStackTy MacroStack;
1493 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001495 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1496 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001497 const FullSourceLoc Loc = I->getLocation().asLocation();
1498
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001499 // Determine the instantiation location, which is the location we group
1500 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001501 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001502 SM.getInstantiationLoc(Loc) :
1503 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001505 if (Loc.isFileID()) {
1506 MacroStack.clear();
1507 Pieces.push_back(&*I);
1508 continue;
1509 }
1510
1511 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001513 // Is the PathDiagnosticPiece within the same macro group?
1514 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1515 MacroStack.back().first->push_back(&*I);
1516 continue;
1517 }
1518
1519 // We aren't in the same group. Are we descending into a new macro
1520 // or are part of an old one?
1521 PathDiagnosticMacroPiece *MacroGroup = 0;
1522
1523 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1524 SM.getInstantiationLoc(Loc) :
1525 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001527 // Walk the entire macro stack.
1528 while (!MacroStack.empty()) {
1529 if (InstantiationLoc == MacroStack.back().second) {
1530 MacroGroup = MacroStack.back().first;
1531 break;
1532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001534 if (ParentInstantiationLoc == MacroStack.back().second) {
1535 MacroGroup = MacroStack.back().first;
1536 break;
1537 }
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001539 MacroStack.pop_back();
1540 }
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001542 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1543 // Create a new macro group and add it to the stack.
1544 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1545
1546 if (MacroGroup)
1547 MacroGroup->push_back(NewGroup);
1548 else {
1549 assert(InstantiationLoc.isFileID());
1550 Pieces.push_back(NewGroup);
1551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001553 MacroGroup = NewGroup;
1554 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1555 }
1556
1557 // Finally, add the PathDiagnosticPiece to the group.
1558 MacroGroup->push_back(&*I);
1559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001561 // Now take the pieces and construct a new PathDiagnostic.
1562 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001564 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1565 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1566 if (!MP->containsEvent()) {
1567 delete MP;
1568 continue;
1569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001571 PD.push_back(*I);
1572 }
1573}
1574
Ted Kremenek7dc86642009-03-31 20:22:36 +00001575void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001576 llvm::SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenek40406fe2010-12-03 06:52:30 +00001578 assert(!bugReports.empty());
1579 llvm::SmallVector<const ExplodedNode *, 10> errorNodes;
1580 for (llvm::SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
1581 E = bugReports.end(); I != E; ++I) {
1582 errorNodes.push_back((*I)->getErrorNode());
1583 }
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001585 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001586 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001587 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001588 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001589 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Ted Kremenekcf118d42009-02-04 23:49:09 +00001591 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001592 assert(GPair.second.second < bugReports.size());
1593 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001594 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001596 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001597 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001598 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
1600 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001601 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Ted Kremenek8966bc12009-05-06 21:39:49 +00001603 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001604 PD.push_back(Piece);
1605 else
1606 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Ted Kremenekff7f7362010-03-20 18:02:01 +00001608 // Register node visitors.
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001609 R->registerInitialVisitors(PDB, N);
Ted Kremenekff7f7362010-03-20 18:02:01 +00001610 bugreporter::registerNilReceiverVisitor(PDB);
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Ted Kremenek7dc86642009-03-31 20:22:36 +00001612 switch (PDB.getGenerationScheme()) {
1613 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001614 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001615 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001616 case PathDiagnosticClient::Minimal:
1617 GenerateMinimalPathDiagnostic(PD, PDB, N);
1618 break;
1619 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001620}
1621
Ted Kremenekcf118d42009-02-04 23:49:09 +00001622void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001623 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001624}
1625
Mike Stump1eb44332009-09-09 15:08:12 +00001626void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001627 // Compute the bug report's hash to determine its equivalence class.
1628 llvm::FoldingSetNodeID ID;
1629 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001630
1631 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001632 BugType& BT = R->getBugType();
1633 Register(&BT);
1634 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001635 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1636
Ted Kremenekcf118d42009-02-04 23:49:09 +00001637 if (!EQ) {
1638 EQ = new BugReportEquivClass(R);
1639 BT.EQClasses.InsertNode(EQ, InsertPos);
1640 }
1641 else
1642 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001643}
1644
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001645
1646//===----------------------------------------------------------------------===//
1647// Emitting reports in equivalence classes.
1648//===----------------------------------------------------------------------===//
1649
1650namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001651struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001652 const ExplodedNode *N;
1653 ExplodedNode::const_succ_iterator I, E;
1654
1655 FRIEC_WLItem(const ExplodedNode *n)
1656 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1657};
1658}
1659
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001660static BugReport *
1661FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001662 llvm::SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001663
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001664 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1665 assert(I != E);
1666 BugReport *R = *I;
1667 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001668
Ted Kremenek40406fe2010-12-03 06:52:30 +00001669 // If we don't need to suppress any of the nodes because they are
1670 // post-dominated by a sink, simply add all the nodes in the equivalence class
1671 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001672 if (!BT.isSuppressOnSink()) {
1673 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Tom Care212f6d32010-09-16 03:50:38 +00001674 const ExplodedNode* N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001675 if (N) {
1676 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001677 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001678 }
1679 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001680 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001681 }
1682
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001683 // For bug reports that should be suppressed when all paths are post-dominated
1684 // by a sink node, iterate through the reports in the equivalence class
1685 // until we find one that isn't post-dominated (if one exists). We use a
1686 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1687 // this as a recursive function, but we don't want to risk blowing out the
1688 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001689 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001690
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001691 for (; I != E; ++I) {
1692 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001693 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001694
Ted Kremenek40406fe2010-12-03 06:52:30 +00001695 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001696 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001697 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001698 assert(false &&
1699 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001700 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001701 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001702 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001703 if (errorNode->succ_empty()) {
1704 bugReports.push_back(R);
1705 if (!exampleReport)
1706 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001707 continue;
1708 }
1709
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001710 // At this point we know that 'N' is not a sink and it has at least one
1711 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1712 typedef FRIEC_WLItem WLItem;
1713 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1714 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1715
1716 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001717 WL.push_back(errorNode);
1718 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001719
1720 while (!WL.empty()) {
1721 WLItem &WI = WL.back();
1722 assert(!WI.N->succ_empty());
1723
1724 for (; WI.I != WI.E; ++WI.I) {
1725 const ExplodedNode *Succ = *WI.I;
1726 // End-of-path node?
1727 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001728 // If we found an end-of-path node that is not a sink.
1729 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001730 bugReports.push_back(R);
1731 if (!exampleReport)
1732 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001733 WL.clear();
1734 break;
1735 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001736 // Found a sink? Continue on to the next successor.
1737 continue;
1738 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001739 // Mark the successor as visited. If it hasn't been explored,
1740 // enqueue it to the DFS worklist.
1741 unsigned &mark = Visited[Succ];
1742 if (!mark) {
1743 mark = 1;
1744 WL.push_back(Succ);
1745 break;
1746 }
1747 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001748
1749 // The worklist may have been cleared at this point. First
1750 // check if it is empty before checking the last item.
1751 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001752 WL.pop_back();
1753 }
1754 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001755
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001756 // ExampleReport will be NULL if all the nodes in the equivalence class
1757 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001758 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001759}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001760
1761//===----------------------------------------------------------------------===//
1762// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1763// uses global state, which eventually should go elsewhere.
1764//===----------------------------------------------------------------------===//
1765namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001766class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001767 llvm::FoldingSetNodeID ID;
1768public:
1769 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1770 ID.AddString(R->getBugType().getName());
1771 ID.AddString(R->getBugType().getCategory());
1772 ID.AddString(R->getDescription());
1773 ID.AddInteger(R->getLocation().getRawEncoding());
1774 PD->Profile(ID);
1775 }
1776
1777 void Profile(llvm::FoldingSetNodeID &id) {
1778 id = ID;
1779 }
1780
1781 llvm::FoldingSetNodeID &getID() { return ID; }
1782};
1783}
1784
1785static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1786 // FIXME: Eventually this diagnostic cache should reside in something
1787 // like AnalysisManager instead of being a static variable. This is
1788 // really unsafe in the long term.
1789 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1790 static DiagnosticCache DC;
1791
1792 void *InsertPos;
1793 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1794
1795 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1796 delete Item;
1797 return true;
1798 }
1799
1800 DC.InsertNode(Item, InsertPos);
1801 return false;
1802}
1803
Ted Kremenekcf118d42009-02-04 23:49:09 +00001804void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001805 llvm::SmallVector<BugReport*, 10> bugReports;
1806 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1807 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001808 return;
1809
Ted Kremenekd49967f2009-04-29 21:58:13 +00001810 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenekcf118d42009-02-04 23:49:09 +00001812 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001813 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001814 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Ted Kremenekd49967f2009-04-29 21:58:13 +00001816 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001817 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001818 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001819 ? exampleReport->getDescription()
1820 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001821 BT.getCategory()));
1822
Ted Kremenek40406fe2010-12-03 06:52:30 +00001823 if (!bugReports.empty())
1824 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Ted Kremenek40406fe2010-12-03 06:52:30 +00001826 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001827 return;
1828
Ted Kremenek072192b2008-04-30 23:47:44 +00001829 // Get the meta data.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001830 std::pair<const char**, const char**> Meta =
1831 exampleReport->getExtraDescriptiveText();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001832 for (const char** s = Meta.first; s != Meta.second; ++s)
1833 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001834
Ted Kremenek3148eb42009-01-24 00:55:43 +00001835 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001836 BugReport::ranges_iterator Beg, End;
1837 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001838 Diagnostic &Diag = getDiagnostic();
1839 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001840
1841 // Search the description for '%', as that will be interpretted as a
1842 // format character by FormatDiagnostics.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001843 llvm::StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001844 unsigned ErrorDiag;
1845 {
1846 llvm::SmallString<512> TmpStr;
1847 llvm::raw_svector_ostream Out(TmpStr);
1848 for (llvm::StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
1849 if (*I == '%')
1850 Out << "%%";
1851 else
1852 Out << *I;
1853
1854 Out.flush();
1855 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1856 }
Ted Kremenek57202072008-07-14 17:40:50 +00001857
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001858 {
1859 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001860 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001861 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001862 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001863
1864 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1865 if (!PD)
1866 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001867
1868 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001869 PathDiagnosticPiece* piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001870 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001871
Ted Kremenek3148eb42009-01-24 00:55:43 +00001872 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1873 D->push_back(piece);
1874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Ted Kremenek3148eb42009-01-24 00:55:43 +00001876 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001877}
Ted Kremenek57202072008-07-14 17:40:50 +00001878
Benjamin Kramerf0171732009-11-29 18:27:55 +00001879void BugReporter::EmitBasicReport(llvm::StringRef name, llvm::StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001880 SourceLocation Loc,
1881 SourceRange* RBeg, unsigned NumRanges) {
1882 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1883}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001884
Benjamin Kramerf0171732009-11-29 18:27:55 +00001885void BugReporter::EmitBasicReport(llvm::StringRef name,
1886 llvm::StringRef category,
1887 llvm::StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001888 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Ted Kremenekcf118d42009-02-04 23:49:09 +00001890 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1891 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001892 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001893 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1894 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1895 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001896}