blob: 90ae459e64cb8e682b416c80c70028b71a3e62c4 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek6b676302010-01-25 17:10:22 +000015#include "clang/Checker/BugReporter/BugReporter.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000016#include "clang/Checker/BugReporter/BugType.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000020#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000021#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
23#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000024#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek6b676302010-01-25 17:10:22 +000025#include "clang/Checker/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000027#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000028#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000029#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000030#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000031
32using namespace clang;
33
Ted Kremenek8966bc12009-05-06 21:39:49 +000034BugReporterVisitor::~BugReporterVisitor() {}
35BugReporterContext::~BugReporterContext() {
36 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I)
37 if ((*I)->isOwnedByReporterContext()) delete *I;
38}
39
Ted Kremenek1b431022010-03-20 18:01:57 +000040void BugReporterContext::addVisitor(BugReporterVisitor* visitor) {
41 if (!visitor)
42 return;
43
44 llvm::FoldingSetNodeID ID;
45 visitor->Profile(ID);
46 void *InsertPos;
47
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000048 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
49 delete visitor;
Ted Kremenek1b431022010-03-20 18:01:57 +000050 return;
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000051 }
Ted Kremenek1b431022010-03-20 18:01:57 +000052
53 CallbacksSet.InsertNode(visitor, InsertPos);
54 Callbacks = F.Add(visitor, Callbacks);
55}
56
Ted Kremenekcf118d42009-02-04 23:49:09 +000057//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000058// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000059//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000060
Zhongxing Xu7f330852010-04-06 03:01:56 +000061static inline const Stmt* GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000062 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
63 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000064 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000065 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekb697b102009-02-23 22:44:26 +000067 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000068}
69
Zhongxing Xuc5619d92009-08-06 01:32:16 +000070static inline const ExplodedNode*
71GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000072 return N->pred_empty() ? NULL : *(N->pred_begin());
73}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000074
Zhongxing Xuc5619d92009-08-06 01:32:16 +000075static inline const ExplodedNode*
76GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000077 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000078}
79
Zhongxing Xuc5619d92009-08-06 01:32:16 +000080static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000081 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000082 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000083 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Ted Kremenekb697b102009-02-23 22:44:26 +000085 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000086}
87
Zhongxing Xuc5619d92009-08-06 01:32:16 +000088static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000089 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000090 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000091 // Check if the statement is '?' or '&&'/'||'. These are "merges",
92 // not actual statement points.
93 switch (S->getStmtClass()) {
94 case Stmt::ChooseExprClass:
95 case Stmt::ConditionalOperatorClass: continue;
96 case Stmt::BinaryOperatorClass: {
97 BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode();
98 if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr)
99 continue;
100 break;
101 }
102 default:
103 break;
104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekb7c51522009-07-28 00:07:15 +0000106 // Some expressions don't have locations.
107 if (S->getLocStart().isInvalid())
108 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenekb697b102009-02-23 22:44:26 +0000113 return 0;
114}
115
Ted Kremenek5f85e172009-07-22 22:35:28 +0000116static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000117GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000118 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000119 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenekb697b102009-02-23 22:44:26 +0000121 return GetPreviousStmt(N);
122}
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Ted Kremenek5f85e172009-07-22 22:35:28 +0000124static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000125GetCurrentOrNextStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000126 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000127 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenekb697b102009-02-23 22:44:26 +0000129 return GetNextStmt(N);
130}
131
132//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000133// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000134//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000135
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000136typedef llvm::DenseMap<const ExplodedNode*,
137const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000138
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000139namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000140class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000141 NodeBackMap& M;
142public:
143 NodeMapClosure(NodeBackMap *m) : M(*m) {}
144 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000146 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000147 NodeBackMap::iterator I = M.find(N);
148 return I == M.end() ? 0 : I->second;
149 }
150};
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000152class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000153 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000154 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000155 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000156 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000157public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000158 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000159 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000160 PathDiagnosticClient *pdc)
161 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000162 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000163 addVisitor(R);
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000166 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek00605e02009-03-27 20:55:39 +0000168 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000169 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000171 Decl const &getCodeDecl() { return R->getEndNode()->getCodeDecl(); }
172
173 ParentMap& getParentMap() { return R->getEndNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000175 const Stmt *getParent(const Stmt *S) {
176 return getParentMap().getParent(S);
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek8966bc12009-05-06 21:39:49 +0000179 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Ted Kremenek7dc86642009-03-31 20:22:36 +0000180 BugReport& getReport() { return *R; }
Douglas Gregor72971342009-04-18 00:02:19 +0000181
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000182 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000184 PathDiagnosticLocation
185 getEnclosingStmtLocation(const PathDiagnosticLocation &L) {
186 if (const Stmt *S = L.asStmt())
187 return getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000189 return L;
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenek7dc86642009-03-31 20:22:36 +0000192 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
193 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
194 }
195
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000196 bool supportsLogicalOpControlFlow() const {
197 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000198 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000199};
200} // end anonymous namespace
201
Ted Kremenek00605e02009-03-27 20:55:39 +0000202PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000203PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000204 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000205 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000206
Mike Stump1eb44332009-09-09 15:08:12 +0000207 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000208 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000209}
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek00605e02009-03-27 20:55:39 +0000211PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000212PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000213 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000214
Ted Kremenek143ca222008-05-06 18:11:09 +0000215 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000216 if (os.str().empty())
217 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenek00605e02009-03-27 20:55:39 +0000219 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek00605e02009-03-27 20:55:39 +0000221 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000222 os << "Execution continues on line "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000223 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
224 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000225 else {
226 os << "Execution jumps to the end of the ";
227 const Decl *D = N->getLocationContext()->getDecl();
228 if (isa<ObjCMethodDecl>(D))
229 os << "method";
230 else if (isa<FunctionDecl>(D))
231 os << "function";
232 else {
233 assert(isa<BlockDecl>(D));
234 os << "anonymous block";
235 }
236 os << '.';
237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000239 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000240}
241
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000242static bool IsNested(const Stmt *S, ParentMap &PM) {
243 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
244 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000246 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000248 if (Parent)
249 switch (Parent->getStmtClass()) {
250 case Stmt::ForStmtClass:
251 case Stmt::DoStmtClass:
252 case Stmt::WhileStmtClass:
253 return true;
254 default:
255 break;
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
258 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000259}
260
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000261PathDiagnosticLocation
262PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
263 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000264 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000265 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000266
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000267 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000268 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000270 if (!Parent)
271 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000273 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000274 case Stmt::BinaryOperatorClass: {
275 const BinaryOperator *B = cast<BinaryOperator>(Parent);
276 if (B->isLogicalOp())
277 return PathDiagnosticLocation(S, SMgr);
278 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000279 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000280 case Stmt::CompoundStmtClass:
281 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000282 return PathDiagnosticLocation(S, SMgr);
283 case Stmt::ChooseExprClass:
284 // Similar to '?' if we are referring to condition, just have the edge
285 // point to the entire choose expression.
286 if (cast<ChooseExpr>(Parent)->getCond() == S)
287 return PathDiagnosticLocation(Parent, SMgr);
288 else
Mike Stump1eb44332009-09-09 15:08:12 +0000289 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000290 case Stmt::ConditionalOperatorClass:
291 // For '?', if we are referring to condition, just have the edge point
292 // to the entire '?' expression.
293 if (cast<ConditionalOperator>(Parent)->getCond() == S)
294 return PathDiagnosticLocation(Parent, SMgr);
295 else
Mike Stump1eb44332009-09-09 15:08:12 +0000296 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000297 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000298 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000299 case Stmt::ForStmtClass:
300 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000301 return PathDiagnosticLocation(S, SMgr);
302 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000303 case Stmt::IfStmtClass:
304 if (cast<IfStmt>(Parent)->getCond() != S)
305 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000306 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000307 case Stmt::ObjCForCollectionStmtClass:
308 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
309 return PathDiagnosticLocation(S, SMgr);
310 break;
311 case Stmt::WhileStmtClass:
312 if (cast<WhileStmt>(Parent)->getCond() != S)
313 return PathDiagnosticLocation(S, SMgr);
314 break;
315 default:
316 break;
317 }
318
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000319 S = Parent;
320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000322 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000323
324 // Special case: DeclStmts can appear in for statement declarations, in which
325 // case the ForStmt is the context.
326 if (isa<DeclStmt>(S)) {
327 if (const Stmt *Parent = P.getParent(S)) {
328 switch (Parent->getStmtClass()) {
329 case Stmt::ForStmtClass:
330 case Stmt::ObjCForCollectionStmtClass:
331 return PathDiagnosticLocation(Parent, SMgr);
332 default:
333 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000334 }
335 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000336 }
337 else if (isa<BinaryOperator>(S)) {
338 // Special case: the binary operator represents the initialization
339 // code in a for statement (this can happen when the variable being
340 // initialized is an old variable.
341 if (const ForStmt *FS =
342 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
343 if (FS->getInit() == S)
344 return PathDiagnosticLocation(FS, SMgr);
345 }
346 }
347
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000348 return PathDiagnosticLocation(S, SMgr);
349}
350
Ted Kremenekcf118d42009-02-04 23:49:09 +0000351//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000352// ScanNotableSymbols: closure-like callback for scanning Store bindings.
353//===----------------------------------------------------------------------===//
354
355static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000356GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000357 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek31061982009-03-31 23:00:32 +0000359 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek31061982009-03-31 23:00:32 +0000361 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek31061982009-03-31 23:00:32 +0000363 if (!isa<PostStmt>(P))
364 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek5f85e172009-07-22 22:35:28 +0000366 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek31061982009-03-31 23:00:32 +0000368 if (!DR)
369 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenek13976632010-02-08 16:18:51 +0000371 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek31061982009-03-31 23:00:32 +0000373 if (X != Y)
374 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek5f85e172009-07-22 22:35:28 +0000376 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek31061982009-03-31 23:00:32 +0000378 if (!VD)
379 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek31061982009-03-31 23:00:32 +0000381 return VD;
382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek31061982009-03-31 23:00:32 +0000384 return 0;
385}
386
387namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000388class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000389: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek31061982009-03-31 23:00:32 +0000391 SymbolRef Sym;
392 const GRState* PrevSt;
393 const Stmt* S;
394 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000395 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000396 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000397 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek31061982009-03-31 23:00:32 +0000399public:
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek31061982009-03-31 23:00:32 +0000401 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000402 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000403 PathDiagnostic& pd, BugReporter& br)
404 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek31061982009-03-31 23:00:32 +0000406 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
407 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek31061982009-03-31 23:00:32 +0000409 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek31061982009-03-31 23:00:32 +0000411 if (ScanSym != Sym)
412 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
414 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000415 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek31061982009-03-31 23:00:32 +0000417 if (X == V) // Same binding?
418 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek31061982009-03-31 23:00:32 +0000420 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000421 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000422 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek31061982009-03-31 23:00:32 +0000424 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek31061982009-03-31 23:00:32 +0000426 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
427 if (!B->isAssignmentOp())
428 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek31061982009-03-31 23:00:32 +0000430 // What variable did we assign to?
431 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek31061982009-03-31 23:00:32 +0000433 if (!DR)
434 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek31061982009-03-31 23:00:32 +0000436 VD = dyn_cast<VarDecl>(DR->getDecl());
437 }
438 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
439 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
440 // assume that each DeclStmt has a single Decl. This invariant
441 // holds by contruction in the CFG.
442 VD = dyn_cast<VarDecl>(*DS->decl_begin());
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek31061982009-03-31 23:00:32 +0000445 if (!VD)
446 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek31061982009-03-31 23:00:32 +0000448 // What is the most recently referenced variable with this binding?
449 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek31061982009-03-31 23:00:32 +0000451 if (!MostRecent)
452 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek31061982009-03-31 23:00:32 +0000454 // Create the diagnostic.
455 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek31061982009-03-31 23:00:32 +0000457 if (Loc::IsLocType(VD->getType())) {
458 std::string msg = "'" + std::string(VD->getNameAsString()) +
459 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek31061982009-03-31 23:00:32 +0000461 PD.push_front(new PathDiagnosticEventPiece(L, msg));
462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000465 }
Ted Kremenek31061982009-03-31 23:00:32 +0000466};
467}
468
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000469static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000470 const Stmt* S,
471 SymbolRef Sym, BugReporter& BR,
472 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000474 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000475 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Ted Kremenek31061982009-03-31 23:00:32 +0000477 if (!PrevSt)
478 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek31061982009-03-31 23:00:32 +0000480 // Look at the region bindings of the current state that map to the
481 // specified symbol. Are any of them not in the previous state?
482 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
483 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
484 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
485}
486
487namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000488class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000489: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek31061982009-03-31 23:00:32 +0000491 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000492 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000493 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000494 GRBugReporter& BR;
495 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek31061982009-03-31 23:00:32 +0000497public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000498 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000499 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000500 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek31061982009-03-31 23:00:32 +0000502 bool HandleBinding(StoreManager& SMgr, Store store,
503 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek31061982009-03-31 23:00:32 +0000507 if (!ScanSym)
508 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenek31061982009-03-31 23:00:32 +0000510 if (!BR.isNotable(ScanSym))
511 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremenek31061982009-03-31 23:00:32 +0000513 if (AlreadyProcessed.count(ScanSym))
514 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenek31061982009-03-31 23:00:32 +0000516 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Ted Kremenek31061982009-03-31 23:00:32 +0000518 HandleNotableSymbol(N, S, ScanSym, BR, PD);
519 return true;
520 }
521};
522} // end anonymous namespace
523
524//===----------------------------------------------------------------------===//
525// "Minimal" path diagnostic generation algorithm.
526//===----------------------------------------------------------------------===//
527
Ted Kremenek14856d72009-04-06 23:06:54 +0000528static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
529
Ted Kremenek31061982009-03-31 23:00:32 +0000530static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
531 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000532 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000533
Ted Kremenek31061982009-03-31 23:00:32 +0000534 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000535 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000536 ? NULL : *(N->pred_begin());
537 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000538 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000539 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek31061982009-03-31 23:00:32 +0000541 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek31061982009-03-31 23:00:32 +0000543 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000544 const CFGBlock* Src = BE->getSrc();
545 const CFGBlock* Dst = BE->getDst();
546 const Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek31061982009-03-31 23:00:32 +0000548 if (!T)
549 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek31061982009-03-31 23:00:32 +0000551 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek31061982009-03-31 23:00:32 +0000553 switch (T->getStmtClass()) {
554 default:
555 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Ted Kremenek31061982009-03-31 23:00:32 +0000557 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000558 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000559 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek31061982009-03-31 23:00:32 +0000561 if (!S)
562 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Ted Kremenek31061982009-03-31 23:00:32 +0000564 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000565 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000566 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek31061982009-03-31 23:00:32 +0000568 os << "Control jumps to line "
569 << End.asLocation().getInstantiationLineNumber();
570 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
571 os.str()));
572 break;
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
575 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000576 // Figure out what case arm we took.
577 std::string sbuf;
578 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000580 if (const Stmt* S = Dst->getLabel()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000581 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Ted Kremenek31061982009-03-31 23:00:32 +0000583 switch (S->getStmtClass()) {
584 default:
585 os << "No cases match in the switch statement. "
586 "Control jumps to line "
587 << End.asLocation().getInstantiationLineNumber();
588 break;
589 case Stmt::DefaultStmtClass:
590 os << "Control jumps to the 'default' case at line "
591 << End.asLocation().getInstantiationLineNumber();
592 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Ted Kremenek31061982009-03-31 23:00:32 +0000594 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000595 os << "Control jumps to 'case ";
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000596 const CaseStmt* Case = cast<CaseStmt>(S);
597 const Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000598
599 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000600 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000602 if (const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000603 // FIXME: Maybe this should be an assertion. Are there cases
604 // were it is not an EnumConstantDecl?
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000605 const EnumConstantDecl* D =
606 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Ted Kremenek31061982009-03-31 23:00:32 +0000608 if (D) {
609 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000610 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000611 }
612 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000613
614 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000615 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000616
Ted Kremenek31061982009-03-31 23:00:32 +0000617 os << ":' at line "
618 << End.asLocation().getInstantiationLineNumber();
619 break;
620 }
621 }
622 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
623 os.str()));
624 }
625 else {
626 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000627 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000628 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
629 os.str()));
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Ted Kremenek31061982009-03-31 23:00:32 +0000632 break;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek31061982009-03-31 23:00:32 +0000635 case Stmt::BreakStmtClass:
636 case Stmt::ContinueStmtClass: {
637 std::string sbuf;
638 llvm::raw_string_ostream os(sbuf);
639 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
640 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
641 os.str()));
642 break;
643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Ted Kremenek31061982009-03-31 23:00:32 +0000645 // Determine control-flow for ternary '?'.
646 case Stmt::ConditionalOperatorClass: {
647 std::string sbuf;
648 llvm::raw_string_ostream os(sbuf);
649 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek31061982009-03-31 23:00:32 +0000651 if (*(Src->succ_begin()+1) == Dst)
652 os << "false";
653 else
654 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek31061982009-03-31 23:00:32 +0000656 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 if (const Stmt *S = End.asStmt())
659 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek31061982009-03-31 23:00:32 +0000661 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
662 os.str()));
663 break;
664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Ted Kremenek31061982009-03-31 23:00:32 +0000666 // Determine control-flow for short-circuited '&&' and '||'.
667 case Stmt::BinaryOperatorClass: {
668 if (!PDB.supportsLogicalOpControlFlow())
669 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000671 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000672 std::string sbuf;
673 llvm::raw_string_ostream os(sbuf);
674 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek31061982009-03-31 23:00:32 +0000676 if (B->getOpcode() == BinaryOperator::LAnd) {
677 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek31061982009-03-31 23:00:32 +0000679 if (*(Src->succ_begin()+1) == Dst) {
680 os << "false";
681 PathDiagnosticLocation End(B->getLHS(), SMgr);
682 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
683 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
684 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000685 }
Ted Kremenek31061982009-03-31 23:00:32 +0000686 else {
687 os << "true";
688 PathDiagnosticLocation Start(B->getLHS(), SMgr);
689 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
690 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
691 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000692 }
Ted Kremenek31061982009-03-31 23:00:32 +0000693 }
694 else {
695 assert(B->getOpcode() == BinaryOperator::LOr);
696 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek31061982009-03-31 23:00:32 +0000698 if (*(Src->succ_begin()+1) == Dst) {
699 os << "false";
700 PathDiagnosticLocation Start(B->getLHS(), SMgr);
701 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
702 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000703 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000704 }
705 else {
706 os << "true";
707 PathDiagnosticLocation End(B->getLHS(), SMgr);
708 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
709 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000710 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000711 }
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek31061982009-03-31 23:00:32 +0000714 break;
715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
717 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000718 if (*(Src->succ_begin()) == Dst) {
719 std::string sbuf;
720 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek31061982009-03-31 23:00:32 +0000722 os << "Loop condition is true. ";
723 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek31061982009-03-31 23:00:32 +0000725 if (const Stmt *S = End.asStmt())
726 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremenek31061982009-03-31 23:00:32 +0000728 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
729 os.str()));
730 }
731 else {
732 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek31061982009-03-31 23:00:32 +0000734 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 "Loop condition is false. Exiting loop"));
739 }
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek31061982009-03-31 23:00:32 +0000741 break;
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Ted Kremenek31061982009-03-31 23:00:32 +0000744 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000745 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000746 if (*(Src->succ_begin()+1) == Dst) {
747 std::string sbuf;
748 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremenek31061982009-03-31 23:00:32 +0000750 os << "Loop condition is false. ";
751 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
752 if (const Stmt *S = End.asStmt())
753 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek31061982009-03-31 23:00:32 +0000755 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
756 os.str()));
757 }
758 else {
759 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
760 if (const Stmt *S = End.asStmt())
761 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek31061982009-03-31 23:00:32 +0000763 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000764 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000765 }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek31061982009-03-31 23:00:32 +0000767 break;
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek31061982009-03-31 23:00:32 +0000770 case Stmt::IfStmtClass: {
771 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek31061982009-03-31 23:00:32 +0000773 if (const Stmt *S = End.asStmt())
774 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek31061982009-03-31 23:00:32 +0000776 if (*(Src->succ_begin()+1) == Dst)
777 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000778 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000779 else
Ted Kremenek31061982009-03-31 23:00:32 +0000780 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000781 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek31061982009-03-31 23:00:32 +0000783 break;
784 }
785 }
786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000788 if (NextNode) {
789 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
790 E = PDB.visitor_end(); I!=E; ++I) {
791 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
792 PD.push_front(p);
793 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
796 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000797 // Scan the region bindings, and see if a "notable" symbol has a new
798 // lval binding.
799 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
800 PDB.getStateManager().iterBindings(N->getState(), SNS);
801 }
802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek14856d72009-04-06 23:06:54 +0000804 // After constructing the full PathDiagnostic, do a pass over it to compact
805 // PathDiagnosticPieces that occur within a macro.
806 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000807}
808
809//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000810// "Extensive" PathDiagnostic generation.
811//===----------------------------------------------------------------------===//
812
813static bool IsControlFlowExpr(const Stmt *S) {
814 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000816 if (!E)
817 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
819 E = E->IgnoreParenCasts();
820
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000821 if (isa<ConditionalOperator>(E))
822 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000824 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
825 if (B->isLogicalOp())
826 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
828 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000829}
830
Ted Kremenek14856d72009-04-06 23:06:54 +0000831namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000832class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000833 bool IsDead;
834public:
835 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
836 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000837
838 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000839 bool isDead() const { return IsDead; }
840};
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000842class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000843 std::vector<ContextLocation> CLocs;
844 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000845 PathDiagnostic &PD;
846 PathDiagnosticBuilder &PDB;
847 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000849 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek14856d72009-04-06 23:06:54 +0000851 bool containsLocation(const PathDiagnosticLocation &Container,
852 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek14856d72009-04-06 23:06:54 +0000854 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Ted Kremenek9650cf32009-05-11 21:42:34 +0000856 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
857 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000858 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000859 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000860 while (1) {
861 // Adjust the location for some expressions that are best referenced
862 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000863 switch (S->getStmtClass()) {
864 default:
865 break;
866 case Stmt::ParenExprClass:
867 S = cast<ParenExpr>(S)->IgnoreParens();
868 firstCharOnly = true;
869 continue;
870 case Stmt::ConditionalOperatorClass:
871 S = cast<ConditionalOperator>(S)->getCond();
872 firstCharOnly = true;
873 continue;
874 case Stmt::ChooseExprClass:
875 S = cast<ChooseExpr>(S)->getCond();
876 firstCharOnly = true;
877 continue;
878 case Stmt::BinaryOperatorClass:
879 S = cast<BinaryOperator>(S)->getLHS();
880 firstCharOnly = true;
881 continue;
882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek9650cf32009-05-11 21:42:34 +0000884 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Ted Kremenek9650cf32009-05-11 21:42:34 +0000887 if (S != Original)
888 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek9650cf32009-05-11 21:42:34 +0000891 if (firstCharOnly)
892 L = PathDiagnosticLocation(L.asLocation());
893
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000894 return L;
895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenek14856d72009-04-06 23:06:54 +0000897 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000898 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000899 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000900 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000901 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000902 CLocs.pop_back();
903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
905 PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000906
907public:
908 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
909 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremeneka301a672009-04-22 18:16:20 +0000911 // If the PathDiagnostic already has pieces, add the enclosing statement
912 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000913 if (!PD.empty()) {
914 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Ted Kremenek14856d72009-04-06 23:06:54 +0000916 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000917 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000918 }
919 }
920
921 ~EdgeBuilder() {
922 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Ted Kremeneka301a672009-04-22 18:16:20 +0000924 // Finally, add an initial edge from the start location of the first
925 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000926 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
927 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000928 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000929 if (!CS->body_empty()) {
930 SourceLocation Loc = (*CS->body_begin())->getLocStart();
931 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek14856d72009-04-06 23:06:54 +0000934 }
935
936 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Ted Kremenek14856d72009-04-06 23:06:54 +0000938 void addEdge(const Stmt *S, bool alwaysAdd = false) {
939 addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd);
940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000942 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek14856d72009-04-06 23:06:54 +0000944 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000945 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000946};
Ted Kremenek14856d72009-04-06 23:06:54 +0000947} // end anonymous namespace
948
949
950PathDiagnosticLocation
951EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
952 if (const Stmt *S = L.asStmt()) {
953 if (IsControlFlowExpr(S))
954 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
956 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Ted Kremenek14856d72009-04-06 23:06:54 +0000959 return L;
960}
961
962bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
963 const PathDiagnosticLocation &Containee) {
964
965 if (Container == Containee)
966 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Ted Kremenek14856d72009-04-06 23:06:54 +0000968 if (Container.asDecl())
969 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Ted Kremenek14856d72009-04-06 23:06:54 +0000971 if (const Stmt *S = Containee.asStmt())
972 if (const Stmt *ContainerS = Container.asStmt()) {
973 while (S) {
974 if (S == ContainerS)
975 return true;
976 S = PDB.getParent(S);
977 }
978 return false;
979 }
980
981 // Less accurate: compare using source ranges.
982 SourceRange ContainerR = Container.asRange();
983 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 SourceManager &SM = PDB.getSourceManager();
986 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
987 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
988 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
989 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Ted Kremenek14856d72009-04-06 23:06:54 +0000991 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
992 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
993 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
994 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek14856d72009-04-06 23:06:54 +0000996 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000997 assert(ContaineeBegLine <= ContaineeEndLine);
998
Ted Kremenek14856d72009-04-06 23:06:54 +0000999 return (ContainerBegLine <= ContaineeBegLine &&
1000 ContainerEndLine >= ContaineeEndLine &&
1001 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +00001002 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +00001003 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
1004 (ContainerEndLine != ContaineeEndLine ||
1005 SM.getInstantiationColumnNumber(ContainerREnd) >=
1006 SM.getInstantiationColumnNumber(ContainerREnd)));
1007}
1008
1009PathDiagnosticLocation
1010EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) {
1011 if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt()))
1012 return PathDiagnosticLocation(E->IgnoreParenCasts(),
1013 PDB.getSourceManager());
1014 return L;
1015}
1016
1017void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1018 if (!PrevLoc.isValid()) {
1019 PrevLoc = NewLoc;
1020 return;
1021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001023 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1024 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001026 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001027 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek14856d72009-04-06 23:06:54 +00001029 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001030 if (NewLocClean.asLocation().getInstantiationLoc() ==
1031 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001032 return;
1033
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001034 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1035 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001036}
1037
1038void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Ted Kremeneka301a672009-04-22 18:16:20 +00001040 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1041 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremenek14856d72009-04-06 23:06:54 +00001043 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1044
1045 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001046 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek14856d72009-04-06 23:06:54 +00001048 // Is the top location context the same as the one for the new location?
1049 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001050 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001051 if (IsConsumedExpr(TopContextLoc) &&
1052 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001053 TopContextLoc.markDead();
1054
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001056 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001057
1058 return;
1059 }
1060
1061 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001062 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001063 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001065 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001066 CLocs.push_back(ContextLocation(CLoc, true));
1067 return;
1068 }
1069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremenek14856d72009-04-06 23:06:54 +00001071 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001072 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001073 }
1074
1075 // Context does not contain the location. Flush it.
1076 popLocation();
1077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001079 // If we reach here, there is no enclosing context. Just add the edge.
1080 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001081}
1082
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001083bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1084 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1085 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001087 return false;
1088}
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremeneke1baed32009-05-05 23:13:38 +00001090void EdgeBuilder::addExtendedContext(const Stmt *S) {
1091 if (!S)
1092 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
1094 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001095 while (Parent) {
1096 if (isa<CompoundStmt>(Parent))
1097 Parent = PDB.getParent(Parent);
1098 else
1099 break;
1100 }
1101
1102 if (Parent) {
1103 switch (Parent->getStmtClass()) {
1104 case Stmt::DoStmtClass:
1105 case Stmt::ObjCAtSynchronizedStmtClass:
1106 addContext(Parent);
1107 default:
1108 break;
1109 }
1110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Ted Kremeneke1baed32009-05-05 23:13:38 +00001112 addContext(S);
1113}
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Ted Kremenek14856d72009-04-06 23:06:54 +00001115void EdgeBuilder::addContext(const Stmt *S) {
1116 if (!S)
1117 return;
1118
1119 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Ted Kremenek14856d72009-04-06 23:06:54 +00001121 while (!CLocs.empty()) {
1122 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1123
1124 // Is the top location context the same as the one for the new location?
1125 if (TopContextLoc == L)
1126 return;
1127
1128 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001129 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001130 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001131 }
1132
1133 // Context does not contain the location. Flush it.
1134 popLocation();
1135 }
1136
1137 CLocs.push_back(L);
1138}
1139
1140static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1141 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001142 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001143 EdgeBuilder EB(PD, PDB);
1144
Zhongxing Xu41ca1342010-03-23 05:13:26 +00001145 const ExplodedNode* NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001146 while (NextNode) {
1147 N = NextNode;
1148 NextNode = GetPredecessorNode(N);
1149 ProgramPoint P = N->getLocation();
1150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 do {
1152 // Block edges.
1153 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1154 const CFGBlock &Blk = *BE->getSrc();
1155 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001157 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001158 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001159 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001160 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001162 if (!Term) {
1163 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1164 CS = dyn_cast<CompoundStmt>(FS->getBody());
1165 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001166 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001167 }
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001169 PathDiagnosticEventPiece *p =
1170 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001171 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001173 EB.addEdge(p->getLocation(), true);
1174 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001176 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001177 PathDiagnosticLocation BL(CS->getRBracLoc(),
1178 PDB.getSourceManager());
1179 BL = PathDiagnosticLocation(BL.asLocation());
1180 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001181 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001184 if (Term)
1185 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001187 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001188 }
1189
Mike Stump1eb44332009-09-09 15:08:12 +00001190 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001191 if (const Stmt* S = BE->getFirstStmt()) {
1192 if (IsControlFlowExpr(S)) {
1193 // Add the proper context for '&&', '||', and '?'.
1194 EB.addContext(S);
1195 }
1196 else
1197 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1198 }
1199
1200 break;
1201 }
1202 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001204 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001205 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Ted Kremenek8966bc12009-05-06 21:39:49 +00001207 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1208 E = PDB.visitor_end(); I!=E; ++I) {
1209 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1210 const PathDiagnosticLocation &Loc = p->getLocation();
1211 EB.addEdge(Loc, true);
1212 PD.push_front(p);
1213 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001214 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001217 }
1218}
1219
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001220//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001221// Methods for BugType and subclasses.
1222//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001223BugType::~BugType() {
1224 // Free up the equivalence class objects. Observe that we get a pointer to
1225 // the object first before incrementing the iterator, as destroying the
1226 // node before doing so means we will read from freed memory.
1227 for (iterator I = begin(), E = end(); I !=E; ) {
1228 BugReportEquivClass *EQ = &*I;
1229 ++I;
1230 delete EQ;
1231 }
1232}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001233void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001234
Ted Kremenekcf118d42009-02-04 23:49:09 +00001235//===----------------------------------------------------------------------===//
1236// Methods for BugReport and subclasses.
1237//===----------------------------------------------------------------------===//
1238BugReport::~BugReport() {}
1239RangedBugReport::~RangedBugReport() {}
1240
Mike Stump1eb44332009-09-09 15:08:12 +00001241const Stmt* BugReport::getStmt() const {
1242 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001243 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenekcf118d42009-02-04 23:49:09 +00001245 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001246 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001247 if (BE->getBlock() == &Exit)
1248 S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001249 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001250 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001251 S = GetStmt(ProgP);
1252
1253 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001254}
1255
1256PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001257BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001258 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001260 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Ted Kremenek61f3e052008-04-03 04:42:52 +00001262 if (!S)
1263 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001264
Ted Kremenekde7161f2008-04-03 18:00:37 +00001265 const SourceRange *Beg, *End;
Mike Stump1eb44332009-09-09 15:08:12 +00001266 getRanges(Beg, End);
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001267 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001269 // Only add the statement itself as a range if we didn't specify any
1270 // special ranges for this report.
1271 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1272 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001274 for (; Beg != End; ++Beg)
1275 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Ted Kremenek61f3e052008-04-03 04:42:52 +00001277 return P;
1278}
1279
Mike Stump1eb44332009-09-09 15:08:12 +00001280void BugReport::getRanges(const SourceRange*& beg, const SourceRange*& end) {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001281 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001282 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001283 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001284 beg = &R;
1285 end = beg+1;
1286 }
1287 else
1288 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001289}
1290
Mike Stump1eb44332009-09-09 15:08:12 +00001291SourceLocation BugReport::getLocation() const {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001292 if (EndNode)
Ted Kremenek5f85e172009-07-22 22:35:28 +00001293 if (const Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001294 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001295 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001296 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001297 // For binary operators, return the location of the operator.
1298 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1299 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001300
Ted Kremenekcf118d42009-02-04 23:49:09 +00001301 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001302 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001303
1304 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001305}
1306
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001307PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1308 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001309 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001310 return NULL;
1311}
1312
Ted Kremenekcf118d42009-02-04 23:49:09 +00001313//===----------------------------------------------------------------------===//
1314// Methods for BugReporter and subclasses.
1315//===----------------------------------------------------------------------===//
1316
1317BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001318 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001319}
1320
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001321GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001322BugReporterData::~BugReporterData() {}
1323
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001324ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001325
1326GRStateManager&
1327GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1328
1329BugReporter::~BugReporter() { FlushReports(); }
1330
1331void BugReporter::FlushReports() {
1332 if (BugTypes.isEmpty())
1333 return;
1334
1335 // First flush the warnings for each BugType. This may end up creating new
1336 // warnings and new BugTypes. Because ImmutableSet is a functional data
1337 // structure, we do not need to worry about the iterators being invalidated.
1338 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1339 const_cast<BugType*>(*I)->FlushReports(*this);
1340
1341 // Iterate through BugTypes a second time. BugTypes may have been updated
1342 // with new BugType objects and new warnings.
1343 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1344 BugType *BT = const_cast<BugType*>(*I);
1345
1346 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1347 SetTy& EQClasses = BT->EQClasses;
1348
1349 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1350 BugReportEquivClass& EQ = *EI;
1351 FlushReport(EQ);
1352 }
Mike Stump1eb44332009-09-09 15:08:12 +00001353
1354 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001355 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001356 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001357
1358 // Remove all references to the BugType objects.
1359 BugTypes = F.GetEmptySet();
1360}
1361
1362//===----------------------------------------------------------------------===//
1363// PathDiagnostics generation.
1364//===----------------------------------------------------------------------===//
1365
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001366static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001367 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001368MakeReportGraph(const ExplodedGraph* G,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001369 const ExplodedNode** NStart,
1370 const ExplodedNode** NEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Ted Kremenekcf118d42009-02-04 23:49:09 +00001372 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001373 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001374 // error node unless there are two or more error nodes with the same minimum
1375 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001376 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001377 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001378
1379 llvm::DenseMap<const void*, const void*> InverseMap;
1380 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Ted Kremenekcf118d42009-02-04 23:49:09 +00001382 // Create owning pointers for GTrim and NMap just to ensure that they are
1383 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001384 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001385 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenekcf118d42009-02-04 23:49:09 +00001387 // Find the (first) error node in the trimmed graph. We just need to consult
1388 // the node map (NMap) which maps from nodes in the original graph to nodes
1389 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001390
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001391 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001392 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001393 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001394
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001395 for (const ExplodedNode** I = NStart; I != NEnd; ++I)
1396 if (const ExplodedNode *N = NMap->getMappedNode(*I)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001397 unsigned NodeIndex = (I - NStart) / sizeof(*I);
1398 WS.push(N);
1399 IndexMap[*I] = NodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001400 }
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Ted Kremenek938332c2009-05-16 01:11:58 +00001402 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001403
1404 // Create a new (third!) graph with a single path. This is the graph
1405 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001406 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Ted Kremenek10aa5542009-03-12 23:41:59 +00001408 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001409 // to the root node, and then construct a new graph that contains only
1410 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001411 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001413 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001414 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001416 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001417 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001418 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001420 if (Visited.find(Node) != Visited.end())
1421 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001423 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001425 if (Node->pred_empty()) {
1426 Root = Node;
1427 break;
1428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001430 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001431 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001432 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenek938332c2009-05-16 01:11:58 +00001435 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek10aa5542009-03-12 23:41:59 +00001437 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001438 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001439 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001440 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001441 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001443 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001444 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001445 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001446 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001448 // Create the equivalent node in the new graph with the same state
1449 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001450 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001452 // Store the mapping to the original node.
1453 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1454 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001455 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001457 // Link up the new node with the previous node.
1458 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001459 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001461 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001463 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001464 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001465 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001466 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001467 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001468 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001469 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 // Find the next successor node. We choose the node that is marked
1473 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001474 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1475 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001476 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001478 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001480 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001482 if (I == Visited.end())
1483 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001485 if (!N || I->second < MinVal) {
1486 N = *SI;
1487 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001488 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001489 }
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek938332c2009-05-16 01:11:58 +00001491 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001492 }
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek938332c2009-05-16 01:11:58 +00001494 assert(First);
1495
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001496 return std::make_pair(std::make_pair(GNew, BM),
1497 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001498}
1499
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001500/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1501/// and collapses PathDiagosticPieces that are expanded by macros.
1502static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1503 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1504 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001506 typedef std::vector<PathDiagnosticPiece*>
1507 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001509 MacroStackTy MacroStack;
1510 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001512 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1513 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001514 const FullSourceLoc Loc = I->getLocation().asLocation();
1515
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001516 // Determine the instantiation location, which is the location we group
1517 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001518 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001519 SM.getInstantiationLoc(Loc) :
1520 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001522 if (Loc.isFileID()) {
1523 MacroStack.clear();
1524 Pieces.push_back(&*I);
1525 continue;
1526 }
1527
1528 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001530 // Is the PathDiagnosticPiece within the same macro group?
1531 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1532 MacroStack.back().first->push_back(&*I);
1533 continue;
1534 }
1535
1536 // We aren't in the same group. Are we descending into a new macro
1537 // or are part of an old one?
1538 PathDiagnosticMacroPiece *MacroGroup = 0;
1539
1540 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1541 SM.getInstantiationLoc(Loc) :
1542 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001544 // Walk the entire macro stack.
1545 while (!MacroStack.empty()) {
1546 if (InstantiationLoc == MacroStack.back().second) {
1547 MacroGroup = MacroStack.back().first;
1548 break;
1549 }
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001551 if (ParentInstantiationLoc == MacroStack.back().second) {
1552 MacroGroup = MacroStack.back().first;
1553 break;
1554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001556 MacroStack.pop_back();
1557 }
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001559 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1560 // Create a new macro group and add it to the stack.
1561 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1562
1563 if (MacroGroup)
1564 MacroGroup->push_back(NewGroup);
1565 else {
1566 assert(InstantiationLoc.isFileID());
1567 Pieces.push_back(NewGroup);
1568 }
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001570 MacroGroup = NewGroup;
1571 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1572 }
1573
1574 // Finally, add the PathDiagnosticPiece to the group.
1575 MacroGroup->push_back(&*I);
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001578 // Now take the pieces and construct a new PathDiagnostic.
1579 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001581 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1582 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1583 if (!MP->containsEvent()) {
1584 delete MP;
1585 continue;
1586 }
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001588 PD.push_back(*I);
1589 }
1590}
1591
Ted Kremenek7dc86642009-03-31 20:22:36 +00001592void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001593 BugReportEquivClass& EQ) {
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001595 std::vector<const ExplodedNode*> Nodes;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Ted Kremenekcf118d42009-02-04 23:49:09 +00001597 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001598 const ExplodedNode* N = I->getEndNode();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001599 if (N) Nodes.push_back(N);
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenekcf118d42009-02-04 23:49:09 +00001602 if (Nodes.empty())
1603 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001605 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001606 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001607 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001608 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek7dc86642009-03-31 20:22:36 +00001609 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenekcf118d42009-02-04 23:49:09 +00001611 // Find the BugReport with the original location.
1612 BugReport *R = 0;
1613 unsigned i = 0;
1614 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
1615 if (i == GPair.second.second) { R = *I; break; }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Ted Kremenekcf118d42009-02-04 23:49:09 +00001617 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001619 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001620 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001621 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001622
1623 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001624 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek8966bc12009-05-06 21:39:49 +00001626 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001627 PD.push_back(Piece);
1628 else
1629 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Ted Kremenekff7f7362010-03-20 18:02:01 +00001631 // Register node visitors.
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001632 R->registerInitialVisitors(PDB, N);
Ted Kremenekff7f7362010-03-20 18:02:01 +00001633 bugreporter::registerNilReceiverVisitor(PDB);
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Ted Kremenek7dc86642009-03-31 20:22:36 +00001635 switch (PDB.getGenerationScheme()) {
1636 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001637 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001638 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001639 case PathDiagnosticClient::Minimal:
1640 GenerateMinimalPathDiagnostic(PD, PDB, N);
1641 break;
1642 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001643}
1644
Ted Kremenekcf118d42009-02-04 23:49:09 +00001645void BugReporter::Register(BugType *BT) {
1646 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001647}
1648
Mike Stump1eb44332009-09-09 15:08:12 +00001649void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001650 // Compute the bug report's hash to determine its equivalence class.
1651 llvm::FoldingSetNodeID ID;
1652 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001653
1654 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001655 BugType& BT = R->getBugType();
1656 Register(&BT);
1657 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001658 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1659
Ted Kremenekcf118d42009-02-04 23:49:09 +00001660 if (!EQ) {
1661 EQ = new BugReportEquivClass(R);
1662 BT.EQClasses.InsertNode(EQ, InsertPos);
1663 }
1664 else
1665 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001666}
1667
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001668
1669//===----------------------------------------------------------------------===//
1670// Emitting reports in equivalence classes.
1671//===----------------------------------------------------------------------===//
1672
1673namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001674struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001675 const ExplodedNode *N;
1676 ExplodedNode::const_succ_iterator I, E;
1677
1678 FRIEC_WLItem(const ExplodedNode *n)
1679 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1680};
1681}
1682
1683static BugReport *FindReportInEquivalenceClass(BugReportEquivClass& EQ) {
1684 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1685 assert(I != E);
1686 BugReport *R = *I;
1687 BugType& BT = R->getBugType();
1688
1689 if (!BT.isSuppressOnSink())
1690 return R;
1691
1692 // For bug reports that should be suppressed when all paths are post-dominated
1693 // by a sink node, iterate through the reports in the equivalence class
1694 // until we find one that isn't post-dominated (if one exists). We use a
1695 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1696 // this as a recursive function, but we don't want to risk blowing out the
1697 // stack for very long paths.
1698 for (; I != E; ++I) {
1699 R = *I;
1700 const ExplodedNode *N = R->getEndNode();
1701
1702 if (!N)
1703 continue;
1704
1705 if (N->isSink()) {
1706 assert(false &&
1707 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
1708 return R;
1709 }
1710
1711 if (N->succ_empty())
1712 return R;
1713
1714 // At this point we know that 'N' is not a sink and it has at least one
1715 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1716 typedef FRIEC_WLItem WLItem;
1717 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1718 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1719
1720 DFSWorkList WL;
1721 WL.push_back(N);
1722 Visited[N] = 1;
1723
1724 while (!WL.empty()) {
1725 WLItem &WI = WL.back();
1726 assert(!WI.N->succ_empty());
1727
1728 for (; WI.I != WI.E; ++WI.I) {
1729 const ExplodedNode *Succ = *WI.I;
1730 // End-of-path node?
1731 if (Succ->succ_empty()) {
1732 // If we found an end-of-path node that is not a sink, then return
1733 // this report.
1734 if (!Succ->isSink())
1735 return R;
1736
1737 // Found a sink? Continue on to the next successor.
1738 continue;
1739 }
1740
1741 // Mark the successor as visited. If it hasn't been explored,
1742 // enqueue it to the DFS worklist.
1743 unsigned &mark = Visited[Succ];
1744 if (!mark) {
1745 mark = 1;
1746 WL.push_back(Succ);
1747 break;
1748 }
1749 }
1750
1751 if (&WL.back() == &WI)
1752 WL.pop_back();
1753 }
1754 }
1755
Ted Kremenek6b0c6eb2009-09-15 03:28:00 +00001756 // If we reach here, the end nodes for all reports in the equivalence
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001757 // class are post-dominated by a sink node.
1758 return NULL;
1759}
1760
Ted Kremeneke0a58072009-09-18 22:37:37 +00001761
1762//===----------------------------------------------------------------------===//
1763// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1764// uses global state, which eventually should go elsewhere.
1765//===----------------------------------------------------------------------===//
1766namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001767class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001768 llvm::FoldingSetNodeID ID;
1769public:
1770 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1771 ID.AddString(R->getBugType().getName());
1772 ID.AddString(R->getBugType().getCategory());
1773 ID.AddString(R->getDescription());
1774 ID.AddInteger(R->getLocation().getRawEncoding());
1775 PD->Profile(ID);
1776 }
1777
1778 void Profile(llvm::FoldingSetNodeID &id) {
1779 id = ID;
1780 }
1781
1782 llvm::FoldingSetNodeID &getID() { return ID; }
1783};
1784}
1785
1786static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1787 // FIXME: Eventually this diagnostic cache should reside in something
1788 // like AnalysisManager instead of being a static variable. This is
1789 // really unsafe in the long term.
1790 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1791 static DiagnosticCache DC;
1792
1793 void *InsertPos;
1794 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1795
1796 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1797 delete Item;
1798 return true;
1799 }
1800
1801 DC.InsertNode(Item, InsertPos);
1802 return false;
1803}
1804
Ted Kremenekcf118d42009-02-04 23:49:09 +00001805void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001806 BugReport *R = FindReportInEquivalenceClass(EQ);
1807
1808 if (!R)
1809 return;
1810
Ted Kremenekd49967f2009-04-29 21:58:13 +00001811 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Ted Kremenekcf118d42009-02-04 23:49:09 +00001813 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001814 // Probably doesn't make a difference in practice.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001815 BugType& BT = R->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Ted Kremenekd49967f2009-04-29 21:58:13 +00001817 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001818 D(new PathDiagnostic(R->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001819 !PD || PD->useVerboseDescription()
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001820 ? R->getDescription() : R->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001821 BT.getCategory()));
1822
Ted Kremenekcf118d42009-02-04 23:49:09 +00001823 GeneratePathDiagnostic(*D.get(), EQ);
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Ted Kremeneke0a58072009-09-18 22:37:37 +00001825 if (IsCachedDiagnostic(R, D.get()))
1826 return;
1827
Ted Kremenek072192b2008-04-30 23:47:44 +00001828 // Get the meta data.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001829 std::pair<const char**, const char**> Meta = R->getExtraDescriptiveText();
1830 for (const char** s = Meta.first; s != Meta.second; ++s)
1831 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001832
Ted Kremenek3148eb42009-01-24 00:55:43 +00001833 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001834 const SourceRange *Beg = 0, *End = 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001835 R->getRanges(Beg, End);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001836 Diagnostic& Diag = getDiagnostic();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001837 FullSourceLoc L(R->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001838
1839 // Search the description for '%', as that will be interpretted as a
1840 // format character by FormatDiagnostics.
1841 llvm::StringRef desc = R->getShortDescription();
1842 unsigned ErrorDiag;
1843 {
1844 llvm::SmallString<512> TmpStr;
1845 llvm::raw_svector_ostream Out(TmpStr);
1846 for (llvm::StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
1847 if (*I == '%')
1848 Out << "%%";
1849 else
1850 Out << *I;
1851
1852 Out.flush();
1853 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1854 }
Ted Kremenek57202072008-07-14 17:40:50 +00001855
Ted Kremenek3148eb42009-01-24 00:55:43 +00001856 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001857 default: assert(0 && "Don't handle this many ranges yet!");
1858 case 0: Diag.Report(L, ErrorDiag); break;
1859 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1860 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1861 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001862 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001863
1864 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1865 if (!PD)
1866 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001867
1868 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001869 PathDiagnosticPiece* piece =
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001870 new PathDiagnosticEventPiece(L, R->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001871
Ted Kremenek3148eb42009-01-24 00:55:43 +00001872 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1873 D->push_back(piece);
1874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Ted Kremenek3148eb42009-01-24 00:55:43 +00001876 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001877}
Ted Kremenek57202072008-07-14 17:40:50 +00001878
Benjamin Kramerf0171732009-11-29 18:27:55 +00001879void BugReporter::EmitBasicReport(llvm::StringRef name, llvm::StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001880 SourceLocation Loc,
1881 SourceRange* RBeg, unsigned NumRanges) {
1882 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1883}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001884
Benjamin Kramerf0171732009-11-29 18:27:55 +00001885void BugReporter::EmitBasicReport(llvm::StringRef name,
1886 llvm::StringRef category,
1887 llvm::StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001888 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Ted Kremenekcf118d42009-02-04 23:49:09 +00001890 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1891 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001892 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001893 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1894 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1895 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001896}