blob: 943d9fcb715db3b526164b9fd1d44406398a87b9 [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
Ted Kremenek9c378f72011-08-12 23:37:29 +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 Kremenek9c378f72011-08-12 23:37:29 +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*
Ted Kremenek9c378f72011-08-12 23:37:29 +000072GetPredecessorNode(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*
Ted Kremenek9c378f72011-08-12 23:37:29 +000077GetSuccessorNode(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
Ted Kremenek9c378f72011-08-12 23:37:29 +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
Ted Kremenek9c378f72011-08-12 23:37:29 +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*
Ted Kremenek9c378f72011-08-12 23:37:29 +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*
Ted Kremenek9c378f72011-08-12 23:37:29 +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
Ted Kremenek9c378f72011-08-12 23:37:29 +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
Ted Kremenek9c378f72011-08-12 23:37:29 +0000168 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremenek9c378f72011-08-12 23:37:29 +0000170 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
171 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
Ted Kremenek9c378f72011-08-12 23:37:29 +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 Kremenek9c378f72011-08-12 23:37:29 +0000205PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
206 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000207
Ted Kremenek143ca222008-05-06 18:11:09 +0000208 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000209 if (os.str().empty())
210 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek00605e02009-03-27 20:55:39 +0000212 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek00605e02009-03-27 20:55:39 +0000214 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000215 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000216 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000217 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000218 else {
219 os << "Execution jumps to the end of the ";
220 const Decl *D = N->getLocationContext()->getDecl();
221 if (isa<ObjCMethodDecl>(D))
222 os << "method";
223 else if (isa<FunctionDecl>(D))
224 os << "function";
225 else {
226 assert(isa<BlockDecl>(D));
227 os << "anonymous block";
228 }
229 os << '.';
230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000232 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000233}
234
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000235static bool IsNested(const Stmt *S, ParentMap &PM) {
236 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
237 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000239 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000241 if (Parent)
242 switch (Parent->getStmtClass()) {
243 case Stmt::ForStmtClass:
244 case Stmt::DoStmtClass:
245 case Stmt::WhileStmtClass:
246 return true;
247 default:
248 break;
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
251 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000252}
253
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000254PathDiagnosticLocation
255PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000256 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
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000349static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
350 ProgramStateManager& VMgr,
351 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 Kremenek9c378f72011-08-12 23:37:29 +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 Kremenek9c378f72011-08-12 23:37:29 +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;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000386 const ProgramState *PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000387 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000388 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +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 Kremenek18c66fd2011-08-15 22:09:50 +0000395 NotableSymbolHandler(SymbolRef sym,
396 const ProgramState *prevst,
397 const Stmt *s,
398 ProgramStateManager& vmgr,
399 const ExplodedNode *pred,
400 PathDiagnostic& pd,
401 BugReporter& br)
402 : Sym(sym),
403 PrevSt(prevst),
404 S(s),
405 VMgr(vmgr),
406 Pred(pred),
407 PD(pd),
408 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremenek31061982009-03-31 23:00:32 +0000410 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
411 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek31061982009-03-31 23:00:32 +0000413 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek31061982009-03-31 23:00:32 +0000415 if (ScanSym != Sym)
416 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
418 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000419 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek31061982009-03-31 23:00:32 +0000421 if (X == V) // Same binding?
422 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek31061982009-03-31 23:00:32 +0000424 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000425 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000426 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek31061982009-03-31 23:00:32 +0000428 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek31061982009-03-31 23:00:32 +0000430 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
431 if (!B->isAssignmentOp())
432 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000435 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 if (!DR)
438 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 VD = dyn_cast<VarDecl>(DR->getDecl());
441 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000442 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000443 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
444 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000445 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000446 VD = dyn_cast<VarDecl>(*DS->decl_begin());
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Ted Kremenek31061982009-03-31 23:00:32 +0000449 if (!VD)
450 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek31061982009-03-31 23:00:32 +0000452 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000453 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek31061982009-03-31 23:00:32 +0000455 if (!MostRecent)
456 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek31061982009-03-31 23:00:32 +0000458 // Create the diagnostic.
459 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000461 if (Loc::isLocType(VD->getType())) {
Ted Kremenek31061982009-03-31 23:00:32 +0000462 std::string msg = "'" + std::string(VD->getNameAsString()) +
463 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek31061982009-03-31 23:00:32 +0000465 PD.push_front(new PathDiagnosticEventPiece(L, msg));
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek31061982009-03-31 23:00:32 +0000468 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000469 }
Ted Kremenek31061982009-03-31 23:00:32 +0000470};
471}
472
Ted Kremenek9c378f72011-08-12 23:37:29 +0000473static void HandleNotableSymbol(const ExplodedNode *N,
474 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000475 SymbolRef Sym, BugReporter& BR,
476 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek9c378f72011-08-12 23:37:29 +0000478 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000479 const ProgramState *PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek31061982009-03-31 23:00:32 +0000481 if (!PrevSt)
482 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek31061982009-03-31 23:00:32 +0000484 // Look at the region bindings of the current state that map to the
485 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000486 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000487 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
488 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
489}
490
491namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000492class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000493: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremenek31061982009-03-31 23:00:32 +0000495 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000496 const ExplodedNode *N;
497 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000498 GRBugReporter& BR;
499 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000502 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000503 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000504 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Ted Kremenek31061982009-03-31 23:00:32 +0000506 bool HandleBinding(StoreManager& SMgr, Store store,
507 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenek31061982009-03-31 23:00:32 +0000509 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Ted Kremenek31061982009-03-31 23:00:32 +0000511 if (!ScanSym)
512 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek31061982009-03-31 23:00:32 +0000514 if (!BR.isNotable(ScanSym))
515 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek31061982009-03-31 23:00:32 +0000517 if (AlreadyProcessed.count(ScanSym))
518 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek31061982009-03-31 23:00:32 +0000520 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Ted Kremenek31061982009-03-31 23:00:32 +0000522 HandleNotableSymbol(N, S, ScanSym, BR, PD);
523 return true;
524 }
525};
526} // end anonymous namespace
527
528//===----------------------------------------------------------------------===//
529// "Minimal" path diagnostic generation algorithm.
530//===----------------------------------------------------------------------===//
531
Ted Kremenek14856d72009-04-06 23:06:54 +0000532static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
533
Ted Kremenek31061982009-03-31 23:00:32 +0000534static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
535 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000536 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000537
Ted Kremenek31061982009-03-31 23:00:32 +0000538 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000539 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000540 ? NULL : *(N->pred_begin());
541 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000542 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000543 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek9c378f72011-08-12 23:37:29 +0000547 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
548 const CFGBlock *Src = BE->getSrc();
549 const CFGBlock *Dst = BE->getDst();
550 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek31061982009-03-31 23:00:32 +0000552 if (!T)
553 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek31061982009-03-31 23:00:32 +0000555 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Ted Kremenek31061982009-03-31 23:00:32 +0000557 switch (T->getStmtClass()) {
558 default:
559 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek31061982009-03-31 23:00:32 +0000561 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000562 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000563 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenek31061982009-03-31 23:00:32 +0000565 if (!S)
566 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek31061982009-03-31 23:00:32 +0000568 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000569 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000570 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Ted Kremenek31061982009-03-31 23:00:32 +0000572 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000573 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000574 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
575 os.str()));
576 break;
577 }
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000580 // Figure out what case arm we took.
581 std::string sbuf;
582 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Ted Kremenek9c378f72011-08-12 23:37:29 +0000584 if (const Stmt *S = Dst->getLabel()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000585 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Ted Kremenek31061982009-03-31 23:00:32 +0000587 switch (S->getStmtClass()) {
588 default:
589 os << "No cases match in the switch statement. "
590 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000591 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000592 break;
593 case Stmt::DefaultStmtClass:
594 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000595 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000596 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremenek31061982009-03-31 23:00:32 +0000598 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000599 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000600 const CaseStmt *Case = cast<CaseStmt>(S);
601 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000602
603 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000604 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Ted Kremenek9c378f72011-08-12 23:37:29 +0000606 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000607 // FIXME: Maybe this should be an assertion. Are there cases
608 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000609 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000610 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenek31061982009-03-31 23:00:32 +0000612 if (D) {
613 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000614 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000615 }
616 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000617
618 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000619 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000620
Ted Kremenek31061982009-03-31 23:00:32 +0000621 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000622 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000623 break;
624 }
625 }
626 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
627 os.str()));
628 }
629 else {
630 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000631 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000632 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
633 os.str()));
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek31061982009-03-31 23:00:32 +0000636 break;
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 case Stmt::BreakStmtClass:
640 case Stmt::ContinueStmtClass: {
641 std::string sbuf;
642 llvm::raw_string_ostream os(sbuf);
643 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
644 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
645 os.str()));
646 break;
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000650 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000651 case Stmt::ConditionalOperatorClass: {
652 std::string sbuf;
653 llvm::raw_string_ostream os(sbuf);
654 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek31061982009-03-31 23:00:32 +0000656 if (*(Src->succ_begin()+1) == Dst)
657 os << "false";
658 else
659 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek31061982009-03-31 23:00:32 +0000661 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek31061982009-03-31 23:00:32 +0000663 if (const Stmt *S = End.asStmt())
664 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Ted Kremenek31061982009-03-31 23:00:32 +0000666 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
667 os.str()));
668 break;
669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek31061982009-03-31 23:00:32 +0000671 // Determine control-flow for short-circuited '&&' and '||'.
672 case Stmt::BinaryOperatorClass: {
673 if (!PDB.supportsLogicalOpControlFlow())
674 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000676 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000677 std::string sbuf;
678 llvm::raw_string_ostream os(sbuf);
679 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000680
John McCall2de56d12010-08-25 11:45:40 +0000681 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000682 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremenek31061982009-03-31 23:00:32 +0000684 if (*(Src->succ_begin()+1) == Dst) {
685 os << "false";
686 PathDiagnosticLocation End(B->getLHS(), SMgr);
687 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
688 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
689 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000690 }
Ted Kremenek31061982009-03-31 23:00:32 +0000691 else {
692 os << "true";
693 PathDiagnosticLocation Start(B->getLHS(), SMgr);
694 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
695 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
696 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000697 }
Ted Kremenek31061982009-03-31 23:00:32 +0000698 }
699 else {
John McCall2de56d12010-08-25 11:45:40 +0000700 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000701 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Ted Kremenek31061982009-03-31 23:00:32 +0000703 if (*(Src->succ_begin()+1) == Dst) {
704 os << "false";
705 PathDiagnosticLocation Start(B->getLHS(), SMgr);
706 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
707 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000708 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000709 }
710 else {
711 os << "true";
712 PathDiagnosticLocation End(B->getLHS(), SMgr);
713 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
714 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000715 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000716 }
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 break;
720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
722 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000723 if (*(Src->succ_begin()) == Dst) {
724 std::string sbuf;
725 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek31061982009-03-31 23:00:32 +0000727 os << "Loop condition is true. ";
728 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek31061982009-03-31 23:00:32 +0000730 if (const Stmt *S = End.asStmt())
731 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek31061982009-03-31 23:00:32 +0000733 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
734 os.str()));
735 }
736 else {
737 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek31061982009-03-31 23:00:32 +0000739 if (const Stmt *S = End.asStmt())
740 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek31061982009-03-31 23:00:32 +0000742 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
743 "Loop condition is false. Exiting loop"));
744 }
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek31061982009-03-31 23:00:32 +0000746 break;
747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek31061982009-03-31 23:00:32 +0000749 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000750 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000751 if (*(Src->succ_begin()+1) == Dst) {
752 std::string sbuf;
753 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek31061982009-03-31 23:00:32 +0000755 os << "Loop condition is false. ";
756 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
757 if (const Stmt *S = End.asStmt())
758 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek31061982009-03-31 23:00:32 +0000760 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
761 os.str()));
762 }
763 else {
764 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
765 if (const Stmt *S = End.asStmt())
766 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek31061982009-03-31 23:00:32 +0000768 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000769 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek31061982009-03-31 23:00:32 +0000772 break;
773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek31061982009-03-31 23:00:32 +0000775 case Stmt::IfStmtClass: {
776 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek31061982009-03-31 23:00:32 +0000778 if (const Stmt *S = End.asStmt())
779 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Ted Kremenek31061982009-03-31 23:00:32 +0000781 if (*(Src->succ_begin()+1) == Dst)
782 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000783 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000784 else
Ted Kremenek31061982009-03-31 23:00:32 +0000785 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000786 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenek31061982009-03-31 23:00:32 +0000788 break;
789 }
790 }
791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000793 if (NextNode) {
794 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
795 E = PDB.visitor_end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000796 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB))
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000797 PD.push_front(p);
798 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Ted Kremenek9c378f72011-08-12 23:37:29 +0000801 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000802 // Scan the region bindings, and see if a "notable" symbol has a new
803 // lval binding.
804 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
805 PDB.getStateManager().iterBindings(N->getState(), SNS);
806 }
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek14856d72009-04-06 23:06:54 +0000809 // After constructing the full PathDiagnostic, do a pass over it to compact
810 // PathDiagnosticPieces that occur within a macro.
811 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000812}
813
814//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000815// "Extensive" PathDiagnostic generation.
816//===----------------------------------------------------------------------===//
817
818static bool IsControlFlowExpr(const Stmt *S) {
819 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000821 if (!E)
822 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
824 E = E->IgnoreParenCasts();
825
John McCall56ca35d2011-02-17 10:25:35 +0000826 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000827 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000829 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
830 if (B->isLogicalOp())
831 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000832
833 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000834}
835
Ted Kremenek14856d72009-04-06 23:06:54 +0000836namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000837class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000838 bool IsDead;
839public:
840 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
841 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000842
843 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000844 bool isDead() const { return IsDead; }
845};
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000847class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000848 std::vector<ContextLocation> CLocs;
849 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000850 PathDiagnostic &PD;
851 PathDiagnosticBuilder &PDB;
852 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000854 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Ted Kremenek14856d72009-04-06 23:06:54 +0000856 bool containsLocation(const PathDiagnosticLocation &Container,
857 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Ted Kremenek14856d72009-04-06 23:06:54 +0000859 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Ted Kremenek9650cf32009-05-11 21:42:34 +0000861 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
862 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000863 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000864 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000865 while (1) {
866 // Adjust the location for some expressions that are best referenced
867 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000868 switch (S->getStmtClass()) {
869 default:
870 break;
871 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000872 case Stmt::GenericSelectionExprClass:
873 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000874 firstCharOnly = true;
875 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000876 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000877 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000878 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000879 firstCharOnly = true;
880 continue;
881 case Stmt::ChooseExprClass:
882 S = cast<ChooseExpr>(S)->getCond();
883 firstCharOnly = true;
884 continue;
885 case Stmt::BinaryOperatorClass:
886 S = cast<BinaryOperator>(S)->getLHS();
887 firstCharOnly = true;
888 continue;
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek9650cf32009-05-11 21:42:34 +0000891 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek9650cf32009-05-11 21:42:34 +0000894 if (S != Original)
895 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek9650cf32009-05-11 21:42:34 +0000898 if (firstCharOnly)
899 L = PathDiagnosticLocation(L.asLocation());
900
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000901 return L;
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Ted Kremenek14856d72009-04-06 23:06:54 +0000904 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000905 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000906 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000907 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000908 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000909 CLocs.pop_back();
910 }
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Ted Kremenek14856d72009-04-06 23:06:54 +0000912public:
913 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
914 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Ted Kremeneka301a672009-04-22 18:16:20 +0000916 // If the PathDiagnostic already has pieces, add the enclosing statement
917 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000918 if (!PD.empty()) {
919 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenek14856d72009-04-06 23:06:54 +0000921 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000922 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000923 }
924 }
925
926 ~EdgeBuilder() {
927 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremeneka301a672009-04-22 18:16:20 +0000929 // Finally, add an initial edge from the start location of the first
930 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000931 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
932 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000933 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000934 if (!CS->body_empty()) {
935 SourceLocation Loc = (*CS->body_begin())->getLocStart();
936 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Ted Kremenek14856d72009-04-06 23:06:54 +0000939 }
940
941 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000943 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Ted Kremenek14856d72009-04-06 23:06:54 +0000945 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000946 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000947};
Ted Kremenek14856d72009-04-06 23:06:54 +0000948} // end anonymous namespace
949
950
951PathDiagnosticLocation
952EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
953 if (const Stmt *S = L.asStmt()) {
954 if (IsControlFlowExpr(S))
955 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
957 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek14856d72009-04-06 23:06:54 +0000960 return L;
961}
962
963bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
964 const PathDiagnosticLocation &Containee) {
965
966 if (Container == Containee)
967 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Ted Kremenek14856d72009-04-06 23:06:54 +0000969 if (Container.asDecl())
970 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Ted Kremenek14856d72009-04-06 23:06:54 +0000972 if (const Stmt *S = Containee.asStmt())
973 if (const Stmt *ContainerS = Container.asStmt()) {
974 while (S) {
975 if (S == ContainerS)
976 return true;
977 S = PDB.getParent(S);
978 }
979 return false;
980 }
981
982 // Less accurate: compare using source ranges.
983 SourceRange ContainerR = Container.asRange();
984 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenek14856d72009-04-06 23:06:54 +0000986 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000987 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
988 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
989 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
990 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chandler Carruth64211622011-07-25 21:09:52 +0000992 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
993 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
994 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
995 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Ted Kremenek14856d72009-04-06 23:06:54 +0000997 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000998 assert(ContaineeBegLine <= ContaineeEndLine);
999
Ted Kremenek14856d72009-04-06 23:06:54 +00001000 return (ContainerBegLine <= ContaineeBegLine &&
1001 ContainerEndLine >= ContaineeEndLine &&
1002 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001003 SM.getExpansionColumnNumber(ContainerRBeg) <=
1004 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001005 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001006 SM.getExpansionColumnNumber(ContainerREnd) >=
1007 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001008}
1009
Ted Kremenek14856d72009-04-06 23:06:54 +00001010void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1011 if (!PrevLoc.isValid()) {
1012 PrevLoc = NewLoc;
1013 return;
1014 }
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001016 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1017 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001019 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001020 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Ted Kremenek14856d72009-04-06 23:06:54 +00001022 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001023 if (NewLocClean.asLocation().getExpansionLoc() ==
1024 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 return;
1026
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001027 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1028 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001029}
1030
1031void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Ted Kremeneka301a672009-04-22 18:16:20 +00001033 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1034 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Ted Kremenek14856d72009-04-06 23:06:54 +00001036 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1037
1038 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Ted Kremenek14856d72009-04-06 23:06:54 +00001041 // Is the top location context the same as the one for the new location?
1042 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001043 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001044 if (IsConsumedExpr(TopContextLoc) &&
1045 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001046 TopContextLoc.markDead();
1047
Ted Kremenek14856d72009-04-06 23:06:54 +00001048 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001049 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001050
1051 return;
1052 }
1053
1054 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001055 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001056 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001058 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001059 CLocs.push_back(ContextLocation(CLoc, true));
1060 return;
1061 }
1062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek14856d72009-04-06 23:06:54 +00001064 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001065 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001066 }
1067
1068 // Context does not contain the location. Flush it.
1069 popLocation();
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001072 // If we reach here, there is no enclosing context. Just add the edge.
1073 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001074}
1075
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001076bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1077 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1078 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001080 return false;
1081}
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Ted Kremeneke1baed32009-05-05 23:13:38 +00001083void EdgeBuilder::addExtendedContext(const Stmt *S) {
1084 if (!S)
1085 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
1087 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001088 while (Parent) {
1089 if (isa<CompoundStmt>(Parent))
1090 Parent = PDB.getParent(Parent);
1091 else
1092 break;
1093 }
1094
1095 if (Parent) {
1096 switch (Parent->getStmtClass()) {
1097 case Stmt::DoStmtClass:
1098 case Stmt::ObjCAtSynchronizedStmtClass:
1099 addContext(Parent);
1100 default:
1101 break;
1102 }
1103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Ted Kremeneke1baed32009-05-05 23:13:38 +00001105 addContext(S);
1106}
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Ted Kremenek14856d72009-04-06 23:06:54 +00001108void EdgeBuilder::addContext(const Stmt *S) {
1109 if (!S)
1110 return;
1111
1112 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Ted Kremenek14856d72009-04-06 23:06:54 +00001114 while (!CLocs.empty()) {
1115 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1116
1117 // Is the top location context the same as the one for the new location?
1118 if (TopContextLoc == L)
1119 return;
1120
1121 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001122 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001123 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001124 }
1125
1126 // Context does not contain the location. Flush it.
1127 popLocation();
1128 }
1129
1130 CLocs.push_back(L);
1131}
1132
1133static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1134 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001135 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001136 EdgeBuilder EB(PD, PDB);
1137
Ted Kremenek9c378f72011-08-12 23:37:29 +00001138 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001139 while (NextNode) {
1140 N = NextNode;
1141 NextNode = GetPredecessorNode(N);
1142 ProgramPoint P = N->getLocation();
1143
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001144 do {
1145 // Block edges.
1146 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1147 const CFGBlock &Blk = *BE->getSrc();
1148 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001150 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001151 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001152 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001153 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001155 if (!Term) {
1156 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1157 CS = dyn_cast<CompoundStmt>(FS->getBody());
1158 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001159 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001162 PathDiagnosticEventPiece *p =
1163 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001164 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001166 EB.addEdge(p->getLocation(), true);
1167 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001169 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001170 PathDiagnosticLocation BL(CS->getRBracLoc(),
1171 PDB.getSourceManager());
1172 BL = PathDiagnosticLocation(BL.asLocation());
1173 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001174 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001175 }
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001177 if (Term)
1178 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001180 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001181 }
1182
Mike Stump1eb44332009-09-09 15:08:12 +00001183 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001184 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1185 const Stmt *stmt = S->getStmt();
1186 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001187 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001188 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001189 }
1190 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001191 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001192 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001193
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001194 break;
1195 }
1196 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001198 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001199 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Ted Kremenek8966bc12009-05-06 21:39:49 +00001201 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1202 E = PDB.visitor_end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001203 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001204 const PathDiagnosticLocation &Loc = p->getLocation();
1205 EB.addEdge(Loc, true);
1206 PD.push_front(p);
1207 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001208 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001211 }
1212}
1213
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001214//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001215// Methods for BugType and subclasses.
1216//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001217BugType::~BugType() { }
1218
Ted Kremenekcf118d42009-02-04 23:49:09 +00001219void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001220
Ted Kremenekcf118d42009-02-04 23:49:09 +00001221//===----------------------------------------------------------------------===//
1222// Methods for BugReport and subclasses.
1223//===----------------------------------------------------------------------===//
1224BugReport::~BugReport() {}
1225RangedBugReport::~RangedBugReport() {}
1226
Ted Kremenek9c378f72011-08-12 23:37:29 +00001227const Stmt *BugReport::getStmt() const {
Tom Care212f6d32010-09-16 03:50:38 +00001228 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001229 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Ted Kremenek9c378f72011-08-12 23:37:29 +00001231 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001232 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001233 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001234 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001235 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001236 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001237 S = GetStmt(ProgP);
1238
1239 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001240}
1241
1242PathDiagnosticPiece*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001243BugReport::getEndPath(BugReporterContext &BRC,
1244 const ExplodedNode *EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Anna Zakseb3058a2011-08-03 01:57:49 +00001246 const ProgramPoint &PP = EndPathNode->getLocation();
1247 PathDiagnosticLocation L;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Anna Zakseb3058a2011-08-03 01:57:49 +00001249 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
1250 const CFGBlock *block = BE->getBlock();
1251 if (block->getBlockID() == 0) {
1252 L = PathDiagnosticLocation(
1253 EndPathNode->getLocationContext()->getDecl()->getBodyRBrace(),
1254 BRC.getSourceManager());
1255 }
1256 }
1257
1258 if (!L.isValid()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001259 const Stmt *S = getStmt();
Anna Zakseb3058a2011-08-03 01:57:49 +00001260
1261 if (!S)
1262 return NULL;
1263
1264 L = PathDiagnosticLocation(S, BRC.getSourceManager());
1265 }
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001266
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001267 BugReport::ranges_iterator Beg, End;
1268 llvm::tie(Beg, End) = getRanges();
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001270 // Only add the statement itself as a range if we didn't specify any
1271 // special ranges for this report.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001272 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L, getDescription(),
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001273 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001275 for (; Beg != End; ++Beg)
1276 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Ted Kremenek61f3e052008-04-03 04:42:52 +00001278 return P;
1279}
1280
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001281std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
1282BugReport::getRanges() const {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001283 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001284 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001285 assert(R.isValid());
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001286 return std::make_pair(&R, &R+1);
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001287 }
1288 else
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001289 return std::make_pair(ranges_iterator(), ranges_iterator());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001290}
1291
Mike Stump1eb44332009-09-09 15:08:12 +00001292SourceLocation BugReport::getLocation() const {
Tom Care212f6d32010-09-16 03:50:38 +00001293 if (ErrorNode)
Ted Kremenek9c378f72011-08-12 23:37:29 +00001294 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001295 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001296 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001297 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001298 // For binary operators, return the location of the operator.
1299 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1300 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001301
Ted Kremenekcf118d42009-02-04 23:49:09 +00001302 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001303 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001304
1305 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001306}
1307
Ted Kremenek9c378f72011-08-12 23:37:29 +00001308PathDiagnosticPiece *BugReport::VisitNode(const ExplodedNode *N,
1309 const ExplodedNode *PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001310 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001311 return NULL;
1312}
1313
Ted Kremenekcf118d42009-02-04 23:49:09 +00001314//===----------------------------------------------------------------------===//
1315// Methods for BugReporter and subclasses.
1316//===----------------------------------------------------------------------===//
1317
1318BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001319 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001320}
1321
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001322GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001323BugReporterData::~BugReporterData() {}
1324
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001325ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001326
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001327ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001328GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1329
1330BugReporter::~BugReporter() { FlushReports(); }
1331
1332void BugReporter::FlushReports() {
1333 if (BugTypes.isEmpty())
1334 return;
1335
1336 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001337 // warnings and new BugTypes.
1338 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1339 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001340 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001341 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001342 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001343 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001344 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001345 const_cast<BugType*>(*I)->FlushReports(*this);
1346
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001347 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1348 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1349 BugReportEquivClass& EQ = *EI;
1350 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001351 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001352
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001353 // BugReporter owns and deletes only BugTypes created implicitly through
1354 // EmitBasicReport.
1355 // FIXME: There are leaks from checkers that assume that the BugTypes they
1356 // create will be destroyed by the BugReporter.
1357 for (llvm::StringMap<BugType*>::iterator
1358 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1359 delete I->second;
1360
Ted Kremenekcf118d42009-02-04 23:49:09 +00001361 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001362 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001363}
1364
1365//===----------------------------------------------------------------------===//
1366// PathDiagnostics generation.
1367//===----------------------------------------------------------------------===//
1368
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001369static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001370 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001371MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001372 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Ted Kremenekcf118d42009-02-04 23:49:09 +00001374 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001375 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376 // error node unless there are two or more error nodes with the same minimum
1377 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001378 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001379 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001380
1381 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001382 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1383 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Ted Kremenekcf118d42009-02-04 23:49:09 +00001385 // Create owning pointers for GTrim and NMap just to ensure that they are
1386 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001387 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001388 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Ted Kremenekcf118d42009-02-04 23:49:09 +00001390 // Find the (first) error node in the trimmed graph. We just need to consult
1391 // the node map (NMap) which maps from nodes in the original graph to nodes
1392 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001393
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001394 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001395 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001396 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001397
Ted Kremenek40406fe2010-12-03 06:52:30 +00001398 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1399 const ExplodedNode *originalNode = nodes[nodeIndex];
1400 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001401 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001402 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001403 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001404 }
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Ted Kremenek938332c2009-05-16 01:11:58 +00001406 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001407
1408 // Create a new (third!) graph with a single path. This is the graph
1409 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001410 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Ted Kremenek10aa5542009-03-12 23:41:59 +00001412 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001413 // to the root node, and then construct a new graph that contains only
1414 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001415 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001417 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001418 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001420 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001421 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001422 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001424 if (Visited.find(Node) != Visited.end())
1425 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001427 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001429 if (Node->pred_empty()) {
1430 Root = Node;
1431 break;
1432 }
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001434 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001435 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001436 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001437 }
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremenek938332c2009-05-16 01:11:58 +00001439 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Ted Kremenek10aa5542009-03-12 23:41:59 +00001441 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001442 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001443 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001444 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001445 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001447 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001448 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001449 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001450 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001452 // Create the equivalent node in the new graph with the same state
1453 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001454 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001456 // Store the mapping to the original node.
1457 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1458 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001459 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001461 // Link up the new node with the previous node.
1462 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001463 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001465 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001467 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001468 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001469 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001470 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001471 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001472 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001473 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001476 // Find the next successor node. We choose the node that is marked
1477 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001478 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1479 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001480 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001482 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001486 if (I == Visited.end())
1487 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001489 if (!N || I->second < MinVal) {
1490 N = *SI;
1491 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001492 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Ted Kremenek938332c2009-05-16 01:11:58 +00001495 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001496 }
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek938332c2009-05-16 01:11:58 +00001498 assert(First);
1499
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001500 return std::make_pair(std::make_pair(GNew, BM),
1501 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001502}
1503
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001504/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1505/// and collapses PathDiagosticPieces that are expanded by macros.
1506static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1507 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1508 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001510 typedef std::vector<PathDiagnosticPiece*>
1511 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001513 MacroStackTy MacroStack;
1514 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001516 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1517 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001518 const FullSourceLoc Loc = I->getLocation().asLocation();
1519
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001520 // Determine the instantiation location, which is the location we group
1521 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001522 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001523 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001524 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001526 if (Loc.isFileID()) {
1527 MacroStack.clear();
1528 Pieces.push_back(&*I);
1529 continue;
1530 }
1531
1532 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001534 // Is the PathDiagnosticPiece within the same macro group?
1535 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1536 MacroStack.back().first->push_back(&*I);
1537 continue;
1538 }
1539
1540 // We aren't in the same group. Are we descending into a new macro
1541 // or are part of an old one?
1542 PathDiagnosticMacroPiece *MacroGroup = 0;
1543
1544 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001545 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001546 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001548 // Walk the entire macro stack.
1549 while (!MacroStack.empty()) {
1550 if (InstantiationLoc == MacroStack.back().second) {
1551 MacroGroup = MacroStack.back().first;
1552 break;
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001555 if (ParentInstantiationLoc == MacroStack.back().second) {
1556 MacroGroup = MacroStack.back().first;
1557 break;
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001560 MacroStack.pop_back();
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001563 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1564 // Create a new macro group and add it to the stack.
1565 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1566
1567 if (MacroGroup)
1568 MacroGroup->push_back(NewGroup);
1569 else {
1570 assert(InstantiationLoc.isFileID());
1571 Pieces.push_back(NewGroup);
1572 }
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001574 MacroGroup = NewGroup;
1575 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1576 }
1577
1578 // Finally, add the PathDiagnosticPiece to the group.
1579 MacroGroup->push_back(&*I);
1580 }
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001582 // Now take the pieces and construct a new PathDiagnostic.
1583 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001585 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1586 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1587 if (!MP->containsEvent()) {
1588 delete MP;
1589 continue;
1590 }
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001592 PD.push_back(*I);
1593 }
1594}
1595
Ted Kremenek7dc86642009-03-31 20:22:36 +00001596void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001597 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Ted Kremenek40406fe2010-12-03 06:52:30 +00001599 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001600 SmallVector<const ExplodedNode *, 10> errorNodes;
1601 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001602 E = bugReports.end(); I != E; ++I) {
1603 errorNodes.push_back((*I)->getErrorNode());
1604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001606 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001607 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001608 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001609 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001610 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Ted Kremenekcf118d42009-02-04 23:49:09 +00001612 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001613 assert(GPair.second.second < bugReports.size());
1614 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001615 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001617 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001618 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001619 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001620
1621 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001622 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremenek9c378f72011-08-12 23:37:29 +00001624 if (PathDiagnosticPiece *Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001625 PD.push_back(Piece);
1626 else
1627 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremenekff7f7362010-03-20 18:02:01 +00001629 // Register node visitors.
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001630 R->registerInitialVisitors(PDB, N);
Ted Kremenekff7f7362010-03-20 18:02:01 +00001631 bugreporter::registerNilReceiverVisitor(PDB);
Ted Kremenek993124e2011-08-06 06:54:45 +00001632 bugreporter::registerConditionVisitor(PDB);
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Ted Kremenek7dc86642009-03-31 20:22:36 +00001634 switch (PDB.getGenerationScheme()) {
1635 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001636 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001637 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001638 case PathDiagnosticClient::Minimal:
1639 GenerateMinimalPathDiagnostic(PD, PDB, N);
1640 break;
1641 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001642}
1643
Ted Kremenekcf118d42009-02-04 23:49:09 +00001644void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001645 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001646}
1647
Mike Stump1eb44332009-09-09 15:08:12 +00001648void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001649 // Compute the bug report's hash to determine its equivalence class.
1650 llvm::FoldingSetNodeID ID;
1651 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001652
1653 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001654 BugType& BT = R->getBugType();
1655 Register(&BT);
1656 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001657 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenekcf118d42009-02-04 23:49:09 +00001659 if (!EQ) {
1660 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001661 EQClasses.InsertNode(EQ, InsertPos);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001662 }
1663 else
1664 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001665}
1666
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001667
1668//===----------------------------------------------------------------------===//
1669// Emitting reports in equivalence classes.
1670//===----------------------------------------------------------------------===//
1671
1672namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001673struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001674 const ExplodedNode *N;
1675 ExplodedNode::const_succ_iterator I, E;
1676
1677 FRIEC_WLItem(const ExplodedNode *n)
1678 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1679};
1680}
1681
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001682static BugReport *
1683FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001684 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001685
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001686 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1687 assert(I != E);
1688 BugReport *R = *I;
1689 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001690
Ted Kremenek40406fe2010-12-03 06:52:30 +00001691 // If we don't need to suppress any of the nodes because they are
1692 // post-dominated by a sink, simply add all the nodes in the equivalence class
1693 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001694 if (!BT.isSuppressOnSink()) {
1695 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001696 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001697 if (N) {
1698 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001699 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001700 }
1701 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001702 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001703 }
1704
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001705 // For bug reports that should be suppressed when all paths are post-dominated
1706 // by a sink node, iterate through the reports in the equivalence class
1707 // until we find one that isn't post-dominated (if one exists). We use a
1708 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1709 // this as a recursive function, but we don't want to risk blowing out the
1710 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001711 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001712
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001713 for (; I != E; ++I) {
1714 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001715 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001716
Ted Kremenek40406fe2010-12-03 06:52:30 +00001717 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001718 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001719 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001720 assert(false &&
1721 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001722 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001723 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001724 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001725 if (errorNode->succ_empty()) {
1726 bugReports.push_back(R);
1727 if (!exampleReport)
1728 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001729 continue;
1730 }
1731
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001732 // At this point we know that 'N' is not a sink and it has at least one
1733 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1734 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001735 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001736 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1737
1738 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001739 WL.push_back(errorNode);
1740 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001741
1742 while (!WL.empty()) {
1743 WLItem &WI = WL.back();
1744 assert(!WI.N->succ_empty());
1745
1746 for (; WI.I != WI.E; ++WI.I) {
1747 const ExplodedNode *Succ = *WI.I;
1748 // End-of-path node?
1749 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001750 // If we found an end-of-path node that is not a sink.
1751 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001752 bugReports.push_back(R);
1753 if (!exampleReport)
1754 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001755 WL.clear();
1756 break;
1757 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001758 // Found a sink? Continue on to the next successor.
1759 continue;
1760 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001761 // Mark the successor as visited. If it hasn't been explored,
1762 // enqueue it to the DFS worklist.
1763 unsigned &mark = Visited[Succ];
1764 if (!mark) {
1765 mark = 1;
1766 WL.push_back(Succ);
1767 break;
1768 }
1769 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001770
1771 // The worklist may have been cleared at this point. First
1772 // check if it is empty before checking the last item.
1773 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001774 WL.pop_back();
1775 }
1776 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001777
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001778 // ExampleReport will be NULL if all the nodes in the equivalence class
1779 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001780 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001781}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001782
1783//===----------------------------------------------------------------------===//
1784// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1785// uses global state, which eventually should go elsewhere.
1786//===----------------------------------------------------------------------===//
1787namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001788class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001789 llvm::FoldingSetNodeID ID;
1790public:
1791 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1792 ID.AddString(R->getBugType().getName());
1793 ID.AddString(R->getBugType().getCategory());
1794 ID.AddString(R->getDescription());
1795 ID.AddInteger(R->getLocation().getRawEncoding());
1796 PD->Profile(ID);
1797 }
1798
1799 void Profile(llvm::FoldingSetNodeID &id) {
1800 id = ID;
1801 }
1802
1803 llvm::FoldingSetNodeID &getID() { return ID; }
1804};
1805}
1806
1807static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1808 // FIXME: Eventually this diagnostic cache should reside in something
1809 // like AnalysisManager instead of being a static variable. This is
1810 // really unsafe in the long term.
1811 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1812 static DiagnosticCache DC;
1813
1814 void *InsertPos;
1815 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1816
1817 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1818 delete Item;
1819 return true;
1820 }
1821
1822 DC.InsertNode(Item, InsertPos);
1823 return false;
1824}
1825
Ted Kremenekcf118d42009-02-04 23:49:09 +00001826void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001827 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001828 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1829 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001830 return;
1831
Ted Kremenekd49967f2009-04-29 21:58:13 +00001832 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Ted Kremenekcf118d42009-02-04 23:49:09 +00001834 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001835 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001836 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Ted Kremenekd49967f2009-04-29 21:58:13 +00001838 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001839 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001840 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001841 ? exampleReport->getDescription()
1842 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001843 BT.getCategory()));
1844
Ted Kremenek40406fe2010-12-03 06:52:30 +00001845 if (!bugReports.empty())
1846 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Ted Kremenek40406fe2010-12-03 06:52:30 +00001848 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001849 return;
1850
Ted Kremenek072192b2008-04-30 23:47:44 +00001851 // Get the meta data.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001852 std::pair<const char**, const char**> Meta =
1853 exampleReport->getExtraDescriptiveText();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001854 for (const char** s = Meta.first; s != Meta.second; ++s)
1855 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001856
Ted Kremenek3148eb42009-01-24 00:55:43 +00001857 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001858 BugReport::ranges_iterator Beg, End;
1859 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001860 Diagnostic &Diag = getDiagnostic();
1861 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001862
1863 // Search the description for '%', as that will be interpretted as a
1864 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001865 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001866 unsigned ErrorDiag;
1867 {
1868 llvm::SmallString<512> TmpStr;
1869 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001870 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001871 if (*I == '%')
1872 Out << "%%";
1873 else
1874 Out << *I;
1875
1876 Out.flush();
1877 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1878 }
Ted Kremenek57202072008-07-14 17:40:50 +00001879
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001880 {
1881 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001882 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001883 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001884 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001885
1886 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1887 if (!PD)
1888 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001889
1890 if (D->empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001891 PathDiagnosticPiece *piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001892 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001893
Ted Kremenek3148eb42009-01-24 00:55:43 +00001894 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1895 D->push_back(piece);
1896 }
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Ted Kremenek3148eb42009-01-24 00:55:43 +00001898 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001899}
Ted Kremenek57202072008-07-14 17:40:50 +00001900
Chris Lattner5f9e2722011-07-23 10:55:15 +00001901void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001902 SourceLocation Loc,
1903 SourceRange* RBeg, unsigned NumRanges) {
1904 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1905}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001906
Chris Lattner5f9e2722011-07-23 10:55:15 +00001907void BugReporter::EmitBasicReport(StringRef name,
1908 StringRef category,
1909 StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001910 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001912 // 'BT' is owned by BugReporter.
1913 BugType *BT = getBugTypeForName(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001914 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001915 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1916 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1917 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001918}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001919
Chris Lattner5f9e2722011-07-23 10:55:15 +00001920BugType *BugReporter::getBugTypeForName(StringRef name,
1921 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001922 llvm::SmallString<136> fullDesc;
1923 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1924 llvm::StringMapEntry<BugType *> &
1925 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1926 BugType *BT = entry.getValue();
1927 if (!BT) {
1928 BT = new BugType(name, category);
1929 entry.setValue(BT);
1930 }
1931 return BT;
1932}