blob: 8235f4acb179acb3731def2a93f603c6091ca491 [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
Ted Kremenekcf118d42009-02-04 23:49:09 +000039//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000040// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000041//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000042
Ted Kremenek5f85e172009-07-22 22:35:28 +000043static inline const Stmt* GetStmt(ProgramPoint P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000044 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
45 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000046 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000047 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000048
Ted Kremenekb697b102009-02-23 22:44:26 +000049 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000050}
51
Zhongxing Xuc5619d92009-08-06 01:32:16 +000052static inline const ExplodedNode*
53GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000054 return N->pred_empty() ? NULL : *(N->pred_begin());
55}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000056
Zhongxing Xuc5619d92009-08-06 01:32:16 +000057static inline const ExplodedNode*
58GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000059 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000060}
61
Zhongxing Xuc5619d92009-08-06 01:32:16 +000062static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000063 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000064 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000065 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekb697b102009-02-23 22:44:26 +000067 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000068}
69
Zhongxing Xuc5619d92009-08-06 01:32:16 +000070static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000071 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000072 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000073 // Check if the statement is '?' or '&&'/'||'. These are "merges",
74 // not actual statement points.
75 switch (S->getStmtClass()) {
76 case Stmt::ChooseExprClass:
77 case Stmt::ConditionalOperatorClass: continue;
78 case Stmt::BinaryOperatorClass: {
79 BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode();
80 if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr)
81 continue;
82 break;
83 }
84 default:
85 break;
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Ted Kremenekb7c51522009-07-28 00:07:15 +000088 // Some expressions don't have locations.
89 if (S->getLocStart().isInvalid())
90 continue;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenekb697b102009-02-23 22:44:26 +000092 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000093 }
Mike Stump1eb44332009-09-09 15:08:12 +000094
Ted Kremenekb697b102009-02-23 22:44:26 +000095 return 0;
96}
97
Ted Kremenek5f85e172009-07-22 22:35:28 +000098static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +000099GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000100 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000101 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekb697b102009-02-23 22:44:26 +0000103 return GetPreviousStmt(N);
104}
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek5f85e172009-07-22 22:35:28 +0000106static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000107GetCurrentOrNextStmt(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 GetNextStmt(N);
112}
113
114//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000115// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000116//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000117
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000118typedef llvm::DenseMap<const ExplodedNode*,
119const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000120
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000121namespace {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000122class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
123 NodeBackMap& M;
124public:
125 NodeMapClosure(NodeBackMap *m) : M(*m) {}
126 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000128 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000129 NodeBackMap::iterator I = M.find(N);
130 return I == M.end() ? 0 : I->second;
131 }
132};
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek8966bc12009-05-06 21:39:49 +0000134class VISIBILITY_HIDDEN PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000135 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000136 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000137 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000138 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000139public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000140 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000141 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000142 PathDiagnosticClient *pdc)
143 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000144 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000145 addVisitor(R);
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000148 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenek00605e02009-03-27 20:55:39 +0000150 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000151 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000153 Decl const &getCodeDecl() { return R->getEndNode()->getCodeDecl(); }
154
155 ParentMap& getParentMap() { return R->getEndNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000157 const Stmt *getParent(const Stmt *S) {
158 return getParentMap().getParent(S);
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenek8966bc12009-05-06 21:39:49 +0000161 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Ted Kremenek7dc86642009-03-31 20:22:36 +0000162 BugReport& getReport() { return *R; }
Douglas Gregor72971342009-04-18 00:02:19 +0000163
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000164 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000166 PathDiagnosticLocation
167 getEnclosingStmtLocation(const PathDiagnosticLocation &L) {
168 if (const Stmt *S = L.asStmt())
169 return getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000171 return L;
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremenek7dc86642009-03-31 20:22:36 +0000174 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
175 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
176 }
177
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000178 bool supportsLogicalOpControlFlow() const {
179 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000180 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000181};
182} // end anonymous namespace
183
Ted Kremenek00605e02009-03-27 20:55:39 +0000184PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000185PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000186 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000187 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000188
Mike Stump1eb44332009-09-09 15:08:12 +0000189 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000190 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000191}
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenek00605e02009-03-27 20:55:39 +0000193PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000194PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000195 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000196
Ted Kremenek143ca222008-05-06 18:11:09 +0000197 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000198 if (os.str().empty())
199 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek00605e02009-03-27 20:55:39 +0000201 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek00605e02009-03-27 20:55:39 +0000203 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000204 os << "Execution continues on line "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000205 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
206 << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000207 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000208 os << "Execution jumps to the end of the "
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000209 << (isa<ObjCMethodDecl>(N->getLocationContext()->getDecl()) ?
210 "method" : "function") << '.';
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000212 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000213}
214
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000215static bool IsNested(const Stmt *S, ParentMap &PM) {
216 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
217 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000219 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000221 if (Parent)
222 switch (Parent->getStmtClass()) {
223 case Stmt::ForStmtClass:
224 case Stmt::DoStmtClass:
225 case Stmt::WhileStmtClass:
226 return true;
227 default:
228 break;
229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230
231 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000232}
233
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000234PathDiagnosticLocation
235PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
236 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000237 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000238 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000239
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000240 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000241 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000243 if (!Parent)
244 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000246 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000247 case Stmt::BinaryOperatorClass: {
248 const BinaryOperator *B = cast<BinaryOperator>(Parent);
249 if (B->isLogicalOp())
250 return PathDiagnosticLocation(S, SMgr);
251 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000252 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000253 case Stmt::CompoundStmtClass:
254 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000255 return PathDiagnosticLocation(S, SMgr);
256 case Stmt::ChooseExprClass:
257 // Similar to '?' if we are referring to condition, just have the edge
258 // point to the entire choose expression.
259 if (cast<ChooseExpr>(Parent)->getCond() == S)
260 return PathDiagnosticLocation(Parent, SMgr);
261 else
Mike Stump1eb44332009-09-09 15:08:12 +0000262 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000263 case Stmt::ConditionalOperatorClass:
264 // For '?', if we are referring to condition, just have the edge point
265 // to the entire '?' expression.
266 if (cast<ConditionalOperator>(Parent)->getCond() == S)
267 return PathDiagnosticLocation(Parent, SMgr);
268 else
Mike Stump1eb44332009-09-09 15:08:12 +0000269 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000270 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000271 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000272 case Stmt::ForStmtClass:
273 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000274 return PathDiagnosticLocation(S, SMgr);
275 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000276 case Stmt::IfStmtClass:
277 if (cast<IfStmt>(Parent)->getCond() != S)
278 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000279 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000280 case Stmt::ObjCForCollectionStmtClass:
281 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
282 return PathDiagnosticLocation(S, SMgr);
283 break;
284 case Stmt::WhileStmtClass:
285 if (cast<WhileStmt>(Parent)->getCond() != S)
286 return PathDiagnosticLocation(S, SMgr);
287 break;
288 default:
289 break;
290 }
291
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000292 S = Parent;
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000295 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000296
297 // Special case: DeclStmts can appear in for statement declarations, in which
298 // case the ForStmt is the context.
299 if (isa<DeclStmt>(S)) {
300 if (const Stmt *Parent = P.getParent(S)) {
301 switch (Parent->getStmtClass()) {
302 case Stmt::ForStmtClass:
303 case Stmt::ObjCForCollectionStmtClass:
304 return PathDiagnosticLocation(Parent, SMgr);
305 default:
306 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000307 }
308 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000309 }
310 else if (isa<BinaryOperator>(S)) {
311 // Special case: the binary operator represents the initialization
312 // code in a for statement (this can happen when the variable being
313 // initialized is an old variable.
314 if (const ForStmt *FS =
315 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
316 if (FS->getInit() == S)
317 return PathDiagnosticLocation(FS, SMgr);
318 }
319 }
320
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000321 return PathDiagnosticLocation(S, SMgr);
322}
323
Ted Kremenekcf118d42009-02-04 23:49:09 +0000324//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000325// ScanNotableSymbols: closure-like callback for scanning Store bindings.
326//===----------------------------------------------------------------------===//
327
328static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000329GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000330 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek31061982009-03-31 23:00:32 +0000332 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek31061982009-03-31 23:00:32 +0000334 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek31061982009-03-31 23:00:32 +0000336 if (!isa<PostStmt>(P))
337 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek5f85e172009-07-22 22:35:28 +0000339 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek31061982009-03-31 23:00:32 +0000341 if (!DR)
342 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000344 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek31061982009-03-31 23:00:32 +0000346 if (X != Y)
347 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek5f85e172009-07-22 22:35:28 +0000349 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek31061982009-03-31 23:00:32 +0000351 if (!VD)
352 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek31061982009-03-31 23:00:32 +0000354 return VD;
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek31061982009-03-31 23:00:32 +0000357 return 0;
358}
359
360namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000361class VISIBILITY_HIDDEN NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000362: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek31061982009-03-31 23:00:32 +0000364 SymbolRef Sym;
365 const GRState* PrevSt;
366 const Stmt* S;
367 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000368 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000369 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000370 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek31061982009-03-31 23:00:32 +0000372public:
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek31061982009-03-31 23:00:32 +0000374 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000375 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000376 PathDiagnostic& pd, BugReporter& br)
377 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Ted Kremenek31061982009-03-31 23:00:32 +0000379 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
380 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek31061982009-03-31 23:00:32 +0000382 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek31061982009-03-31 23:00:32 +0000384 if (ScanSym != Sym)
385 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
387 // Check if the previous state has this binding.
Ted Kremenekdbc2afc2009-06-23 17:55:07 +0000388 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek31061982009-03-31 23:00:32 +0000390 if (X == V) // Same binding?
391 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek31061982009-03-31 23:00:32 +0000393 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000394 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000395 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek31061982009-03-31 23:00:32 +0000397 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek31061982009-03-31 23:00:32 +0000399 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
400 if (!B->isAssignmentOp())
401 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 // What variable did we assign to?
404 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek31061982009-03-31 23:00:32 +0000406 if (!DR)
407 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek31061982009-03-31 23:00:32 +0000409 VD = dyn_cast<VarDecl>(DR->getDecl());
410 }
411 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
412 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
413 // assume that each DeclStmt has a single Decl. This invariant
414 // holds by contruction in the CFG.
415 VD = dyn_cast<VarDecl>(*DS->decl_begin());
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek31061982009-03-31 23:00:32 +0000418 if (!VD)
419 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek31061982009-03-31 23:00:32 +0000421 // What is the most recently referenced variable with this binding?
422 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek31061982009-03-31 23:00:32 +0000424 if (!MostRecent)
425 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 // Create the diagnostic.
428 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek31061982009-03-31 23:00:32 +0000430 if (Loc::IsLocType(VD->getType())) {
431 std::string msg = "'" + std::string(VD->getNameAsString()) +
432 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 PD.push_front(new PathDiagnosticEventPiece(L, msg));
435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000438 }
Ted Kremenek31061982009-03-31 23:00:32 +0000439};
440}
441
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000442static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000443 const Stmt* S,
444 SymbolRef Sym, BugReporter& BR,
445 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000447 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000448 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Ted Kremenek31061982009-03-31 23:00:32 +0000450 if (!PrevSt)
451 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenek31061982009-03-31 23:00:32 +0000453 // Look at the region bindings of the current state that map to the
454 // specified symbol. Are any of them not in the previous state?
455 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
456 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
457 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
458}
459
460namespace {
461class VISIBILITY_HIDDEN ScanNotableSymbols
462: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000465 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000466 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000467 GRBugReporter& BR;
468 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremenek31061982009-03-31 23:00:32 +0000470public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000471 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000472 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000473 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremenek31061982009-03-31 23:00:32 +0000475 bool HandleBinding(StoreManager& SMgr, Store store,
476 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek31061982009-03-31 23:00:32 +0000478 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek31061982009-03-31 23:00:32 +0000480 if (!ScanSym)
481 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek31061982009-03-31 23:00:32 +0000483 if (!BR.isNotable(ScanSym))
484 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek31061982009-03-31 23:00:32 +0000486 if (AlreadyProcessed.count(ScanSym))
487 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek31061982009-03-31 23:00:32 +0000491 HandleNotableSymbol(N, S, ScanSym, BR, PD);
492 return true;
493 }
494};
495} // end anonymous namespace
496
497//===----------------------------------------------------------------------===//
498// "Minimal" path diagnostic generation algorithm.
499//===----------------------------------------------------------------------===//
500
Ted Kremenek14856d72009-04-06 23:06:54 +0000501static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
502
Ted Kremenek31061982009-03-31 23:00:32 +0000503static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
504 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000505 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000506
Ted Kremenek31061982009-03-31 23:00:32 +0000507 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000508 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000509 ? NULL : *(N->pred_begin());
510 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000511 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000512 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek31061982009-03-31 23:00:32 +0000514 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenek31061982009-03-31 23:00:32 +0000516 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
517 CFGBlock* Src = BE->getSrc();
518 CFGBlock* Dst = BE->getDst();
519 Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek31061982009-03-31 23:00:32 +0000521 if (!T)
522 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenek31061982009-03-31 23:00:32 +0000524 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Ted Kremenek31061982009-03-31 23:00:32 +0000526 switch (T->getStmtClass()) {
527 default:
528 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek31061982009-03-31 23:00:32 +0000530 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000531 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000532 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Ted Kremenek31061982009-03-31 23:00:32 +0000534 if (!S)
535 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Ted Kremenek31061982009-03-31 23:00:32 +0000537 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000538 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000539 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek31061982009-03-31 23:00:32 +0000541 os << "Control jumps to line "
542 << End.asLocation().getInstantiationLineNumber();
543 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
544 os.str()));
545 break;
546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
548 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000549 // Figure out what case arm we took.
550 std::string sbuf;
551 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek31061982009-03-31 23:00:32 +0000553 if (Stmt* S = Dst->getLabel()) {
554 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Ted Kremenek31061982009-03-31 23:00:32 +0000556 switch (S->getStmtClass()) {
557 default:
558 os << "No cases match in the switch statement. "
559 "Control jumps to line "
560 << End.asLocation().getInstantiationLineNumber();
561 break;
562 case Stmt::DefaultStmtClass:
563 os << "Control jumps to the 'default' case at line "
564 << End.asLocation().getInstantiationLineNumber();
565 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek31061982009-03-31 23:00:32 +0000567 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000568 os << "Control jumps to 'case ";
569 CaseStmt* Case = cast<CaseStmt>(S);
Ted Kremenek31061982009-03-31 23:00:32 +0000570 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000571
572 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000573 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Ted Kremenek31061982009-03-31 23:00:32 +0000575 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
576 // FIXME: Maybe this should be an assertion. Are there cases
577 // were it is not an EnumConstantDecl?
578 EnumConstantDecl* D =
579 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremenek31061982009-03-31 23:00:32 +0000581 if (D) {
582 GetRawInt = false;
583 os << D->getNameAsString();
584 }
585 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000586
587 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000588 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000589
Ted Kremenek31061982009-03-31 23:00:32 +0000590 os << ":' at line "
591 << End.asLocation().getInstantiationLineNumber();
592 break;
593 }
594 }
595 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
596 os.str()));
597 }
598 else {
599 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000600 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000601 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
602 os.str()));
603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenek31061982009-03-31 23:00:32 +0000605 break;
606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Ted Kremenek31061982009-03-31 23:00:32 +0000608 case Stmt::BreakStmtClass:
609 case Stmt::ContinueStmtClass: {
610 std::string sbuf;
611 llvm::raw_string_ostream os(sbuf);
612 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
613 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
614 os.str()));
615 break;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenek31061982009-03-31 23:00:32 +0000618 // Determine control-flow for ternary '?'.
619 case Stmt::ConditionalOperatorClass: {
620 std::string sbuf;
621 llvm::raw_string_ostream os(sbuf);
622 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Ted Kremenek31061982009-03-31 23:00:32 +0000624 if (*(Src->succ_begin()+1) == Dst)
625 os << "false";
626 else
627 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek31061982009-03-31 23:00:32 +0000629 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Ted Kremenek31061982009-03-31 23:00:32 +0000631 if (const Stmt *S = End.asStmt())
632 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Ted Kremenek31061982009-03-31 23:00:32 +0000634 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
635 os.str()));
636 break;
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 // Determine control-flow for short-circuited '&&' and '||'.
640 case Stmt::BinaryOperatorClass: {
641 if (!PDB.supportsLogicalOpControlFlow())
642 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek31061982009-03-31 23:00:32 +0000644 BinaryOperator *B = cast<BinaryOperator>(T);
645 std::string sbuf;
646 llvm::raw_string_ostream os(sbuf);
647 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 if (B->getOpcode() == BinaryOperator::LAnd) {
650 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek31061982009-03-31 23:00:32 +0000652 if (*(Src->succ_begin()+1) == Dst) {
653 os << "false";
654 PathDiagnosticLocation End(B->getLHS(), SMgr);
655 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
656 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
657 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000658 }
Ted Kremenek31061982009-03-31 23:00:32 +0000659 else {
660 os << "true";
661 PathDiagnosticLocation Start(B->getLHS(), SMgr);
662 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
663 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
664 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000665 }
Ted Kremenek31061982009-03-31 23:00:32 +0000666 }
667 else {
668 assert(B->getOpcode() == BinaryOperator::LOr);
669 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek31061982009-03-31 23:00:32 +0000671 if (*(Src->succ_begin()+1) == Dst) {
672 os << "false";
673 PathDiagnosticLocation Start(B->getLHS(), SMgr);
674 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
675 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000676 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000677 }
678 else {
679 os << "true";
680 PathDiagnosticLocation End(B->getLHS(), SMgr);
681 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
682 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000683 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000684 }
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremenek31061982009-03-31 23:00:32 +0000687 break;
688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
690 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000691 if (*(Src->succ_begin()) == Dst) {
692 std::string sbuf;
693 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Ted Kremenek31061982009-03-31 23:00:32 +0000695 os << "Loop condition is true. ";
696 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek31061982009-03-31 23:00:32 +0000698 if (const Stmt *S = End.asStmt())
699 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek31061982009-03-31 23:00:32 +0000701 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
702 os.str()));
703 }
704 else {
705 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 if (const Stmt *S = End.asStmt())
708 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek31061982009-03-31 23:00:32 +0000710 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
711 "Loop condition is false. Exiting loop"));
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek31061982009-03-31 23:00:32 +0000714 break;
715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenek31061982009-03-31 23:00:32 +0000717 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000718 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000719 if (*(Src->succ_begin()+1) == Dst) {
720 std::string sbuf;
721 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek31061982009-03-31 23:00:32 +0000723 os << "Loop condition is false. ";
724 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
725 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 os.str()));
730 }
731 else {
732 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
733 if (const Stmt *S = End.asStmt())
734 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Ted Kremenek31061982009-03-31 23:00:32 +0000736 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000737 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek31061982009-03-31 23:00:32 +0000740 break;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek31061982009-03-31 23:00:32 +0000743 case Stmt::IfStmtClass: {
744 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek31061982009-03-31 23:00:32 +0000746 if (const Stmt *S = End.asStmt())
747 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek31061982009-03-31 23:00:32 +0000749 if (*(Src->succ_begin()+1) == Dst)
750 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000751 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000752 else
Ted Kremenek31061982009-03-31 23:00:32 +0000753 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000754 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek31061982009-03-31 23:00:32 +0000756 break;
757 }
758 }
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000761 if (NextNode) {
762 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
763 E = PDB.visitor_end(); I!=E; ++I) {
764 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
765 PD.push_front(p);
766 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
769 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000770 // Scan the region bindings, and see if a "notable" symbol has a new
771 // lval binding.
772 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
773 PDB.getStateManager().iterBindings(N->getState(), SNS);
774 }
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek14856d72009-04-06 23:06:54 +0000777 // After constructing the full PathDiagnostic, do a pass over it to compact
778 // PathDiagnosticPieces that occur within a macro.
779 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000780}
781
782//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000783// "Extensive" PathDiagnostic generation.
784//===----------------------------------------------------------------------===//
785
786static bool IsControlFlowExpr(const Stmt *S) {
787 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000789 if (!E)
790 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000791
792 E = E->IgnoreParenCasts();
793
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000794 if (isa<ConditionalOperator>(E))
795 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000797 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
798 if (B->isLogicalOp())
799 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
801 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000802}
803
Ted Kremenek14856d72009-04-06 23:06:54 +0000804namespace {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000805class VISIBILITY_HIDDEN ContextLocation : public PathDiagnosticLocation {
806 bool IsDead;
807public:
808 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
809 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000810
811 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000812 bool isDead() const { return IsDead; }
813};
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek14856d72009-04-06 23:06:54 +0000815class VISIBILITY_HIDDEN EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000816 std::vector<ContextLocation> CLocs;
817 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000818 PathDiagnostic &PD;
819 PathDiagnosticBuilder &PDB;
820 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000822 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenek14856d72009-04-06 23:06:54 +0000824 bool containsLocation(const PathDiagnosticLocation &Container,
825 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek14856d72009-04-06 23:06:54 +0000827 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek9650cf32009-05-11 21:42:34 +0000829 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
830 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000831 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000832 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000833 while (1) {
834 // Adjust the location for some expressions that are best referenced
835 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000836 switch (S->getStmtClass()) {
837 default:
838 break;
839 case Stmt::ParenExprClass:
840 S = cast<ParenExpr>(S)->IgnoreParens();
841 firstCharOnly = true;
842 continue;
843 case Stmt::ConditionalOperatorClass:
844 S = cast<ConditionalOperator>(S)->getCond();
845 firstCharOnly = true;
846 continue;
847 case Stmt::ChooseExprClass:
848 S = cast<ChooseExpr>(S)->getCond();
849 firstCharOnly = true;
850 continue;
851 case Stmt::BinaryOperatorClass:
852 S = cast<BinaryOperator>(S)->getLHS();
853 firstCharOnly = true;
854 continue;
855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek9650cf32009-05-11 21:42:34 +0000857 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Ted Kremenek9650cf32009-05-11 21:42:34 +0000860 if (S != Original)
861 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Ted Kremenek9650cf32009-05-11 21:42:34 +0000864 if (firstCharOnly)
865 L = PathDiagnosticLocation(L.asLocation());
866
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000867 return L;
868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek14856d72009-04-06 23:06:54 +0000870 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000871 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000872 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000873 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000874 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000875 CLocs.pop_back();
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
878 PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000879
880public:
881 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
882 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremeneka301a672009-04-22 18:16:20 +0000884 // If the PathDiagnostic already has pieces, add the enclosing statement
885 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000886 if (!PD.empty()) {
887 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Ted Kremenek14856d72009-04-06 23:06:54 +0000889 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000890 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000891 }
892 }
893
894 ~EdgeBuilder() {
895 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremeneka301a672009-04-22 18:16:20 +0000897 // Finally, add an initial edge from the start location of the first
898 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000899 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
900 if (const CompoundStmt *CS =
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000901 PDB.getCodeDecl().getCompoundBody())
Ted Kremeneka301a672009-04-22 18:16:20 +0000902 if (!CS->body_empty()) {
903 SourceLocation Loc = (*CS->body_begin())->getLocStart();
904 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Ted Kremenek14856d72009-04-06 23:06:54 +0000907 }
908
909 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek14856d72009-04-06 23:06:54 +0000911 void addEdge(const Stmt *S, bool alwaysAdd = false) {
912 addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd);
913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000915 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenek14856d72009-04-06 23:06:54 +0000917 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000918 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000919};
Ted Kremenek14856d72009-04-06 23:06:54 +0000920} // end anonymous namespace
921
922
923PathDiagnosticLocation
924EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
925 if (const Stmt *S = L.asStmt()) {
926 if (IsControlFlowExpr(S))
927 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000928
929 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Ted Kremenek14856d72009-04-06 23:06:54 +0000932 return L;
933}
934
935bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
936 const PathDiagnosticLocation &Containee) {
937
938 if (Container == Containee)
939 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek14856d72009-04-06 23:06:54 +0000941 if (Container.asDecl())
942 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek14856d72009-04-06 23:06:54 +0000944 if (const Stmt *S = Containee.asStmt())
945 if (const Stmt *ContainerS = Container.asStmt()) {
946 while (S) {
947 if (S == ContainerS)
948 return true;
949 S = PDB.getParent(S);
950 }
951 return false;
952 }
953
954 // Less accurate: compare using source ranges.
955 SourceRange ContainerR = Container.asRange();
956 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Ted Kremenek14856d72009-04-06 23:06:54 +0000958 SourceManager &SM = PDB.getSourceManager();
959 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
960 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
961 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
962 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Ted Kremenek14856d72009-04-06 23:06:54 +0000964 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
965 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
966 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
967 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Ted Kremenek14856d72009-04-06 23:06:54 +0000969 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000970 assert(ContaineeBegLine <= ContaineeEndLine);
971
Ted Kremenek14856d72009-04-06 23:06:54 +0000972 return (ContainerBegLine <= ContaineeBegLine &&
973 ContainerEndLine >= ContaineeEndLine &&
974 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +0000975 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +0000976 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
977 (ContainerEndLine != ContaineeEndLine ||
978 SM.getInstantiationColumnNumber(ContainerREnd) >=
979 SM.getInstantiationColumnNumber(ContainerREnd)));
980}
981
982PathDiagnosticLocation
983EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) {
984 if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt()))
985 return PathDiagnosticLocation(E->IgnoreParenCasts(),
986 PDB.getSourceManager());
987 return L;
988}
989
990void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
991 if (!PrevLoc.isValid()) {
992 PrevLoc = NewLoc;
993 return;
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000996 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
997 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000999 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001000 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek14856d72009-04-06 23:06:54 +00001002 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001003 if (NewLocClean.asLocation().getInstantiationLoc() ==
1004 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001005 return;
1006
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001007 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1008 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001009}
1010
1011void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Ted Kremeneka301a672009-04-22 18:16:20 +00001013 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1014 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Ted Kremenek14856d72009-04-06 23:06:54 +00001016 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1017
1018 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001019 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Ted Kremenek14856d72009-04-06 23:06:54 +00001021 // Is the top location context the same as the one for the new location?
1022 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001023 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001024 if (IsConsumedExpr(TopContextLoc) &&
1025 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001026 TopContextLoc.markDead();
1027
Ted Kremenek14856d72009-04-06 23:06:54 +00001028 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001029 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001030
1031 return;
1032 }
1033
1034 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001035 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001036 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001038 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039 CLocs.push_back(ContextLocation(CLoc, true));
1040 return;
1041 }
1042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Ted Kremenek14856d72009-04-06 23:06:54 +00001044 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001045 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001046 }
1047
1048 // Context does not contain the location. Flush it.
1049 popLocation();
1050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001052 // If we reach here, there is no enclosing context. Just add the edge.
1053 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001054}
1055
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001056bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1057 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1058 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001060 return false;
1061}
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Ted Kremeneke1baed32009-05-05 23:13:38 +00001063void EdgeBuilder::addExtendedContext(const Stmt *S) {
1064 if (!S)
1065 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001066
1067 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001068 while (Parent) {
1069 if (isa<CompoundStmt>(Parent))
1070 Parent = PDB.getParent(Parent);
1071 else
1072 break;
1073 }
1074
1075 if (Parent) {
1076 switch (Parent->getStmtClass()) {
1077 case Stmt::DoStmtClass:
1078 case Stmt::ObjCAtSynchronizedStmtClass:
1079 addContext(Parent);
1080 default:
1081 break;
1082 }
1083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Ted Kremeneke1baed32009-05-05 23:13:38 +00001085 addContext(S);
1086}
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Ted Kremenek14856d72009-04-06 23:06:54 +00001088void EdgeBuilder::addContext(const Stmt *S) {
1089 if (!S)
1090 return;
1091
1092 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Ted Kremenek14856d72009-04-06 23:06:54 +00001094 while (!CLocs.empty()) {
1095 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1096
1097 // Is the top location context the same as the one for the new location?
1098 if (TopContextLoc == L)
1099 return;
1100
1101 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001102 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001103 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001104 }
1105
1106 // Context does not contain the location. Flush it.
1107 popLocation();
1108 }
1109
1110 CLocs.push_back(L);
1111}
1112
1113static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1114 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001115 const ExplodedNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +00001116
1117
Ted Kremenek14856d72009-04-06 23:06:54 +00001118 EdgeBuilder EB(PD, PDB);
1119
Mike Stump1eb44332009-09-09 15:08:12 +00001120 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek14856d72009-04-06 23:06:54 +00001121 ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001122 while (NextNode) {
1123 N = NextNode;
1124 NextNode = GetPredecessorNode(N);
1125 ProgramPoint P = N->getLocation();
1126
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001127 do {
1128 // Block edges.
1129 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1130 const CFGBlock &Blk = *BE->getSrc();
1131 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001133 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001134 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001135 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001136 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001138 if (!Term) {
1139 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1140 CS = dyn_cast<CompoundStmt>(FS->getBody());
1141 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001142 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001145 PathDiagnosticEventPiece *p =
1146 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001147 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001149 EB.addEdge(p->getLocation(), true);
1150 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001152 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001153 PathDiagnosticLocation BL(CS->getRBracLoc(),
1154 PDB.getSourceManager());
1155 BL = PathDiagnosticLocation(BL.asLocation());
1156 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001157 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001160 if (Term)
1161 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001163 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001164 }
1165
Mike Stump1eb44332009-09-09 15:08:12 +00001166 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001167 if (const Stmt* S = BE->getFirstStmt()) {
1168 if (IsControlFlowExpr(S)) {
1169 // Add the proper context for '&&', '||', and '?'.
1170 EB.addContext(S);
1171 }
1172 else
1173 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1174 }
1175
1176 break;
1177 }
1178 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001180 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001181 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Ted Kremenek8966bc12009-05-06 21:39:49 +00001183 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1184 E = PDB.visitor_end(); I!=E; ++I) {
1185 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1186 const PathDiagnosticLocation &Loc = p->getLocation();
1187 EB.addEdge(Loc, true);
1188 PD.push_front(p);
1189 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001190 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001191 }
Mike Stump1eb44332009-09-09 15:08:12 +00001192 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001193 }
1194}
1195
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001196//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001197// Methods for BugType and subclasses.
1198//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001199BugType::~BugType() {
1200 // Free up the equivalence class objects. Observe that we get a pointer to
1201 // the object first before incrementing the iterator, as destroying the
1202 // node before doing so means we will read from freed memory.
1203 for (iterator I = begin(), E = end(); I !=E; ) {
1204 BugReportEquivClass *EQ = &*I;
1205 ++I;
1206 delete EQ;
1207 }
1208}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001209void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001210
Ted Kremenekcf118d42009-02-04 23:49:09 +00001211//===----------------------------------------------------------------------===//
1212// Methods for BugReport and subclasses.
1213//===----------------------------------------------------------------------===//
1214BugReport::~BugReport() {}
1215RangedBugReport::~RangedBugReport() {}
1216
Mike Stump1eb44332009-09-09 15:08:12 +00001217const Stmt* BugReport::getStmt() const {
1218 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001219 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenekcf118d42009-02-04 23:49:09 +00001221 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001222 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001223 if (BE->getBlock() == &Exit)
1224 S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001225 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001226 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001227 S = GetStmt(ProgP);
1228
1229 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001230}
1231
1232PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001233BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001234 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001236 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Ted Kremenek61f3e052008-04-03 04:42:52 +00001238 if (!S)
1239 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001240
Ted Kremenekde7161f2008-04-03 18:00:37 +00001241 const SourceRange *Beg, *End;
Mike Stump1eb44332009-09-09 15:08:12 +00001242 getRanges(Beg, End);
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001243 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001245 // Only add the statement itself as a range if we didn't specify any
1246 // special ranges for this report.
1247 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1248 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001250 for (; Beg != End; ++Beg)
1251 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenek61f3e052008-04-03 04:42:52 +00001253 return P;
1254}
1255
Mike Stump1eb44332009-09-09 15:08:12 +00001256void BugReport::getRanges(const SourceRange*& beg, const SourceRange*& end) {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001257 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001258 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001259 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001260 beg = &R;
1261 end = beg+1;
1262 }
1263 else
1264 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001265}
1266
Mike Stump1eb44332009-09-09 15:08:12 +00001267SourceLocation BugReport::getLocation() const {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001268 if (EndNode)
Ted Kremenek5f85e172009-07-22 22:35:28 +00001269 if (const Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001270 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001271 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001272 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001273 // For binary operators, return the location of the operator.
1274 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1275 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001276
Ted Kremenekcf118d42009-02-04 23:49:09 +00001277 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001278 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001279
1280 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001281}
1282
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001283PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1284 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001285 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001286 return NULL;
1287}
1288
Ted Kremenekcf118d42009-02-04 23:49:09 +00001289//===----------------------------------------------------------------------===//
1290// Methods for BugReporter and subclasses.
1291//===----------------------------------------------------------------------===//
1292
1293BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001294 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001295}
1296
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001297GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001298BugReporterData::~BugReporterData() {}
1299
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001300ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001301
1302GRStateManager&
1303GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1304
1305BugReporter::~BugReporter() { FlushReports(); }
1306
1307void BugReporter::FlushReports() {
1308 if (BugTypes.isEmpty())
1309 return;
1310
1311 // First flush the warnings for each BugType. This may end up creating new
1312 // warnings and new BugTypes. Because ImmutableSet is a functional data
1313 // structure, we do not need to worry about the iterators being invalidated.
1314 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1315 const_cast<BugType*>(*I)->FlushReports(*this);
1316
1317 // Iterate through BugTypes a second time. BugTypes may have been updated
1318 // with new BugType objects and new warnings.
1319 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1320 BugType *BT = const_cast<BugType*>(*I);
1321
1322 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1323 SetTy& EQClasses = BT->EQClasses;
1324
1325 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1326 BugReportEquivClass& EQ = *EI;
1327 FlushReport(EQ);
1328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
1330 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001331 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001332 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001333
1334 // Remove all references to the BugType objects.
1335 BugTypes = F.GetEmptySet();
1336}
1337
1338//===----------------------------------------------------------------------===//
1339// PathDiagnostics generation.
1340//===----------------------------------------------------------------------===//
1341
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001342static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001343 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001344MakeReportGraph(const ExplodedGraph* G,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001345 const ExplodedNode** NStart,
1346 const ExplodedNode** NEnd) {
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;
1356 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Ted Kremenekcf118d42009-02-04 23:49:09 +00001358 // Create owning pointers for GTrim and NMap just to ensure that they are
1359 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001360 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001361 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Ted Kremenekcf118d42009-02-04 23:49:09 +00001363 // Find the (first) error node in the trimmed graph. We just need to consult
1364 // the node map (NMap) which maps from nodes in the original graph to nodes
1365 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001366
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001367 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001368 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001369 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001370
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001371 for (const ExplodedNode** I = NStart; I != NEnd; ++I)
1372 if (const ExplodedNode *N = NMap->getMappedNode(*I)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001373 unsigned NodeIndex = (I - NStart) / sizeof(*I);
1374 WS.push(N);
1375 IndexMap[*I] = NodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Ted Kremenek938332c2009-05-16 01:11:58 +00001378 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001379
1380 // Create a new (third!) graph with a single path. This is the graph
1381 // that will be returned to the caller.
Zhongxing Xucc025532009-08-25 03:33:41 +00001382 ExplodedGraph *GNew = new ExplodedGraph(GTrim->getContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Ted Kremenek10aa5542009-03-12 23:41:59 +00001384 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001385 // to the root node, and then construct a new graph that contains only
1386 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001387 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001389 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001390 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001392 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001393 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001394 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001396 if (Visited.find(Node) != Visited.end())
1397 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001399 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001401 if (Node->pred_empty()) {
1402 Root = Node;
1403 break;
1404 }
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001406 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001407 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001408 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Ted Kremenek938332c2009-05-16 01:11:58 +00001411 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Ted Kremenek10aa5542009-03-12 23:41:59 +00001413 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001414 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001415 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001416 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001417 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001419 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001420 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001421 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001422 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001424 // Create the equivalent node in the new graph with the same state
1425 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001426 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001428 // Store the mapping to the original node.
1429 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1430 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001431 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001433 // Link up the new node with the previous node.
1434 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001435 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001437 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001439 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001440 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001441 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001442 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001443 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001444 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001445 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 }
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001448 // Find the next successor node. We choose the node that is marked
1449 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001450 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1451 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001452 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001454 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001456 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001458 if (I == Visited.end())
1459 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001461 if (!N || I->second < MinVal) {
1462 N = *SI;
1463 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001464 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 }
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Ted Kremenek938332c2009-05-16 01:11:58 +00001467 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001468 }
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek938332c2009-05-16 01:11:58 +00001470 assert(First);
1471
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001472 return std::make_pair(std::make_pair(GNew, BM),
1473 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001474}
1475
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001476/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1477/// and collapses PathDiagosticPieces that are expanded by macros.
1478static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1479 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1480 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001482 typedef std::vector<PathDiagnosticPiece*>
1483 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001485 MacroStackTy MacroStack;
1486 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001488 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1489 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001490 const FullSourceLoc Loc = I->getLocation().asLocation();
1491
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001492 // Determine the instantiation location, which is the location we group
1493 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001494 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001495 SM.getInstantiationLoc(Loc) :
1496 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001498 if (Loc.isFileID()) {
1499 MacroStack.clear();
1500 Pieces.push_back(&*I);
1501 continue;
1502 }
1503
1504 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001506 // Is the PathDiagnosticPiece within the same macro group?
1507 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1508 MacroStack.back().first->push_back(&*I);
1509 continue;
1510 }
1511
1512 // We aren't in the same group. Are we descending into a new macro
1513 // or are part of an old one?
1514 PathDiagnosticMacroPiece *MacroGroup = 0;
1515
1516 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1517 SM.getInstantiationLoc(Loc) :
1518 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001520 // Walk the entire macro stack.
1521 while (!MacroStack.empty()) {
1522 if (InstantiationLoc == MacroStack.back().second) {
1523 MacroGroup = MacroStack.back().first;
1524 break;
1525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001527 if (ParentInstantiationLoc == MacroStack.back().second) {
1528 MacroGroup = MacroStack.back().first;
1529 break;
1530 }
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001532 MacroStack.pop_back();
1533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001535 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1536 // Create a new macro group and add it to the stack.
1537 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1538
1539 if (MacroGroup)
1540 MacroGroup->push_back(NewGroup);
1541 else {
1542 assert(InstantiationLoc.isFileID());
1543 Pieces.push_back(NewGroup);
1544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001546 MacroGroup = NewGroup;
1547 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1548 }
1549
1550 // Finally, add the PathDiagnosticPiece to the group.
1551 MacroGroup->push_back(&*I);
1552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001554 // Now take the pieces and construct a new PathDiagnostic.
1555 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001557 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1558 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1559 if (!MP->containsEvent()) {
1560 delete MP;
1561 continue;
1562 }
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001564 PD.push_back(*I);
1565 }
1566}
1567
Ted Kremenek7dc86642009-03-31 20:22:36 +00001568void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001569 BugReportEquivClass& EQ) {
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001571 std::vector<const ExplodedNode*> Nodes;
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Ted Kremenekcf118d42009-02-04 23:49:09 +00001573 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001574 const ExplodedNode* N = I->getEndNode();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001575 if (N) Nodes.push_back(N);
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenekcf118d42009-02-04 23:49:09 +00001578 if (Nodes.empty())
1579 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001581 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001582 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001583 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001584 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek7dc86642009-03-31 20:22:36 +00001585 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Ted Kremenekcf118d42009-02-04 23:49:09 +00001587 // Find the BugReport with the original location.
1588 BugReport *R = 0;
1589 unsigned i = 0;
1590 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
1591 if (i == GPair.second.second) { R = *I; break; }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Ted Kremenekcf118d42009-02-04 23:49:09 +00001593 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001595 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001596 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001597 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001598
1599 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001600 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenek8966bc12009-05-06 21:39:49 +00001602 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001603 PD.push_back(Piece);
1604 else
1605 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001607 R->registerInitialVisitors(PDB, N);
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Ted Kremenek7dc86642009-03-31 20:22:36 +00001609 switch (PDB.getGenerationScheme()) {
1610 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001611 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001612 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001613 case PathDiagnosticClient::Minimal:
1614 GenerateMinimalPathDiagnostic(PD, PDB, N);
1615 break;
1616 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001617}
1618
Ted Kremenekcf118d42009-02-04 23:49:09 +00001619void BugReporter::Register(BugType *BT) {
1620 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001621}
1622
Mike Stump1eb44332009-09-09 15:08:12 +00001623void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001624 // Compute the bug report's hash to determine its equivalence class.
1625 llvm::FoldingSetNodeID ID;
1626 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001627
1628 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001629 BugType& BT = R->getBugType();
1630 Register(&BT);
1631 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001632 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1633
Ted Kremenekcf118d42009-02-04 23:49:09 +00001634 if (!EQ) {
1635 EQ = new BugReportEquivClass(R);
1636 BT.EQClasses.InsertNode(EQ, InsertPos);
1637 }
1638 else
1639 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001640}
1641
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001642
1643//===----------------------------------------------------------------------===//
1644// Emitting reports in equivalence classes.
1645//===----------------------------------------------------------------------===//
1646
1647namespace {
1648struct VISIBILITY_HIDDEN FRIEC_WLItem {
1649 const ExplodedNode *N;
1650 ExplodedNode::const_succ_iterator I, E;
1651
1652 FRIEC_WLItem(const ExplodedNode *n)
1653 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1654};
1655}
1656
1657static BugReport *FindReportInEquivalenceClass(BugReportEquivClass& EQ) {
1658 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1659 assert(I != E);
1660 BugReport *R = *I;
1661 BugType& BT = R->getBugType();
1662
1663 if (!BT.isSuppressOnSink())
1664 return R;
1665
1666 // For bug reports that should be suppressed when all paths are post-dominated
1667 // by a sink node, iterate through the reports in the equivalence class
1668 // until we find one that isn't post-dominated (if one exists). We use a
1669 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1670 // this as a recursive function, but we don't want to risk blowing out the
1671 // stack for very long paths.
1672 for (; I != E; ++I) {
1673 R = *I;
1674 const ExplodedNode *N = R->getEndNode();
1675
1676 if (!N)
1677 continue;
1678
1679 if (N->isSink()) {
1680 assert(false &&
1681 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
1682 return R;
1683 }
1684
1685 if (N->succ_empty())
1686 return R;
1687
1688 // At this point we know that 'N' is not a sink and it has at least one
1689 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1690 typedef FRIEC_WLItem WLItem;
1691 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1692 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1693
1694 DFSWorkList WL;
1695 WL.push_back(N);
1696 Visited[N] = 1;
1697
1698 while (!WL.empty()) {
1699 WLItem &WI = WL.back();
1700 assert(!WI.N->succ_empty());
1701
1702 for (; WI.I != WI.E; ++WI.I) {
1703 const ExplodedNode *Succ = *WI.I;
1704 // End-of-path node?
1705 if (Succ->succ_empty()) {
1706 // If we found an end-of-path node that is not a sink, then return
1707 // this report.
1708 if (!Succ->isSink())
1709 return R;
1710
1711 // Found a sink? Continue on to the next successor.
1712 continue;
1713 }
1714
1715 // Mark the successor as visited. If it hasn't been explored,
1716 // enqueue it to the DFS worklist.
1717 unsigned &mark = Visited[Succ];
1718 if (!mark) {
1719 mark = 1;
1720 WL.push_back(Succ);
1721 break;
1722 }
1723 }
1724
1725 if (&WL.back() == &WI)
1726 WL.pop_back();
1727 }
1728 }
1729
Ted Kremenek6b0c6eb2009-09-15 03:28:00 +00001730 // If we reach here, the end nodes for all reports in the equivalence
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001731 // class are post-dominated by a sink node.
1732 return NULL;
1733}
1734
Ted Kremeneke0a58072009-09-18 22:37:37 +00001735
1736//===----------------------------------------------------------------------===//
1737// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1738// uses global state, which eventually should go elsewhere.
1739//===----------------------------------------------------------------------===//
1740namespace {
1741class VISIBILITY_HIDDEN DiagCacheItem : public llvm::FoldingSetNode {
1742 llvm::FoldingSetNodeID ID;
1743public:
1744 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1745 ID.AddString(R->getBugType().getName());
1746 ID.AddString(R->getBugType().getCategory());
1747 ID.AddString(R->getDescription());
1748 ID.AddInteger(R->getLocation().getRawEncoding());
1749 PD->Profile(ID);
1750 }
1751
1752 void Profile(llvm::FoldingSetNodeID &id) {
1753 id = ID;
1754 }
1755
1756 llvm::FoldingSetNodeID &getID() { return ID; }
1757};
1758}
1759
1760static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1761 // FIXME: Eventually this diagnostic cache should reside in something
1762 // like AnalysisManager instead of being a static variable. This is
1763 // really unsafe in the long term.
1764 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1765 static DiagnosticCache DC;
1766
1767 void *InsertPos;
1768 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1769
1770 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1771 delete Item;
1772 return true;
1773 }
1774
1775 DC.InsertNode(Item, InsertPos);
1776 return false;
1777}
1778
Ted Kremenekcf118d42009-02-04 23:49:09 +00001779void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001780 BugReport *R = FindReportInEquivalenceClass(EQ);
1781
1782 if (!R)
1783 return;
1784
Ted Kremenekd49967f2009-04-29 21:58:13 +00001785 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Ted Kremenekcf118d42009-02-04 23:49:09 +00001787 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001788 // Probably doesn't make a difference in practice.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001789 BugType& BT = R->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Ted Kremenekd49967f2009-04-29 21:58:13 +00001791 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001792 D(new PathDiagnostic(R->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001793 !PD || PD->useVerboseDescription()
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001794 ? R->getDescription() : R->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001795 BT.getCategory()));
1796
Ted Kremenekcf118d42009-02-04 23:49:09 +00001797 GeneratePathDiagnostic(*D.get(), EQ);
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Ted Kremeneke0a58072009-09-18 22:37:37 +00001799 if (IsCachedDiagnostic(R, D.get()))
1800 return;
1801
Ted Kremenek072192b2008-04-30 23:47:44 +00001802 // Get the meta data.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001803 std::pair<const char**, const char**> Meta = R->getExtraDescriptiveText();
1804 for (const char** s = Meta.first; s != Meta.second; ++s)
1805 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001806
Ted Kremenek3148eb42009-01-24 00:55:43 +00001807 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001808 const SourceRange *Beg = 0, *End = 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001809 R->getRanges(Beg, End);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001810 Diagnostic& Diag = getDiagnostic();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001811 FullSourceLoc L(R->getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001812 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001813 R->getShortDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001814
Ted Kremenek3148eb42009-01-24 00:55:43 +00001815 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001816 default: assert(0 && "Don't handle this many ranges yet!");
1817 case 0: Diag.Report(L, ErrorDiag); break;
1818 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1819 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1820 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001821 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001822
1823 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1824 if (!PD)
1825 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001826
1827 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001828 PathDiagnosticPiece* piece =
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001829 new PathDiagnosticEventPiece(L, R->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001830
Ted Kremenek3148eb42009-01-24 00:55:43 +00001831 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1832 D->push_back(piece);
1833 }
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Ted Kremenek3148eb42009-01-24 00:55:43 +00001835 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001836}
Ted Kremenek57202072008-07-14 17:40:50 +00001837
Ted Kremenek8c036c72008-09-20 04:23:38 +00001838void BugReporter::EmitBasicReport(const char* name, const char* str,
1839 SourceLocation Loc,
1840 SourceRange* RBeg, unsigned NumRanges) {
1841 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1842}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001843
Ted Kremenek8c036c72008-09-20 04:23:38 +00001844void BugReporter::EmitBasicReport(const char* name, const char* category,
1845 const char* str, SourceLocation Loc,
1846 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Ted Kremenekcf118d42009-02-04 23:49:09 +00001848 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1849 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001850 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001851 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1852 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1853 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001854}