blob: e6482698dd43d94dd3a1cac626ff39349646245f [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 {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000122class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000123 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
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000134class 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 Kremenek4f1db532009-12-04 20:34:31 +0000207 else {
208 os << "Execution jumps to the end of the ";
209 const Decl *D = N->getLocationContext()->getDecl();
210 if (isa<ObjCMethodDecl>(D))
211 os << "method";
212 else if (isa<FunctionDecl>(D))
213 os << "function";
214 else {
215 assert(isa<BlockDecl>(D));
216 os << "anonymous block";
217 }
218 os << '.';
219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000221 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000222}
223
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000224static bool IsNested(const Stmt *S, ParentMap &PM) {
225 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
226 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000228 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000230 if (Parent)
231 switch (Parent->getStmtClass()) {
232 case Stmt::ForStmtClass:
233 case Stmt::DoStmtClass:
234 case Stmt::WhileStmtClass:
235 return true;
236 default:
237 break;
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
240 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000241}
242
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000243PathDiagnosticLocation
244PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
245 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000246 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000247 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000248
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000249 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000250 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000252 if (!Parent)
253 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000255 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000256 case Stmt::BinaryOperatorClass: {
257 const BinaryOperator *B = cast<BinaryOperator>(Parent);
258 if (B->isLogicalOp())
259 return PathDiagnosticLocation(S, SMgr);
260 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000261 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000262 case Stmt::CompoundStmtClass:
263 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000264 return PathDiagnosticLocation(S, SMgr);
265 case Stmt::ChooseExprClass:
266 // Similar to '?' if we are referring to condition, just have the edge
267 // point to the entire choose expression.
268 if (cast<ChooseExpr>(Parent)->getCond() == S)
269 return PathDiagnosticLocation(Parent, SMgr);
270 else
Mike Stump1eb44332009-09-09 15:08:12 +0000271 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000272 case Stmt::ConditionalOperatorClass:
273 // For '?', if we are referring to condition, just have the edge point
274 // to the entire '?' expression.
275 if (cast<ConditionalOperator>(Parent)->getCond() == S)
276 return PathDiagnosticLocation(Parent, SMgr);
277 else
Mike Stump1eb44332009-09-09 15:08:12 +0000278 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000279 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000280 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000281 case Stmt::ForStmtClass:
282 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000283 return PathDiagnosticLocation(S, SMgr);
284 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000285 case Stmt::IfStmtClass:
286 if (cast<IfStmt>(Parent)->getCond() != S)
287 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000288 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000289 case Stmt::ObjCForCollectionStmtClass:
290 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
291 return PathDiagnosticLocation(S, SMgr);
292 break;
293 case Stmt::WhileStmtClass:
294 if (cast<WhileStmt>(Parent)->getCond() != S)
295 return PathDiagnosticLocation(S, SMgr);
296 break;
297 default:
298 break;
299 }
300
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000301 S = Parent;
302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000304 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000305
306 // Special case: DeclStmts can appear in for statement declarations, in which
307 // case the ForStmt is the context.
308 if (isa<DeclStmt>(S)) {
309 if (const Stmt *Parent = P.getParent(S)) {
310 switch (Parent->getStmtClass()) {
311 case Stmt::ForStmtClass:
312 case Stmt::ObjCForCollectionStmtClass:
313 return PathDiagnosticLocation(Parent, SMgr);
314 default:
315 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000316 }
317 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000318 }
319 else if (isa<BinaryOperator>(S)) {
320 // Special case: the binary operator represents the initialization
321 // code in a for statement (this can happen when the variable being
322 // initialized is an old variable.
323 if (const ForStmt *FS =
324 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
325 if (FS->getInit() == S)
326 return PathDiagnosticLocation(FS, SMgr);
327 }
328 }
329
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000330 return PathDiagnosticLocation(S, SMgr);
331}
332
Ted Kremenekcf118d42009-02-04 23:49:09 +0000333//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000334// ScanNotableSymbols: closure-like callback for scanning Store bindings.
335//===----------------------------------------------------------------------===//
336
337static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000338GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000339 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek31061982009-03-31 23:00:32 +0000341 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek31061982009-03-31 23:00:32 +0000343 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek31061982009-03-31 23:00:32 +0000345 if (!isa<PostStmt>(P))
346 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek5f85e172009-07-22 22:35:28 +0000348 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek31061982009-03-31 23:00:32 +0000350 if (!DR)
351 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000353 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek31061982009-03-31 23:00:32 +0000355 if (X != Y)
356 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek5f85e172009-07-22 22:35:28 +0000358 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek31061982009-03-31 23:00:32 +0000360 if (!VD)
361 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek31061982009-03-31 23:00:32 +0000363 return VD;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek31061982009-03-31 23:00:32 +0000366 return 0;
367}
368
369namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000370class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000371: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek31061982009-03-31 23:00:32 +0000373 SymbolRef Sym;
374 const GRState* PrevSt;
375 const Stmt* S;
376 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000377 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000378 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000379 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek31061982009-03-31 23:00:32 +0000381public:
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek31061982009-03-31 23:00:32 +0000383 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000384 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000385 PathDiagnostic& pd, BugReporter& br)
386 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek31061982009-03-31 23:00:32 +0000388 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
389 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek31061982009-03-31 23:00:32 +0000391 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek31061982009-03-31 23:00:32 +0000393 if (ScanSym != Sym)
394 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
396 // Check if the previous state has this binding.
Ted Kremenekdbc2afc2009-06-23 17:55:07 +0000397 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek31061982009-03-31 23:00:32 +0000399 if (X == V) // Same binding?
400 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek31061982009-03-31 23:00:32 +0000402 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000403 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000404 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek31061982009-03-31 23:00:32 +0000406 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek31061982009-03-31 23:00:32 +0000408 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
409 if (!B->isAssignmentOp())
410 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek31061982009-03-31 23:00:32 +0000412 // What variable did we assign to?
413 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek31061982009-03-31 23:00:32 +0000415 if (!DR)
416 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek31061982009-03-31 23:00:32 +0000418 VD = dyn_cast<VarDecl>(DR->getDecl());
419 }
420 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
421 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
422 // assume that each DeclStmt has a single Decl. This invariant
423 // holds by contruction in the CFG.
424 VD = dyn_cast<VarDecl>(*DS->decl_begin());
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 if (!VD)
428 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek31061982009-03-31 23:00:32 +0000430 // What is the most recently referenced variable with this binding?
431 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek31061982009-03-31 23:00:32 +0000433 if (!MostRecent)
434 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek31061982009-03-31 23:00:32 +0000436 // Create the diagnostic.
437 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek31061982009-03-31 23:00:32 +0000439 if (Loc::IsLocType(VD->getType())) {
440 std::string msg = "'" + std::string(VD->getNameAsString()) +
441 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Ted Kremenek31061982009-03-31 23:00:32 +0000443 PD.push_front(new PathDiagnosticEventPiece(L, msg));
444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek31061982009-03-31 23:00:32 +0000446 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000447 }
Ted Kremenek31061982009-03-31 23:00:32 +0000448};
449}
450
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000451static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000452 const Stmt* S,
453 SymbolRef Sym, BugReporter& BR,
454 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000456 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000457 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Ted Kremenek31061982009-03-31 23:00:32 +0000459 if (!PrevSt)
460 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Ted Kremenek31061982009-03-31 23:00:32 +0000462 // Look at the region bindings of the current state that map to the
463 // specified symbol. Are any of them not in the previous state?
464 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
465 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
466 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
467}
468
469namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000470class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000471: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Ted Kremenek31061982009-03-31 23:00:32 +0000473 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000474 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000475 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000476 GRBugReporter& BR;
477 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek31061982009-03-31 23:00:32 +0000479public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000480 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000481 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000482 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek31061982009-03-31 23:00:32 +0000484 bool HandleBinding(StoreManager& SMgr, Store store,
485 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Ted Kremenek31061982009-03-31 23:00:32 +0000487 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 if (!ScanSym)
490 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek31061982009-03-31 23:00:32 +0000492 if (!BR.isNotable(ScanSym))
493 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremenek31061982009-03-31 23:00:32 +0000495 if (AlreadyProcessed.count(ScanSym))
496 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek31061982009-03-31 23:00:32 +0000498 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek31061982009-03-31 23:00:32 +0000500 HandleNotableSymbol(N, S, ScanSym, BR, PD);
501 return true;
502 }
503};
504} // end anonymous namespace
505
506//===----------------------------------------------------------------------===//
507// "Minimal" path diagnostic generation algorithm.
508//===----------------------------------------------------------------------===//
509
Ted Kremenek14856d72009-04-06 23:06:54 +0000510static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
511
Ted Kremenek31061982009-03-31 23:00:32 +0000512static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
513 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000514 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000515
Ted Kremenek31061982009-03-31 23:00:32 +0000516 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000517 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000518 ? NULL : *(N->pred_begin());
519 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000520 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000521 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Ted Kremenek31061982009-03-31 23:00:32 +0000523 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Ted Kremenek31061982009-03-31 23:00:32 +0000525 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
526 CFGBlock* Src = BE->getSrc();
527 CFGBlock* Dst = BE->getDst();
528 Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek31061982009-03-31 23:00:32 +0000530 if (!T)
531 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek31061982009-03-31 23:00:32 +0000533 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Ted Kremenek31061982009-03-31 23:00:32 +0000535 switch (T->getStmtClass()) {
536 default:
537 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek31061982009-03-31 23:00:32 +0000539 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000540 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000541 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek31061982009-03-31 23:00:32 +0000543 if (!S)
544 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Ted Kremenek31061982009-03-31 23:00:32 +0000546 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000547 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000548 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek31061982009-03-31 23:00:32 +0000550 os << "Control jumps to line "
551 << End.asLocation().getInstantiationLineNumber();
552 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
553 os.str()));
554 break;
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
557 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000558 // Figure out what case arm we took.
559 std::string sbuf;
560 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Ted Kremenek31061982009-03-31 23:00:32 +0000562 if (Stmt* S = Dst->getLabel()) {
563 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenek31061982009-03-31 23:00:32 +0000565 switch (S->getStmtClass()) {
566 default:
567 os << "No cases match in the switch statement. "
568 "Control jumps to line "
569 << End.asLocation().getInstantiationLineNumber();
570 break;
571 case Stmt::DefaultStmtClass:
572 os << "Control jumps to the 'default' case at line "
573 << End.asLocation().getInstantiationLineNumber();
574 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Ted Kremenek31061982009-03-31 23:00:32 +0000576 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000577 os << "Control jumps to 'case ";
578 CaseStmt* Case = cast<CaseStmt>(S);
Ted Kremenek31061982009-03-31 23:00:32 +0000579 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000580
581 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000582 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Ted Kremenek31061982009-03-31 23:00:32 +0000584 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
585 // FIXME: Maybe this should be an assertion. Are there cases
586 // were it is not an EnumConstantDecl?
587 EnumConstantDecl* D =
588 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Ted Kremenek31061982009-03-31 23:00:32 +0000590 if (D) {
591 GetRawInt = false;
592 os << D->getNameAsString();
593 }
594 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000595
596 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000597 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000598
Ted Kremenek31061982009-03-31 23:00:32 +0000599 os << ":' at line "
600 << End.asLocation().getInstantiationLineNumber();
601 break;
602 }
603 }
604 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
605 os.str()));
606 }
607 else {
608 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000609 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000610 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
611 os.str()));
612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremenek31061982009-03-31 23:00:32 +0000614 break;
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek31061982009-03-31 23:00:32 +0000617 case Stmt::BreakStmtClass:
618 case Stmt::ContinueStmtClass: {
619 std::string sbuf;
620 llvm::raw_string_ostream os(sbuf);
621 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
622 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
623 os.str()));
624 break;
625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Ted Kremenek31061982009-03-31 23:00:32 +0000627 // Determine control-flow for ternary '?'.
628 case Stmt::ConditionalOperatorClass: {
629 std::string sbuf;
630 llvm::raw_string_ostream os(sbuf);
631 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek31061982009-03-31 23:00:32 +0000633 if (*(Src->succ_begin()+1) == Dst)
634 os << "false";
635 else
636 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Ted Kremenek31061982009-03-31 23:00:32 +0000638 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek31061982009-03-31 23:00:32 +0000640 if (const Stmt *S = End.asStmt())
641 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek31061982009-03-31 23:00:32 +0000643 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
644 os.str()));
645 break;
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremenek31061982009-03-31 23:00:32 +0000648 // Determine control-flow for short-circuited '&&' and '||'.
649 case Stmt::BinaryOperatorClass: {
650 if (!PDB.supportsLogicalOpControlFlow())
651 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 BinaryOperator *B = cast<BinaryOperator>(T);
654 std::string sbuf;
655 llvm::raw_string_ostream os(sbuf);
656 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 if (B->getOpcode() == BinaryOperator::LAnd) {
659 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek31061982009-03-31 23:00:32 +0000661 if (*(Src->succ_begin()+1) == Dst) {
662 os << "false";
663 PathDiagnosticLocation End(B->getLHS(), SMgr);
664 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
665 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
666 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000667 }
Ted Kremenek31061982009-03-31 23:00:32 +0000668 else {
669 os << "true";
670 PathDiagnosticLocation Start(B->getLHS(), SMgr);
671 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
672 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
673 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000674 }
Ted Kremenek31061982009-03-31 23:00:32 +0000675 }
676 else {
677 assert(B->getOpcode() == BinaryOperator::LOr);
678 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremenek31061982009-03-31 23:00:32 +0000680 if (*(Src->succ_begin()+1) == Dst) {
681 os << "false";
682 PathDiagnosticLocation Start(B->getLHS(), SMgr);
683 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
684 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000685 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000686 }
687 else {
688 os << "true";
689 PathDiagnosticLocation End(B->getLHS(), SMgr);
690 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
691 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000692 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000693 }
694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Ted Kremenek31061982009-03-31 23:00:32 +0000696 break;
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
699 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000700 if (*(Src->succ_begin()) == Dst) {
701 std::string sbuf;
702 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Ted Kremenek31061982009-03-31 23:00:32 +0000704 os << "Loop condition is true. ";
705 PathDiagnosticLocation End = PDB.ExecutionContinues(os, 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 os.str()));
712 }
713 else {
714 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremenek31061982009-03-31 23:00:32 +0000716 if (const Stmt *S = End.asStmt())
717 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
720 "Loop condition is false. Exiting loop"));
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek31061982009-03-31 23:00:32 +0000723 break;
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek31061982009-03-31 23:00:32 +0000726 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000727 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000728 if (*(Src->succ_begin()+1) == Dst) {
729 std::string sbuf;
730 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek31061982009-03-31 23:00:32 +0000732 os << "Loop condition is false. ";
733 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
734 if (const Stmt *S = End.asStmt())
735 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek31061982009-03-31 23:00:32 +0000737 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
738 os.str()));
739 }
740 else {
741 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
742 if (const Stmt *S = End.asStmt())
743 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek31061982009-03-31 23:00:32 +0000745 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000746 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek31061982009-03-31 23:00:32 +0000749 break;
750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Ted Kremenek31061982009-03-31 23:00:32 +0000752 case Stmt::IfStmtClass: {
753 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek31061982009-03-31 23:00:32 +0000755 if (const Stmt *S = End.asStmt())
756 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek31061982009-03-31 23:00:32 +0000758 if (*(Src->succ_begin()+1) == Dst)
759 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000760 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000761 else
Ted Kremenek31061982009-03-31 23:00:32 +0000762 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000763 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek31061982009-03-31 23:00:32 +0000765 break;
766 }
767 }
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000770 if (NextNode) {
771 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
772 E = PDB.visitor_end(); I!=E; ++I) {
773 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
774 PD.push_front(p);
775 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
778 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000779 // Scan the region bindings, and see if a "notable" symbol has a new
780 // lval binding.
781 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
782 PDB.getStateManager().iterBindings(N->getState(), SNS);
783 }
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Ted Kremenek14856d72009-04-06 23:06:54 +0000786 // After constructing the full PathDiagnostic, do a pass over it to compact
787 // PathDiagnosticPieces that occur within a macro.
788 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000789}
790
791//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000792// "Extensive" PathDiagnostic generation.
793//===----------------------------------------------------------------------===//
794
795static bool IsControlFlowExpr(const Stmt *S) {
796 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000798 if (!E)
799 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
801 E = E->IgnoreParenCasts();
802
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000803 if (isa<ConditionalOperator>(E))
804 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000806 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
807 if (B->isLogicalOp())
808 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
810 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000811}
812
Ted Kremenek14856d72009-04-06 23:06:54 +0000813namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000814class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000815 bool IsDead;
816public:
817 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
818 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000819
820 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000821 bool isDead() const { return IsDead; }
822};
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000824class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000825 std::vector<ContextLocation> CLocs;
826 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000827 PathDiagnostic &PD;
828 PathDiagnosticBuilder &PDB;
829 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000831 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek14856d72009-04-06 23:06:54 +0000833 bool containsLocation(const PathDiagnosticLocation &Container,
834 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek14856d72009-04-06 23:06:54 +0000836 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek9650cf32009-05-11 21:42:34 +0000838 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
839 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000840 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000841 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000842 while (1) {
843 // Adjust the location for some expressions that are best referenced
844 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000845 switch (S->getStmtClass()) {
846 default:
847 break;
848 case Stmt::ParenExprClass:
849 S = cast<ParenExpr>(S)->IgnoreParens();
850 firstCharOnly = true;
851 continue;
852 case Stmt::ConditionalOperatorClass:
853 S = cast<ConditionalOperator>(S)->getCond();
854 firstCharOnly = true;
855 continue;
856 case Stmt::ChooseExprClass:
857 S = cast<ChooseExpr>(S)->getCond();
858 firstCharOnly = true;
859 continue;
860 case Stmt::BinaryOperatorClass:
861 S = cast<BinaryOperator>(S)->getLHS();
862 firstCharOnly = true;
863 continue;
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Ted Kremenek9650cf32009-05-11 21:42:34 +0000866 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000867 }
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Ted Kremenek9650cf32009-05-11 21:42:34 +0000869 if (S != Original)
870 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenek9650cf32009-05-11 21:42:34 +0000873 if (firstCharOnly)
874 L = PathDiagnosticLocation(L.asLocation());
875
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000876 return L;
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek14856d72009-04-06 23:06:54 +0000879 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000880 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000881 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000882 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000883 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000884 CLocs.pop_back();
885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
887 PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000888
889public:
890 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
891 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremeneka301a672009-04-22 18:16:20 +0000893 // If the PathDiagnostic already has pieces, add the enclosing statement
894 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000895 if (!PD.empty()) {
896 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek14856d72009-04-06 23:06:54 +0000898 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000899 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000900 }
901 }
902
903 ~EdgeBuilder() {
904 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremeneka301a672009-04-22 18:16:20 +0000906 // Finally, add an initial edge from the start location of the first
907 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000908 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
909 if (const CompoundStmt *CS =
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000910 PDB.getCodeDecl().getCompoundBody())
Ted Kremeneka301a672009-04-22 18:16:20 +0000911 if (!CS->body_empty()) {
912 SourceLocation Loc = (*CS->body_begin())->getLocStart();
913 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
914 }
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Ted Kremenek14856d72009-04-06 23:06:54 +0000916 }
917
918 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Ted Kremenek14856d72009-04-06 23:06:54 +0000920 void addEdge(const Stmt *S, bool alwaysAdd = false) {
921 addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd);
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000924 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Ted Kremenek14856d72009-04-06 23:06:54 +0000926 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000927 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000928};
Ted Kremenek14856d72009-04-06 23:06:54 +0000929} // end anonymous namespace
930
931
932PathDiagnosticLocation
933EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
934 if (const Stmt *S = L.asStmt()) {
935 if (IsControlFlowExpr(S))
936 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
938 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek14856d72009-04-06 23:06:54 +0000941 return L;
942}
943
944bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
945 const PathDiagnosticLocation &Containee) {
946
947 if (Container == Containee)
948 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Ted Kremenek14856d72009-04-06 23:06:54 +0000950 if (Container.asDecl())
951 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Ted Kremenek14856d72009-04-06 23:06:54 +0000953 if (const Stmt *S = Containee.asStmt())
954 if (const Stmt *ContainerS = Container.asStmt()) {
955 while (S) {
956 if (S == ContainerS)
957 return true;
958 S = PDB.getParent(S);
959 }
960 return false;
961 }
962
963 // Less accurate: compare using source ranges.
964 SourceRange ContainerR = Container.asRange();
965 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Ted Kremenek14856d72009-04-06 23:06:54 +0000967 SourceManager &SM = PDB.getSourceManager();
968 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
969 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
970 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
971 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek14856d72009-04-06 23:06:54 +0000973 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
974 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
975 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
976 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Ted Kremenek14856d72009-04-06 23:06:54 +0000978 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000979 assert(ContaineeBegLine <= ContaineeEndLine);
980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 return (ContainerBegLine <= ContaineeBegLine &&
982 ContainerEndLine >= ContaineeEndLine &&
983 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +0000984 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
986 (ContainerEndLine != ContaineeEndLine ||
987 SM.getInstantiationColumnNumber(ContainerREnd) >=
988 SM.getInstantiationColumnNumber(ContainerREnd)));
989}
990
991PathDiagnosticLocation
992EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) {
993 if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt()))
994 return PathDiagnosticLocation(E->IgnoreParenCasts(),
995 PDB.getSourceManager());
996 return L;
997}
998
999void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1000 if (!PrevLoc.isValid()) {
1001 PrevLoc = NewLoc;
1002 return;
1003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001005 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1006 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001008 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001009 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Ted Kremenek14856d72009-04-06 23:06:54 +00001011 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001012 if (NewLocClean.asLocation().getInstantiationLoc() ==
1013 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001014 return;
1015
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001016 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1017 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001018}
1019
1020void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Ted Kremeneka301a672009-04-22 18:16:20 +00001022 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1023 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1026
1027 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001028 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremenek14856d72009-04-06 23:06:54 +00001030 // Is the top location context the same as the one for the new location?
1031 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001032 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001033 if (IsConsumedExpr(TopContextLoc) &&
1034 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001035 TopContextLoc.markDead();
1036
Ted Kremenek14856d72009-04-06 23:06:54 +00001037 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001038 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001039
1040 return;
1041 }
1042
1043 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001044 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001045 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001047 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001048 CLocs.push_back(ContextLocation(CLoc, true));
1049 return;
1050 }
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Ted Kremenek14856d72009-04-06 23:06:54 +00001053 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001054 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 }
1056
1057 // Context does not contain the location. Flush it.
1058 popLocation();
1059 }
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001061 // If we reach here, there is no enclosing context. Just add the edge.
1062 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001063}
1064
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001065bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1066 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1067 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001069 return false;
1070}
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremeneke1baed32009-05-05 23:13:38 +00001072void EdgeBuilder::addExtendedContext(const Stmt *S) {
1073 if (!S)
1074 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001075
1076 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001077 while (Parent) {
1078 if (isa<CompoundStmt>(Parent))
1079 Parent = PDB.getParent(Parent);
1080 else
1081 break;
1082 }
1083
1084 if (Parent) {
1085 switch (Parent->getStmtClass()) {
1086 case Stmt::DoStmtClass:
1087 case Stmt::ObjCAtSynchronizedStmtClass:
1088 addContext(Parent);
1089 default:
1090 break;
1091 }
1092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Ted Kremeneke1baed32009-05-05 23:13:38 +00001094 addContext(S);
1095}
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Ted Kremenek14856d72009-04-06 23:06:54 +00001097void EdgeBuilder::addContext(const Stmt *S) {
1098 if (!S)
1099 return;
1100
1101 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Ted Kremenek14856d72009-04-06 23:06:54 +00001103 while (!CLocs.empty()) {
1104 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1105
1106 // Is the top location context the same as the one for the new location?
1107 if (TopContextLoc == L)
1108 return;
1109
1110 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001111 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001112 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001113 }
1114
1115 // Context does not contain the location. Flush it.
1116 popLocation();
1117 }
1118
1119 CLocs.push_back(L);
1120}
1121
1122static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1123 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001124 const ExplodedNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +00001125
1126
Ted Kremenek14856d72009-04-06 23:06:54 +00001127 EdgeBuilder EB(PD, PDB);
1128
Mike Stump1eb44332009-09-09 15:08:12 +00001129 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek14856d72009-04-06 23:06:54 +00001130 ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001131 while (NextNode) {
1132 N = NextNode;
1133 NextNode = GetPredecessorNode(N);
1134 ProgramPoint P = N->getLocation();
1135
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001136 do {
1137 // Block edges.
1138 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1139 const CFGBlock &Blk = *BE->getSrc();
1140 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001142 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001143 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001144 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001145 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001147 if (!Term) {
1148 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1149 CS = dyn_cast<CompoundStmt>(FS->getBody());
1150 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001151 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001154 PathDiagnosticEventPiece *p =
1155 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001156 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001158 EB.addEdge(p->getLocation(), true);
1159 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001161 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001162 PathDiagnosticLocation BL(CS->getRBracLoc(),
1163 PDB.getSourceManager());
1164 BL = PathDiagnosticLocation(BL.asLocation());
1165 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001166 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001167 }
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001169 if (Term)
1170 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001172 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001173 }
1174
Mike Stump1eb44332009-09-09 15:08:12 +00001175 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001176 if (const Stmt* S = BE->getFirstStmt()) {
1177 if (IsControlFlowExpr(S)) {
1178 // Add the proper context for '&&', '||', and '?'.
1179 EB.addContext(S);
1180 }
1181 else
1182 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1183 }
1184
1185 break;
1186 }
1187 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001189 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001190 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Ted Kremenek8966bc12009-05-06 21:39:49 +00001192 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1193 E = PDB.visitor_end(); I!=E; ++I) {
1194 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1195 const PathDiagnosticLocation &Loc = p->getLocation();
1196 EB.addEdge(Loc, true);
1197 PD.push_front(p);
1198 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001199 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001200 }
Mike Stump1eb44332009-09-09 15:08:12 +00001201 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001202 }
1203}
1204
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001205//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001206// Methods for BugType and subclasses.
1207//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001208BugType::~BugType() {
1209 // Free up the equivalence class objects. Observe that we get a pointer to
1210 // the object first before incrementing the iterator, as destroying the
1211 // node before doing so means we will read from freed memory.
1212 for (iterator I = begin(), E = end(); I !=E; ) {
1213 BugReportEquivClass *EQ = &*I;
1214 ++I;
1215 delete EQ;
1216 }
1217}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001218void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001219
Ted Kremenekcf118d42009-02-04 23:49:09 +00001220//===----------------------------------------------------------------------===//
1221// Methods for BugReport and subclasses.
1222//===----------------------------------------------------------------------===//
1223BugReport::~BugReport() {}
1224RangedBugReport::~RangedBugReport() {}
1225
Mike Stump1eb44332009-09-09 15:08:12 +00001226const Stmt* BugReport::getStmt() const {
1227 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001228 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Ted Kremenekcf118d42009-02-04 23:49:09 +00001230 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001231 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001232 if (BE->getBlock() == &Exit)
1233 S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001234 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001235 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001236 S = GetStmt(ProgP);
1237
1238 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001239}
1240
1241PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001242BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001243 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001245 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenek61f3e052008-04-03 04:42:52 +00001247 if (!S)
1248 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001249
Ted Kremenekde7161f2008-04-03 18:00:37 +00001250 const SourceRange *Beg, *End;
Mike Stump1eb44332009-09-09 15:08:12 +00001251 getRanges(Beg, End);
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001252 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001254 // Only add the statement itself as a range if we didn't specify any
1255 // special ranges for this report.
1256 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1257 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001259 for (; Beg != End; ++Beg)
1260 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Ted Kremenek61f3e052008-04-03 04:42:52 +00001262 return P;
1263}
1264
Mike Stump1eb44332009-09-09 15:08:12 +00001265void BugReport::getRanges(const SourceRange*& beg, const SourceRange*& end) {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001266 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001267 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001268 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001269 beg = &R;
1270 end = beg+1;
1271 }
1272 else
1273 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001274}
1275
Mike Stump1eb44332009-09-09 15:08:12 +00001276SourceLocation BugReport::getLocation() const {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001277 if (EndNode)
Ted Kremenek5f85e172009-07-22 22:35:28 +00001278 if (const Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001279 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001280 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001281 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001282 // For binary operators, return the location of the operator.
1283 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1284 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001285
Ted Kremenekcf118d42009-02-04 23:49:09 +00001286 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001287 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001288
1289 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001290}
1291
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001292PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1293 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001294 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001295 return NULL;
1296}
1297
Ted Kremenekcf118d42009-02-04 23:49:09 +00001298//===----------------------------------------------------------------------===//
1299// Methods for BugReporter and subclasses.
1300//===----------------------------------------------------------------------===//
1301
1302BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001303 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001304}
1305
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001306GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001307BugReporterData::~BugReporterData() {}
1308
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001309ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001310
1311GRStateManager&
1312GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1313
1314BugReporter::~BugReporter() { FlushReports(); }
1315
1316void BugReporter::FlushReports() {
1317 if (BugTypes.isEmpty())
1318 return;
1319
1320 // First flush the warnings for each BugType. This may end up creating new
1321 // warnings and new BugTypes. Because ImmutableSet is a functional data
1322 // structure, we do not need to worry about the iterators being invalidated.
1323 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1324 const_cast<BugType*>(*I)->FlushReports(*this);
1325
1326 // Iterate through BugTypes a second time. BugTypes may have been updated
1327 // with new BugType objects and new warnings.
1328 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1329 BugType *BT = const_cast<BugType*>(*I);
1330
1331 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1332 SetTy& EQClasses = BT->EQClasses;
1333
1334 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1335 BugReportEquivClass& EQ = *EI;
1336 FlushReport(EQ);
1337 }
Mike Stump1eb44332009-09-09 15:08:12 +00001338
1339 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001340 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001341 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001342
1343 // Remove all references to the BugType objects.
1344 BugTypes = F.GetEmptySet();
1345}
1346
1347//===----------------------------------------------------------------------===//
1348// PathDiagnostics generation.
1349//===----------------------------------------------------------------------===//
1350
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001351static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001352 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001353MakeReportGraph(const ExplodedGraph* G,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001354 const ExplodedNode** NStart,
1355 const ExplodedNode** NEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Ted Kremenekcf118d42009-02-04 23:49:09 +00001357 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001358 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001359 // error node unless there are two or more error nodes with the same minimum
1360 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001361 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001362 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001363
1364 llvm::DenseMap<const void*, const void*> InverseMap;
1365 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Ted Kremenekcf118d42009-02-04 23:49:09 +00001367 // Create owning pointers for GTrim and NMap just to ensure that they are
1368 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001369 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001370 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Ted Kremenekcf118d42009-02-04 23:49:09 +00001372 // Find the (first) error node in the trimmed graph. We just need to consult
1373 // the node map (NMap) which maps from nodes in the original graph to nodes
1374 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001375
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001376 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001377 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001378 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001379
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001380 for (const ExplodedNode** I = NStart; I != NEnd; ++I)
1381 if (const ExplodedNode *N = NMap->getMappedNode(*I)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001382 unsigned NodeIndex = (I - NStart) / sizeof(*I);
1383 WS.push(N);
1384 IndexMap[*I] = NodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenek938332c2009-05-16 01:11:58 +00001387 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001388
1389 // Create a new (third!) graph with a single path. This is the graph
1390 // that will be returned to the caller.
Zhongxing Xucc025532009-08-25 03:33:41 +00001391 ExplodedGraph *GNew = new ExplodedGraph(GTrim->getContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Ted Kremenek10aa5542009-03-12 23:41:59 +00001393 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001394 // to the root node, and then construct a new graph that contains only
1395 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001396 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001398 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001399 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001401 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001402 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001403 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001405 if (Visited.find(Node) != Visited.end())
1406 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001408 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001410 if (Node->pred_empty()) {
1411 Root = Node;
1412 break;
1413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001415 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001416 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001417 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek938332c2009-05-16 01:11:58 +00001420 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Ted Kremenek10aa5542009-03-12 23:41:59 +00001422 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001423 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001424 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001425 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001426 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001428 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001429 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001430 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001431 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001433 // Create the equivalent node in the new graph with the same state
1434 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001435 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001437 // Store the mapping to the original node.
1438 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1439 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001440 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001442 // Link up the new node with the previous node.
1443 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001444 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001446 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001448 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001449 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001450 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001451 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001452 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001453 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001454 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001457 // Find the next successor node. We choose the node that is marked
1458 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001459 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1460 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001461 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001463 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001467 if (I == Visited.end())
1468 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001470 if (!N || I->second < MinVal) {
1471 N = *SI;
1472 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001473 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Ted Kremenek938332c2009-05-16 01:11:58 +00001476 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Ted Kremenek938332c2009-05-16 01:11:58 +00001479 assert(First);
1480
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001481 return std::make_pair(std::make_pair(GNew, BM),
1482 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001483}
1484
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001485/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1486/// and collapses PathDiagosticPieces that are expanded by macros.
1487static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1488 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1489 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001491 typedef std::vector<PathDiagnosticPiece*>
1492 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001494 MacroStackTy MacroStack;
1495 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001497 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1498 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001499 const FullSourceLoc Loc = I->getLocation().asLocation();
1500
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001501 // Determine the instantiation location, which is the location we group
1502 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001503 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001504 SM.getInstantiationLoc(Loc) :
1505 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001507 if (Loc.isFileID()) {
1508 MacroStack.clear();
1509 Pieces.push_back(&*I);
1510 continue;
1511 }
1512
1513 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001515 // Is the PathDiagnosticPiece within the same macro group?
1516 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1517 MacroStack.back().first->push_back(&*I);
1518 continue;
1519 }
1520
1521 // We aren't in the same group. Are we descending into a new macro
1522 // or are part of an old one?
1523 PathDiagnosticMacroPiece *MacroGroup = 0;
1524
1525 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1526 SM.getInstantiationLoc(Loc) :
1527 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001529 // Walk the entire macro stack.
1530 while (!MacroStack.empty()) {
1531 if (InstantiationLoc == MacroStack.back().second) {
1532 MacroGroup = MacroStack.back().first;
1533 break;
1534 }
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001536 if (ParentInstantiationLoc == MacroStack.back().second) {
1537 MacroGroup = MacroStack.back().first;
1538 break;
1539 }
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001541 MacroStack.pop_back();
1542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001544 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1545 // Create a new macro group and add it to the stack.
1546 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1547
1548 if (MacroGroup)
1549 MacroGroup->push_back(NewGroup);
1550 else {
1551 assert(InstantiationLoc.isFileID());
1552 Pieces.push_back(NewGroup);
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001555 MacroGroup = NewGroup;
1556 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1557 }
1558
1559 // Finally, add the PathDiagnosticPiece to the group.
1560 MacroGroup->push_back(&*I);
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001563 // Now take the pieces and construct a new PathDiagnostic.
1564 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001566 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1567 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1568 if (!MP->containsEvent()) {
1569 delete MP;
1570 continue;
1571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001573 PD.push_back(*I);
1574 }
1575}
1576
Ted Kremenek7dc86642009-03-31 20:22:36 +00001577void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001578 BugReportEquivClass& EQ) {
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001580 std::vector<const ExplodedNode*> Nodes;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremenekcf118d42009-02-04 23:49:09 +00001582 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001583 const ExplodedNode* N = I->getEndNode();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001584 if (N) Nodes.push_back(N);
1585 }
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Ted Kremenekcf118d42009-02-04 23:49:09 +00001587 if (Nodes.empty())
1588 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001590 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001591 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001592 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001593 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek7dc86642009-03-31 20:22:36 +00001594 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Ted Kremenekcf118d42009-02-04 23:49:09 +00001596 // Find the BugReport with the original location.
1597 BugReport *R = 0;
1598 unsigned i = 0;
1599 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
1600 if (i == GPair.second.second) { R = *I; break; }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenekcf118d42009-02-04 23:49:09 +00001602 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001604 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001605 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001606 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
1608 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001609 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenek8966bc12009-05-06 21:39:49 +00001611 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001612 PD.push_back(Piece);
1613 else
1614 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001616 R->registerInitialVisitors(PDB, N);
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenek7dc86642009-03-31 20:22:36 +00001618 switch (PDB.getGenerationScheme()) {
1619 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001620 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001621 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001622 case PathDiagnosticClient::Minimal:
1623 GenerateMinimalPathDiagnostic(PD, PDB, N);
1624 break;
1625 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001626}
1627
Ted Kremenekcf118d42009-02-04 23:49:09 +00001628void BugReporter::Register(BugType *BT) {
1629 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001630}
1631
Mike Stump1eb44332009-09-09 15:08:12 +00001632void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001633 // Compute the bug report's hash to determine its equivalence class.
1634 llvm::FoldingSetNodeID ID;
1635 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001636
1637 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001638 BugType& BT = R->getBugType();
1639 Register(&BT);
1640 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001641 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1642
Ted Kremenekcf118d42009-02-04 23:49:09 +00001643 if (!EQ) {
1644 EQ = new BugReportEquivClass(R);
1645 BT.EQClasses.InsertNode(EQ, InsertPos);
1646 }
1647 else
1648 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001649}
1650
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001651
1652//===----------------------------------------------------------------------===//
1653// Emitting reports in equivalence classes.
1654//===----------------------------------------------------------------------===//
1655
1656namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001657struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001658 const ExplodedNode *N;
1659 ExplodedNode::const_succ_iterator I, E;
1660
1661 FRIEC_WLItem(const ExplodedNode *n)
1662 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1663};
1664}
1665
1666static BugReport *FindReportInEquivalenceClass(BugReportEquivClass& EQ) {
1667 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1668 assert(I != E);
1669 BugReport *R = *I;
1670 BugType& BT = R->getBugType();
1671
1672 if (!BT.isSuppressOnSink())
1673 return R;
1674
1675 // For bug reports that should be suppressed when all paths are post-dominated
1676 // by a sink node, iterate through the reports in the equivalence class
1677 // until we find one that isn't post-dominated (if one exists). We use a
1678 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1679 // this as a recursive function, but we don't want to risk blowing out the
1680 // stack for very long paths.
1681 for (; I != E; ++I) {
1682 R = *I;
1683 const ExplodedNode *N = R->getEndNode();
1684
1685 if (!N)
1686 continue;
1687
1688 if (N->isSink()) {
1689 assert(false &&
1690 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
1691 return R;
1692 }
1693
1694 if (N->succ_empty())
1695 return R;
1696
1697 // At this point we know that 'N' is not a sink and it has at least one
1698 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1699 typedef FRIEC_WLItem WLItem;
1700 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1701 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1702
1703 DFSWorkList WL;
1704 WL.push_back(N);
1705 Visited[N] = 1;
1706
1707 while (!WL.empty()) {
1708 WLItem &WI = WL.back();
1709 assert(!WI.N->succ_empty());
1710
1711 for (; WI.I != WI.E; ++WI.I) {
1712 const ExplodedNode *Succ = *WI.I;
1713 // End-of-path node?
1714 if (Succ->succ_empty()) {
1715 // If we found an end-of-path node that is not a sink, then return
1716 // this report.
1717 if (!Succ->isSink())
1718 return R;
1719
1720 // Found a sink? Continue on to the next successor.
1721 continue;
1722 }
1723
1724 // Mark the successor as visited. If it hasn't been explored,
1725 // enqueue it to the DFS worklist.
1726 unsigned &mark = Visited[Succ];
1727 if (!mark) {
1728 mark = 1;
1729 WL.push_back(Succ);
1730 break;
1731 }
1732 }
1733
1734 if (&WL.back() == &WI)
1735 WL.pop_back();
1736 }
1737 }
1738
Ted Kremenek6b0c6eb2009-09-15 03:28:00 +00001739 // If we reach here, the end nodes for all reports in the equivalence
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001740 // class are post-dominated by a sink node.
1741 return NULL;
1742}
1743
Ted Kremeneke0a58072009-09-18 22:37:37 +00001744
1745//===----------------------------------------------------------------------===//
1746// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1747// uses global state, which eventually should go elsewhere.
1748//===----------------------------------------------------------------------===//
1749namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001750class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001751 llvm::FoldingSetNodeID ID;
1752public:
1753 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1754 ID.AddString(R->getBugType().getName());
1755 ID.AddString(R->getBugType().getCategory());
1756 ID.AddString(R->getDescription());
1757 ID.AddInteger(R->getLocation().getRawEncoding());
1758 PD->Profile(ID);
1759 }
1760
1761 void Profile(llvm::FoldingSetNodeID &id) {
1762 id = ID;
1763 }
1764
1765 llvm::FoldingSetNodeID &getID() { return ID; }
1766};
1767}
1768
1769static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1770 // FIXME: Eventually this diagnostic cache should reside in something
1771 // like AnalysisManager instead of being a static variable. This is
1772 // really unsafe in the long term.
1773 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1774 static DiagnosticCache DC;
1775
1776 void *InsertPos;
1777 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1778
1779 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1780 delete Item;
1781 return true;
1782 }
1783
1784 DC.InsertNode(Item, InsertPos);
1785 return false;
1786}
1787
Ted Kremenekcf118d42009-02-04 23:49:09 +00001788void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001789 BugReport *R = FindReportInEquivalenceClass(EQ);
1790
1791 if (!R)
1792 return;
1793
Ted Kremenekd49967f2009-04-29 21:58:13 +00001794 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Ted Kremenekcf118d42009-02-04 23:49:09 +00001796 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001797 // Probably doesn't make a difference in practice.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001798 BugType& BT = R->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Ted Kremenekd49967f2009-04-29 21:58:13 +00001800 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001801 D(new PathDiagnostic(R->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001802 !PD || PD->useVerboseDescription()
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001803 ? R->getDescription() : R->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001804 BT.getCategory()));
1805
Ted Kremenekcf118d42009-02-04 23:49:09 +00001806 GeneratePathDiagnostic(*D.get(), EQ);
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Ted Kremeneke0a58072009-09-18 22:37:37 +00001808 if (IsCachedDiagnostic(R, D.get()))
1809 return;
1810
Ted Kremenek072192b2008-04-30 23:47:44 +00001811 // Get the meta data.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001812 std::pair<const char**, const char**> Meta = R->getExtraDescriptiveText();
1813 for (const char** s = Meta.first; s != Meta.second; ++s)
1814 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001815
Ted Kremenek3148eb42009-01-24 00:55:43 +00001816 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001817 const SourceRange *Beg = 0, *End = 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001818 R->getRanges(Beg, End);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001819 Diagnostic& Diag = getDiagnostic();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001820 FullSourceLoc L(R->getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001821 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001822 R->getShortDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001823
Ted Kremenek3148eb42009-01-24 00:55:43 +00001824 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001825 default: assert(0 && "Don't handle this many ranges yet!");
1826 case 0: Diag.Report(L, ErrorDiag); break;
1827 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1828 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1829 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001830 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001831
1832 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1833 if (!PD)
1834 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
1836 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001837 PathDiagnosticPiece* piece =
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001838 new PathDiagnosticEventPiece(L, R->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001839
Ted Kremenek3148eb42009-01-24 00:55:43 +00001840 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1841 D->push_back(piece);
1842 }
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Ted Kremenek3148eb42009-01-24 00:55:43 +00001844 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001845}
Ted Kremenek57202072008-07-14 17:40:50 +00001846
Benjamin Kramerf0171732009-11-29 18:27:55 +00001847void BugReporter::EmitBasicReport(llvm::StringRef name, llvm::StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001848 SourceLocation Loc,
1849 SourceRange* RBeg, unsigned NumRanges) {
1850 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1851}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001852
Benjamin Kramerf0171732009-11-29 18:27:55 +00001853void BugReporter::EmitBasicReport(llvm::StringRef name,
1854 llvm::StringRef category,
1855 llvm::StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001856 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Ted Kremenekcf118d42009-02-04 23:49:09 +00001858 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1859 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001860 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001861 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1862 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1863 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001864}