blob: 431c5d75060163c9ae3218fec477c5c4028c9ed0 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000020#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000021#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
23#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000024#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000025#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000027#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000028#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000029#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000030#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000031
32using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000033using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
Ted Kremenek8966bc12009-05-06 21:39:49 +000035BugReporterVisitor::~BugReporterVisitor() {}
36BugReporterContext::~BugReporterContext() {
37 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I)
38 if ((*I)->isOwnedByReporterContext()) delete *I;
39}
40
Ted Kremenek1b431022010-03-20 18:01:57 +000041void BugReporterContext::addVisitor(BugReporterVisitor* visitor) {
42 if (!visitor)
43 return;
44
45 llvm::FoldingSetNodeID ID;
46 visitor->Profile(ID);
47 void *InsertPos;
48
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000049 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
50 delete visitor;
Ted Kremenek1b431022010-03-20 18:01:57 +000051 return;
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000052 }
Ted Kremenek1b431022010-03-20 18:01:57 +000053
54 CallbacksSet.InsertNode(visitor, InsertPos);
Ted Kremenek3baf6722010-11-24 00:54:37 +000055 Callbacks = F.add(visitor, Callbacks);
Ted Kremenek1b431022010-03-20 18:01:57 +000056}
57
Ted Kremenekcf118d42009-02-04 23:49:09 +000058//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000059// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000060//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000061
Zhongxing Xu7f330852010-04-06 03:01:56 +000062static inline const Stmt* GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000063 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
64 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000065 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000066 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenekb697b102009-02-23 22:44:26 +000068 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000069}
70
Zhongxing Xuc5619d92009-08-06 01:32:16 +000071static inline const ExplodedNode*
72GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000073 return N->pred_empty() ? NULL : *(N->pred_begin());
74}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000075
Zhongxing Xuc5619d92009-08-06 01:32:16 +000076static inline const ExplodedNode*
77GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000079}
80
Zhongxing Xuc5619d92009-08-06 01:32:16 +000081static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000082 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000083 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000084 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000085
Ted Kremenekb697b102009-02-23 22:44:26 +000086 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000087}
88
Zhongxing Xuc5619d92009-08-06 01:32:16 +000089static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000090 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000091 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000092 // Check if the statement is '?' or '&&'/'||'. These are "merges",
93 // not actual statement points.
94 switch (S->getStmtClass()) {
95 case Stmt::ChooseExprClass:
96 case Stmt::ConditionalOperatorClass: continue;
97 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000098 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
99 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000100 continue;
101 break;
102 }
103 default:
104 break;
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenekb7c51522009-07-28 00:07:15 +0000107 // Some expressions don't have locations.
108 if (S->getLocStart().isInvalid())
109 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenekb697b102009-02-23 22:44:26 +0000111 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekb697b102009-02-23 22:44:26 +0000114 return 0;
115}
116
Ted Kremenek5f85e172009-07-22 22:35:28 +0000117static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000118GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000119 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000120 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenekb697b102009-02-23 22:44:26 +0000122 return GetPreviousStmt(N);
123}
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek5f85e172009-07-22 22:35:28 +0000125static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000126GetCurrentOrNextStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000127 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000128 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenekb697b102009-02-23 22:44:26 +0000130 return GetNextStmt(N);
131}
132
133//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000134// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000135//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000136
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000137typedef llvm::DenseMap<const ExplodedNode*,
138const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000139
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000140namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000141class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000142 NodeBackMap& M;
143public:
144 NodeMapClosure(NodeBackMap *m) : M(*m) {}
145 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000147 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000148 NodeBackMap::iterator I = M.find(N);
149 return I == M.end() ? 0 : I->second;
150 }
151};
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000153class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000154 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000155 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000156 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000157 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000158public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000159 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000160 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000161 PathDiagnosticClient *pdc)
162 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000163 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000164 addVisitor(R);
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000167 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Ted Kremenek00605e02009-03-27 20:55:39 +0000169 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000170 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Tom Care212f6d32010-09-16 03:50:38 +0000172 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000173
Tom Care212f6d32010-09-16 03:50:38 +0000174 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000176 const Stmt *getParent(const Stmt *S) {
177 return getParentMap().getParent(S);
178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek8966bc12009-05-06 21:39:49 +0000180 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000181
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000182 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek7dc86642009-03-31 20:22:36 +0000184 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
185 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
186 }
187
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000188 bool supportsLogicalOpControlFlow() const {
189 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000190 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000191};
192} // end anonymous namespace
193
Ted Kremenek00605e02009-03-27 20:55:39 +0000194PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000195PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000196 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000197 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000198
Mike Stump1eb44332009-09-09 15:08:12 +0000199 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000200 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000201}
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek00605e02009-03-27 20:55:39 +0000203PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000204PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000205 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000206
Ted Kremenek143ca222008-05-06 18:11:09 +0000207 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000208 if (os.str().empty())
209 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek00605e02009-03-27 20:55:39 +0000211 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek00605e02009-03-27 20:55:39 +0000213 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000214 os << "Execution continues on line "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000215 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
216 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000217 else {
218 os << "Execution jumps to the end of the ";
219 const Decl *D = N->getLocationContext()->getDecl();
220 if (isa<ObjCMethodDecl>(D))
221 os << "method";
222 else if (isa<FunctionDecl>(D))
223 os << "function";
224 else {
225 assert(isa<BlockDecl>(D));
226 os << "anonymous block";
227 }
228 os << '.';
229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000231 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000232}
233
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000234static bool IsNested(const Stmt *S, ParentMap &PM) {
235 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
236 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000238 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000240 if (Parent)
241 switch (Parent->getStmtClass()) {
242 case Stmt::ForStmtClass:
243 case Stmt::DoStmtClass:
244 case Stmt::WhileStmtClass:
245 return true;
246 default:
247 break;
248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
250 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000251}
252
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000253PathDiagnosticLocation
254PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
255 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000256 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000257 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000258
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000259 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000260 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000262 if (!Parent)
263 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000265 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000266 case Stmt::BinaryOperatorClass: {
267 const BinaryOperator *B = cast<BinaryOperator>(Parent);
268 if (B->isLogicalOp())
269 return PathDiagnosticLocation(S, SMgr);
270 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000271 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000272 case Stmt::CompoundStmtClass:
273 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000274 return PathDiagnosticLocation(S, SMgr);
275 case Stmt::ChooseExprClass:
276 // Similar to '?' if we are referring to condition, just have the edge
277 // point to the entire choose expression.
278 if (cast<ChooseExpr>(Parent)->getCond() == S)
279 return PathDiagnosticLocation(Parent, SMgr);
280 else
Mike Stump1eb44332009-09-09 15:08:12 +0000281 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000282 case Stmt::ConditionalOperatorClass:
283 // For '?', if we are referring to condition, just have the edge point
284 // to the entire '?' expression.
285 if (cast<ConditionalOperator>(Parent)->getCond() == S)
286 return PathDiagnosticLocation(Parent, SMgr);
287 else
Mike Stump1eb44332009-09-09 15:08:12 +0000288 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000289 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000290 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000291 case Stmt::ForStmtClass:
292 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000293 return PathDiagnosticLocation(S, SMgr);
294 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000295 case Stmt::IfStmtClass:
296 if (cast<IfStmt>(Parent)->getCond() != S)
297 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000298 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000299 case Stmt::ObjCForCollectionStmtClass:
300 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
301 return PathDiagnosticLocation(S, SMgr);
302 break;
303 case Stmt::WhileStmtClass:
304 if (cast<WhileStmt>(Parent)->getCond() != S)
305 return PathDiagnosticLocation(S, SMgr);
306 break;
307 default:
308 break;
309 }
310
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000311 S = Parent;
312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000314 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000315
316 // Special case: DeclStmts can appear in for statement declarations, in which
317 // case the ForStmt is the context.
318 if (isa<DeclStmt>(S)) {
319 if (const Stmt *Parent = P.getParent(S)) {
320 switch (Parent->getStmtClass()) {
321 case Stmt::ForStmtClass:
322 case Stmt::ObjCForCollectionStmtClass:
323 return PathDiagnosticLocation(Parent, SMgr);
324 default:
325 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000326 }
327 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000328 }
329 else if (isa<BinaryOperator>(S)) {
330 // Special case: the binary operator represents the initialization
331 // code in a for statement (this can happen when the variable being
332 // initialized is an old variable.
333 if (const ForStmt *FS =
334 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
335 if (FS->getInit() == S)
336 return PathDiagnosticLocation(FS, SMgr);
337 }
338 }
339
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000340 return PathDiagnosticLocation(S, SMgr);
341}
342
Ted Kremenekcf118d42009-02-04 23:49:09 +0000343//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000344// ScanNotableSymbols: closure-like callback for scanning Store bindings.
345//===----------------------------------------------------------------------===//
346
347static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000348GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000349 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek31061982009-03-31 23:00:32 +0000351 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek31061982009-03-31 23:00:32 +0000353 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek31061982009-03-31 23:00:32 +0000355 if (!isa<PostStmt>(P))
356 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek5f85e172009-07-22 22:35:28 +0000358 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek31061982009-03-31 23:00:32 +0000360 if (!DR)
361 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek13976632010-02-08 16:18:51 +0000363 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek31061982009-03-31 23:00:32 +0000365 if (X != Y)
366 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek5f85e172009-07-22 22:35:28 +0000368 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek31061982009-03-31 23:00:32 +0000370 if (!VD)
371 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek31061982009-03-31 23:00:32 +0000373 return VD;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek31061982009-03-31 23:00:32 +0000376 return 0;
377}
378
379namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000380class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000381: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek31061982009-03-31 23:00:32 +0000383 SymbolRef Sym;
384 const GRState* PrevSt;
385 const Stmt* S;
386 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000387 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000388 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000389 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek31061982009-03-31 23:00:32 +0000391public:
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek31061982009-03-31 23:00:32 +0000393 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000394 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000395 PathDiagnostic& pd, BugReporter& br)
396 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek31061982009-03-31 23:00:32 +0000398 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
399 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek31061982009-03-31 23:00:32 +0000401 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 if (ScanSym != Sym)
404 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
406 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000407 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek31061982009-03-31 23:00:32 +0000409 if (X == V) // Same binding?
410 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek31061982009-03-31 23:00:32 +0000412 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000413 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000414 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek31061982009-03-31 23:00:32 +0000418 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
419 if (!B->isAssignmentOp())
420 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek31061982009-03-31 23:00:32 +0000422 // What variable did we assign to?
423 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek31061982009-03-31 23:00:32 +0000425 if (!DR)
426 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek31061982009-03-31 23:00:32 +0000428 VD = dyn_cast<VarDecl>(DR->getDecl());
429 }
430 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
431 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
432 // assume that each DeclStmt has a single Decl. This invariant
433 // holds by contruction in the CFG.
434 VD = dyn_cast<VarDecl>(*DS->decl_begin());
435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 if (!VD)
438 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 // What is the most recently referenced variable with this binding?
441 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Ted Kremenek31061982009-03-31 23:00:32 +0000443 if (!MostRecent)
444 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek31061982009-03-31 23:00:32 +0000446 // Create the diagnostic.
447 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Ted Kremenek31061982009-03-31 23:00:32 +0000449 if (Loc::IsLocType(VD->getType())) {
450 std::string msg = "'" + std::string(VD->getNameAsString()) +
451 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenek31061982009-03-31 23:00:32 +0000453 PD.push_front(new PathDiagnosticEventPiece(L, msg));
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek31061982009-03-31 23:00:32 +0000456 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000457 }
Ted Kremenek31061982009-03-31 23:00:32 +0000458};
459}
460
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000461static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000462 const Stmt* S,
463 SymbolRef Sym, BugReporter& BR,
464 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000466 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000467 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek31061982009-03-31 23:00:32 +0000469 if (!PrevSt)
470 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenek31061982009-03-31 23:00:32 +0000472 // Look at the region bindings of the current state that map to the
473 // specified symbol. Are any of them not in the previous state?
474 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
475 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
476 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
477}
478
479namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000480class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000481: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek31061982009-03-31 23:00:32 +0000483 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000484 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000485 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000486 GRBugReporter& BR;
487 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000490 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000491 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000492 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 bool HandleBinding(StoreManager& SMgr, Store store,
495 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek31061982009-03-31 23:00:32 +0000497 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Ted Kremenek31061982009-03-31 23:00:32 +0000499 if (!ScanSym)
500 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek31061982009-03-31 23:00:32 +0000502 if (!BR.isNotable(ScanSym))
503 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 if (AlreadyProcessed.count(ScanSym))
506 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Ted Kremenek31061982009-03-31 23:00:32 +0000508 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenek31061982009-03-31 23:00:32 +0000510 HandleNotableSymbol(N, S, ScanSym, BR, PD);
511 return true;
512 }
513};
514} // end anonymous namespace
515
516//===----------------------------------------------------------------------===//
517// "Minimal" path diagnostic generation algorithm.
518//===----------------------------------------------------------------------===//
519
Ted Kremenek14856d72009-04-06 23:06:54 +0000520static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
521
Ted Kremenek31061982009-03-31 23:00:32 +0000522static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
523 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000524 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000525
Ted Kremenek31061982009-03-31 23:00:32 +0000526 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000527 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000528 ? NULL : *(N->pred_begin());
529 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000530 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000531 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek31061982009-03-31 23:00:32 +0000533 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Ted Kremenek31061982009-03-31 23:00:32 +0000535 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000536 const CFGBlock* Src = BE->getSrc();
537 const CFGBlock* Dst = BE->getDst();
538 const Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 if (!T)
541 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek31061982009-03-31 23:00:32 +0000543 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 switch (T->getStmtClass()) {
546 default:
547 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Ted Kremenek31061982009-03-31 23:00:32 +0000549 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000550 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000551 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek31061982009-03-31 23:00:32 +0000553 if (!S)
554 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Ted Kremenek31061982009-03-31 23:00:32 +0000556 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000557 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000558 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Ted Kremenek31061982009-03-31 23:00:32 +0000560 os << "Control jumps to line "
561 << End.asLocation().getInstantiationLineNumber();
562 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
563 os.str()));
564 break;
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
567 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000568 // Figure out what case arm we took.
569 std::string sbuf;
570 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000572 if (const Stmt* S = Dst->getLabel()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000573 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Ted Kremenek31061982009-03-31 23:00:32 +0000575 switch (S->getStmtClass()) {
576 default:
577 os << "No cases match in the switch statement. "
578 "Control jumps to line "
579 << End.asLocation().getInstantiationLineNumber();
580 break;
581 case Stmt::DefaultStmtClass:
582 os << "Control jumps to the 'default' case at line "
583 << End.asLocation().getInstantiationLineNumber();
584 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Ted Kremenek31061982009-03-31 23:00:32 +0000586 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000587 os << "Control jumps to 'case ";
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000588 const CaseStmt* Case = cast<CaseStmt>(S);
589 const Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000590
591 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000592 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000594 if (const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000595 // FIXME: Maybe this should be an assertion. Are there cases
596 // were it is not an EnumConstantDecl?
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000597 const EnumConstantDecl* D =
598 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Ted Kremenek31061982009-03-31 23:00:32 +0000600 if (D) {
601 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000602 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000603 }
604 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000605
606 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000607 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000608
Ted Kremenek31061982009-03-31 23:00:32 +0000609 os << ":' at line "
610 << End.asLocation().getInstantiationLineNumber();
611 break;
612 }
613 }
614 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
615 os.str()));
616 }
617 else {
618 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000619 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000620 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
621 os.str()));
622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Ted Kremenek31061982009-03-31 23:00:32 +0000624 break;
625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Ted Kremenek31061982009-03-31 23:00:32 +0000627 case Stmt::BreakStmtClass:
628 case Stmt::ContinueStmtClass: {
629 std::string sbuf;
630 llvm::raw_string_ostream os(sbuf);
631 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
632 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
633 os.str()));
634 break;
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek31061982009-03-31 23:00:32 +0000637 // Determine control-flow for ternary '?'.
638 case Stmt::ConditionalOperatorClass: {
639 std::string sbuf;
640 llvm::raw_string_ostream os(sbuf);
641 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek31061982009-03-31 23:00:32 +0000643 if (*(Src->succ_begin()+1) == Dst)
644 os << "false";
645 else
646 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremenek31061982009-03-31 23:00:32 +0000648 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek31061982009-03-31 23:00:32 +0000650 if (const Stmt *S = End.asStmt())
651 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
654 os.str()));
655 break;
656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 // Determine control-flow for short-circuited '&&' and '||'.
659 case Stmt::BinaryOperatorClass: {
660 if (!PDB.supportsLogicalOpControlFlow())
661 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000663 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000664 std::string sbuf;
665 llvm::raw_string_ostream os(sbuf);
666 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000667
John McCall2de56d12010-08-25 11:45:40 +0000668 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000669 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek31061982009-03-31 23:00:32 +0000671 if (*(Src->succ_begin()+1) == Dst) {
672 os << "false";
673 PathDiagnosticLocation End(B->getLHS(), SMgr);
674 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
675 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
676 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000677 }
Ted Kremenek31061982009-03-31 23:00:32 +0000678 else {
679 os << "true";
680 PathDiagnosticLocation Start(B->getLHS(), SMgr);
681 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
682 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
683 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000684 }
Ted Kremenek31061982009-03-31 23:00:32 +0000685 }
686 else {
John McCall2de56d12010-08-25 11:45:40 +0000687 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000688 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek31061982009-03-31 23:00:32 +0000690 if (*(Src->succ_begin()+1) == Dst) {
691 os << "false";
692 PathDiagnosticLocation Start(B->getLHS(), SMgr);
693 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
694 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000695 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000696 }
697 else {
698 os << "true";
699 PathDiagnosticLocation End(B->getLHS(), SMgr);
700 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
701 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000702 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000703 }
704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Ted Kremenek31061982009-03-31 23:00:32 +0000706 break;
707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
709 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000710 if (*(Src->succ_begin()) == Dst) {
711 std::string sbuf;
712 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek31061982009-03-31 23:00:32 +0000714 os << "Loop condition is true. ";
715 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenek31061982009-03-31 23:00:32 +0000717 if (const Stmt *S = End.asStmt())
718 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Ted Kremenek31061982009-03-31 23:00:32 +0000720 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
721 os.str()));
722 }
723 else {
724 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek31061982009-03-31 23:00:32 +0000726 if (const Stmt *S = End.asStmt())
727 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek31061982009-03-31 23:00:32 +0000729 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
730 "Loop condition is false. Exiting loop"));
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek31061982009-03-31 23:00:32 +0000733 break;
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Ted Kremenek31061982009-03-31 23:00:32 +0000736 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000737 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000738 if (*(Src->succ_begin()+1) == Dst) {
739 std::string sbuf;
740 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek31061982009-03-31 23:00:32 +0000742 os << "Loop condition is false. ";
743 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
744 if (const Stmt *S = End.asStmt())
745 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Ted Kremenek31061982009-03-31 23:00:32 +0000747 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
748 os.str()));
749 }
750 else {
751 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
752 if (const Stmt *S = End.asStmt())
753 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek31061982009-03-31 23:00:32 +0000755 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000756 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Ted Kremenek31061982009-03-31 23:00:32 +0000759 break;
760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek31061982009-03-31 23:00:32 +0000762 case Stmt::IfStmtClass: {
763 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek31061982009-03-31 23:00:32 +0000765 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 if (*(Src->succ_begin()+1) == Dst)
769 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000770 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000771 else
Ted Kremenek31061982009-03-31 23:00:32 +0000772 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000773 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek31061982009-03-31 23:00:32 +0000775 break;
776 }
777 }
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000780 if (NextNode) {
781 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
782 E = PDB.visitor_end(); I!=E; ++I) {
783 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
784 PD.push_front(p);
785 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
788 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000789 // Scan the region bindings, and see if a "notable" symbol has a new
790 // lval binding.
791 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
792 PDB.getStateManager().iterBindings(N->getState(), SNS);
793 }
794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek14856d72009-04-06 23:06:54 +0000796 // After constructing the full PathDiagnostic, do a pass over it to compact
797 // PathDiagnosticPieces that occur within a macro.
798 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000799}
800
801//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000802// "Extensive" PathDiagnostic generation.
803//===----------------------------------------------------------------------===//
804
805static bool IsControlFlowExpr(const Stmt *S) {
806 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000808 if (!E)
809 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
811 E = E->IgnoreParenCasts();
812
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000813 if (isa<ConditionalOperator>(E))
814 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000816 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
817 if (B->isLogicalOp())
818 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
820 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000821}
822
Ted Kremenek14856d72009-04-06 23:06:54 +0000823namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000824class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000825 bool IsDead;
826public:
827 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
828 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000829
830 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000831 bool isDead() const { return IsDead; }
832};
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000834class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000835 std::vector<ContextLocation> CLocs;
836 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000837 PathDiagnostic &PD;
838 PathDiagnosticBuilder &PDB;
839 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000841 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek14856d72009-04-06 23:06:54 +0000843 bool containsLocation(const PathDiagnosticLocation &Container,
844 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek14856d72009-04-06 23:06:54 +0000846 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek9650cf32009-05-11 21:42:34 +0000848 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
849 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000850 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000851 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000852 while (1) {
853 // Adjust the location for some expressions that are best referenced
854 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000855 switch (S->getStmtClass()) {
856 default:
857 break;
858 case Stmt::ParenExprClass:
859 S = cast<ParenExpr>(S)->IgnoreParens();
860 firstCharOnly = true;
861 continue;
862 case Stmt::ConditionalOperatorClass:
863 S = cast<ConditionalOperator>(S)->getCond();
864 firstCharOnly = true;
865 continue;
866 case Stmt::ChooseExprClass:
867 S = cast<ChooseExpr>(S)->getCond();
868 firstCharOnly = true;
869 continue;
870 case Stmt::BinaryOperatorClass:
871 S = cast<BinaryOperator>(S)->getLHS();
872 firstCharOnly = true;
873 continue;
874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Ted Kremenek9650cf32009-05-11 21:42:34 +0000876 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek9650cf32009-05-11 21:42:34 +0000879 if (S != Original)
880 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek9650cf32009-05-11 21:42:34 +0000883 if (firstCharOnly)
884 L = PathDiagnosticLocation(L.asLocation());
885
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000886 return L;
887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Ted Kremenek14856d72009-04-06 23:06:54 +0000889 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000890 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000891 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000892 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000893 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000894 CLocs.pop_back();
895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenek14856d72009-04-06 23:06:54 +0000897public:
898 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
899 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Ted Kremeneka301a672009-04-22 18:16:20 +0000901 // If the PathDiagnostic already has pieces, add the enclosing statement
902 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000903 if (!PD.empty()) {
904 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremenek14856d72009-04-06 23:06:54 +0000906 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000907 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000908 }
909 }
910
911 ~EdgeBuilder() {
912 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremeneka301a672009-04-22 18:16:20 +0000914 // Finally, add an initial edge from the start location of the first
915 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000916 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
917 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000918 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000919 if (!CS->body_empty()) {
920 SourceLocation Loc = (*CS->body_begin())->getLocStart();
921 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Ted Kremenek14856d72009-04-06 23:06:54 +0000924 }
925
926 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000928 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek14856d72009-04-06 23:06:54 +0000930 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000931 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000932};
Ted Kremenek14856d72009-04-06 23:06:54 +0000933} // end anonymous namespace
934
935
936PathDiagnosticLocation
937EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
938 if (const Stmt *S = L.asStmt()) {
939 if (IsControlFlowExpr(S))
940 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
942 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Ted Kremenek14856d72009-04-06 23:06:54 +0000945 return L;
946}
947
948bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
949 const PathDiagnosticLocation &Containee) {
950
951 if (Container == Containee)
952 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek14856d72009-04-06 23:06:54 +0000954 if (Container.asDecl())
955 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Ted Kremenek14856d72009-04-06 23:06:54 +0000957 if (const Stmt *S = Containee.asStmt())
958 if (const Stmt *ContainerS = Container.asStmt()) {
959 while (S) {
960 if (S == ContainerS)
961 return true;
962 S = PDB.getParent(S);
963 }
964 return false;
965 }
966
967 // Less accurate: compare using source ranges.
968 SourceRange ContainerR = Container.asRange();
969 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Ted Kremenek14856d72009-04-06 23:06:54 +0000971 SourceManager &SM = PDB.getSourceManager();
972 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
973 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
974 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
975 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremenek14856d72009-04-06 23:06:54 +0000977 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
978 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
979 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
980 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Ted Kremenek14856d72009-04-06 23:06:54 +0000982 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000983 assert(ContaineeBegLine <= ContaineeEndLine);
984
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 return (ContainerBegLine <= ContaineeBegLine &&
986 ContainerEndLine >= ContaineeEndLine &&
987 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +0000988 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +0000989 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
990 (ContainerEndLine != ContaineeEndLine ||
991 SM.getInstantiationColumnNumber(ContainerREnd) >=
992 SM.getInstantiationColumnNumber(ContainerREnd)));
993}
994
Ted Kremenek14856d72009-04-06 23:06:54 +0000995void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
996 if (!PrevLoc.isValid()) {
997 PrevLoc = NewLoc;
998 return;
999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001001 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1002 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001004 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001005 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek14856d72009-04-06 23:06:54 +00001007 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001008 if (NewLocClean.asLocation().getInstantiationLoc() ==
1009 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001010 return;
1011
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001012 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1013 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001014}
1015
1016void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremeneka301a672009-04-22 18:16:20 +00001018 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1019 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Ted Kremenek14856d72009-04-06 23:06:54 +00001021 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1022
1023 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001024 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek14856d72009-04-06 23:06:54 +00001026 // Is the top location context the same as the one for the new location?
1027 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001028 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001029 if (IsConsumedExpr(TopContextLoc) &&
1030 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001031 TopContextLoc.markDead();
1032
Ted Kremenek14856d72009-04-06 23:06:54 +00001033 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001034 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001035
1036 return;
1037 }
1038
1039 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001040 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001041 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001043 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001044 CLocs.push_back(ContextLocation(CLoc, true));
1045 return;
1046 }
1047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Ted Kremenek14856d72009-04-06 23:06:54 +00001049 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001050 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001051 }
1052
1053 // Context does not contain the location. Flush it.
1054 popLocation();
1055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001057 // If we reach here, there is no enclosing context. Just add the edge.
1058 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001059}
1060
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001061bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1062 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1063 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001065 return false;
1066}
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremeneke1baed32009-05-05 23:13:38 +00001068void EdgeBuilder::addExtendedContext(const Stmt *S) {
1069 if (!S)
1070 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001071
1072 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001073 while (Parent) {
1074 if (isa<CompoundStmt>(Parent))
1075 Parent = PDB.getParent(Parent);
1076 else
1077 break;
1078 }
1079
1080 if (Parent) {
1081 switch (Parent->getStmtClass()) {
1082 case Stmt::DoStmtClass:
1083 case Stmt::ObjCAtSynchronizedStmtClass:
1084 addContext(Parent);
1085 default:
1086 break;
1087 }
1088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremeneke1baed32009-05-05 23:13:38 +00001090 addContext(S);
1091}
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Ted Kremenek14856d72009-04-06 23:06:54 +00001093void EdgeBuilder::addContext(const Stmt *S) {
1094 if (!S)
1095 return;
1096
1097 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Ted Kremenek14856d72009-04-06 23:06:54 +00001099 while (!CLocs.empty()) {
1100 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1101
1102 // Is the top location context the same as the one for the new location?
1103 if (TopContextLoc == L)
1104 return;
1105
1106 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001107 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001108 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001109 }
1110
1111 // Context does not contain the location. Flush it.
1112 popLocation();
1113 }
1114
1115 CLocs.push_back(L);
1116}
1117
1118static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1119 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001120 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001121 EdgeBuilder EB(PD, PDB);
1122
Zhongxing Xu41ca1342010-03-23 05:13:26 +00001123 const ExplodedNode* NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001124 while (NextNode) {
1125 N = NextNode;
1126 NextNode = GetPredecessorNode(N);
1127 ProgramPoint P = N->getLocation();
1128
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001129 do {
1130 // Block edges.
1131 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1132 const CFGBlock &Blk = *BE->getSrc();
1133 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001135 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001136 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001137 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001138 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001140 if (!Term) {
1141 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1142 CS = dyn_cast<CompoundStmt>(FS->getBody());
1143 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001144 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001147 PathDiagnosticEventPiece *p =
1148 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001149 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 EB.addEdge(p->getLocation(), true);
1152 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001154 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001155 PathDiagnosticLocation BL(CS->getRBracLoc(),
1156 PDB.getSourceManager());
1157 BL = PathDiagnosticLocation(BL.asLocation());
1158 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001159 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001162 if (Term)
1163 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001165 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001166 }
1167
Mike Stump1eb44332009-09-09 15:08:12 +00001168 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001169 if (CFGStmt S = BE->getFirstElement().getAs<CFGStmt>()) {
1170 if (IsControlFlowExpr(S)) {
1171 // Add the proper context for '&&', '||', and '?'.
1172 EB.addContext(S);
1173 }
1174 else
1175 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001176 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001177
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001178 break;
1179 }
1180 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001182 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001183 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Ted Kremenek8966bc12009-05-06 21:39:49 +00001185 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1186 E = PDB.visitor_end(); I!=E; ++I) {
1187 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1188 const PathDiagnosticLocation &Loc = p->getLocation();
1189 EB.addEdge(Loc, true);
1190 PD.push_front(p);
1191 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001192 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001195 }
1196}
1197
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001198//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001199// Methods for BugType and subclasses.
1200//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001201BugType::~BugType() {
1202 // Free up the equivalence class objects. Observe that we get a pointer to
1203 // the object first before incrementing the iterator, as destroying the
1204 // node before doing so means we will read from freed memory.
1205 for (iterator I = begin(), E = end(); I !=E; ) {
1206 BugReportEquivClass *EQ = &*I;
1207 ++I;
1208 delete EQ;
1209 }
1210}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001211void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001212
Ted Kremenekcf118d42009-02-04 23:49:09 +00001213//===----------------------------------------------------------------------===//
1214// Methods for BugReport and subclasses.
1215//===----------------------------------------------------------------------===//
1216BugReport::~BugReport() {}
1217RangedBugReport::~RangedBugReport() {}
1218
Mike Stump1eb44332009-09-09 15:08:12 +00001219const Stmt* BugReport::getStmt() const {
Tom Care212f6d32010-09-16 03:50:38 +00001220 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001221 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremenekcf118d42009-02-04 23:49:09 +00001223 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001224 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001225 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001226 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001227 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001228 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001229 S = GetStmt(ProgP);
1230
1231 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001232}
1233
1234PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001235BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001236 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001238 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Ted Kremenek61f3e052008-04-03 04:42:52 +00001240 if (!S)
1241 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001242
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001243 BugReport::ranges_iterator Beg, End;
1244 llvm::tie(Beg, End) = getRanges();
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001245 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001247 // Only add the statement itself as a range if we didn't specify any
1248 // special ranges for this report.
1249 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1250 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001252 for (; Beg != End; ++Beg)
1253 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Ted Kremenek61f3e052008-04-03 04:42:52 +00001255 return P;
1256}
1257
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001258std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
1259BugReport::getRanges() const {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001260 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001261 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001262 assert(R.isValid());
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001263 return std::make_pair(&R, &R+1);
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001264 }
1265 else
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001266 return std::make_pair(ranges_iterator(), ranges_iterator());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001267}
1268
Mike Stump1eb44332009-09-09 15:08:12 +00001269SourceLocation BugReport::getLocation() const {
Tom Care212f6d32010-09-16 03:50:38 +00001270 if (ErrorNode)
1271 if (const Stmt* S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001272 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001273 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001274 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001275 // For binary operators, return the location of the operator.
1276 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1277 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001278
Ted Kremenekcf118d42009-02-04 23:49:09 +00001279 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001280 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001281
1282 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001283}
1284
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001285PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1286 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001287 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001288 return NULL;
1289}
1290
Ted Kremenekcf118d42009-02-04 23:49:09 +00001291//===----------------------------------------------------------------------===//
1292// Methods for BugReporter and subclasses.
1293//===----------------------------------------------------------------------===//
1294
1295BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001296 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001297}
1298
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001299GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001300BugReporterData::~BugReporterData() {}
1301
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001302ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001303
1304GRStateManager&
1305GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1306
1307BugReporter::~BugReporter() { FlushReports(); }
1308
1309void BugReporter::FlushReports() {
1310 if (BugTypes.isEmpty())
1311 return;
1312
1313 // First flush the warnings for each BugType. This may end up creating new
1314 // warnings and new BugTypes. Because ImmutableSet is a functional data
1315 // structure, we do not need to worry about the iterators being invalidated.
1316 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1317 const_cast<BugType*>(*I)->FlushReports(*this);
1318
1319 // Iterate through BugTypes a second time. BugTypes may have been updated
1320 // with new BugType objects and new warnings.
1321 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1322 BugType *BT = const_cast<BugType*>(*I);
1323
1324 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1325 SetTy& EQClasses = BT->EQClasses;
1326
1327 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1328 BugReportEquivClass& EQ = *EI;
1329 FlushReport(EQ);
1330 }
Mike Stump1eb44332009-09-09 15:08:12 +00001331
1332 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001333 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001334 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001335
1336 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001337 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001338}
1339
1340//===----------------------------------------------------------------------===//
1341// PathDiagnostics generation.
1342//===----------------------------------------------------------------------===//
1343
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001344static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001345 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001346MakeReportGraph(const ExplodedGraph* G,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001347 llvm::SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Ted Kremenekcf118d42009-02-04 23:49:09 +00001349 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001350 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001351 // error node unless there are two or more error nodes with the same minimum
1352 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001353 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001354 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001355
1356 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001357 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1358 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Ted Kremenekcf118d42009-02-04 23:49:09 +00001360 // Create owning pointers for GTrim and NMap just to ensure that they are
1361 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001362 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001363 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenekcf118d42009-02-04 23:49:09 +00001365 // Find the (first) error node in the trimmed graph. We just need to consult
1366 // the node map (NMap) which maps from nodes in the original graph to nodes
1367 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001368
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001369 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001370 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001371 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001372
Ted Kremenek40406fe2010-12-03 06:52:30 +00001373 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1374 const ExplodedNode *originalNode = nodes[nodeIndex];
1375 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001376 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001377 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001378 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenek938332c2009-05-16 01:11:58 +00001381 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001382
1383 // Create a new (third!) graph with a single path. This is the graph
1384 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001385 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenek10aa5542009-03-12 23:41:59 +00001387 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001388 // to the root node, and then construct a new graph that contains only
1389 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001390 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001392 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001393 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001395 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001396 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001397 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001399 if (Visited.find(Node) != Visited.end())
1400 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001402 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001404 if (Node->pred_empty()) {
1405 Root = Node;
1406 break;
1407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001409 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001410 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001411 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Ted Kremenek938332c2009-05-16 01:11:58 +00001414 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenek10aa5542009-03-12 23:41:59 +00001416 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001417 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001418 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001419 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001420 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001422 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001423 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001424 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001425 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001427 // Create the equivalent node in the new graph with the same state
1428 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001429 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001431 // Store the mapping to the original node.
1432 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1433 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001434 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001436 // Link up the new node with the previous node.
1437 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001438 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001440 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001442 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001443 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001444 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001445 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001447 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001448 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001451 // Find the next successor node. We choose the node that is marked
1452 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001453 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1454 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001455 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001457 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001459 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001461 if (I == Visited.end())
1462 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001464 if (!N || I->second < MinVal) {
1465 N = *SI;
1466 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001467 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001468 }
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek938332c2009-05-16 01:11:58 +00001470 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenek938332c2009-05-16 01:11:58 +00001473 assert(First);
1474
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001475 return std::make_pair(std::make_pair(GNew, BM),
1476 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001477}
1478
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001479/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1480/// and collapses PathDiagosticPieces that are expanded by macros.
1481static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1482 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1483 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001485 typedef std::vector<PathDiagnosticPiece*>
1486 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001488 MacroStackTy MacroStack;
1489 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001491 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1492 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001493 const FullSourceLoc Loc = I->getLocation().asLocation();
1494
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001495 // Determine the instantiation location, which is the location we group
1496 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001497 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001498 SM.getInstantiationLoc(Loc) :
1499 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001501 if (Loc.isFileID()) {
1502 MacroStack.clear();
1503 Pieces.push_back(&*I);
1504 continue;
1505 }
1506
1507 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001509 // Is the PathDiagnosticPiece within the same macro group?
1510 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1511 MacroStack.back().first->push_back(&*I);
1512 continue;
1513 }
1514
1515 // We aren't in the same group. Are we descending into a new macro
1516 // or are part of an old one?
1517 PathDiagnosticMacroPiece *MacroGroup = 0;
1518
1519 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1520 SM.getInstantiationLoc(Loc) :
1521 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001523 // Walk the entire macro stack.
1524 while (!MacroStack.empty()) {
1525 if (InstantiationLoc == MacroStack.back().second) {
1526 MacroGroup = MacroStack.back().first;
1527 break;
1528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001530 if (ParentInstantiationLoc == MacroStack.back().second) {
1531 MacroGroup = MacroStack.back().first;
1532 break;
1533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001535 MacroStack.pop_back();
1536 }
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001538 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1539 // Create a new macro group and add it to the stack.
1540 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1541
1542 if (MacroGroup)
1543 MacroGroup->push_back(NewGroup);
1544 else {
1545 assert(InstantiationLoc.isFileID());
1546 Pieces.push_back(NewGroup);
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001549 MacroGroup = NewGroup;
1550 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1551 }
1552
1553 // Finally, add the PathDiagnosticPiece to the group.
1554 MacroGroup->push_back(&*I);
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001557 // Now take the pieces and construct a new PathDiagnostic.
1558 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001560 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1561 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1562 if (!MP->containsEvent()) {
1563 delete MP;
1564 continue;
1565 }
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001567 PD.push_back(*I);
1568 }
1569}
1570
Ted Kremenek7dc86642009-03-31 20:22:36 +00001571void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001572 llvm::SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Ted Kremenek40406fe2010-12-03 06:52:30 +00001574 assert(!bugReports.empty());
1575 llvm::SmallVector<const ExplodedNode *, 10> errorNodes;
1576 for (llvm::SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
1577 E = bugReports.end(); I != E; ++I) {
1578 errorNodes.push_back((*I)->getErrorNode());
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001581 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001582 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001583 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001584 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001585 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Ted Kremenekcf118d42009-02-04 23:49:09 +00001587 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001588 assert(GPair.second.second < bugReports.size());
1589 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001590 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001592 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001593 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001594 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
1596 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001597 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Ted Kremenek8966bc12009-05-06 21:39:49 +00001599 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001600 PD.push_back(Piece);
1601 else
1602 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Ted Kremenekff7f7362010-03-20 18:02:01 +00001604 // Register node visitors.
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001605 R->registerInitialVisitors(PDB, N);
Ted Kremenekff7f7362010-03-20 18:02:01 +00001606 bugreporter::registerNilReceiverVisitor(PDB);
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Ted Kremenek7dc86642009-03-31 20:22:36 +00001608 switch (PDB.getGenerationScheme()) {
1609 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001610 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001611 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001612 case PathDiagnosticClient::Minimal:
1613 GenerateMinimalPathDiagnostic(PD, PDB, N);
1614 break;
1615 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001616}
1617
Ted Kremenekcf118d42009-02-04 23:49:09 +00001618void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001619 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001620}
1621
Mike Stump1eb44332009-09-09 15:08:12 +00001622void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001623 // Compute the bug report's hash to determine its equivalence class.
1624 llvm::FoldingSetNodeID ID;
1625 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001626
1627 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001628 BugType& BT = R->getBugType();
1629 Register(&BT);
1630 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001631 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1632
Ted Kremenekcf118d42009-02-04 23:49:09 +00001633 if (!EQ) {
1634 EQ = new BugReportEquivClass(R);
1635 BT.EQClasses.InsertNode(EQ, InsertPos);
1636 }
1637 else
1638 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001639}
1640
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001641
1642//===----------------------------------------------------------------------===//
1643// Emitting reports in equivalence classes.
1644//===----------------------------------------------------------------------===//
1645
1646namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001647struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001648 const ExplodedNode *N;
1649 ExplodedNode::const_succ_iterator I, E;
1650
1651 FRIEC_WLItem(const ExplodedNode *n)
1652 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1653};
1654}
1655
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001656static BugReport *
1657FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001658 llvm::SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001659
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001660 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1661 assert(I != E);
1662 BugReport *R = *I;
1663 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001664
Ted Kremenek40406fe2010-12-03 06:52:30 +00001665 // If we don't need to suppress any of the nodes because they are
1666 // post-dominated by a sink, simply add all the nodes in the equivalence class
1667 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001668 if (!BT.isSuppressOnSink()) {
1669 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Tom Care212f6d32010-09-16 03:50:38 +00001670 const ExplodedNode* N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001671 if (N) {
1672 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001673 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001674 }
1675 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001676 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001677 }
1678
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001679 // For bug reports that should be suppressed when all paths are post-dominated
1680 // by a sink node, iterate through the reports in the equivalence class
1681 // until we find one that isn't post-dominated (if one exists). We use a
1682 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1683 // this as a recursive function, but we don't want to risk blowing out the
1684 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001685 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001686
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001687 for (; I != E; ++I) {
1688 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001689 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001690
Ted Kremenek40406fe2010-12-03 06:52:30 +00001691 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001692 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001693 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001694 assert(false &&
1695 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001696 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001697 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001698 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001699 if (errorNode->succ_empty()) {
1700 bugReports.push_back(R);
1701 if (!exampleReport)
1702 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001703 continue;
1704 }
1705
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001706 // At this point we know that 'N' is not a sink and it has at least one
1707 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1708 typedef FRIEC_WLItem WLItem;
1709 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1710 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1711
1712 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001713 WL.push_back(errorNode);
1714 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001715
1716 while (!WL.empty()) {
1717 WLItem &WI = WL.back();
1718 assert(!WI.N->succ_empty());
1719
1720 for (; WI.I != WI.E; ++WI.I) {
1721 const ExplodedNode *Succ = *WI.I;
1722 // End-of-path node?
1723 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001724 // If we found an end-of-path node that is not a sink.
1725 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001726 bugReports.push_back(R);
1727 if (!exampleReport)
1728 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001729 WL.clear();
1730 break;
1731 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001732 // Found a sink? Continue on to the next successor.
1733 continue;
1734 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001735 // Mark the successor as visited. If it hasn't been explored,
1736 // enqueue it to the DFS worklist.
1737 unsigned &mark = Visited[Succ];
1738 if (!mark) {
1739 mark = 1;
1740 WL.push_back(Succ);
1741 break;
1742 }
1743 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001744
1745 // The worklist may have been cleared at this point. First
1746 // check if it is empty before checking the last item.
1747 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001748 WL.pop_back();
1749 }
1750 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001751
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001752 // ExampleReport will be NULL if all the nodes in the equivalence class
1753 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001754 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001755}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001756
1757//===----------------------------------------------------------------------===//
1758// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1759// uses global state, which eventually should go elsewhere.
1760//===----------------------------------------------------------------------===//
1761namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001762class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001763 llvm::FoldingSetNodeID ID;
1764public:
1765 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1766 ID.AddString(R->getBugType().getName());
1767 ID.AddString(R->getBugType().getCategory());
1768 ID.AddString(R->getDescription());
1769 ID.AddInteger(R->getLocation().getRawEncoding());
1770 PD->Profile(ID);
1771 }
1772
1773 void Profile(llvm::FoldingSetNodeID &id) {
1774 id = ID;
1775 }
1776
1777 llvm::FoldingSetNodeID &getID() { return ID; }
1778};
1779}
1780
1781static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1782 // FIXME: Eventually this diagnostic cache should reside in something
1783 // like AnalysisManager instead of being a static variable. This is
1784 // really unsafe in the long term.
1785 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1786 static DiagnosticCache DC;
1787
1788 void *InsertPos;
1789 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1790
1791 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1792 delete Item;
1793 return true;
1794 }
1795
1796 DC.InsertNode(Item, InsertPos);
1797 return false;
1798}
1799
Ted Kremenekcf118d42009-02-04 23:49:09 +00001800void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001801 llvm::SmallVector<BugReport*, 10> bugReports;
1802 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1803 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001804 return;
1805
Ted Kremenekd49967f2009-04-29 21:58:13 +00001806 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Ted Kremenekcf118d42009-02-04 23:49:09 +00001808 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001809 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001810 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenekd49967f2009-04-29 21:58:13 +00001812 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001813 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001814 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001815 ? exampleReport->getDescription()
1816 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001817 BT.getCategory()));
1818
Ted Kremenek40406fe2010-12-03 06:52:30 +00001819 if (!bugReports.empty())
1820 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Ted Kremenek40406fe2010-12-03 06:52:30 +00001822 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001823 return;
1824
Ted Kremenek072192b2008-04-30 23:47:44 +00001825 // Get the meta data.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001826 std::pair<const char**, const char**> Meta =
1827 exampleReport->getExtraDescriptiveText();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001828 for (const char** s = Meta.first; s != Meta.second; ++s)
1829 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001830
Ted Kremenek3148eb42009-01-24 00:55:43 +00001831 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001832 BugReport::ranges_iterator Beg, End;
1833 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001834 Diagnostic &Diag = getDiagnostic();
1835 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001836
1837 // Search the description for '%', as that will be interpretted as a
1838 // format character by FormatDiagnostics.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001839 llvm::StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001840 unsigned ErrorDiag;
1841 {
1842 llvm::SmallString<512> TmpStr;
1843 llvm::raw_svector_ostream Out(TmpStr);
1844 for (llvm::StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
1845 if (*I == '%')
1846 Out << "%%";
1847 else
1848 Out << *I;
1849
1850 Out.flush();
1851 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1852 }
Ted Kremenek57202072008-07-14 17:40:50 +00001853
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001854 {
1855 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001856 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001857 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001858 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001859
1860 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1861 if (!PD)
1862 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001863
1864 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001865 PathDiagnosticPiece* piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001866 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001867
Ted Kremenek3148eb42009-01-24 00:55:43 +00001868 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1869 D->push_back(piece);
1870 }
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Ted Kremenek3148eb42009-01-24 00:55:43 +00001872 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001873}
Ted Kremenek57202072008-07-14 17:40:50 +00001874
Benjamin Kramerf0171732009-11-29 18:27:55 +00001875void BugReporter::EmitBasicReport(llvm::StringRef name, llvm::StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001876 SourceLocation Loc,
1877 SourceRange* RBeg, unsigned NumRanges) {
1878 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1879}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001880
Benjamin Kramerf0171732009-11-29 18:27:55 +00001881void BugReporter::EmitBasicReport(llvm::StringRef name,
1882 llvm::StringRef category,
1883 llvm::StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001884 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Ted Kremenekcf118d42009-02-04 23:49:09 +00001886 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1887 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001888 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001889 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1890 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1891 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001892}