blob: 2dcdb6177fc71ec7bb22b8dde6bd499f2dbd4b81 [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 Kremenek6b676302010-01-25 17:10:22 +000015#include "clang/Checker/BugReporter/BugReporter.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000016#include "clang/Checker/BugReporter/BugType.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/GRExprEngine.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 Kremenek6b676302010-01-25 17:10:22 +000025#include "clang/Checker/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;
33
Ted Kremenek8966bc12009-05-06 21:39:49 +000034BugReporterVisitor::~BugReporterVisitor() {}
35BugReporterContext::~BugReporterContext() {
36 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I)
37 if ((*I)->isOwnedByReporterContext()) delete *I;
38}
39
Ted Kremenek1b431022010-03-20 18:01:57 +000040void BugReporterContext::addVisitor(BugReporterVisitor* visitor) {
41 if (!visitor)
42 return;
43
44 llvm::FoldingSetNodeID ID;
45 visitor->Profile(ID);
46 void *InsertPos;
47
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000048 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
49 delete visitor;
Ted Kremenek1b431022010-03-20 18:01:57 +000050 return;
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000051 }
Ted Kremenek1b431022010-03-20 18:01:57 +000052
53 CallbacksSet.InsertNode(visitor, InsertPos);
Ted Kremenek3baf6722010-11-24 00:54:37 +000054 Callbacks = F.add(visitor, Callbacks);
Ted Kremenek1b431022010-03-20 18:01:57 +000055}
56
Ted Kremenekcf118d42009-02-04 23:49:09 +000057//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000058// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000059//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000060
Zhongxing Xu7f330852010-04-06 03:01:56 +000061static inline const Stmt* GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000062 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
63 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000064 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000065 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekb697b102009-02-23 22:44:26 +000067 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000068}
69
Zhongxing Xuc5619d92009-08-06 01:32:16 +000070static inline const ExplodedNode*
71GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000072 return N->pred_empty() ? NULL : *(N->pred_begin());
73}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000074
Zhongxing Xuc5619d92009-08-06 01:32:16 +000075static inline const ExplodedNode*
76GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000077 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000078}
79
Zhongxing Xuc5619d92009-08-06 01:32:16 +000080static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000081 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000082 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000083 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Ted Kremenekb697b102009-02-23 22:44:26 +000085 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000086}
87
Zhongxing Xuc5619d92009-08-06 01:32:16 +000088static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000089 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000090 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000091 // Check if the statement is '?' or '&&'/'||'. These are "merges",
92 // not actual statement points.
93 switch (S->getStmtClass()) {
94 case Stmt::ChooseExprClass:
95 case Stmt::ConditionalOperatorClass: continue;
96 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000097 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
98 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000099 continue;
100 break;
101 }
102 default:
103 break;
104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekb7c51522009-07-28 00:07:15 +0000106 // Some expressions don't have locations.
107 if (S->getLocStart().isInvalid())
108 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenekb697b102009-02-23 22:44:26 +0000113 return 0;
114}
115
Ted Kremenek5f85e172009-07-22 22:35:28 +0000116static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000117GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000118 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000119 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenekb697b102009-02-23 22:44:26 +0000121 return GetPreviousStmt(N);
122}
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Ted Kremenek5f85e172009-07-22 22:35:28 +0000124static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000125GetCurrentOrNextStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000126 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000127 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenekb697b102009-02-23 22:44:26 +0000129 return GetNextStmt(N);
130}
131
132//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000133// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000134//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000135
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000136typedef llvm::DenseMap<const ExplodedNode*,
137const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000138
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000139namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000140class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000141 NodeBackMap& M;
142public:
143 NodeMapClosure(NodeBackMap *m) : M(*m) {}
144 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000146 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000147 NodeBackMap::iterator I = M.find(N);
148 return I == M.end() ? 0 : I->second;
149 }
150};
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000152class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000153 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000154 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000155 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000156 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000157public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000158 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000159 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000160 PathDiagnosticClient *pdc)
161 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000162 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000163 addVisitor(R);
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000166 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek00605e02009-03-27 20:55:39 +0000168 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000169 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Tom Care212f6d32010-09-16 03:50:38 +0000171 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000172
Tom Care212f6d32010-09-16 03:50:38 +0000173 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000175 const Stmt *getParent(const Stmt *S) {
176 return getParentMap().getParent(S);
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek8966bc12009-05-06 21:39:49 +0000179 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000180
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000181 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek7dc86642009-03-31 20:22:36 +0000183 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
184 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
185 }
186
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000187 bool supportsLogicalOpControlFlow() const {
188 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000189 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000190};
191} // end anonymous namespace
192
Ted Kremenek00605e02009-03-27 20:55:39 +0000193PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000194PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000195 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000196 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000197
Mike Stump1eb44332009-09-09 15:08:12 +0000198 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000199 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000200}
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek00605e02009-03-27 20:55:39 +0000202PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000203PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000204 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000205
Ted Kremenek143ca222008-05-06 18:11:09 +0000206 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000207 if (os.str().empty())
208 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek00605e02009-03-27 20:55:39 +0000210 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek00605e02009-03-27 20:55:39 +0000212 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000213 os << "Execution continues on line "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000214 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
215 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000216 else {
217 os << "Execution jumps to the end of the ";
218 const Decl *D = N->getLocationContext()->getDecl();
219 if (isa<ObjCMethodDecl>(D))
220 os << "method";
221 else if (isa<FunctionDecl>(D))
222 os << "function";
223 else {
224 assert(isa<BlockDecl>(D));
225 os << "anonymous block";
226 }
227 os << '.';
228 }
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000230 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000231}
232
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000233static bool IsNested(const Stmt *S, ParentMap &PM) {
234 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
235 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000237 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000239 if (Parent)
240 switch (Parent->getStmtClass()) {
241 case Stmt::ForStmtClass:
242 case Stmt::DoStmtClass:
243 case Stmt::WhileStmtClass:
244 return true;
245 default:
246 break;
247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
249 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000250}
251
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000252PathDiagnosticLocation
253PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
254 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000255 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000256 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000257
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000258 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000259 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000261 if (!Parent)
262 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000264 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000265 case Stmt::BinaryOperatorClass: {
266 const BinaryOperator *B = cast<BinaryOperator>(Parent);
267 if (B->isLogicalOp())
268 return PathDiagnosticLocation(S, SMgr);
269 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000270 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000271 case Stmt::CompoundStmtClass:
272 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000273 return PathDiagnosticLocation(S, SMgr);
274 case Stmt::ChooseExprClass:
275 // Similar to '?' if we are referring to condition, just have the edge
276 // point to the entire choose expression.
277 if (cast<ChooseExpr>(Parent)->getCond() == S)
278 return PathDiagnosticLocation(Parent, SMgr);
279 else
Mike Stump1eb44332009-09-09 15:08:12 +0000280 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000281 case Stmt::ConditionalOperatorClass:
282 // For '?', if we are referring to condition, just have the edge point
283 // to the entire '?' expression.
284 if (cast<ConditionalOperator>(Parent)->getCond() == S)
285 return PathDiagnosticLocation(Parent, SMgr);
286 else
Mike Stump1eb44332009-09-09 15:08:12 +0000287 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000288 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000289 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000290 case Stmt::ForStmtClass:
291 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000292 return PathDiagnosticLocation(S, SMgr);
293 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000294 case Stmt::IfStmtClass:
295 if (cast<IfStmt>(Parent)->getCond() != S)
296 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000297 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000298 case Stmt::ObjCForCollectionStmtClass:
299 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
300 return PathDiagnosticLocation(S, SMgr);
301 break;
302 case Stmt::WhileStmtClass:
303 if (cast<WhileStmt>(Parent)->getCond() != S)
304 return PathDiagnosticLocation(S, SMgr);
305 break;
306 default:
307 break;
308 }
309
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000310 S = Parent;
311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000313 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000314
315 // Special case: DeclStmts can appear in for statement declarations, in which
316 // case the ForStmt is the context.
317 if (isa<DeclStmt>(S)) {
318 if (const Stmt *Parent = P.getParent(S)) {
319 switch (Parent->getStmtClass()) {
320 case Stmt::ForStmtClass:
321 case Stmt::ObjCForCollectionStmtClass:
322 return PathDiagnosticLocation(Parent, SMgr);
323 default:
324 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000325 }
326 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000327 }
328 else if (isa<BinaryOperator>(S)) {
329 // Special case: the binary operator represents the initialization
330 // code in a for statement (this can happen when the variable being
331 // initialized is an old variable.
332 if (const ForStmt *FS =
333 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
334 if (FS->getInit() == S)
335 return PathDiagnosticLocation(FS, SMgr);
336 }
337 }
338
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000339 return PathDiagnosticLocation(S, SMgr);
340}
341
Ted Kremenekcf118d42009-02-04 23:49:09 +0000342//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000343// ScanNotableSymbols: closure-like callback for scanning Store bindings.
344//===----------------------------------------------------------------------===//
345
346static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000347GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000348 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek31061982009-03-31 23:00:32 +0000350 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek31061982009-03-31 23:00:32 +0000352 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek31061982009-03-31 23:00:32 +0000354 if (!isa<PostStmt>(P))
355 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek5f85e172009-07-22 22:35:28 +0000357 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek31061982009-03-31 23:00:32 +0000359 if (!DR)
360 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek13976632010-02-08 16:18:51 +0000362 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek31061982009-03-31 23:00:32 +0000364 if (X != Y)
365 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek5f85e172009-07-22 22:35:28 +0000367 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek31061982009-03-31 23:00:32 +0000369 if (!VD)
370 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek31061982009-03-31 23:00:32 +0000372 return VD;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek31061982009-03-31 23:00:32 +0000375 return 0;
376}
377
378namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000379class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000380: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek31061982009-03-31 23:00:32 +0000382 SymbolRef Sym;
383 const GRState* PrevSt;
384 const Stmt* S;
385 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000386 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000387 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000388 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek31061982009-03-31 23:00:32 +0000390public:
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek31061982009-03-31 23:00:32 +0000392 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000393 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000394 PathDiagnostic& pd, BugReporter& br)
395 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek31061982009-03-31 23:00:32 +0000397 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
398 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek31061982009-03-31 23:00:32 +0000400 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek31061982009-03-31 23:00:32 +0000402 if (ScanSym != Sym)
403 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
405 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000406 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek31061982009-03-31 23:00:32 +0000408 if (X == V) // Same binding?
409 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek31061982009-03-31 23:00:32 +0000411 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000412 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000413 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek31061982009-03-31 23:00:32 +0000415 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek31061982009-03-31 23:00:32 +0000417 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
418 if (!B->isAssignmentOp())
419 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek31061982009-03-31 23:00:32 +0000421 // What variable did we assign to?
422 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek31061982009-03-31 23:00:32 +0000424 if (!DR)
425 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 VD = dyn_cast<VarDecl>(DR->getDecl());
428 }
429 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
430 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
431 // assume that each DeclStmt has a single Decl. This invariant
432 // holds by contruction in the CFG.
433 VD = dyn_cast<VarDecl>(*DS->decl_begin());
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek31061982009-03-31 23:00:32 +0000436 if (!VD)
437 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek31061982009-03-31 23:00:32 +0000439 // What is the most recently referenced variable with this binding?
440 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek31061982009-03-31 23:00:32 +0000442 if (!MostRecent)
443 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek31061982009-03-31 23:00:32 +0000445 // Create the diagnostic.
446 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek31061982009-03-31 23:00:32 +0000448 if (Loc::IsLocType(VD->getType())) {
449 std::string msg = "'" + std::string(VD->getNameAsString()) +
450 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek31061982009-03-31 23:00:32 +0000452 PD.push_front(new PathDiagnosticEventPiece(L, msg));
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek31061982009-03-31 23:00:32 +0000455 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000456 }
Ted Kremenek31061982009-03-31 23:00:32 +0000457};
458}
459
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000460static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000461 const Stmt* S,
462 SymbolRef Sym, BugReporter& BR,
463 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000465 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000466 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek31061982009-03-31 23:00:32 +0000468 if (!PrevSt)
469 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek31061982009-03-31 23:00:32 +0000471 // Look at the region bindings of the current state that map to the
472 // specified symbol. Are any of them not in the previous state?
473 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
474 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
475 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
476}
477
478namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000479class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000480: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek31061982009-03-31 23:00:32 +0000482 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000483 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000484 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000485 GRBugReporter& BR;
486 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Ted Kremenek31061982009-03-31 23:00:32 +0000488public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000489 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000490 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000491 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek31061982009-03-31 23:00:32 +0000493 bool HandleBinding(StoreManager& SMgr, Store store,
494 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek31061982009-03-31 23:00:32 +0000498 if (!ScanSym)
499 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501 if (!BR.isNotable(ScanSym))
502 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek31061982009-03-31 23:00:32 +0000504 if (AlreadyProcessed.count(ScanSym))
505 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek31061982009-03-31 23:00:32 +0000507 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenek31061982009-03-31 23:00:32 +0000509 HandleNotableSymbol(N, S, ScanSym, BR, PD);
510 return true;
511 }
512};
513} // end anonymous namespace
514
515//===----------------------------------------------------------------------===//
516// "Minimal" path diagnostic generation algorithm.
517//===----------------------------------------------------------------------===//
518
Ted Kremenek14856d72009-04-06 23:06:54 +0000519static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
520
Ted Kremenek31061982009-03-31 23:00:32 +0000521static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
522 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000523 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000524
Ted Kremenek31061982009-03-31 23:00:32 +0000525 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000526 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000527 ? NULL : *(N->pred_begin());
528 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000529 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000530 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Ted Kremenek31061982009-03-31 23:00:32 +0000532 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Ted Kremenek31061982009-03-31 23:00:32 +0000534 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000535 const CFGBlock* Src = BE->getSrc();
536 const CFGBlock* Dst = BE->getDst();
537 const Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek31061982009-03-31 23:00:32 +0000539 if (!T)
540 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenek31061982009-03-31 23:00:32 +0000542 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek31061982009-03-31 23:00:32 +0000544 switch (T->getStmtClass()) {
545 default:
546 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek31061982009-03-31 23:00:32 +0000548 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000549 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000550 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek31061982009-03-31 23:00:32 +0000552 if (!S)
553 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek31061982009-03-31 23:00:32 +0000555 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000556 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000557 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek31061982009-03-31 23:00:32 +0000559 os << "Control jumps to line "
560 << End.asLocation().getInstantiationLineNumber();
561 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
562 os.str()));
563 break;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
566 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000567 // Figure out what case arm we took.
568 std::string sbuf;
569 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000571 if (const Stmt* S = Dst->getLabel()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000572 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek31061982009-03-31 23:00:32 +0000574 switch (S->getStmtClass()) {
575 default:
576 os << "No cases match in the switch statement. "
577 "Control jumps to line "
578 << End.asLocation().getInstantiationLineNumber();
579 break;
580 case Stmt::DefaultStmtClass:
581 os << "Control jumps to the 'default' case at line "
582 << End.asLocation().getInstantiationLineNumber();
583 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000586 os << "Control jumps to 'case ";
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000587 const CaseStmt* Case = cast<CaseStmt>(S);
588 const Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000589
590 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000591 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000593 if (const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000594 // FIXME: Maybe this should be an assertion. Are there cases
595 // were it is not an EnumConstantDecl?
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000596 const EnumConstantDecl* D =
597 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Ted Kremenek31061982009-03-31 23:00:32 +0000599 if (D) {
600 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000601 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000602 }
603 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000604
605 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000606 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000607
Ted Kremenek31061982009-03-31 23:00:32 +0000608 os << ":' at line "
609 << End.asLocation().getInstantiationLineNumber();
610 break;
611 }
612 }
613 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
614 os.str()));
615 }
616 else {
617 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000618 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000619 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
620 os.str()));
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenek31061982009-03-31 23:00:32 +0000623 break;
624 }
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek31061982009-03-31 23:00:32 +0000626 case Stmt::BreakStmtClass:
627 case Stmt::ContinueStmtClass: {
628 std::string sbuf;
629 llvm::raw_string_ostream os(sbuf);
630 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
631 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
632 os.str()));
633 break;
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek31061982009-03-31 23:00:32 +0000636 // Determine control-flow for ternary '?'.
637 case Stmt::ConditionalOperatorClass: {
638 std::string sbuf;
639 llvm::raw_string_ostream os(sbuf);
640 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek31061982009-03-31 23:00:32 +0000642 if (*(Src->succ_begin()+1) == Dst)
643 os << "false";
644 else
645 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Ted Kremenek31061982009-03-31 23:00:32 +0000647 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 if (const Stmt *S = End.asStmt())
650 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek31061982009-03-31 23:00:32 +0000652 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
653 os.str()));
654 break;
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremenek31061982009-03-31 23:00:32 +0000657 // Determine control-flow for short-circuited '&&' and '||'.
658 case Stmt::BinaryOperatorClass: {
659 if (!PDB.supportsLogicalOpControlFlow())
660 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000662 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000663 std::string sbuf;
664 llvm::raw_string_ostream os(sbuf);
665 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000666
John McCall2de56d12010-08-25 11:45:40 +0000667 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000668 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 if (*(Src->succ_begin()+1) == Dst) {
671 os << "false";
672 PathDiagnosticLocation End(B->getLHS(), SMgr);
673 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
674 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
675 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000676 }
Ted Kremenek31061982009-03-31 23:00:32 +0000677 else {
678 os << "true";
679 PathDiagnosticLocation Start(B->getLHS(), SMgr);
680 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
681 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
682 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000683 }
Ted Kremenek31061982009-03-31 23:00:32 +0000684 }
685 else {
John McCall2de56d12010-08-25 11:45:40 +0000686 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000687 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Ted Kremenek31061982009-03-31 23:00:32 +0000689 if (*(Src->succ_begin()+1) == Dst) {
690 os << "false";
691 PathDiagnosticLocation Start(B->getLHS(), SMgr);
692 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
693 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000694 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000695 }
696 else {
697 os << "true";
698 PathDiagnosticLocation End(B->getLHS(), SMgr);
699 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
700 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000701 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000702 }
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenek31061982009-03-31 23:00:32 +0000705 break;
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
708 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000709 if (*(Src->succ_begin()) == Dst) {
710 std::string sbuf;
711 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek31061982009-03-31 23:00:32 +0000713 os << "Loop condition is true. ";
714 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremenek31061982009-03-31 23:00:32 +0000716 if (const Stmt *S = End.asStmt())
717 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
720 os.str()));
721 }
722 else {
723 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek31061982009-03-31 23:00:32 +0000725 if (const Stmt *S = End.asStmt())
726 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremenek31061982009-03-31 23:00:32 +0000728 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
729 "Loop condition is false. Exiting loop"));
730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek31061982009-03-31 23:00:32 +0000732 break;
733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek31061982009-03-31 23:00:32 +0000735 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000736 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000737 if (*(Src->succ_begin()+1) == Dst) {
738 std::string sbuf;
739 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek31061982009-03-31 23:00:32 +0000741 os << "Loop condition is false. ";
742 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
743 if (const Stmt *S = End.asStmt())
744 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek31061982009-03-31 23:00:32 +0000746 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
747 os.str()));
748 }
749 else {
750 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
751 if (const Stmt *S = End.asStmt())
752 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenek31061982009-03-31 23:00:32 +0000754 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000755 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek31061982009-03-31 23:00:32 +0000758 break;
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek31061982009-03-31 23:00:32 +0000761 case Stmt::IfStmtClass: {
762 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek31061982009-03-31 23:00:32 +0000764 if (const Stmt *S = End.asStmt())
765 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek31061982009-03-31 23:00:32 +0000767 if (*(Src->succ_begin()+1) == Dst)
768 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000769 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000770 else
Ted Kremenek31061982009-03-31 23:00:32 +0000771 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000772 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Ted Kremenek31061982009-03-31 23:00:32 +0000774 break;
775 }
776 }
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000779 if (NextNode) {
780 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
781 E = PDB.visitor_end(); I!=E; ++I) {
782 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
783 PD.push_front(p);
784 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
787 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000788 // Scan the region bindings, and see if a "notable" symbol has a new
789 // lval binding.
790 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
791 PDB.getStateManager().iterBindings(N->getState(), SNS);
792 }
793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek14856d72009-04-06 23:06:54 +0000795 // After constructing the full PathDiagnostic, do a pass over it to compact
796 // PathDiagnosticPieces that occur within a macro.
797 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000798}
799
800//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000801// "Extensive" PathDiagnostic generation.
802//===----------------------------------------------------------------------===//
803
804static bool IsControlFlowExpr(const Stmt *S) {
805 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000807 if (!E)
808 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
810 E = E->IgnoreParenCasts();
811
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000812 if (isa<ConditionalOperator>(E))
813 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000815 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
816 if (B->isLogicalOp())
817 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
819 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000820}
821
Ted Kremenek14856d72009-04-06 23:06:54 +0000822namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000823class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000824 bool IsDead;
825public:
826 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
827 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000828
829 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000830 bool isDead() const { return IsDead; }
831};
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000833class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000834 std::vector<ContextLocation> CLocs;
835 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000836 PathDiagnostic &PD;
837 PathDiagnosticBuilder &PDB;
838 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000840 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek14856d72009-04-06 23:06:54 +0000842 bool containsLocation(const PathDiagnosticLocation &Container,
843 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek14856d72009-04-06 23:06:54 +0000845 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Ted Kremenek9650cf32009-05-11 21:42:34 +0000847 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
848 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000849 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000850 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000851 while (1) {
852 // Adjust the location for some expressions that are best referenced
853 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000854 switch (S->getStmtClass()) {
855 default:
856 break;
857 case Stmt::ParenExprClass:
858 S = cast<ParenExpr>(S)->IgnoreParens();
859 firstCharOnly = true;
860 continue;
861 case Stmt::ConditionalOperatorClass:
862 S = cast<ConditionalOperator>(S)->getCond();
863 firstCharOnly = true;
864 continue;
865 case Stmt::ChooseExprClass:
866 S = cast<ChooseExpr>(S)->getCond();
867 firstCharOnly = true;
868 continue;
869 case Stmt::BinaryOperatorClass:
870 S = cast<BinaryOperator>(S)->getLHS();
871 firstCharOnly = true;
872 continue;
873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek9650cf32009-05-11 21:42:34 +0000875 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek9650cf32009-05-11 21:42:34 +0000878 if (S != Original)
879 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Ted Kremenek9650cf32009-05-11 21:42:34 +0000882 if (firstCharOnly)
883 L = PathDiagnosticLocation(L.asLocation());
884
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000885 return L;
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Ted Kremenek14856d72009-04-06 23:06:54 +0000888 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000889 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000890 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000891 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000892 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000893 CLocs.pop_back();
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Ted Kremenek14856d72009-04-06 23:06:54 +0000896public:
897 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
898 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremeneka301a672009-04-22 18:16:20 +0000900 // If the PathDiagnostic already has pieces, add the enclosing statement
901 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000902 if (!PD.empty()) {
903 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Ted Kremenek14856d72009-04-06 23:06:54 +0000905 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000906 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000907 }
908 }
909
910 ~EdgeBuilder() {
911 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Ted Kremeneka301a672009-04-22 18:16:20 +0000913 // Finally, add an initial edge from the start location of the first
914 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000915 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
916 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000917 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000918 if (!CS->body_empty()) {
919 SourceLocation Loc = (*CS->body_begin())->getLocStart();
920 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremenek14856d72009-04-06 23:06:54 +0000923 }
924
925 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000927 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremenek14856d72009-04-06 23:06:54 +0000929 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000930 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000931};
Ted Kremenek14856d72009-04-06 23:06:54 +0000932} // end anonymous namespace
933
934
935PathDiagnosticLocation
936EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
937 if (const Stmt *S = L.asStmt()) {
938 if (IsControlFlowExpr(S))
939 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000940
941 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek14856d72009-04-06 23:06:54 +0000944 return L;
945}
946
947bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
948 const PathDiagnosticLocation &Containee) {
949
950 if (Container == Containee)
951 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Ted Kremenek14856d72009-04-06 23:06:54 +0000953 if (Container.asDecl())
954 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek14856d72009-04-06 23:06:54 +0000956 if (const Stmt *S = Containee.asStmt())
957 if (const Stmt *ContainerS = Container.asStmt()) {
958 while (S) {
959 if (S == ContainerS)
960 return true;
961 S = PDB.getParent(S);
962 }
963 return false;
964 }
965
966 // Less accurate: compare using source ranges.
967 SourceRange ContainerR = Container.asRange();
968 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremenek14856d72009-04-06 23:06:54 +0000970 SourceManager &SM = PDB.getSourceManager();
971 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
972 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
973 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
974 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Ted Kremenek14856d72009-04-06 23:06:54 +0000976 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
977 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
978 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
979 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000982 assert(ContaineeBegLine <= ContaineeEndLine);
983
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 return (ContainerBegLine <= ContaineeBegLine &&
985 ContainerEndLine >= ContaineeEndLine &&
986 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +0000987 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +0000988 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
989 (ContainerEndLine != ContaineeEndLine ||
990 SM.getInstantiationColumnNumber(ContainerREnd) >=
991 SM.getInstantiationColumnNumber(ContainerREnd)));
992}
993
Ted Kremenek14856d72009-04-06 23:06:54 +0000994void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
995 if (!PrevLoc.isValid()) {
996 PrevLoc = NewLoc;
997 return;
998 }
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001000 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1001 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001003 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001007 if (NewLocClean.asLocation().getInstantiationLoc() ==
1008 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001009 return;
1010
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001011 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1012 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001013}
1014
1015void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremeneka301a672009-04-22 18:16:20 +00001017 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1018 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek14856d72009-04-06 23:06:54 +00001020 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1021
1022 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001023 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 // Is the top location context the same as the one for the new location?
1026 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001027 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001028 if (IsConsumedExpr(TopContextLoc) &&
1029 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001030 TopContextLoc.markDead();
1031
Ted Kremenek14856d72009-04-06 23:06:54 +00001032 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001033 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001034
1035 return;
1036 }
1037
1038 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001040 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001042 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001043 CLocs.push_back(ContextLocation(CLoc, true));
1044 return;
1045 }
1046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek14856d72009-04-06 23:06:54 +00001048 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001049 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001050 }
1051
1052 // Context does not contain the location. Flush it.
1053 popLocation();
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001056 // If we reach here, there is no enclosing context. Just add the edge.
1057 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001058}
1059
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001060bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1061 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1062 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001064 return false;
1065}
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremeneke1baed32009-05-05 23:13:38 +00001067void EdgeBuilder::addExtendedContext(const Stmt *S) {
1068 if (!S)
1069 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001070
1071 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001072 while (Parent) {
1073 if (isa<CompoundStmt>(Parent))
1074 Parent = PDB.getParent(Parent);
1075 else
1076 break;
1077 }
1078
1079 if (Parent) {
1080 switch (Parent->getStmtClass()) {
1081 case Stmt::DoStmtClass:
1082 case Stmt::ObjCAtSynchronizedStmtClass:
1083 addContext(Parent);
1084 default:
1085 break;
1086 }
1087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Ted Kremeneke1baed32009-05-05 23:13:38 +00001089 addContext(S);
1090}
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremenek14856d72009-04-06 23:06:54 +00001092void EdgeBuilder::addContext(const Stmt *S) {
1093 if (!S)
1094 return;
1095
1096 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Ted Kremenek14856d72009-04-06 23:06:54 +00001098 while (!CLocs.empty()) {
1099 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1100
1101 // Is the top location context the same as the one for the new location?
1102 if (TopContextLoc == L)
1103 return;
1104
1105 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001106 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001107 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001108 }
1109
1110 // Context does not contain the location. Flush it.
1111 popLocation();
1112 }
1113
1114 CLocs.push_back(L);
1115}
1116
1117static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1118 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001119 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001120 EdgeBuilder EB(PD, PDB);
1121
Zhongxing Xu41ca1342010-03-23 05:13:26 +00001122 const ExplodedNode* NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001123 while (NextNode) {
1124 N = NextNode;
1125 NextNode = GetPredecessorNode(N);
1126 ProgramPoint P = N->getLocation();
1127
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001128 do {
1129 // Block edges.
1130 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1131 const CFGBlock &Blk = *BE->getSrc();
1132 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001134 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001135 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001136 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001137 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001139 if (!Term) {
1140 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1141 CS = dyn_cast<CompoundStmt>(FS->getBody());
1142 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001143 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001146 PathDiagnosticEventPiece *p =
1147 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001148 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001150 EB.addEdge(p->getLocation(), true);
1151 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001153 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001154 PathDiagnosticLocation BL(CS->getRBracLoc(),
1155 PDB.getSourceManager());
1156 BL = PathDiagnosticLocation(BL.asLocation());
1157 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001158 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001161 if (Term)
1162 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001164 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001165 }
1166
Mike Stump1eb44332009-09-09 15:08:12 +00001167 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001168 if (CFGStmt S = BE->getFirstElement().getAs<CFGStmt>()) {
1169 if (IsControlFlowExpr(S)) {
1170 // Add the proper context for '&&', '||', and '?'.
1171 EB.addContext(S);
1172 }
1173 else
1174 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001175 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001176
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001177 break;
1178 }
1179 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001181 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001182 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenek8966bc12009-05-06 21:39:49 +00001184 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1185 E = PDB.visitor_end(); I!=E; ++I) {
1186 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1187 const PathDiagnosticLocation &Loc = p->getLocation();
1188 EB.addEdge(Loc, true);
1189 PD.push_front(p);
1190 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001191 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001192 }
Mike Stump1eb44332009-09-09 15:08:12 +00001193 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001194 }
1195}
1196
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001197//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001198// Methods for BugType and subclasses.
1199//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001200BugType::~BugType() {
1201 // Free up the equivalence class objects. Observe that we get a pointer to
1202 // the object first before incrementing the iterator, as destroying the
1203 // node before doing so means we will read from freed memory.
1204 for (iterator I = begin(), E = end(); I !=E; ) {
1205 BugReportEquivClass *EQ = &*I;
1206 ++I;
1207 delete EQ;
1208 }
1209}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001210void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001211
Ted Kremenekcf118d42009-02-04 23:49:09 +00001212//===----------------------------------------------------------------------===//
1213// Methods for BugReport and subclasses.
1214//===----------------------------------------------------------------------===//
1215BugReport::~BugReport() {}
1216RangedBugReport::~RangedBugReport() {}
1217
Mike Stump1eb44332009-09-09 15:08:12 +00001218const Stmt* BugReport::getStmt() const {
Tom Care212f6d32010-09-16 03:50:38 +00001219 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001220 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenekcf118d42009-02-04 23:49:09 +00001222 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001223 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001224 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001225 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001226 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001227 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001228 S = GetStmt(ProgP);
1229
1230 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001231}
1232
1233PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001234BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001235 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001237 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Ted Kremenek61f3e052008-04-03 04:42:52 +00001239 if (!S)
1240 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001241
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001242 BugReport::ranges_iterator Beg, End;
1243 llvm::tie(Beg, End) = getRanges();
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001244 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001246 // Only add the statement itself as a range if we didn't specify any
1247 // special ranges for this report.
1248 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1249 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001251 for (; Beg != End; ++Beg)
1252 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek61f3e052008-04-03 04:42:52 +00001254 return P;
1255}
1256
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001257std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
1258BugReport::getRanges() const {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001259 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001260 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001261 assert(R.isValid());
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001262 return std::make_pair(&R, &R+1);
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001263 }
1264 else
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001265 return std::make_pair(ranges_iterator(), ranges_iterator());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001266}
1267
Mike Stump1eb44332009-09-09 15:08:12 +00001268SourceLocation BugReport::getLocation() const {
Tom Care212f6d32010-09-16 03:50:38 +00001269 if (ErrorNode)
1270 if (const Stmt* S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001271 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001272 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001273 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001274 // For binary operators, return the location of the operator.
1275 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1276 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001277
Ted Kremenekcf118d42009-02-04 23:49:09 +00001278 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001279 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001280
1281 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001282}
1283
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001284PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1285 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001286 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001287 return NULL;
1288}
1289
Ted Kremenekcf118d42009-02-04 23:49:09 +00001290//===----------------------------------------------------------------------===//
1291// Methods for BugReporter and subclasses.
1292//===----------------------------------------------------------------------===//
1293
1294BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001295 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001296}
1297
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001298GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001299BugReporterData::~BugReporterData() {}
1300
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001301ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001302
1303GRStateManager&
1304GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1305
1306BugReporter::~BugReporter() { FlushReports(); }
1307
1308void BugReporter::FlushReports() {
1309 if (BugTypes.isEmpty())
1310 return;
1311
1312 // First flush the warnings for each BugType. This may end up creating new
1313 // warnings and new BugTypes. Because ImmutableSet is a functional data
1314 // structure, we do not need to worry about the iterators being invalidated.
1315 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1316 const_cast<BugType*>(*I)->FlushReports(*this);
1317
1318 // Iterate through BugTypes a second time. BugTypes may have been updated
1319 // with new BugType objects and new warnings.
1320 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1321 BugType *BT = const_cast<BugType*>(*I);
1322
1323 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1324 SetTy& EQClasses = BT->EQClasses;
1325
1326 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1327 BugReportEquivClass& EQ = *EI;
1328 FlushReport(EQ);
1329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
1331 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001332 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001333 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001334
1335 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001336 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001337}
1338
1339//===----------------------------------------------------------------------===//
1340// PathDiagnostics generation.
1341//===----------------------------------------------------------------------===//
1342
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001343static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001344 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001345MakeReportGraph(const ExplodedGraph* G,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001346 llvm::SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Ted Kremenekcf118d42009-02-04 23:49:09 +00001348 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001349 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001350 // error node unless there are two or more error nodes with the same minimum
1351 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001352 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001353 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001354
1355 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001356 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1357 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Ted Kremenekcf118d42009-02-04 23:49:09 +00001359 // Create owning pointers for GTrim and NMap just to ensure that they are
1360 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001361 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001362 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Ted Kremenekcf118d42009-02-04 23:49:09 +00001364 // Find the (first) error node in the trimmed graph. We just need to consult
1365 // the node map (NMap) which maps from nodes in the original graph to nodes
1366 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001367
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001368 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001369 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001370 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001371
Ted Kremenek40406fe2010-12-03 06:52:30 +00001372 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1373 const ExplodedNode *originalNode = nodes[nodeIndex];
1374 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001375 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001376 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001377 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001378 }
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Ted Kremenek938332c2009-05-16 01:11:58 +00001380 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001381
1382 // Create a new (third!) graph with a single path. This is the graph
1383 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001384 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Ted Kremenek10aa5542009-03-12 23:41:59 +00001386 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001387 // to the root node, and then construct a new graph that contains only
1388 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001389 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001391 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001392 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001394 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001395 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001396 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001398 if (Visited.find(Node) != Visited.end())
1399 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001401 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001403 if (Node->pred_empty()) {
1404 Root = Node;
1405 break;
1406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001408 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001409 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001410 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001411 }
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Ted Kremenek938332c2009-05-16 01:11:58 +00001413 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Ted Kremenek10aa5542009-03-12 23:41:59 +00001415 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001416 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001417 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001418 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001419 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001421 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001422 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001423 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001424 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001426 // Create the equivalent node in the new graph with the same state
1427 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001428 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001430 // Store the mapping to the original node.
1431 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1432 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001433 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001435 // Link up the new node with the previous node.
1436 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001437 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001439 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001441 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001442 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001443 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001444 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001445 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001446 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001447 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001448 }
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001450 // Find the next successor node. We choose the node that is marked
1451 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001452 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1453 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001454 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001456 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001458 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001460 if (I == Visited.end())
1461 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001463 if (!N || I->second < MinVal) {
1464 N = *SI;
1465 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001466 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001467 }
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Ted Kremenek938332c2009-05-16 01:11:58 +00001469 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Ted Kremenek938332c2009-05-16 01:11:58 +00001472 assert(First);
1473
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001474 return std::make_pair(std::make_pair(GNew, BM),
1475 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001476}
1477
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001478/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1479/// and collapses PathDiagosticPieces that are expanded by macros.
1480static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1481 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1482 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001484 typedef std::vector<PathDiagnosticPiece*>
1485 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001487 MacroStackTy MacroStack;
1488 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001490 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1491 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001492 const FullSourceLoc Loc = I->getLocation().asLocation();
1493
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001494 // Determine the instantiation location, which is the location we group
1495 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001496 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001497 SM.getInstantiationLoc(Loc) :
1498 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001500 if (Loc.isFileID()) {
1501 MacroStack.clear();
1502 Pieces.push_back(&*I);
1503 continue;
1504 }
1505
1506 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001508 // Is the PathDiagnosticPiece within the same macro group?
1509 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1510 MacroStack.back().first->push_back(&*I);
1511 continue;
1512 }
1513
1514 // We aren't in the same group. Are we descending into a new macro
1515 // or are part of an old one?
1516 PathDiagnosticMacroPiece *MacroGroup = 0;
1517
1518 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1519 SM.getInstantiationLoc(Loc) :
1520 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001522 // Walk the entire macro stack.
1523 while (!MacroStack.empty()) {
1524 if (InstantiationLoc == MacroStack.back().second) {
1525 MacroGroup = MacroStack.back().first;
1526 break;
1527 }
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001529 if (ParentInstantiationLoc == MacroStack.back().second) {
1530 MacroGroup = MacroStack.back().first;
1531 break;
1532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001534 MacroStack.pop_back();
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001537 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1538 // Create a new macro group and add it to the stack.
1539 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1540
1541 if (MacroGroup)
1542 MacroGroup->push_back(NewGroup);
1543 else {
1544 assert(InstantiationLoc.isFileID());
1545 Pieces.push_back(NewGroup);
1546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001548 MacroGroup = NewGroup;
1549 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1550 }
1551
1552 // Finally, add the PathDiagnosticPiece to the group.
1553 MacroGroup->push_back(&*I);
1554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001556 // Now take the pieces and construct a new PathDiagnostic.
1557 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001559 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1560 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1561 if (!MP->containsEvent()) {
1562 delete MP;
1563 continue;
1564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001566 PD.push_back(*I);
1567 }
1568}
1569
Ted Kremenek7dc86642009-03-31 20:22:36 +00001570void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001571 llvm::SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Ted Kremenek40406fe2010-12-03 06:52:30 +00001573 assert(!bugReports.empty());
1574 llvm::SmallVector<const ExplodedNode *, 10> errorNodes;
1575 for (llvm::SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
1576 E = bugReports.end(); I != E; ++I) {
1577 errorNodes.push_back((*I)->getErrorNode());
1578 }
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001580 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001581 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001582 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001583 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001584 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Ted Kremenekcf118d42009-02-04 23:49:09 +00001586 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001587 assert(GPair.second.second < bugReports.size());
1588 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001589 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001591 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001592 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001593 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001594
1595 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001596 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Ted Kremenek8966bc12009-05-06 21:39:49 +00001598 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001599 PD.push_back(Piece);
1600 else
1601 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Ted Kremenekff7f7362010-03-20 18:02:01 +00001603 // Register node visitors.
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001604 R->registerInitialVisitors(PDB, N);
Ted Kremenekff7f7362010-03-20 18:02:01 +00001605 bugreporter::registerNilReceiverVisitor(PDB);
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Ted Kremenek7dc86642009-03-31 20:22:36 +00001607 switch (PDB.getGenerationScheme()) {
1608 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001609 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001610 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001611 case PathDiagnosticClient::Minimal:
1612 GenerateMinimalPathDiagnostic(PD, PDB, N);
1613 break;
1614 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001615}
1616
Ted Kremenekcf118d42009-02-04 23:49:09 +00001617void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001618 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001619}
1620
Mike Stump1eb44332009-09-09 15:08:12 +00001621void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001622 // Compute the bug report's hash to determine its equivalence class.
1623 llvm::FoldingSetNodeID ID;
1624 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001625
1626 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001627 BugType& BT = R->getBugType();
1628 Register(&BT);
1629 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001630 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1631
Ted Kremenekcf118d42009-02-04 23:49:09 +00001632 if (!EQ) {
1633 EQ = new BugReportEquivClass(R);
1634 BT.EQClasses.InsertNode(EQ, InsertPos);
1635 }
1636 else
1637 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001638}
1639
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001640
1641//===----------------------------------------------------------------------===//
1642// Emitting reports in equivalence classes.
1643//===----------------------------------------------------------------------===//
1644
1645namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001646struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001647 const ExplodedNode *N;
1648 ExplodedNode::const_succ_iterator I, E;
1649
1650 FRIEC_WLItem(const ExplodedNode *n)
1651 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1652};
1653}
1654
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001655static BugReport *
1656FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Ted Kremenek40406fe2010-12-03 06:52:30 +00001657 llvm::SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001658
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001659 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1660 assert(I != E);
1661 BugReport *R = *I;
1662 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001663
Ted Kremenek40406fe2010-12-03 06:52:30 +00001664 // If we don't need to suppress any of the nodes because they are
1665 // post-dominated by a sink, simply add all the nodes in the equivalence class
1666 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001667 if (!BT.isSuppressOnSink()) {
1668 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Tom Care212f6d32010-09-16 03:50:38 +00001669 const ExplodedNode* N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001670 if (N) {
1671 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001672 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001673 }
1674 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001675 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001676 }
1677
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001678 // For bug reports that should be suppressed when all paths are post-dominated
1679 // by a sink node, iterate through the reports in the equivalence class
1680 // until we find one that isn't post-dominated (if one exists). We use a
1681 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1682 // this as a recursive function, but we don't want to risk blowing out the
1683 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001684 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001685
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001686 for (; I != E; ++I) {
1687 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001688 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001689
Ted Kremenek40406fe2010-12-03 06:52:30 +00001690 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001691 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001692 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001693 assert(false &&
1694 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001695 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001696 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001697 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001698 if (errorNode->succ_empty()) {
1699 bugReports.push_back(R);
1700 if (!exampleReport)
1701 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001702 continue;
1703 }
1704
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001705 // At this point we know that 'N' is not a sink and it has at least one
1706 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1707 typedef FRIEC_WLItem WLItem;
1708 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1709 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1710
1711 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001712 WL.push_back(errorNode);
1713 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001714
1715 while (!WL.empty()) {
1716 WLItem &WI = WL.back();
1717 assert(!WI.N->succ_empty());
1718
1719 for (; WI.I != WI.E; ++WI.I) {
1720 const ExplodedNode *Succ = *WI.I;
1721 // End-of-path node?
1722 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001723 // If we found an end-of-path node that is not a sink.
1724 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001725 bugReports.push_back(R);
1726 if (!exampleReport)
1727 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001728 WL.clear();
1729 break;
1730 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001731 // Found a sink? Continue on to the next successor.
1732 continue;
1733 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001734 // Mark the successor as visited. If it hasn't been explored,
1735 // enqueue it to the DFS worklist.
1736 unsigned &mark = Visited[Succ];
1737 if (!mark) {
1738 mark = 1;
1739 WL.push_back(Succ);
1740 break;
1741 }
1742 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001743
1744 // The worklist may have been cleared at this point. First
1745 // check if it is empty before checking the last item.
1746 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001747 WL.pop_back();
1748 }
1749 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001750
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001751 // ExampleReport will be NULL if all the nodes in the equivalence class
1752 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001753 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001754}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001755
1756//===----------------------------------------------------------------------===//
1757// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1758// uses global state, which eventually should go elsewhere.
1759//===----------------------------------------------------------------------===//
1760namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001761class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001762 llvm::FoldingSetNodeID ID;
1763public:
1764 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1765 ID.AddString(R->getBugType().getName());
1766 ID.AddString(R->getBugType().getCategory());
1767 ID.AddString(R->getDescription());
1768 ID.AddInteger(R->getLocation().getRawEncoding());
1769 PD->Profile(ID);
1770 }
1771
1772 void Profile(llvm::FoldingSetNodeID &id) {
1773 id = ID;
1774 }
1775
1776 llvm::FoldingSetNodeID &getID() { return ID; }
1777};
1778}
1779
1780static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1781 // FIXME: Eventually this diagnostic cache should reside in something
1782 // like AnalysisManager instead of being a static variable. This is
1783 // really unsafe in the long term.
1784 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1785 static DiagnosticCache DC;
1786
1787 void *InsertPos;
1788 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1789
1790 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1791 delete Item;
1792 return true;
1793 }
1794
1795 DC.InsertNode(Item, InsertPos);
1796 return false;
1797}
1798
Ted Kremenekcf118d42009-02-04 23:49:09 +00001799void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001800 llvm::SmallVector<BugReport*, 10> bugReports;
1801 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1802 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001803 return;
1804
Ted Kremenekd49967f2009-04-29 21:58:13 +00001805 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Ted Kremenekcf118d42009-02-04 23:49:09 +00001807 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001808 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001809 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Ted Kremenekd49967f2009-04-29 21:58:13 +00001811 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001812 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001813 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001814 ? exampleReport->getDescription()
1815 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001816 BT.getCategory()));
1817
Ted Kremenek40406fe2010-12-03 06:52:30 +00001818 if (!bugReports.empty())
1819 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Ted Kremenek40406fe2010-12-03 06:52:30 +00001821 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001822 return;
1823
Ted Kremenek072192b2008-04-30 23:47:44 +00001824 // Get the meta data.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001825 std::pair<const char**, const char**> Meta =
1826 exampleReport->getExtraDescriptiveText();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001827 for (const char** s = Meta.first; s != Meta.second; ++s)
1828 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001829
Ted Kremenek3148eb42009-01-24 00:55:43 +00001830 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001831 BugReport::ranges_iterator Beg, End;
1832 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001833 Diagnostic &Diag = getDiagnostic();
1834 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001835
1836 // Search the description for '%', as that will be interpretted as a
1837 // format character by FormatDiagnostics.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001838 llvm::StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001839 unsigned ErrorDiag;
1840 {
1841 llvm::SmallString<512> TmpStr;
1842 llvm::raw_svector_ostream Out(TmpStr);
1843 for (llvm::StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
1844 if (*I == '%')
1845 Out << "%%";
1846 else
1847 Out << *I;
1848
1849 Out.flush();
1850 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1851 }
Ted Kremenek57202072008-07-14 17:40:50 +00001852
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001853 {
1854 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001855 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001856 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001857 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001858
1859 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1860 if (!PD)
1861 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
1863 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001864 PathDiagnosticPiece* piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001865 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001866
Ted Kremenek3148eb42009-01-24 00:55:43 +00001867 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1868 D->push_back(piece);
1869 }
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Ted Kremenek3148eb42009-01-24 00:55:43 +00001871 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001872}
Ted Kremenek57202072008-07-14 17:40:50 +00001873
Benjamin Kramerf0171732009-11-29 18:27:55 +00001874void BugReporter::EmitBasicReport(llvm::StringRef name, llvm::StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001875 SourceLocation Loc,
1876 SourceRange* RBeg, unsigned NumRanges) {
1877 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1878}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001879
Benjamin Kramerf0171732009-11-29 18:27:55 +00001880void BugReporter::EmitBasicReport(llvm::StringRef name,
1881 llvm::StringRef category,
1882 llvm::StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001883 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Ted Kremenekcf118d42009-02-04 23:49:09 +00001885 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1886 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001887 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001888 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1889 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1890 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001891}