blob: 7272b348581b20389c3455acada3250e0edb9dfe [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"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000017#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000018#include "clang/Analysis/CFG.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000019#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000020#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000021#include "clang/AST/StmtObjC.h"
22#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000023#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek6b676302010-01-25 17:10:22 +000024#include "clang/Checker/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000026#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000027#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000028#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000029#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000030
31using namespace clang;
32
Ted Kremenek8966bc12009-05-06 21:39:49 +000033BugReporterVisitor::~BugReporterVisitor() {}
34BugReporterContext::~BugReporterContext() {
35 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I)
36 if ((*I)->isOwnedByReporterContext()) delete *I;
37}
38
Ted Kremenek1b431022010-03-20 18:01:57 +000039void BugReporterContext::addVisitor(BugReporterVisitor* visitor) {
40 if (!visitor)
41 return;
42
43 llvm::FoldingSetNodeID ID;
44 visitor->Profile(ID);
45 void *InsertPos;
46
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000047 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
48 delete visitor;
Ted Kremenek1b431022010-03-20 18:01:57 +000049 return;
Ted Kremenek3e0e41c2010-03-21 04:38:40 +000050 }
Ted Kremenek1b431022010-03-20 18:01:57 +000051
52 CallbacksSet.InsertNode(visitor, InsertPos);
53 Callbacks = F.Add(visitor, Callbacks);
54}
55
Ted Kremenekcf118d42009-02-04 23:49:09 +000056//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000057// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000058//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000059
Ted Kremenek5f85e172009-07-22 22:35:28 +000060static inline const Stmt* GetStmt(ProgramPoint P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000061 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
62 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000063 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000064 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000065
Ted Kremenekb697b102009-02-23 22:44:26 +000066 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000067}
68
Zhongxing Xuc5619d92009-08-06 01:32:16 +000069static inline const ExplodedNode*
70GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000071 return N->pred_empty() ? NULL : *(N->pred_begin());
72}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000073
Zhongxing Xuc5619d92009-08-06 01:32:16 +000074static inline const ExplodedNode*
75GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000076 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000077}
78
Zhongxing Xuc5619d92009-08-06 01:32:16 +000079static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000080 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000081 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000082 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000083
Ted Kremenekb697b102009-02-23 22:44:26 +000084 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000085}
86
Zhongxing Xuc5619d92009-08-06 01:32:16 +000087static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000088 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000089 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000090 // Check if the statement is '?' or '&&'/'||'. These are "merges",
91 // not actual statement points.
92 switch (S->getStmtClass()) {
93 case Stmt::ChooseExprClass:
94 case Stmt::ConditionalOperatorClass: continue;
95 case Stmt::BinaryOperatorClass: {
96 BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode();
97 if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr)
98 continue;
99 break;
100 }
101 default:
102 break;
103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenekb7c51522009-07-28 00:07:15 +0000105 // Some expressions don't have locations.
106 if (S->getLocStart().isInvalid())
107 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenekb697b102009-02-23 22:44:26 +0000109 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenekb697b102009-02-23 22:44:26 +0000112 return 0;
113}
114
Ted Kremenek5f85e172009-07-22 22:35:28 +0000115static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000116GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000117 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000118 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenekb697b102009-02-23 22:44:26 +0000120 return GetPreviousStmt(N);
121}
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremenek5f85e172009-07-22 22:35:28 +0000123static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000124GetCurrentOrNextStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000125 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000126 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenekb697b102009-02-23 22:44:26 +0000128 return GetNextStmt(N);
129}
130
131//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000132// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000133//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000134
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000135typedef llvm::DenseMap<const ExplodedNode*,
136const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000137
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000138namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000139class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000140 NodeBackMap& M;
141public:
142 NodeMapClosure(NodeBackMap *m) : M(*m) {}
143 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000145 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000146 NodeBackMap::iterator I = M.find(N);
147 return I == M.end() ? 0 : I->second;
148 }
149};
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000151class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000152 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000153 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000154 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000155 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000156public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000157 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000158 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000159 PathDiagnosticClient *pdc)
160 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000161 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000162 addVisitor(R);
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000165 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek00605e02009-03-27 20:55:39 +0000167 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000168 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000170 Decl const &getCodeDecl() { return R->getEndNode()->getCodeDecl(); }
171
172 ParentMap& getParentMap() { return R->getEndNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000174 const Stmt *getParent(const Stmt *S) {
175 return getParentMap().getParent(S);
176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek8966bc12009-05-06 21:39:49 +0000178 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Ted Kremenek7dc86642009-03-31 20:22:36 +0000179 BugReport& getReport() { return *R; }
Douglas Gregor72971342009-04-18 00:02:19 +0000180
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000181 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000183 PathDiagnosticLocation
184 getEnclosingStmtLocation(const PathDiagnosticLocation &L) {
185 if (const Stmt *S = L.asStmt())
186 return getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000188 return L;
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenek7dc86642009-03-31 20:22:36 +0000191 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
192 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
193 }
194
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000195 bool supportsLogicalOpControlFlow() const {
196 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000197 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000198};
199} // end anonymous namespace
200
Ted Kremenek00605e02009-03-27 20:55:39 +0000201PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000202PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000203 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000204 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000205
Mike Stump1eb44332009-09-09 15:08:12 +0000206 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000207 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000208}
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek00605e02009-03-27 20:55:39 +0000210PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000211PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000212 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000213
Ted Kremenek143ca222008-05-06 18:11:09 +0000214 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000215 if (os.str().empty())
216 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremenek00605e02009-03-27 20:55:39 +0000218 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenek00605e02009-03-27 20:55:39 +0000220 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000221 os << "Execution continues on line "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000222 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
223 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000224 else {
225 os << "Execution jumps to the end of the ";
226 const Decl *D = N->getLocationContext()->getDecl();
227 if (isa<ObjCMethodDecl>(D))
228 os << "method";
229 else if (isa<FunctionDecl>(D))
230 os << "function";
231 else {
232 assert(isa<BlockDecl>(D));
233 os << "anonymous block";
234 }
235 os << '.';
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000238 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000239}
240
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000241static bool IsNested(const Stmt *S, ParentMap &PM) {
242 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
243 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000245 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000247 if (Parent)
248 switch (Parent->getStmtClass()) {
249 case Stmt::ForStmtClass:
250 case Stmt::DoStmtClass:
251 case Stmt::WhileStmtClass:
252 return true;
253 default:
254 break;
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
257 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000258}
259
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000260PathDiagnosticLocation
261PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
262 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000263 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000264 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000265
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000266 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000267 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000269 if (!Parent)
270 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000272 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000273 case Stmt::BinaryOperatorClass: {
274 const BinaryOperator *B = cast<BinaryOperator>(Parent);
275 if (B->isLogicalOp())
276 return PathDiagnosticLocation(S, SMgr);
277 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000278 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000279 case Stmt::CompoundStmtClass:
280 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000281 return PathDiagnosticLocation(S, SMgr);
282 case Stmt::ChooseExprClass:
283 // Similar to '?' if we are referring to condition, just have the edge
284 // point to the entire choose expression.
285 if (cast<ChooseExpr>(Parent)->getCond() == S)
286 return PathDiagnosticLocation(Parent, SMgr);
287 else
Mike Stump1eb44332009-09-09 15:08:12 +0000288 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000289 case Stmt::ConditionalOperatorClass:
290 // For '?', if we are referring to condition, just have the edge point
291 // to the entire '?' expression.
292 if (cast<ConditionalOperator>(Parent)->getCond() == S)
293 return PathDiagnosticLocation(Parent, SMgr);
294 else
Mike Stump1eb44332009-09-09 15:08:12 +0000295 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000296 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000297 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000298 case Stmt::ForStmtClass:
299 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000300 return PathDiagnosticLocation(S, SMgr);
301 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000302 case Stmt::IfStmtClass:
303 if (cast<IfStmt>(Parent)->getCond() != S)
304 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000305 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000306 case Stmt::ObjCForCollectionStmtClass:
307 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
308 return PathDiagnosticLocation(S, SMgr);
309 break;
310 case Stmt::WhileStmtClass:
311 if (cast<WhileStmt>(Parent)->getCond() != S)
312 return PathDiagnosticLocation(S, SMgr);
313 break;
314 default:
315 break;
316 }
317
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000318 S = Parent;
319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000321 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000322
323 // Special case: DeclStmts can appear in for statement declarations, in which
324 // case the ForStmt is the context.
325 if (isa<DeclStmt>(S)) {
326 if (const Stmt *Parent = P.getParent(S)) {
327 switch (Parent->getStmtClass()) {
328 case Stmt::ForStmtClass:
329 case Stmt::ObjCForCollectionStmtClass:
330 return PathDiagnosticLocation(Parent, SMgr);
331 default:
332 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000333 }
334 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000335 }
336 else if (isa<BinaryOperator>(S)) {
337 // Special case: the binary operator represents the initialization
338 // code in a for statement (this can happen when the variable being
339 // initialized is an old variable.
340 if (const ForStmt *FS =
341 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
342 if (FS->getInit() == S)
343 return PathDiagnosticLocation(FS, SMgr);
344 }
345 }
346
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000347 return PathDiagnosticLocation(S, SMgr);
348}
349
Ted Kremenekcf118d42009-02-04 23:49:09 +0000350//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000351// ScanNotableSymbols: closure-like callback for scanning Store bindings.
352//===----------------------------------------------------------------------===//
353
354static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000355GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000356 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek31061982009-03-31 23:00:32 +0000358 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek31061982009-03-31 23:00:32 +0000360 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek31061982009-03-31 23:00:32 +0000362 if (!isa<PostStmt>(P))
363 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek5f85e172009-07-22 22:35:28 +0000365 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek31061982009-03-31 23:00:32 +0000367 if (!DR)
368 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek13976632010-02-08 16:18:51 +0000370 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek31061982009-03-31 23:00:32 +0000372 if (X != Y)
373 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek5f85e172009-07-22 22:35:28 +0000375 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek31061982009-03-31 23:00:32 +0000377 if (!VD)
378 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek31061982009-03-31 23:00:32 +0000380 return VD;
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek31061982009-03-31 23:00:32 +0000383 return 0;
384}
385
386namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000387class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000388: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek31061982009-03-31 23:00:32 +0000390 SymbolRef Sym;
391 const GRState* PrevSt;
392 const Stmt* S;
393 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000394 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000395 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000396 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek31061982009-03-31 23:00:32 +0000398public:
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek31061982009-03-31 23:00:32 +0000400 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000401 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000402 PathDiagnostic& pd, BugReporter& br)
403 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek31061982009-03-31 23:00:32 +0000405 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
406 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek31061982009-03-31 23:00:32 +0000408 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremenek31061982009-03-31 23:00:32 +0000410 if (ScanSym != Sym)
411 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
413 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000414 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 if (X == V) // Same binding?
417 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000420 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000421 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek31061982009-03-31 23:00:32 +0000423 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek31061982009-03-31 23:00:32 +0000425 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
426 if (!B->isAssignmentOp())
427 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek31061982009-03-31 23:00:32 +0000429 // What variable did we assign to?
430 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek31061982009-03-31 23:00:32 +0000432 if (!DR)
433 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek31061982009-03-31 23:00:32 +0000435 VD = dyn_cast<VarDecl>(DR->getDecl());
436 }
437 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
438 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
439 // assume that each DeclStmt has a single Decl. This invariant
440 // holds by contruction in the CFG.
441 VD = dyn_cast<VarDecl>(*DS->decl_begin());
442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek31061982009-03-31 23:00:32 +0000444 if (!VD)
445 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek31061982009-03-31 23:00:32 +0000447 // What is the most recently referenced variable with this binding?
448 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Ted Kremenek31061982009-03-31 23:00:32 +0000450 if (!MostRecent)
451 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenek31061982009-03-31 23:00:32 +0000453 // Create the diagnostic.
454 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek31061982009-03-31 23:00:32 +0000456 if (Loc::IsLocType(VD->getType())) {
457 std::string msg = "'" + std::string(VD->getNameAsString()) +
458 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek31061982009-03-31 23:00:32 +0000460 PD.push_front(new PathDiagnosticEventPiece(L, msg));
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek31061982009-03-31 23:00:32 +0000463 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000464 }
Ted Kremenek31061982009-03-31 23:00:32 +0000465};
466}
467
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000468static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000469 const Stmt* S,
470 SymbolRef Sym, BugReporter& BR,
471 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000473 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000474 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenek31061982009-03-31 23:00:32 +0000476 if (!PrevSt)
477 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek31061982009-03-31 23:00:32 +0000479 // Look at the region bindings of the current state that map to the
480 // specified symbol. Are any of them not in the previous state?
481 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
482 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
483 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
484}
485
486namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000487class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000488: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek31061982009-03-31 23:00:32 +0000490 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000491 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000492 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000493 GRBugReporter& BR;
494 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000497 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000498 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000499 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501 bool HandleBinding(StoreManager& SMgr, Store store,
502 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek31061982009-03-31 23:00:32 +0000504 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Ted Kremenek31061982009-03-31 23:00:32 +0000506 if (!ScanSym)
507 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenek31061982009-03-31 23:00:32 +0000509 if (!BR.isNotable(ScanSym))
510 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek31061982009-03-31 23:00:32 +0000512 if (AlreadyProcessed.count(ScanSym))
513 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenek31061982009-03-31 23:00:32 +0000515 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek31061982009-03-31 23:00:32 +0000517 HandleNotableSymbol(N, S, ScanSym, BR, PD);
518 return true;
519 }
520};
521} // end anonymous namespace
522
523//===----------------------------------------------------------------------===//
524// "Minimal" path diagnostic generation algorithm.
525//===----------------------------------------------------------------------===//
526
Ted Kremenek14856d72009-04-06 23:06:54 +0000527static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
528
Ted Kremenek31061982009-03-31 23:00:32 +0000529static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
530 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000531 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000532
Ted Kremenek31061982009-03-31 23:00:32 +0000533 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000534 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000535 ? NULL : *(N->pred_begin());
536 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000537 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000538 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenek31061982009-03-31 23:00:32 +0000542 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
543 CFGBlock* Src = BE->getSrc();
544 CFGBlock* Dst = BE->getDst();
545 Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 if (!T)
548 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek31061982009-03-31 23:00:32 +0000550 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek31061982009-03-31 23:00:32 +0000552 switch (T->getStmtClass()) {
553 default:
554 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Ted Kremenek31061982009-03-31 23:00:32 +0000556 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000557 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000558 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Ted Kremenek31061982009-03-31 23:00:32 +0000560 if (!S)
561 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Ted Kremenek31061982009-03-31 23:00:32 +0000563 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000564 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000565 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek31061982009-03-31 23:00:32 +0000567 os << "Control jumps to line "
568 << End.asLocation().getInstantiationLineNumber();
569 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
570 os.str()));
571 break;
572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
574 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000575 // Figure out what case arm we took.
576 std::string sbuf;
577 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Ted Kremenek31061982009-03-31 23:00:32 +0000579 if (Stmt* S = Dst->getLabel()) {
580 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Ted Kremenek31061982009-03-31 23:00:32 +0000582 switch (S->getStmtClass()) {
583 default:
584 os << "No cases match in the switch statement. "
585 "Control jumps to line "
586 << End.asLocation().getInstantiationLineNumber();
587 break;
588 case Stmt::DefaultStmtClass:
589 os << "Control jumps to the 'default' case at line "
590 << End.asLocation().getInstantiationLineNumber();
591 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Ted Kremenek31061982009-03-31 23:00:32 +0000593 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000594 os << "Control jumps to 'case ";
595 CaseStmt* Case = cast<CaseStmt>(S);
Ted Kremenek31061982009-03-31 23:00:32 +0000596 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000597
598 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000599 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Ted Kremenek31061982009-03-31 23:00:32 +0000601 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
602 // FIXME: Maybe this should be an assertion. Are there cases
603 // were it is not an EnumConstantDecl?
604 EnumConstantDecl* D =
605 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Ted Kremenek31061982009-03-31 23:00:32 +0000607 if (D) {
608 GetRawInt = false;
609 os << D->getNameAsString();
610 }
611 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000612
613 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000614 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000615
Ted Kremenek31061982009-03-31 23:00:32 +0000616 os << ":' at line "
617 << End.asLocation().getInstantiationLineNumber();
618 break;
619 }
620 }
621 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
622 os.str()));
623 }
624 else {
625 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000626 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000627 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
628 os.str()));
629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Ted Kremenek31061982009-03-31 23:00:32 +0000631 break;
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Ted Kremenek31061982009-03-31 23:00:32 +0000634 case Stmt::BreakStmtClass:
635 case Stmt::ContinueStmtClass: {
636 std::string sbuf;
637 llvm::raw_string_ostream os(sbuf);
638 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
639 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
640 os.str()));
641 break;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek31061982009-03-31 23:00:32 +0000644 // Determine control-flow for ternary '?'.
645 case Stmt::ConditionalOperatorClass: {
646 std::string sbuf;
647 llvm::raw_string_ostream os(sbuf);
648 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek31061982009-03-31 23:00:32 +0000650 if (*(Src->succ_begin()+1) == Dst)
651 os << "false";
652 else
653 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Ted Kremenek31061982009-03-31 23:00:32 +0000655 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremenek31061982009-03-31 23:00:32 +0000657 if (const Stmt *S = End.asStmt())
658 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Ted Kremenek31061982009-03-31 23:00:32 +0000660 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
661 os.str()));
662 break;
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Ted Kremenek31061982009-03-31 23:00:32 +0000665 // Determine control-flow for short-circuited '&&' and '||'.
666 case Stmt::BinaryOperatorClass: {
667 if (!PDB.supportsLogicalOpControlFlow())
668 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 BinaryOperator *B = cast<BinaryOperator>(T);
671 std::string sbuf;
672 llvm::raw_string_ostream os(sbuf);
673 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Ted Kremenek31061982009-03-31 23:00:32 +0000675 if (B->getOpcode() == BinaryOperator::LAnd) {
676 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Ted Kremenek31061982009-03-31 23:00:32 +0000678 if (*(Src->succ_begin()+1) == Dst) {
679 os << "false";
680 PathDiagnosticLocation End(B->getLHS(), SMgr);
681 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
682 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
683 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000684 }
Ted Kremenek31061982009-03-31 23:00:32 +0000685 else {
686 os << "true";
687 PathDiagnosticLocation Start(B->getLHS(), SMgr);
688 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
689 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
690 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000691 }
Ted Kremenek31061982009-03-31 23:00:32 +0000692 }
693 else {
694 assert(B->getOpcode() == BinaryOperator::LOr);
695 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek31061982009-03-31 23:00:32 +0000697 if (*(Src->succ_begin()+1) == Dst) {
698 os << "false";
699 PathDiagnosticLocation Start(B->getLHS(), SMgr);
700 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
701 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000702 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000703 }
704 else {
705 os << "true";
706 PathDiagnosticLocation End(B->getLHS(), SMgr);
707 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
708 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000709 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000710 }
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek31061982009-03-31 23:00:32 +0000713 break;
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
716 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000717 if (*(Src->succ_begin()) == Dst) {
718 std::string sbuf;
719 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenek31061982009-03-31 23:00:32 +0000721 os << "Loop condition is true. ";
722 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Ted Kremenek31061982009-03-31 23:00:32 +0000724 if (const Stmt *S = End.asStmt())
725 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek31061982009-03-31 23:00:32 +0000727 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
728 os.str()));
729 }
730 else {
731 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek31061982009-03-31 23:00:32 +0000733 if (const Stmt *S = End.asStmt())
734 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Ted Kremenek31061982009-03-31 23:00:32 +0000736 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
737 "Loop condition is false. Exiting loop"));
738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek31061982009-03-31 23:00:32 +0000740 break;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek31061982009-03-31 23:00:32 +0000743 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000744 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000745 if (*(Src->succ_begin()+1) == Dst) {
746 std::string sbuf;
747 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek31061982009-03-31 23:00:32 +0000749 os << "Loop condition is false. ";
750 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
751 if (const Stmt *S = End.asStmt())
752 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenek31061982009-03-31 23:00:32 +0000754 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
755 os.str()));
756 }
757 else {
758 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
759 if (const Stmt *S = End.asStmt())
760 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek31061982009-03-31 23:00:32 +0000762 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000763 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Ted Kremenek31061982009-03-31 23:00:32 +0000766 break;
767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenek31061982009-03-31 23:00:32 +0000769 case Stmt::IfStmtClass: {
770 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek31061982009-03-31 23:00:32 +0000772 if (const Stmt *S = End.asStmt())
773 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek31061982009-03-31 23:00:32 +0000775 if (*(Src->succ_begin()+1) == Dst)
776 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000777 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000778 else
Ted Kremenek31061982009-03-31 23:00:32 +0000779 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000780 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Ted Kremenek31061982009-03-31 23:00:32 +0000782 break;
783 }
784 }
785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000787 if (NextNode) {
788 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
789 E = PDB.visitor_end(); I!=E; ++I) {
790 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
791 PD.push_front(p);
792 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
795 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000796 // Scan the region bindings, and see if a "notable" symbol has a new
797 // lval binding.
798 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
799 PDB.getStateManager().iterBindings(N->getState(), SNS);
800 }
801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek14856d72009-04-06 23:06:54 +0000803 // After constructing the full PathDiagnostic, do a pass over it to compact
804 // PathDiagnosticPieces that occur within a macro.
805 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000806}
807
808//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000809// "Extensive" PathDiagnostic generation.
810//===----------------------------------------------------------------------===//
811
812static bool IsControlFlowExpr(const Stmt *S) {
813 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000815 if (!E)
816 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
818 E = E->IgnoreParenCasts();
819
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000820 if (isa<ConditionalOperator>(E))
821 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000823 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
824 if (B->isLogicalOp())
825 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
827 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000828}
829
Ted Kremenek14856d72009-04-06 23:06:54 +0000830namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000831class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000832 bool IsDead;
833public:
834 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
835 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000836
837 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000838 bool isDead() const { return IsDead; }
839};
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000841class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000842 std::vector<ContextLocation> CLocs;
843 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000844 PathDiagnostic &PD;
845 PathDiagnosticBuilder &PDB;
846 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000848 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Ted Kremenek14856d72009-04-06 23:06:54 +0000850 bool containsLocation(const PathDiagnosticLocation &Container,
851 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek14856d72009-04-06 23:06:54 +0000853 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek9650cf32009-05-11 21:42:34 +0000855 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
856 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000857 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000858 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000859 while (1) {
860 // Adjust the location for some expressions that are best referenced
861 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000862 switch (S->getStmtClass()) {
863 default:
864 break;
865 case Stmt::ParenExprClass:
866 S = cast<ParenExpr>(S)->IgnoreParens();
867 firstCharOnly = true;
868 continue;
869 case Stmt::ConditionalOperatorClass:
870 S = cast<ConditionalOperator>(S)->getCond();
871 firstCharOnly = true;
872 continue;
873 case Stmt::ChooseExprClass:
874 S = cast<ChooseExpr>(S)->getCond();
875 firstCharOnly = true;
876 continue;
877 case Stmt::BinaryOperatorClass:
878 S = cast<BinaryOperator>(S)->getLHS();
879 firstCharOnly = true;
880 continue;
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek9650cf32009-05-11 21:42:34 +0000883 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Ted Kremenek9650cf32009-05-11 21:42:34 +0000886 if (S != Original)
887 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000888 }
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Ted Kremenek9650cf32009-05-11 21:42:34 +0000890 if (firstCharOnly)
891 L = PathDiagnosticLocation(L.asLocation());
892
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000893 return L;
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Ted Kremenek14856d72009-04-06 23:06:54 +0000896 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000897 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000898 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000899 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000900 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000901 CLocs.pop_back();
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
904 PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000905
906public:
907 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
908 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremeneka301a672009-04-22 18:16:20 +0000910 // If the PathDiagnostic already has pieces, add the enclosing statement
911 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000912 if (!PD.empty()) {
913 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek14856d72009-04-06 23:06:54 +0000915 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000916 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000917 }
918 }
919
920 ~EdgeBuilder() {
921 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremeneka301a672009-04-22 18:16:20 +0000923 // Finally, add an initial edge from the start location of the first
924 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000925 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
926 if (const CompoundStmt *CS =
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000927 PDB.getCodeDecl().getCompoundBody())
Ted Kremeneka301a672009-04-22 18:16:20 +0000928 if (!CS->body_empty()) {
929 SourceLocation Loc = (*CS->body_begin())->getLocStart();
930 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
931 }
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Ted Kremenek14856d72009-04-06 23:06:54 +0000933 }
934
935 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Ted Kremenek14856d72009-04-06 23:06:54 +0000937 void addEdge(const Stmt *S, bool alwaysAdd = false) {
938 addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd);
939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000941 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Ted Kremenek14856d72009-04-06 23:06:54 +0000943 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000944 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000945};
Ted Kremenek14856d72009-04-06 23:06:54 +0000946} // end anonymous namespace
947
948
949PathDiagnosticLocation
950EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
951 if (const Stmt *S = L.asStmt()) {
952 if (IsControlFlowExpr(S))
953 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
955 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Ted Kremenek14856d72009-04-06 23:06:54 +0000958 return L;
959}
960
961bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
962 const PathDiagnosticLocation &Containee) {
963
964 if (Container == Containee)
965 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Ted Kremenek14856d72009-04-06 23:06:54 +0000967 if (Container.asDecl())
968 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremenek14856d72009-04-06 23:06:54 +0000970 if (const Stmt *S = Containee.asStmt())
971 if (const Stmt *ContainerS = Container.asStmt()) {
972 while (S) {
973 if (S == ContainerS)
974 return true;
975 S = PDB.getParent(S);
976 }
977 return false;
978 }
979
980 // Less accurate: compare using source ranges.
981 SourceRange ContainerR = Container.asRange();
982 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 SourceManager &SM = PDB.getSourceManager();
985 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
986 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
987 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
988 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Ted Kremenek14856d72009-04-06 23:06:54 +0000990 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
991 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
992 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
993 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Ted Kremenek14856d72009-04-06 23:06:54 +0000995 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000996 assert(ContaineeBegLine <= ContaineeEndLine);
997
Ted Kremenek14856d72009-04-06 23:06:54 +0000998 return (ContainerBegLine <= ContaineeBegLine &&
999 ContainerEndLine >= ContaineeEndLine &&
1000 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +00001001 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +00001002 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
1003 (ContainerEndLine != ContaineeEndLine ||
1004 SM.getInstantiationColumnNumber(ContainerREnd) >=
1005 SM.getInstantiationColumnNumber(ContainerREnd)));
1006}
1007
1008PathDiagnosticLocation
1009EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) {
1010 if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt()))
1011 return PathDiagnosticLocation(E->IgnoreParenCasts(),
1012 PDB.getSourceManager());
1013 return L;
1014}
1015
1016void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1017 if (!PrevLoc.isValid()) {
1018 PrevLoc = NewLoc;
1019 return;
1020 }
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001022 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1023 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001025 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001026 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Ted Kremenek14856d72009-04-06 23:06:54 +00001028 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001029 if (NewLocClean.asLocation().getInstantiationLoc() ==
1030 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001031 return;
1032
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001033 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1034 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001035}
1036
1037void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Ted Kremeneka301a672009-04-22 18:16:20 +00001039 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1040 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Ted Kremenek14856d72009-04-06 23:06:54 +00001042 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1043
1044 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001045 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Ted Kremenek14856d72009-04-06 23:06:54 +00001047 // Is the top location context the same as the one for the new location?
1048 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001049 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001050 if (IsConsumedExpr(TopContextLoc) &&
1051 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001052 TopContextLoc.markDead();
1053
Ted Kremenek14856d72009-04-06 23:06:54 +00001054 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001055 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001056
1057 return;
1058 }
1059
1060 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001061 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001062 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001064 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001065 CLocs.push_back(ContextLocation(CLoc, true));
1066 return;
1067 }
1068 }
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Ted Kremenek14856d72009-04-06 23:06:54 +00001070 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001071 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001072 }
1073
1074 // Context does not contain the location. Flush it.
1075 popLocation();
1076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001078 // If we reach here, there is no enclosing context. Just add the edge.
1079 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001080}
1081
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001082bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1083 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1084 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001086 return false;
1087}
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Ted Kremeneke1baed32009-05-05 23:13:38 +00001089void EdgeBuilder::addExtendedContext(const Stmt *S) {
1090 if (!S)
1091 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001092
1093 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001094 while (Parent) {
1095 if (isa<CompoundStmt>(Parent))
1096 Parent = PDB.getParent(Parent);
1097 else
1098 break;
1099 }
1100
1101 if (Parent) {
1102 switch (Parent->getStmtClass()) {
1103 case Stmt::DoStmtClass:
1104 case Stmt::ObjCAtSynchronizedStmtClass:
1105 addContext(Parent);
1106 default:
1107 break;
1108 }
1109 }
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Ted Kremeneke1baed32009-05-05 23:13:38 +00001111 addContext(S);
1112}
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Ted Kremenek14856d72009-04-06 23:06:54 +00001114void EdgeBuilder::addContext(const Stmt *S) {
1115 if (!S)
1116 return;
1117
1118 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Ted Kremenek14856d72009-04-06 23:06:54 +00001120 while (!CLocs.empty()) {
1121 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1122
1123 // Is the top location context the same as the one for the new location?
1124 if (TopContextLoc == L)
1125 return;
1126
1127 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001128 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001129 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001130 }
1131
1132 // Context does not contain the location. Flush it.
1133 popLocation();
1134 }
1135
1136 CLocs.push_back(L);
1137}
1138
1139static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1140 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001141 const ExplodedNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +00001142
1143
Ted Kremenek14856d72009-04-06 23:06:54 +00001144 EdgeBuilder EB(PD, PDB);
1145
Mike Stump1eb44332009-09-09 15:08:12 +00001146 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek14856d72009-04-06 23:06:54 +00001147 ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001148 while (NextNode) {
1149 N = NextNode;
1150 NextNode = GetPredecessorNode(N);
1151 ProgramPoint P = N->getLocation();
1152
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001153 do {
1154 // Block edges.
1155 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1156 const CFGBlock &Blk = *BE->getSrc();
1157 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001159 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001160 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001161 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001162 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001164 if (!Term) {
1165 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1166 CS = dyn_cast<CompoundStmt>(FS->getBody());
1167 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001168 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001169 }
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001171 PathDiagnosticEventPiece *p =
1172 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001173 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001175 EB.addEdge(p->getLocation(), true);
1176 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001178 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001179 PathDiagnosticLocation BL(CS->getRBracLoc(),
1180 PDB.getSourceManager());
1181 BL = PathDiagnosticLocation(BL.asLocation());
1182 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001183 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001186 if (Term)
1187 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001189 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001190 }
1191
Mike Stump1eb44332009-09-09 15:08:12 +00001192 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001193 if (const Stmt* S = BE->getFirstStmt()) {
1194 if (IsControlFlowExpr(S)) {
1195 // Add the proper context for '&&', '||', and '?'.
1196 EB.addContext(S);
1197 }
1198 else
1199 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1200 }
1201
1202 break;
1203 }
1204 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001206 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001207 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Ted Kremenek8966bc12009-05-06 21:39:49 +00001209 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1210 E = PDB.visitor_end(); I!=E; ++I) {
1211 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1212 const PathDiagnosticLocation &Loc = p->getLocation();
1213 EB.addEdge(Loc, true);
1214 PD.push_front(p);
1215 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001216 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001217 }
Mike Stump1eb44332009-09-09 15:08:12 +00001218 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001219 }
1220}
1221
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001222//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001223// Methods for BugType and subclasses.
1224//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001225BugType::~BugType() {
1226 // Free up the equivalence class objects. Observe that we get a pointer to
1227 // the object first before incrementing the iterator, as destroying the
1228 // node before doing so means we will read from freed memory.
1229 for (iterator I = begin(), E = end(); I !=E; ) {
1230 BugReportEquivClass *EQ = &*I;
1231 ++I;
1232 delete EQ;
1233 }
1234}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001235void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001236
Ted Kremenekcf118d42009-02-04 23:49:09 +00001237//===----------------------------------------------------------------------===//
1238// Methods for BugReport and subclasses.
1239//===----------------------------------------------------------------------===//
1240BugReport::~BugReport() {}
1241RangedBugReport::~RangedBugReport() {}
1242
Mike Stump1eb44332009-09-09 15:08:12 +00001243const Stmt* BugReport::getStmt() const {
1244 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001245 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenekcf118d42009-02-04 23:49:09 +00001247 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001248 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001249 if (BE->getBlock() == &Exit)
1250 S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001251 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001252 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001253 S = GetStmt(ProgP);
1254
1255 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001256}
1257
1258PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001259BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001260 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001262 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Ted Kremenek61f3e052008-04-03 04:42:52 +00001264 if (!S)
1265 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001266
Ted Kremenekde7161f2008-04-03 18:00:37 +00001267 const SourceRange *Beg, *End;
Mike Stump1eb44332009-09-09 15:08:12 +00001268 getRanges(Beg, End);
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001269 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001271 // Only add the statement itself as a range if we didn't specify any
1272 // special ranges for this report.
1273 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1274 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001276 for (; Beg != End; ++Beg)
1277 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Ted Kremenek61f3e052008-04-03 04:42:52 +00001279 return P;
1280}
1281
Mike Stump1eb44332009-09-09 15:08:12 +00001282void BugReport::getRanges(const SourceRange*& beg, const SourceRange*& end) {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001283 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001284 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001285 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001286 beg = &R;
1287 end = beg+1;
1288 }
1289 else
1290 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001291}
1292
Mike Stump1eb44332009-09-09 15:08:12 +00001293SourceLocation BugReport::getLocation() const {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001294 if (EndNode)
Ted Kremenek5f85e172009-07-22 22:35:28 +00001295 if (const Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001296 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001297 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001298 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001299 // For binary operators, return the location of the operator.
1300 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1301 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001302
Ted Kremenekcf118d42009-02-04 23:49:09 +00001303 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001304 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001305
1306 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001307}
1308
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001309PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1310 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001311 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001312 return NULL;
1313}
1314
Ted Kremenekcf118d42009-02-04 23:49:09 +00001315//===----------------------------------------------------------------------===//
1316// Methods for BugReporter and subclasses.
1317//===----------------------------------------------------------------------===//
1318
1319BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001320 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001321}
1322
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001323GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001324BugReporterData::~BugReporterData() {}
1325
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001326ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001327
1328GRStateManager&
1329GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1330
1331BugReporter::~BugReporter() { FlushReports(); }
1332
1333void BugReporter::FlushReports() {
1334 if (BugTypes.isEmpty())
1335 return;
1336
1337 // First flush the warnings for each BugType. This may end up creating new
1338 // warnings and new BugTypes. Because ImmutableSet is a functional data
1339 // structure, we do not need to worry about the iterators being invalidated.
1340 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1341 const_cast<BugType*>(*I)->FlushReports(*this);
1342
1343 // Iterate through BugTypes a second time. BugTypes may have been updated
1344 // with new BugType objects and new warnings.
1345 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1346 BugType *BT = const_cast<BugType*>(*I);
1347
1348 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1349 SetTy& EQClasses = BT->EQClasses;
1350
1351 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1352 BugReportEquivClass& EQ = *EI;
1353 FlushReport(EQ);
1354 }
Mike Stump1eb44332009-09-09 15:08:12 +00001355
1356 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001357 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001358 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001359
1360 // Remove all references to the BugType objects.
1361 BugTypes = F.GetEmptySet();
1362}
1363
1364//===----------------------------------------------------------------------===//
1365// PathDiagnostics generation.
1366//===----------------------------------------------------------------------===//
1367
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001368static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001369 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001370MakeReportGraph(const ExplodedGraph* G,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001371 const ExplodedNode** NStart,
1372 const ExplodedNode** NEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Ted Kremenekcf118d42009-02-04 23:49:09 +00001374 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001375 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376 // error node unless there are two or more error nodes with the same minimum
1377 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001378 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001379 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001380
1381 llvm::DenseMap<const void*, const void*> InverseMap;
1382 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Ted Kremenekcf118d42009-02-04 23:49:09 +00001384 // Create owning pointers for GTrim and NMap just to ensure that they are
1385 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001386 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001387 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Ted Kremenekcf118d42009-02-04 23:49:09 +00001389 // Find the (first) error node in the trimmed graph. We just need to consult
1390 // the node map (NMap) which maps from nodes in the original graph to nodes
1391 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001392
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001393 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001394 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001395 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001396
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001397 for (const ExplodedNode** I = NStart; I != NEnd; ++I)
1398 if (const ExplodedNode *N = NMap->getMappedNode(*I)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001399 unsigned NodeIndex = (I - NStart) / sizeof(*I);
1400 WS.push(N);
1401 IndexMap[*I] = NodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Ted Kremenek938332c2009-05-16 01:11:58 +00001404 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001405
1406 // Create a new (third!) graph with a single path. This is the graph
1407 // that will be returned to the caller.
Zhongxing Xucc025532009-08-25 03:33:41 +00001408 ExplodedGraph *GNew = new ExplodedGraph(GTrim->getContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Ted Kremenek10aa5542009-03-12 23:41:59 +00001410 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001411 // to the root node, and then construct a new graph that contains only
1412 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001413 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001415 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001416 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001418 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001419 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001420 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001422 if (Visited.find(Node) != Visited.end())
1423 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001425 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001427 if (Node->pred_empty()) {
1428 Root = Node;
1429 break;
1430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001432 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001433 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001434 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001435 }
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek938332c2009-05-16 01:11:58 +00001437 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremenek10aa5542009-03-12 23:41:59 +00001439 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001440 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001441 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001442 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001443 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001445 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001447 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001448 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001450 // Create the equivalent node in the new graph with the same state
1451 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001452 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001454 // Store the mapping to the original node.
1455 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1456 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001457 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001459 // Link up the new node with the previous node.
1460 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001461 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001463 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001466 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001467 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001468 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001469 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001470 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001471 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001474 // Find the next successor node. We choose the node that is marked
1475 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001476 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1477 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001478 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001480 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001482 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 if (I == Visited.end())
1485 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001487 if (!N || I->second < MinVal) {
1488 N = *SI;
1489 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001490 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Ted Kremenek938332c2009-05-16 01:11:58 +00001493 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001494 }
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Ted Kremenek938332c2009-05-16 01:11:58 +00001496 assert(First);
1497
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001498 return std::make_pair(std::make_pair(GNew, BM),
1499 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001500}
1501
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001502/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1503/// and collapses PathDiagosticPieces that are expanded by macros.
1504static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1505 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1506 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001508 typedef std::vector<PathDiagnosticPiece*>
1509 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001511 MacroStackTy MacroStack;
1512 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001514 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1515 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001516 const FullSourceLoc Loc = I->getLocation().asLocation();
1517
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001518 // Determine the instantiation location, which is the location we group
1519 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001520 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001521 SM.getInstantiationLoc(Loc) :
1522 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001524 if (Loc.isFileID()) {
1525 MacroStack.clear();
1526 Pieces.push_back(&*I);
1527 continue;
1528 }
1529
1530 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001532 // Is the PathDiagnosticPiece within the same macro group?
1533 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1534 MacroStack.back().first->push_back(&*I);
1535 continue;
1536 }
1537
1538 // We aren't in the same group. Are we descending into a new macro
1539 // or are part of an old one?
1540 PathDiagnosticMacroPiece *MacroGroup = 0;
1541
1542 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1543 SM.getInstantiationLoc(Loc) :
1544 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001546 // Walk the entire macro stack.
1547 while (!MacroStack.empty()) {
1548 if (InstantiationLoc == MacroStack.back().second) {
1549 MacroGroup = MacroStack.back().first;
1550 break;
1551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001553 if (ParentInstantiationLoc == MacroStack.back().second) {
1554 MacroGroup = MacroStack.back().first;
1555 break;
1556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001558 MacroStack.pop_back();
1559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001561 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1562 // Create a new macro group and add it to the stack.
1563 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1564
1565 if (MacroGroup)
1566 MacroGroup->push_back(NewGroup);
1567 else {
1568 assert(InstantiationLoc.isFileID());
1569 Pieces.push_back(NewGroup);
1570 }
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001572 MacroGroup = NewGroup;
1573 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1574 }
1575
1576 // Finally, add the PathDiagnosticPiece to the group.
1577 MacroGroup->push_back(&*I);
1578 }
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001580 // Now take the pieces and construct a new PathDiagnostic.
1581 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001583 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1584 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1585 if (!MP->containsEvent()) {
1586 delete MP;
1587 continue;
1588 }
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001590 PD.push_back(*I);
1591 }
1592}
1593
Ted Kremenek7dc86642009-03-31 20:22:36 +00001594void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001595 BugReportEquivClass& EQ) {
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001597 std::vector<const ExplodedNode*> Nodes;
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Ted Kremenekcf118d42009-02-04 23:49:09 +00001599 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001600 const ExplodedNode* N = I->getEndNode();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001601 if (N) Nodes.push_back(N);
1602 }
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Ted Kremenekcf118d42009-02-04 23:49:09 +00001604 if (Nodes.empty())
1605 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001607 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001608 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001609 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001610 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek7dc86642009-03-31 20:22:36 +00001611 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Ted Kremenekcf118d42009-02-04 23:49:09 +00001613 // Find the BugReport with the original location.
1614 BugReport *R = 0;
1615 unsigned i = 0;
1616 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
1617 if (i == GPair.second.second) { R = *I; break; }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Ted Kremenekcf118d42009-02-04 23:49:09 +00001619 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001621 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001622 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001623 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
1625 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001626 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Ted Kremenek8966bc12009-05-06 21:39:49 +00001628 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001629 PD.push_back(Piece);
1630 else
1631 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Ted Kremenekff7f7362010-03-20 18:02:01 +00001633 // Register node visitors.
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001634 R->registerInitialVisitors(PDB, N);
Ted Kremenekff7f7362010-03-20 18:02:01 +00001635 bugreporter::registerNilReceiverVisitor(PDB);
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Ted Kremenek7dc86642009-03-31 20:22:36 +00001637 switch (PDB.getGenerationScheme()) {
1638 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001639 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001640 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001641 case PathDiagnosticClient::Minimal:
1642 GenerateMinimalPathDiagnostic(PD, PDB, N);
1643 break;
1644 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001645}
1646
Ted Kremenekcf118d42009-02-04 23:49:09 +00001647void BugReporter::Register(BugType *BT) {
1648 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001649}
1650
Mike Stump1eb44332009-09-09 15:08:12 +00001651void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001652 // Compute the bug report's hash to determine its equivalence class.
1653 llvm::FoldingSetNodeID ID;
1654 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001655
1656 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001657 BugType& BT = R->getBugType();
1658 Register(&BT);
1659 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001660 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1661
Ted Kremenekcf118d42009-02-04 23:49:09 +00001662 if (!EQ) {
1663 EQ = new BugReportEquivClass(R);
1664 BT.EQClasses.InsertNode(EQ, InsertPos);
1665 }
1666 else
1667 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001668}
1669
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001670
1671//===----------------------------------------------------------------------===//
1672// Emitting reports in equivalence classes.
1673//===----------------------------------------------------------------------===//
1674
1675namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001676struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001677 const ExplodedNode *N;
1678 ExplodedNode::const_succ_iterator I, E;
1679
1680 FRIEC_WLItem(const ExplodedNode *n)
1681 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1682};
1683}
1684
1685static BugReport *FindReportInEquivalenceClass(BugReportEquivClass& EQ) {
1686 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1687 assert(I != E);
1688 BugReport *R = *I;
1689 BugType& BT = R->getBugType();
1690
1691 if (!BT.isSuppressOnSink())
1692 return R;
1693
1694 // For bug reports that should be suppressed when all paths are post-dominated
1695 // by a sink node, iterate through the reports in the equivalence class
1696 // until we find one that isn't post-dominated (if one exists). We use a
1697 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1698 // this as a recursive function, but we don't want to risk blowing out the
1699 // stack for very long paths.
1700 for (; I != E; ++I) {
1701 R = *I;
1702 const ExplodedNode *N = R->getEndNode();
1703
1704 if (!N)
1705 continue;
1706
1707 if (N->isSink()) {
1708 assert(false &&
1709 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
1710 return R;
1711 }
1712
1713 if (N->succ_empty())
1714 return R;
1715
1716 // At this point we know that 'N' is not a sink and it has at least one
1717 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1718 typedef FRIEC_WLItem WLItem;
1719 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1720 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1721
1722 DFSWorkList WL;
1723 WL.push_back(N);
1724 Visited[N] = 1;
1725
1726 while (!WL.empty()) {
1727 WLItem &WI = WL.back();
1728 assert(!WI.N->succ_empty());
1729
1730 for (; WI.I != WI.E; ++WI.I) {
1731 const ExplodedNode *Succ = *WI.I;
1732 // End-of-path node?
1733 if (Succ->succ_empty()) {
1734 // If we found an end-of-path node that is not a sink, then return
1735 // this report.
1736 if (!Succ->isSink())
1737 return R;
1738
1739 // Found a sink? Continue on to the next successor.
1740 continue;
1741 }
1742
1743 // Mark the successor as visited. If it hasn't been explored,
1744 // enqueue it to the DFS worklist.
1745 unsigned &mark = Visited[Succ];
1746 if (!mark) {
1747 mark = 1;
1748 WL.push_back(Succ);
1749 break;
1750 }
1751 }
1752
1753 if (&WL.back() == &WI)
1754 WL.pop_back();
1755 }
1756 }
1757
Ted Kremenek6b0c6eb2009-09-15 03:28:00 +00001758 // If we reach here, the end nodes for all reports in the equivalence
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001759 // class are post-dominated by a sink node.
1760 return NULL;
1761}
1762
Ted Kremeneke0a58072009-09-18 22:37:37 +00001763
1764//===----------------------------------------------------------------------===//
1765// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1766// uses global state, which eventually should go elsewhere.
1767//===----------------------------------------------------------------------===//
1768namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001769class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001770 llvm::FoldingSetNodeID ID;
1771public:
1772 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1773 ID.AddString(R->getBugType().getName());
1774 ID.AddString(R->getBugType().getCategory());
1775 ID.AddString(R->getDescription());
1776 ID.AddInteger(R->getLocation().getRawEncoding());
1777 PD->Profile(ID);
1778 }
1779
1780 void Profile(llvm::FoldingSetNodeID &id) {
1781 id = ID;
1782 }
1783
1784 llvm::FoldingSetNodeID &getID() { return ID; }
1785};
1786}
1787
1788static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1789 // FIXME: Eventually this diagnostic cache should reside in something
1790 // like AnalysisManager instead of being a static variable. This is
1791 // really unsafe in the long term.
1792 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1793 static DiagnosticCache DC;
1794
1795 void *InsertPos;
1796 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1797
1798 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1799 delete Item;
1800 return true;
1801 }
1802
1803 DC.InsertNode(Item, InsertPos);
1804 return false;
1805}
1806
Ted Kremenekcf118d42009-02-04 23:49:09 +00001807void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001808 BugReport *R = FindReportInEquivalenceClass(EQ);
1809
1810 if (!R)
1811 return;
1812
Ted Kremenekd49967f2009-04-29 21:58:13 +00001813 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Ted Kremenekcf118d42009-02-04 23:49:09 +00001815 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001816 // Probably doesn't make a difference in practice.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001817 BugType& BT = R->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Ted Kremenekd49967f2009-04-29 21:58:13 +00001819 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001820 D(new PathDiagnostic(R->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001821 !PD || PD->useVerboseDescription()
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001822 ? R->getDescription() : R->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001823 BT.getCategory()));
1824
Ted Kremenekcf118d42009-02-04 23:49:09 +00001825 GeneratePathDiagnostic(*D.get(), EQ);
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Ted Kremeneke0a58072009-09-18 22:37:37 +00001827 if (IsCachedDiagnostic(R, D.get()))
1828 return;
1829
Ted Kremenek072192b2008-04-30 23:47:44 +00001830 // Get the meta data.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001831 std::pair<const char**, const char**> Meta = R->getExtraDescriptiveText();
1832 for (const char** s = Meta.first; s != Meta.second; ++s)
1833 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001834
Ted Kremenek3148eb42009-01-24 00:55:43 +00001835 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001836 const SourceRange *Beg = 0, *End = 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001837 R->getRanges(Beg, End);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001838 Diagnostic& Diag = getDiagnostic();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001839 FullSourceLoc L(R->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001840
1841 // Search the description for '%', as that will be interpretted as a
1842 // format character by FormatDiagnostics.
1843 llvm::StringRef desc = R->getShortDescription();
1844 unsigned ErrorDiag;
1845 {
1846 llvm::SmallString<512> TmpStr;
1847 llvm::raw_svector_ostream Out(TmpStr);
1848 for (llvm::StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
1849 if (*I == '%')
1850 Out << "%%";
1851 else
1852 Out << *I;
1853
1854 Out.flush();
1855 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1856 }
Ted Kremenek57202072008-07-14 17:40:50 +00001857
Ted Kremenek3148eb42009-01-24 00:55:43 +00001858 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001859 default: assert(0 && "Don't handle this many ranges yet!");
1860 case 0: Diag.Report(L, ErrorDiag); break;
1861 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1862 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1863 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001864 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001865
1866 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1867 if (!PD)
1868 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001869
1870 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001871 PathDiagnosticPiece* piece =
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001872 new PathDiagnosticEventPiece(L, R->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001873
Ted Kremenek3148eb42009-01-24 00:55:43 +00001874 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1875 D->push_back(piece);
1876 }
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Ted Kremenek3148eb42009-01-24 00:55:43 +00001878 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001879}
Ted Kremenek57202072008-07-14 17:40:50 +00001880
Benjamin Kramerf0171732009-11-29 18:27:55 +00001881void BugReporter::EmitBasicReport(llvm::StringRef name, llvm::StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001882 SourceLocation Loc,
1883 SourceRange* RBeg, unsigned NumRanges) {
1884 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1885}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001886
Benjamin Kramerf0171732009-11-29 18:27:55 +00001887void BugReporter::EmitBasicReport(llvm::StringRef name,
1888 llvm::StringRef category,
1889 llvm::StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001890 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Ted Kremenekcf118d42009-02-04 23:49:09 +00001892 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1893 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001894 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001895 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1896 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1897 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001898}