blob: 23ca53d6e97880f28486c9eb76050f5a3ba27205 [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
15#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000017#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000018#include "clang/Analysis/CFG.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000019#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000020#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000021#include "clang/AST/StmtObjC.h"
22#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000023#include "clang/Analysis/ProgramPoint.h"
24#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000026#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000027#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000028#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000029#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000030
31using namespace clang;
32
Ted Kremenek8966bc12009-05-06 21:39:49 +000033BugReporterVisitor::~BugReporterVisitor() {}
34BugReporterContext::~BugReporterContext() {
35 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I)
36 if ((*I)->isOwnedByReporterContext()) delete *I;
37}
38
Zhongxing Xu5032ffe2009-08-25 06:51:30 +000039const Decl& BugReporterContext::getCodeDecl() {
40 return *BR.getEngine().getAnalysisManager().getCodeDecl();
41}
42
43const CFG& BugReporterContext::getCFG() {
44 return *BR.getEngine().getAnalysisManager().getCFG();
45}
46
Ted Kremenekcf118d42009-02-04 23:49:09 +000047//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000048// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000049//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000050
Ted Kremenek5f85e172009-07-22 22:35:28 +000051static inline const Stmt* GetStmt(ProgramPoint P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000052 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
53 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000054 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000055 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000056
Ted Kremenekb697b102009-02-23 22:44:26 +000057 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000058}
59
Zhongxing Xuc5619d92009-08-06 01:32:16 +000060static inline const ExplodedNode*
61GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000062 return N->pred_empty() ? NULL : *(N->pred_begin());
63}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000064
Zhongxing Xuc5619d92009-08-06 01:32:16 +000065static inline const ExplodedNode*
66GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000067 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000068}
69
Zhongxing Xuc5619d92009-08-06 01:32:16 +000070static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000071 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000072 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000073 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000074
Ted Kremenekb697b102009-02-23 22:44:26 +000075 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000076}
77
Zhongxing Xuc5619d92009-08-06 01:32:16 +000078static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000079 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000080 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000081 // Check if the statement is '?' or '&&'/'||'. These are "merges",
82 // not actual statement points.
83 switch (S->getStmtClass()) {
84 case Stmt::ChooseExprClass:
85 case Stmt::ConditionalOperatorClass: continue;
86 case Stmt::BinaryOperatorClass: {
87 BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode();
88 if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr)
89 continue;
90 break;
91 }
92 default:
93 break;
94 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Ted Kremenekb7c51522009-07-28 00:07:15 +000096 // Some expressions don't have locations.
97 if (S->getLocStart().isInvalid())
98 continue;
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenekb697b102009-02-23 22:44:26 +0000100 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekb697b102009-02-23 22:44:26 +0000103 return 0;
104}
105
Ted Kremenek5f85e172009-07-22 22:35:28 +0000106static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000107GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000108 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000109 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenekb697b102009-02-23 22:44:26 +0000111 return GetPreviousStmt(N);
112}
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenek5f85e172009-07-22 22:35:28 +0000114static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000115GetCurrentOrNextStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000116 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000117 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenekb697b102009-02-23 22:44:26 +0000119 return GetNextStmt(N);
120}
121
122//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000123// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000124//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000125
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000126typedef llvm::DenseMap<const ExplodedNode*,
127const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000128
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000129namespace {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000130class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
131 NodeBackMap& M;
132public:
133 NodeMapClosure(NodeBackMap *m) : M(*m) {}
134 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000136 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000137 NodeBackMap::iterator I = M.find(N);
138 return I == M.end() ? 0 : I->second;
139 }
140};
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenek8966bc12009-05-06 21:39:49 +0000142class VISIBILITY_HIDDEN PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000143 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000144 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000145 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000146 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000147public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000148 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000149 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000150 PathDiagnosticClient *pdc)
151 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000152 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000153 addVisitor(R);
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000156 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Ted Kremenek00605e02009-03-27 20:55:39 +0000158 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000159 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenek00605e02009-03-27 20:55:39 +0000161 ParentMap& getParentMap() {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000162 if (PM.get() == 0)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000163 PM.reset(new ParentMap(getCodeDecl().getBody()));
Ted Kremenek00605e02009-03-27 20:55:39 +0000164 return *PM.get();
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000167 const Stmt *getParent(const Stmt *S) {
168 return getParentMap().getParent(S);
169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek8966bc12009-05-06 21:39:49 +0000171 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Ted Kremenek7dc86642009-03-31 20:22:36 +0000172 BugReport& getReport() { return *R; }
Douglas Gregor72971342009-04-18 00:02:19 +0000173
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000174 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000176 PathDiagnosticLocation
177 getEnclosingStmtLocation(const PathDiagnosticLocation &L) {
178 if (const Stmt *S = L.asStmt())
179 return getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000181 return L;
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek7dc86642009-03-31 20:22:36 +0000184 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
185 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
186 }
187
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000188 bool supportsLogicalOpControlFlow() const {
189 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000190 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000191};
192} // end anonymous namespace
193
Ted Kremenek00605e02009-03-27 20:55:39 +0000194PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000195PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000196 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000197 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000198
Mike Stump1eb44332009-09-09 15:08:12 +0000199 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000200 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000201}
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek00605e02009-03-27 20:55:39 +0000203PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000204PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000205 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000206
Ted Kremenek143ca222008-05-06 18:11:09 +0000207 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000208 if (os.str().empty())
209 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek00605e02009-03-27 20:55:39 +0000211 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek00605e02009-03-27 20:55:39 +0000213 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000214 os << "Execution continues on line "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000215 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
216 << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000217 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000218 os << "Execution jumps to the end of the "
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000219 << (isa<ObjCMethodDecl>(N->getLocationContext()->getDecl()) ?
220 "method" : "function") << '.';
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000222 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000223}
224
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000225static bool IsNested(const Stmt *S, ParentMap &PM) {
226 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
227 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000229 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000231 if (Parent)
232 switch (Parent->getStmtClass()) {
233 case Stmt::ForStmtClass:
234 case Stmt::DoStmtClass:
235 case Stmt::WhileStmtClass:
236 return true;
237 default:
238 break;
239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
241 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000242}
243
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000244PathDiagnosticLocation
245PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
246 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000247 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000248 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000249
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000250 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000251 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000253 if (!Parent)
254 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000256 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000257 case Stmt::BinaryOperatorClass: {
258 const BinaryOperator *B = cast<BinaryOperator>(Parent);
259 if (B->isLogicalOp())
260 return PathDiagnosticLocation(S, SMgr);
261 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000262 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000263 case Stmt::CompoundStmtClass:
264 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000265 return PathDiagnosticLocation(S, SMgr);
266 case Stmt::ChooseExprClass:
267 // Similar to '?' if we are referring to condition, just have the edge
268 // point to the entire choose expression.
269 if (cast<ChooseExpr>(Parent)->getCond() == S)
270 return PathDiagnosticLocation(Parent, SMgr);
271 else
Mike Stump1eb44332009-09-09 15:08:12 +0000272 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000273 case Stmt::ConditionalOperatorClass:
274 // For '?', if we are referring to condition, just have the edge point
275 // to the entire '?' expression.
276 if (cast<ConditionalOperator>(Parent)->getCond() == S)
277 return PathDiagnosticLocation(Parent, SMgr);
278 else
Mike Stump1eb44332009-09-09 15:08:12 +0000279 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000280 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000281 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000282 case Stmt::ForStmtClass:
283 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000284 return PathDiagnosticLocation(S, SMgr);
285 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000286 case Stmt::IfStmtClass:
287 if (cast<IfStmt>(Parent)->getCond() != S)
288 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000289 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000290 case Stmt::ObjCForCollectionStmtClass:
291 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
292 return PathDiagnosticLocation(S, SMgr);
293 break;
294 case Stmt::WhileStmtClass:
295 if (cast<WhileStmt>(Parent)->getCond() != S)
296 return PathDiagnosticLocation(S, SMgr);
297 break;
298 default:
299 break;
300 }
301
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000302 S = Parent;
303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000305 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000306
307 // Special case: DeclStmts can appear in for statement declarations, in which
308 // case the ForStmt is the context.
309 if (isa<DeclStmt>(S)) {
310 if (const Stmt *Parent = P.getParent(S)) {
311 switch (Parent->getStmtClass()) {
312 case Stmt::ForStmtClass:
313 case Stmt::ObjCForCollectionStmtClass:
314 return PathDiagnosticLocation(Parent, SMgr);
315 default:
316 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000317 }
318 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000319 }
320 else if (isa<BinaryOperator>(S)) {
321 // Special case: the binary operator represents the initialization
322 // code in a for statement (this can happen when the variable being
323 // initialized is an old variable.
324 if (const ForStmt *FS =
325 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
326 if (FS->getInit() == S)
327 return PathDiagnosticLocation(FS, SMgr);
328 }
329 }
330
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000331 return PathDiagnosticLocation(S, SMgr);
332}
333
Ted Kremenekcf118d42009-02-04 23:49:09 +0000334//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000335// ScanNotableSymbols: closure-like callback for scanning Store bindings.
336//===----------------------------------------------------------------------===//
337
338static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000339GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000340 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek31061982009-03-31 23:00:32 +0000342 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek31061982009-03-31 23:00:32 +0000344 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek31061982009-03-31 23:00:32 +0000346 if (!isa<PostStmt>(P))
347 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek5f85e172009-07-22 22:35:28 +0000349 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek31061982009-03-31 23:00:32 +0000351 if (!DR)
352 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000354 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek31061982009-03-31 23:00:32 +0000356 if (X != Y)
357 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek5f85e172009-07-22 22:35:28 +0000359 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek31061982009-03-31 23:00:32 +0000361 if (!VD)
362 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek31061982009-03-31 23:00:32 +0000364 return VD;
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek31061982009-03-31 23:00:32 +0000367 return 0;
368}
369
370namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000371class VISIBILITY_HIDDEN NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000372: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek31061982009-03-31 23:00:32 +0000374 SymbolRef Sym;
375 const GRState* PrevSt;
376 const Stmt* S;
377 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000378 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000379 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000380 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek31061982009-03-31 23:00:32 +0000382public:
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek31061982009-03-31 23:00:32 +0000384 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000385 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000386 PathDiagnostic& pd, BugReporter& br)
387 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek31061982009-03-31 23:00:32 +0000389 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
390 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek31061982009-03-31 23:00:32 +0000392 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek31061982009-03-31 23:00:32 +0000394 if (ScanSym != Sym)
395 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
397 // Check if the previous state has this binding.
Ted Kremenekdbc2afc2009-06-23 17:55:07 +0000398 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek31061982009-03-31 23:00:32 +0000400 if (X == V) // Same binding?
401 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000404 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000405 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenek31061982009-03-31 23:00:32 +0000407 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek31061982009-03-31 23:00:32 +0000409 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
410 if (!B->isAssignmentOp())
411 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek31061982009-03-31 23:00:32 +0000413 // What variable did we assign to?
414 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 if (!DR)
417 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419 VD = dyn_cast<VarDecl>(DR->getDecl());
420 }
421 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
422 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
423 // assume that each DeclStmt has a single Decl. This invariant
424 // holds by contruction in the CFG.
425 VD = dyn_cast<VarDecl>(*DS->decl_begin());
426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek31061982009-03-31 23:00:32 +0000428 if (!VD)
429 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek31061982009-03-31 23:00:32 +0000431 // What is the most recently referenced variable with this binding?
432 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 if (!MostRecent)
435 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 // Create the diagnostic.
438 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 if (Loc::IsLocType(VD->getType())) {
441 std::string msg = "'" + std::string(VD->getNameAsString()) +
442 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek31061982009-03-31 23:00:32 +0000444 PD.push_front(new PathDiagnosticEventPiece(L, msg));
445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek31061982009-03-31 23:00:32 +0000447 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000448 }
Ted Kremenek31061982009-03-31 23:00:32 +0000449};
450}
451
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000452static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000453 const Stmt* S,
454 SymbolRef Sym, BugReporter& BR,
455 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000457 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000458 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek31061982009-03-31 23:00:32 +0000460 if (!PrevSt)
461 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek31061982009-03-31 23:00:32 +0000463 // Look at the region bindings of the current state that map to the
464 // specified symbol. Are any of them not in the previous state?
465 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
466 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
467 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
468}
469
470namespace {
471class VISIBILITY_HIDDEN ScanNotableSymbols
472: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek31061982009-03-31 23:00:32 +0000474 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000475 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000476 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000477 GRBugReporter& BR;
478 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek31061982009-03-31 23:00:32 +0000480public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000481 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000482 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000483 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek31061982009-03-31 23:00:32 +0000485 bool HandleBinding(StoreManager& SMgr, Store store,
486 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Ted Kremenek31061982009-03-31 23:00:32 +0000488 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek31061982009-03-31 23:00:32 +0000490 if (!ScanSym)
491 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek31061982009-03-31 23:00:32 +0000493 if (!BR.isNotable(ScanSym))
494 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 if (AlreadyProcessed.count(ScanSym))
497 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Ted Kremenek31061982009-03-31 23:00:32 +0000499 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501 HandleNotableSymbol(N, S, ScanSym, BR, PD);
502 return true;
503 }
504};
505} // end anonymous namespace
506
507//===----------------------------------------------------------------------===//
508// "Minimal" path diagnostic generation algorithm.
509//===----------------------------------------------------------------------===//
510
Ted Kremenek14856d72009-04-06 23:06:54 +0000511static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
512
Ted Kremenek31061982009-03-31 23:00:32 +0000513static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
514 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000515 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000516
Ted Kremenek31061982009-03-31 23:00:32 +0000517 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000518 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000519 ? NULL : *(N->pred_begin());
520 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000521 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000522 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenek31061982009-03-31 23:00:32 +0000524 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Ted Kremenek31061982009-03-31 23:00:32 +0000526 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
527 CFGBlock* Src = BE->getSrc();
528 CFGBlock* Dst = BE->getDst();
529 Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Ted Kremenek31061982009-03-31 23:00:32 +0000531 if (!T)
532 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Ted Kremenek31061982009-03-31 23:00:32 +0000534 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek31061982009-03-31 23:00:32 +0000536 switch (T->getStmtClass()) {
537 default:
538 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000541 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000542 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek31061982009-03-31 23:00:32 +0000544 if (!S)
545 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000548 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000549 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek31061982009-03-31 23:00:32 +0000551 os << "Control jumps to line "
552 << End.asLocation().getInstantiationLineNumber();
553 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
554 os.str()));
555 break;
556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
558 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000559 // Figure out what case arm we took.
560 std::string sbuf;
561 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Ted Kremenek31061982009-03-31 23:00:32 +0000563 if (Stmt* S = Dst->getLabel()) {
564 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Ted Kremenek31061982009-03-31 23:00:32 +0000566 switch (S->getStmtClass()) {
567 default:
568 os << "No cases match in the switch statement. "
569 "Control jumps to line "
570 << End.asLocation().getInstantiationLineNumber();
571 break;
572 case Stmt::DefaultStmtClass:
573 os << "Control jumps to the 'default' case at line "
574 << End.asLocation().getInstantiationLineNumber();
575 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenek31061982009-03-31 23:00:32 +0000577 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000578 os << "Control jumps to 'case ";
579 CaseStmt* Case = cast<CaseStmt>(S);
Ted Kremenek31061982009-03-31 23:00:32 +0000580 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000583 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
586 // FIXME: Maybe this should be an assertion. Are there cases
587 // were it is not an EnumConstantDecl?
588 EnumConstantDecl* D =
589 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Ted Kremenek31061982009-03-31 23:00:32 +0000591 if (D) {
592 GetRawInt = false;
593 os << D->getNameAsString();
594 }
595 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000596
597 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000598 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000599
Ted Kremenek31061982009-03-31 23:00:32 +0000600 os << ":' at line "
601 << End.asLocation().getInstantiationLineNumber();
602 break;
603 }
604 }
605 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
606 os.str()));
607 }
608 else {
609 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000610 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000611 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
612 os.str()));
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Ted Kremenek31061982009-03-31 23:00:32 +0000615 break;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenek31061982009-03-31 23:00:32 +0000618 case Stmt::BreakStmtClass:
619 case Stmt::ContinueStmtClass: {
620 std::string sbuf;
621 llvm::raw_string_ostream os(sbuf);
622 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
623 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
624 os.str()));
625 break;
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Ted Kremenek31061982009-03-31 23:00:32 +0000628 // Determine control-flow for ternary '?'.
629 case Stmt::ConditionalOperatorClass: {
630 std::string sbuf;
631 llvm::raw_string_ostream os(sbuf);
632 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Ted Kremenek31061982009-03-31 23:00:32 +0000634 if (*(Src->succ_begin()+1) == Dst)
635 os << "false";
636 else
637 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek31061982009-03-31 23:00:32 +0000641 if (const Stmt *S = End.asStmt())
642 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek31061982009-03-31 23:00:32 +0000644 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
645 os.str()));
646 break;
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 // Determine control-flow for short-circuited '&&' and '||'.
650 case Stmt::BinaryOperatorClass: {
651 if (!PDB.supportsLogicalOpControlFlow())
652 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek31061982009-03-31 23:00:32 +0000654 BinaryOperator *B = cast<BinaryOperator>(T);
655 std::string sbuf;
656 llvm::raw_string_ostream os(sbuf);
657 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Ted Kremenek31061982009-03-31 23:00:32 +0000659 if (B->getOpcode() == BinaryOperator::LAnd) {
660 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek31061982009-03-31 23:00:32 +0000662 if (*(Src->succ_begin()+1) == Dst) {
663 os << "false";
664 PathDiagnosticLocation End(B->getLHS(), SMgr);
665 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
666 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
667 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000668 }
Ted Kremenek31061982009-03-31 23:00:32 +0000669 else {
670 os << "true";
671 PathDiagnosticLocation Start(B->getLHS(), SMgr);
672 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
673 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
674 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000675 }
Ted Kremenek31061982009-03-31 23:00:32 +0000676 }
677 else {
678 assert(B->getOpcode() == BinaryOperator::LOr);
679 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Ted Kremenek31061982009-03-31 23:00:32 +0000681 if (*(Src->succ_begin()+1) == Dst) {
682 os << "false";
683 PathDiagnosticLocation Start(B->getLHS(), SMgr);
684 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
685 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000686 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000687 }
688 else {
689 os << "true";
690 PathDiagnosticLocation End(B->getLHS(), SMgr);
691 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
692 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000693 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000694 }
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek31061982009-03-31 23:00:32 +0000697 break;
698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
700 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000701 if (*(Src->succ_begin()) == Dst) {
702 std::string sbuf;
703 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenek31061982009-03-31 23:00:32 +0000705 os << "Loop condition is true. ";
706 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek31061982009-03-31 23:00:32 +0000708 if (const Stmt *S = End.asStmt())
709 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Ted Kremenek31061982009-03-31 23:00:32 +0000711 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
712 os.str()));
713 }
714 else {
715 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenek31061982009-03-31 23:00:32 +0000717 if (const Stmt *S = End.asStmt())
718 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Ted Kremenek31061982009-03-31 23:00:32 +0000720 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
721 "Loop condition is false. Exiting loop"));
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Ted Kremenek31061982009-03-31 23:00:32 +0000724 break;
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek31061982009-03-31 23:00:32 +0000727 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000728 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000729 if (*(Src->succ_begin()+1) == Dst) {
730 std::string sbuf;
731 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek31061982009-03-31 23:00:32 +0000733 os << "Loop condition is false. ";
734 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
735 if (const Stmt *S = End.asStmt())
736 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek31061982009-03-31 23:00:32 +0000738 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
739 os.str()));
740 }
741 else {
742 PathDiagnosticLocation End = PDB.ExecutionContinues(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,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000747 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremenek31061982009-03-31 23:00:32 +0000750 break;
751 }
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenek31061982009-03-31 23:00:32 +0000753 case Stmt::IfStmtClass: {
754 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek31061982009-03-31 23:00:32 +0000756 if (const Stmt *S = End.asStmt())
757 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Ted Kremenek31061982009-03-31 23:00:32 +0000759 if (*(Src->succ_begin()+1) == Dst)
760 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000761 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000762 else
Ted Kremenek31061982009-03-31 23:00:32 +0000763 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000764 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Ted Kremenek31061982009-03-31 23:00:32 +0000766 break;
767 }
768 }
769 }
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000771 if (NextNode) {
772 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
773 E = PDB.visitor_end(); I!=E; ++I) {
774 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
775 PD.push_front(p);
776 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
779 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000780 // Scan the region bindings, and see if a "notable" symbol has a new
781 // lval binding.
782 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
783 PDB.getStateManager().iterBindings(N->getState(), SNS);
784 }
785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek14856d72009-04-06 23:06:54 +0000787 // After constructing the full PathDiagnostic, do a pass over it to compact
788 // PathDiagnosticPieces that occur within a macro.
789 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000790}
791
792//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000793// "Extensive" PathDiagnostic generation.
794//===----------------------------------------------------------------------===//
795
796static bool IsControlFlowExpr(const Stmt *S) {
797 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000799 if (!E)
800 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
802 E = E->IgnoreParenCasts();
803
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000804 if (isa<ConditionalOperator>(E))
805 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000807 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
808 if (B->isLogicalOp())
809 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
811 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000812}
813
Ted Kremenek14856d72009-04-06 23:06:54 +0000814namespace {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000815class VISIBILITY_HIDDEN ContextLocation : public PathDiagnosticLocation {
816 bool IsDead;
817public:
818 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
819 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000820
821 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000822 bool isDead() const { return IsDead; }
823};
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek14856d72009-04-06 23:06:54 +0000825class VISIBILITY_HIDDEN EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000826 std::vector<ContextLocation> CLocs;
827 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000828 PathDiagnostic &PD;
829 PathDiagnosticBuilder &PDB;
830 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000832 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremenek14856d72009-04-06 23:06:54 +0000834 bool containsLocation(const PathDiagnosticLocation &Container,
835 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenek14856d72009-04-06 23:06:54 +0000837 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek9650cf32009-05-11 21:42:34 +0000839 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
840 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000841 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000842 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000843 while (1) {
844 // Adjust the location for some expressions that are best referenced
845 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000846 switch (S->getStmtClass()) {
847 default:
848 break;
849 case Stmt::ParenExprClass:
850 S = cast<ParenExpr>(S)->IgnoreParens();
851 firstCharOnly = true;
852 continue;
853 case Stmt::ConditionalOperatorClass:
854 S = cast<ConditionalOperator>(S)->getCond();
855 firstCharOnly = true;
856 continue;
857 case Stmt::ChooseExprClass:
858 S = cast<ChooseExpr>(S)->getCond();
859 firstCharOnly = true;
860 continue;
861 case Stmt::BinaryOperatorClass:
862 S = cast<BinaryOperator>(S)->getLHS();
863 firstCharOnly = true;
864 continue;
865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Ted Kremenek9650cf32009-05-11 21:42:34 +0000867 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek9650cf32009-05-11 21:42:34 +0000870 if (S != Original)
871 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Ted Kremenek9650cf32009-05-11 21:42:34 +0000874 if (firstCharOnly)
875 L = PathDiagnosticLocation(L.asLocation());
876
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000877 return L;
878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek14856d72009-04-06 23:06:54 +0000880 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000881 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000882 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000883 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000884 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000885 CLocs.pop_back();
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
888 PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000889
890public:
891 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
892 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremeneka301a672009-04-22 18:16:20 +0000894 // If the PathDiagnostic already has pieces, add the enclosing statement
895 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000896 if (!PD.empty()) {
897 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Ted Kremenek14856d72009-04-06 23:06:54 +0000899 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000900 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000901 }
902 }
903
904 ~EdgeBuilder() {
905 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Ted Kremeneka301a672009-04-22 18:16:20 +0000907 // Finally, add an initial edge from the start location of the first
908 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000909 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
910 if (const CompoundStmt *CS =
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000911 PDB.getCodeDecl().getCompoundBody())
Ted Kremeneka301a672009-04-22 18:16:20 +0000912 if (!CS->body_empty()) {
913 SourceLocation Loc = (*CS->body_begin())->getLocStart();
914 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenek14856d72009-04-06 23:06:54 +0000917 }
918
919 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenek14856d72009-04-06 23:06:54 +0000921 void addEdge(const Stmt *S, bool alwaysAdd = false) {
922 addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd);
923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000925 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenek14856d72009-04-06 23:06:54 +0000927 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000928 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000929};
Ted Kremenek14856d72009-04-06 23:06:54 +0000930} // end anonymous namespace
931
932
933PathDiagnosticLocation
934EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
935 if (const Stmt *S = L.asStmt()) {
936 if (IsControlFlowExpr(S))
937 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
939 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremenek14856d72009-04-06 23:06:54 +0000942 return L;
943}
944
945bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
946 const PathDiagnosticLocation &Containee) {
947
948 if (Container == Containee)
949 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenek14856d72009-04-06 23:06:54 +0000951 if (Container.asDecl())
952 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek14856d72009-04-06 23:06:54 +0000954 if (const Stmt *S = Containee.asStmt())
955 if (const Stmt *ContainerS = Container.asStmt()) {
956 while (S) {
957 if (S == ContainerS)
958 return true;
959 S = PDB.getParent(S);
960 }
961 return false;
962 }
963
964 // Less accurate: compare using source ranges.
965 SourceRange ContainerR = Container.asRange();
966 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Ted Kremenek14856d72009-04-06 23:06:54 +0000968 SourceManager &SM = PDB.getSourceManager();
969 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
970 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
971 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
972 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Ted Kremenek14856d72009-04-06 23:06:54 +0000974 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
975 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
976 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
977 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenek14856d72009-04-06 23:06:54 +0000979 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000980 assert(ContaineeBegLine <= ContaineeEndLine);
981
Ted Kremenek14856d72009-04-06 23:06:54 +0000982 return (ContainerBegLine <= ContaineeBegLine &&
983 ContainerEndLine >= ContaineeEndLine &&
984 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +0000985 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +0000986 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
987 (ContainerEndLine != ContaineeEndLine ||
988 SM.getInstantiationColumnNumber(ContainerREnd) >=
989 SM.getInstantiationColumnNumber(ContainerREnd)));
990}
991
992PathDiagnosticLocation
993EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) {
994 if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt()))
995 return PathDiagnosticLocation(E->IgnoreParenCasts(),
996 PDB.getSourceManager());
997 return L;
998}
999
1000void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1001 if (!PrevLoc.isValid()) {
1002 PrevLoc = NewLoc;
1003 return;
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001006 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1007 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001009 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001010 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Ted Kremenek14856d72009-04-06 23:06:54 +00001012 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001013 if (NewLocClean.asLocation().getInstantiationLoc() ==
1014 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001015 return;
1016
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001017 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1018 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001019}
1020
1021void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremeneka301a672009-04-22 18:16:20 +00001023 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1024 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek14856d72009-04-06 23:06:54 +00001026 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1027
1028 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001029 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Ted Kremenek14856d72009-04-06 23:06:54 +00001031 // Is the top location context the same as the one for the new location?
1032 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001033 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001034 if (IsConsumedExpr(TopContextLoc) &&
1035 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001036 TopContextLoc.markDead();
1037
Ted Kremenek14856d72009-04-06 23:06:54 +00001038 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001040
1041 return;
1042 }
1043
1044 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001045 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001046 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001048 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001049 CLocs.push_back(ContextLocation(CLoc, true));
1050 return;
1051 }
1052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Ted Kremenek14856d72009-04-06 23:06:54 +00001054 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001055 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001056 }
1057
1058 // Context does not contain the location. Flush it.
1059 popLocation();
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001062 // If we reach here, there is no enclosing context. Just add the edge.
1063 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001064}
1065
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001066bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1067 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1068 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001070 return false;
1071}
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Ted Kremeneke1baed32009-05-05 23:13:38 +00001073void EdgeBuilder::addExtendedContext(const Stmt *S) {
1074 if (!S)
1075 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
1077 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001078 while (Parent) {
1079 if (isa<CompoundStmt>(Parent))
1080 Parent = PDB.getParent(Parent);
1081 else
1082 break;
1083 }
1084
1085 if (Parent) {
1086 switch (Parent->getStmtClass()) {
1087 case Stmt::DoStmtClass:
1088 case Stmt::ObjCAtSynchronizedStmtClass:
1089 addContext(Parent);
1090 default:
1091 break;
1092 }
1093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremeneke1baed32009-05-05 23:13:38 +00001095 addContext(S);
1096}
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Ted Kremenek14856d72009-04-06 23:06:54 +00001098void EdgeBuilder::addContext(const Stmt *S) {
1099 if (!S)
1100 return;
1101
1102 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Ted Kremenek14856d72009-04-06 23:06:54 +00001104 while (!CLocs.empty()) {
1105 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1106
1107 // Is the top location context the same as the one for the new location?
1108 if (TopContextLoc == L)
1109 return;
1110
1111 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001112 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001113 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001114 }
1115
1116 // Context does not contain the location. Flush it.
1117 popLocation();
1118 }
1119
1120 CLocs.push_back(L);
1121}
1122
1123static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1124 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001125 const ExplodedNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +00001126
1127
Ted Kremenek14856d72009-04-06 23:06:54 +00001128 EdgeBuilder EB(PD, PDB);
1129
Mike Stump1eb44332009-09-09 15:08:12 +00001130 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek14856d72009-04-06 23:06:54 +00001131 ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001132 while (NextNode) {
1133 N = NextNode;
1134 NextNode = GetPredecessorNode(N);
1135 ProgramPoint P = N->getLocation();
1136
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001137 do {
1138 // Block edges.
1139 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1140 const CFGBlock &Blk = *BE->getSrc();
1141 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001143 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001144 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001145 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001146 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001148 if (!Term) {
1149 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1150 CS = dyn_cast<CompoundStmt>(FS->getBody());
1151 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001152 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001153 }
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001155 PathDiagnosticEventPiece *p =
1156 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001157 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001159 EB.addEdge(p->getLocation(), true);
1160 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001162 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001163 PathDiagnosticLocation BL(CS->getRBracLoc(),
1164 PDB.getSourceManager());
1165 BL = PathDiagnosticLocation(BL.asLocation());
1166 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001167 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001170 if (Term)
1171 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001173 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001174 }
1175
Mike Stump1eb44332009-09-09 15:08:12 +00001176 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001177 if (const Stmt* S = BE->getFirstStmt()) {
1178 if (IsControlFlowExpr(S)) {
1179 // Add the proper context for '&&', '||', and '?'.
1180 EB.addContext(S);
1181 }
1182 else
1183 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1184 }
1185
1186 break;
1187 }
1188 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001190 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001191 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremenek8966bc12009-05-06 21:39:49 +00001193 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1194 E = PDB.visitor_end(); I!=E; ++I) {
1195 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1196 const PathDiagnosticLocation &Loc = p->getLocation();
1197 EB.addEdge(Loc, true);
1198 PD.push_front(p);
1199 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001200 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001203 }
1204}
1205
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001206//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001207// Methods for BugType and subclasses.
1208//===----------------------------------------------------------------------===//
1209BugType::~BugType() {}
1210void 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 {
1219 ProgramPoint ProgP = EndNode->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)
1225 S = GetPreviousStmt(EndNode);
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
Ted Kremenekde7161f2008-04-03 18:00:37 +00001242 const SourceRange *Beg, *End;
Mike Stump1eb44332009-09-09 15:08:12 +00001243 getRanges(Beg, End);
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
Mike Stump1eb44332009-09-09 15:08:12 +00001257void BugReport::getRanges(const SourceRange*& beg, const SourceRange*& end) {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001258 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001259 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001260 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001261 beg = &R;
1262 end = beg+1;
1263 }
1264 else
1265 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001266}
1267
Mike Stump1eb44332009-09-09 15:08:12 +00001268SourceLocation BugReport::getLocation() const {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001269 if (EndNode)
Ted Kremenek5f85e172009-07-22 22:35:28 +00001270 if (const Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001271 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5f85e172009-07-22 22:35:28 +00001272 if (const MemberExpr* ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001273 return ME->getMemberLoc();
1274
Ted Kremenekcf118d42009-02-04 23:49:09 +00001275 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001276 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001277
1278 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001279}
1280
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001281PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1282 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001283 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001284 return NULL;
1285}
1286
Ted Kremenekcf118d42009-02-04 23:49:09 +00001287//===----------------------------------------------------------------------===//
1288// Methods for BugReporter and subclasses.
1289//===----------------------------------------------------------------------===//
1290
1291BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001292 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001293}
1294
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001295GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001296BugReporterData::~BugReporterData() {}
1297
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001298ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001299
1300GRStateManager&
1301GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1302
1303BugReporter::~BugReporter() { FlushReports(); }
1304
1305void BugReporter::FlushReports() {
1306 if (BugTypes.isEmpty())
1307 return;
1308
1309 // First flush the warnings for each BugType. This may end up creating new
1310 // warnings and new BugTypes. Because ImmutableSet is a functional data
1311 // structure, we do not need to worry about the iterators being invalidated.
1312 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1313 const_cast<BugType*>(*I)->FlushReports(*this);
1314
1315 // Iterate through BugTypes a second time. BugTypes may have been updated
1316 // with new BugType objects and new warnings.
1317 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1318 BugType *BT = const_cast<BugType*>(*I);
1319
1320 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1321 SetTy& EQClasses = BT->EQClasses;
1322
1323 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1324 BugReportEquivClass& EQ = *EI;
1325 FlushReport(EQ);
1326 }
Mike Stump1eb44332009-09-09 15:08:12 +00001327
1328 // Delete the BugType object.
Zhongxing Xud9b401c2009-07-29 08:13:37 +00001329
1330 // FIXME: this will *not* delete the BugReportEquivClasses, since FoldingSet
1331 // only deletes the buckets, not the nodes themselves.
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.
1336 BugTypes = F.GetEmptySet();
1337}
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,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001346 const ExplodedNode** NStart,
1347 const ExplodedNode** NEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Ted Kremenekcf118d42009-02-04 23:49:09 +00001349 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001350 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001351 // error node unless there are two or more error nodes with the same minimum
1352 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001353 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001354 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001355
1356 llvm::DenseMap<const void*, const void*> InverseMap;
1357 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &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
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001372 for (const ExplodedNode** I = NStart; I != NEnd; ++I)
1373 if (const ExplodedNode *N = NMap->getMappedNode(*I)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001374 unsigned NodeIndex = (I - NStart) / sizeof(*I);
1375 WS.push(N);
1376 IndexMap[*I] = NodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001377 }
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Ted Kremenek938332c2009-05-16 01:11:58 +00001379 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001380
1381 // Create a new (third!) graph with a single path. This is the graph
1382 // that will be returned to the caller.
Zhongxing Xucc025532009-08-25 03:33:41 +00001383 ExplodedGraph *GNew = new ExplodedGraph(GTrim->getContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Ted Kremenek10aa5542009-03-12 23:41:59 +00001385 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001386 // to the root node, and then construct a new graph that contains only
1387 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001388 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001390 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001391 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001393 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001394 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001395 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001397 if (Visited.find(Node) != Visited.end())
1398 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001400 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001402 if (Node->pred_empty()) {
1403 Root = Node;
1404 break;
1405 }
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001407 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001408 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001409 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001410 }
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Ted Kremenek938332c2009-05-16 01:11:58 +00001412 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Ted Kremenek10aa5542009-03-12 23:41:59 +00001414 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001415 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001416 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001417 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001418 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001420 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001421 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001422 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001423 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001425 // Create the equivalent node in the new graph with the same state
1426 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001427 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001429 // Store the mapping to the original node.
1430 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1431 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001432 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001434 // Link up the new node with the previous node.
1435 if (Last)
1436 NewN->addPredecessor(Last);
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001438 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001440 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001441 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001442 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001443 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001444 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001445 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001446 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001449 // Find the next successor node. We choose the node that is marked
1450 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001451 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1452 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001453 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001455 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001457 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001459 if (I == Visited.end())
1460 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001462 if (!N || I->second < MinVal) {
1463 N = *SI;
1464 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001465 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001466 }
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Ted Kremenek938332c2009-05-16 01:11:58 +00001468 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001469 }
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Ted Kremenek938332c2009-05-16 01:11:58 +00001471 assert(First);
1472
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001473 return std::make_pair(std::make_pair(GNew, BM),
1474 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001475}
1476
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001477/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1478/// and collapses PathDiagosticPieces that are expanded by macros.
1479static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1480 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1481 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001483 typedef std::vector<PathDiagnosticPiece*>
1484 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001486 MacroStackTy MacroStack;
1487 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001489 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1490 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001491 const FullSourceLoc Loc = I->getLocation().asLocation();
1492
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001493 // Determine the instantiation location, which is the location we group
1494 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001495 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001496 SM.getInstantiationLoc(Loc) :
1497 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001499 if (Loc.isFileID()) {
1500 MacroStack.clear();
1501 Pieces.push_back(&*I);
1502 continue;
1503 }
1504
1505 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001507 // Is the PathDiagnosticPiece within the same macro group?
1508 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1509 MacroStack.back().first->push_back(&*I);
1510 continue;
1511 }
1512
1513 // We aren't in the same group. Are we descending into a new macro
1514 // or are part of an old one?
1515 PathDiagnosticMacroPiece *MacroGroup = 0;
1516
1517 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1518 SM.getInstantiationLoc(Loc) :
1519 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001521 // Walk the entire macro stack.
1522 while (!MacroStack.empty()) {
1523 if (InstantiationLoc == MacroStack.back().second) {
1524 MacroGroup = MacroStack.back().first;
1525 break;
1526 }
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001528 if (ParentInstantiationLoc == MacroStack.back().second) {
1529 MacroGroup = MacroStack.back().first;
1530 break;
1531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001533 MacroStack.pop_back();
1534 }
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001536 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1537 // Create a new macro group and add it to the stack.
1538 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1539
1540 if (MacroGroup)
1541 MacroGroup->push_back(NewGroup);
1542 else {
1543 assert(InstantiationLoc.isFileID());
1544 Pieces.push_back(NewGroup);
1545 }
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001547 MacroGroup = NewGroup;
1548 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1549 }
1550
1551 // Finally, add the PathDiagnosticPiece to the group.
1552 MacroGroup->push_back(&*I);
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001555 // Now take the pieces and construct a new PathDiagnostic.
1556 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001558 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1559 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1560 if (!MP->containsEvent()) {
1561 delete MP;
1562 continue;
1563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001565 PD.push_back(*I);
1566 }
1567}
1568
Ted Kremenek7dc86642009-03-31 20:22:36 +00001569void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001570 BugReportEquivClass& EQ) {
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001572 std::vector<const ExplodedNode*> Nodes;
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Ted Kremenekcf118d42009-02-04 23:49:09 +00001574 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001575 const ExplodedNode* N = I->getEndNode();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001576 if (N) Nodes.push_back(N);
1577 }
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Ted Kremenekcf118d42009-02-04 23:49:09 +00001579 if (Nodes.empty())
1580 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001582 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001583 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001584 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001585 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek7dc86642009-03-31 20:22:36 +00001586 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenekcf118d42009-02-04 23:49:09 +00001588 // Find the BugReport with the original location.
1589 BugReport *R = 0;
1590 unsigned i = 0;
1591 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
1592 if (i == GPair.second.second) { R = *I; break; }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenekcf118d42009-02-04 23:49:09 +00001594 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001596 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001597 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001598 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
1600 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001601 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Ted Kremenek8966bc12009-05-06 21:39:49 +00001603 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001604 PD.push_back(Piece);
1605 else
1606 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001608 R->registerInitialVisitors(PDB, N);
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Ted Kremenek7dc86642009-03-31 20:22:36 +00001610 switch (PDB.getGenerationScheme()) {
1611 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001612 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001613 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001614 case PathDiagnosticClient::Minimal:
1615 GenerateMinimalPathDiagnostic(PD, PDB, N);
1616 break;
1617 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001618}
1619
Ted Kremenekcf118d42009-02-04 23:49:09 +00001620void BugReporter::Register(BugType *BT) {
1621 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001622}
1623
Mike Stump1eb44332009-09-09 15:08:12 +00001624void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001625 // Compute the bug report's hash to determine its equivalence class.
1626 llvm::FoldingSetNodeID ID;
1627 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001628
1629 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001630 BugType& BT = R->getBugType();
1631 Register(&BT);
1632 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001633 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1634
Ted Kremenekcf118d42009-02-04 23:49:09 +00001635 if (!EQ) {
1636 EQ = new BugReportEquivClass(R);
1637 BT.EQClasses.InsertNode(EQ, InsertPos);
1638 }
1639 else
1640 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001641}
1642
Ted Kremenekcf118d42009-02-04 23:49:09 +00001643void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1644 assert(!EQ.Reports.empty());
1645 BugReport &R = **EQ.begin();
Ted Kremenekd49967f2009-04-29 21:58:13 +00001646 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Ted Kremenekcf118d42009-02-04 23:49:09 +00001648 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001649 // Probably doesn't make a difference in practice.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001650 BugType& BT = R.getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Ted Kremenekd49967f2009-04-29 21:58:13 +00001652 llvm::OwningPtr<PathDiagnostic>
1653 D(new PathDiagnostic(R.getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001654 !PD || PD->useVerboseDescription()
Ted Kremenekd49967f2009-04-29 21:58:13 +00001655 ? R.getDescription() : R.getShortDescription(),
1656 BT.getCategory()));
1657
Ted Kremenekcf118d42009-02-04 23:49:09 +00001658 GeneratePathDiagnostic(*D.get(), EQ);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Ted Kremenek072192b2008-04-30 23:47:44 +00001660 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001661 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001662 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001663
Ted Kremenek3148eb42009-01-24 00:55:43 +00001664 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001665 const SourceRange *Beg = 0, *End = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001666 R.getRanges(Beg, End);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001667 Diagnostic& Diag = getDiagnostic();
Mike Stump1eb44332009-09-09 15:08:12 +00001668 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001669 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001670 R.getShortDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001671
Ted Kremenek3148eb42009-01-24 00:55:43 +00001672 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001673 default: assert(0 && "Don't handle this many ranges yet!");
1674 case 0: Diag.Report(L, ErrorDiag); break;
1675 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1676 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1677 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001678 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001679
1680 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1681 if (!PD)
1682 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001683
1684 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001685 PathDiagnosticPiece* piece =
1686 new PathDiagnosticEventPiece(L, R.getDescription());
1687
Ted Kremenek3148eb42009-01-24 00:55:43 +00001688 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1689 D->push_back(piece);
1690 }
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Ted Kremenek3148eb42009-01-24 00:55:43 +00001692 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001693}
Ted Kremenek57202072008-07-14 17:40:50 +00001694
Ted Kremenek8c036c72008-09-20 04:23:38 +00001695void BugReporter::EmitBasicReport(const char* name, const char* str,
1696 SourceLocation Loc,
1697 SourceRange* RBeg, unsigned NumRanges) {
1698 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1699}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001700
Ted Kremenek8c036c72008-09-20 04:23:38 +00001701void BugReporter::EmitBasicReport(const char* name, const char* category,
1702 const char* str, SourceLocation Loc,
1703 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Ted Kremenekcf118d42009-02-04 23:49:09 +00001705 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1706 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001707 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001708 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1709 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1710 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001711}