blob: f26d16120e931ba59aa7af15e3e186b3bc9dc6c6 [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
47 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos))
48 return;
49
50 CallbacksSet.InsertNode(visitor, InsertPos);
51 Callbacks = F.Add(visitor, Callbacks);
52}
53
Ted Kremenekcf118d42009-02-04 23:49:09 +000054//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000055// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000056//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000057
Ted Kremenek5f85e172009-07-22 22:35:28 +000058static inline const Stmt* GetStmt(ProgramPoint P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000059 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
60 return SP->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000061 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000062 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenekb697b102009-02-23 22:44:26 +000064 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000065}
66
Zhongxing Xuc5619d92009-08-06 01:32:16 +000067static inline const ExplodedNode*
68GetPredecessorNode(const ExplodedNode* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000069 return N->pred_empty() ? NULL : *(N->pred_begin());
70}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000071
Zhongxing Xuc5619d92009-08-06 01:32:16 +000072static inline const ExplodedNode*
73GetSuccessorNode(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000074 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000075}
76
Zhongxing Xuc5619d92009-08-06 01:32:16 +000077static const Stmt* GetPreviousStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000079 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000080 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ted Kremenekb697b102009-02-23 22:44:26 +000082 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000083}
84
Zhongxing Xuc5619d92009-08-06 01:32:16 +000085static const Stmt* GetNextStmt(const ExplodedNode* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000086 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000087 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000088 // Check if the statement is '?' or '&&'/'||'. These are "merges",
89 // not actual statement points.
90 switch (S->getStmtClass()) {
91 case Stmt::ChooseExprClass:
92 case Stmt::ConditionalOperatorClass: continue;
93 case Stmt::BinaryOperatorClass: {
94 BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode();
95 if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr)
96 continue;
97 break;
98 }
99 default:
100 break;
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekb7c51522009-07-28 00:07:15 +0000103 // Some expressions don't have locations.
104 if (S->getLocStart().isInvalid())
105 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenekb697b102009-02-23 22:44:26 +0000107 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return 0;
111}
112
Ted Kremenek5f85e172009-07-22 22:35:28 +0000113static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000114GetCurrentOrPreviousStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000115 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000116 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Ted Kremenekb697b102009-02-23 22:44:26 +0000118 return GetPreviousStmt(N);
119}
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek5f85e172009-07-22 22:35:28 +0000121static inline const Stmt*
Mike Stump1eb44332009-09-09 15:08:12 +0000122GetCurrentOrNextStmt(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000123 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000124 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenekb697b102009-02-23 22:44:26 +0000126 return GetNextStmt(N);
127}
128
129//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000130// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000131//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000132
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000133typedef llvm::DenseMap<const ExplodedNode*,
134const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000135
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000136namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000137class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000138 NodeBackMap& M;
139public:
140 NodeMapClosure(NodeBackMap *m) : M(*m) {}
141 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000143 const ExplodedNode* getOriginalNode(const ExplodedNode* N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000144 NodeBackMap::iterator I = M.find(N);
145 return I == M.end() ? 0 : I->second;
146 }
147};
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000149class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000150 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000151 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000152 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000153 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000154public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000155 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000156 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000157 PathDiagnosticClient *pdc)
158 : BugReporterContext(br),
Mike Stump1eb44332009-09-09 15:08:12 +0000159 R(r), PDC(pdc), NMC(Backmap) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000160 addVisitor(R);
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000163 PathDiagnosticLocation ExecutionContinues(const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek00605e02009-03-27 20:55:39 +0000165 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000166 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000168 Decl const &getCodeDecl() { return R->getEndNode()->getCodeDecl(); }
169
170 ParentMap& getParentMap() { return R->getEndNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000172 const Stmt *getParent(const Stmt *S) {
173 return getParentMap().getParent(S);
174 }
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek8966bc12009-05-06 21:39:49 +0000176 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Ted Kremenek7dc86642009-03-31 20:22:36 +0000177 BugReport& getReport() { return *R; }
Douglas Gregor72971342009-04-18 00:02:19 +0000178
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000179 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000181 PathDiagnosticLocation
182 getEnclosingStmtLocation(const PathDiagnosticLocation &L) {
183 if (const Stmt *S = L.asStmt())
184 return getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000186 return L;
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek7dc86642009-03-31 20:22:36 +0000189 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
190 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
191 }
192
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000193 bool supportsLogicalOpControlFlow() const {
194 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000195 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000196};
197} // end anonymous namespace
198
Ted Kremenek00605e02009-03-27 20:55:39 +0000199PathDiagnosticLocation
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000200PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode* N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000201 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000202 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000203
Mike Stump1eb44332009-09-09 15:08:12 +0000204 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000205 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000206}
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Ted Kremenek00605e02009-03-27 20:55:39 +0000208PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000209PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000210 const ExplodedNode* N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000211
Ted Kremenek143ca222008-05-06 18:11:09 +0000212 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000213 if (os.str().empty())
214 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek00605e02009-03-27 20:55:39 +0000216 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremenek00605e02009-03-27 20:55:39 +0000218 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000219 os << "Execution continues on line "
Ted Kremenek8966bc12009-05-06 21:39:49 +0000220 << getSourceManager().getInstantiationLineNumber(Loc.asLocation())
221 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000222 else {
223 os << "Execution jumps to the end of the ";
224 const Decl *D = N->getLocationContext()->getDecl();
225 if (isa<ObjCMethodDecl>(D))
226 os << "method";
227 else if (isa<FunctionDecl>(D))
228 os << "function";
229 else {
230 assert(isa<BlockDecl>(D));
231 os << "anonymous block";
232 }
233 os << '.';
234 }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000236 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000237}
238
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000239static bool IsNested(const Stmt *S, ParentMap &PM) {
240 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
241 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000243 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000245 if (Parent)
246 switch (Parent->getStmtClass()) {
247 case Stmt::ForStmtClass:
248 case Stmt::DoStmtClass:
249 case Stmt::WhileStmtClass:
250 return true;
251 default:
252 break;
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
255 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000256}
257
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000258PathDiagnosticLocation
259PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
260 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000261 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000262 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000263
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000264 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000265 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000267 if (!Parent)
268 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000270 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000271 case Stmt::BinaryOperatorClass: {
272 const BinaryOperator *B = cast<BinaryOperator>(Parent);
273 if (B->isLogicalOp())
274 return PathDiagnosticLocation(S, SMgr);
275 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000276 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000277 case Stmt::CompoundStmtClass:
278 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000279 return PathDiagnosticLocation(S, SMgr);
280 case Stmt::ChooseExprClass:
281 // Similar to '?' if we are referring to condition, just have the edge
282 // point to the entire choose expression.
283 if (cast<ChooseExpr>(Parent)->getCond() == S)
284 return PathDiagnosticLocation(Parent, SMgr);
285 else
Mike Stump1eb44332009-09-09 15:08:12 +0000286 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000287 case Stmt::ConditionalOperatorClass:
288 // For '?', if we are referring to condition, just have the edge point
289 // to the entire '?' expression.
290 if (cast<ConditionalOperator>(Parent)->getCond() == S)
291 return PathDiagnosticLocation(Parent, SMgr);
292 else
Mike Stump1eb44332009-09-09 15:08:12 +0000293 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000294 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000295 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000296 case Stmt::ForStmtClass:
297 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000298 return PathDiagnosticLocation(S, SMgr);
299 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000300 case Stmt::IfStmtClass:
301 if (cast<IfStmt>(Parent)->getCond() != S)
302 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000303 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000304 case Stmt::ObjCForCollectionStmtClass:
305 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
306 return PathDiagnosticLocation(S, SMgr);
307 break;
308 case Stmt::WhileStmtClass:
309 if (cast<WhileStmt>(Parent)->getCond() != S)
310 return PathDiagnosticLocation(S, SMgr);
311 break;
312 default:
313 break;
314 }
315
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000316 S = Parent;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000319 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000320
321 // Special case: DeclStmts can appear in for statement declarations, in which
322 // case the ForStmt is the context.
323 if (isa<DeclStmt>(S)) {
324 if (const Stmt *Parent = P.getParent(S)) {
325 switch (Parent->getStmtClass()) {
326 case Stmt::ForStmtClass:
327 case Stmt::ObjCForCollectionStmtClass:
328 return PathDiagnosticLocation(Parent, SMgr);
329 default:
330 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000331 }
332 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000333 }
334 else if (isa<BinaryOperator>(S)) {
335 // Special case: the binary operator represents the initialization
336 // code in a for statement (this can happen when the variable being
337 // initialized is an old variable.
338 if (const ForStmt *FS =
339 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
340 if (FS->getInit() == S)
341 return PathDiagnosticLocation(FS, SMgr);
342 }
343 }
344
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000345 return PathDiagnosticLocation(S, SMgr);
346}
347
Ted Kremenekcf118d42009-02-04 23:49:09 +0000348//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000349// ScanNotableSymbols: closure-like callback for scanning Store bindings.
350//===----------------------------------------------------------------------===//
351
352static const VarDecl*
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000353GetMostRecentVarDeclBinding(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000354 GRStateManager& VMgr, SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek31061982009-03-31 23:00:32 +0000356 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek31061982009-03-31 23:00:32 +0000358 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek31061982009-03-31 23:00:32 +0000360 if (!isa<PostStmt>(P))
361 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek5f85e172009-07-22 22:35:28 +0000363 const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek31061982009-03-31 23:00:32 +0000365 if (!DR)
366 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek13976632010-02-08 16:18:51 +0000368 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek31061982009-03-31 23:00:32 +0000370 if (X != Y)
371 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek5f85e172009-07-22 22:35:28 +0000373 const VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek31061982009-03-31 23:00:32 +0000375 if (!VD)
376 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek31061982009-03-31 23:00:32 +0000378 return VD;
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek31061982009-03-31 23:00:32 +0000381 return 0;
382}
383
384namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000385class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000386: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek31061982009-03-31 23:00:32 +0000388 SymbolRef Sym;
389 const GRState* PrevSt;
390 const Stmt* S;
391 GRStateManager& VMgr;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000392 const ExplodedNode* Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000393 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000394 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek31061982009-03-31 23:00:32 +0000396public:
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek31061982009-03-31 23:00:32 +0000398 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000399 GRStateManager& vmgr, const ExplodedNode* pred,
Ted Kremenek31061982009-03-31 23:00:32 +0000400 PathDiagnostic& pd, BugReporter& br)
401 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
404 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek31061982009-03-31 23:00:32 +0000406 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek31061982009-03-31 23:00:32 +0000408 if (ScanSym != Sym)
409 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
411 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000412 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremenek31061982009-03-31 23:00:32 +0000414 if (X == V) // Same binding?
415 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek31061982009-03-31 23:00:32 +0000417 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000418 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000419 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek31061982009-03-31 23:00:32 +0000421 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek31061982009-03-31 23:00:32 +0000423 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
424 if (!B->isAssignmentOp())
425 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 // What variable did we assign to?
428 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek31061982009-03-31 23:00:32 +0000430 if (!DR)
431 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek31061982009-03-31 23:00:32 +0000433 VD = dyn_cast<VarDecl>(DR->getDecl());
434 }
435 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
436 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
437 // assume that each DeclStmt has a single Decl. This invariant
438 // holds by contruction in the CFG.
439 VD = dyn_cast<VarDecl>(*DS->decl_begin());
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek31061982009-03-31 23:00:32 +0000442 if (!VD)
443 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek31061982009-03-31 23:00:32 +0000445 // What is the most recently referenced variable with this binding?
446 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek31061982009-03-31 23:00:32 +0000448 if (!MostRecent)
449 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek31061982009-03-31 23:00:32 +0000451 // Create the diagnostic.
452 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek31061982009-03-31 23:00:32 +0000454 if (Loc::IsLocType(VD->getType())) {
455 std::string msg = "'" + std::string(VD->getNameAsString()) +
456 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek31061982009-03-31 23:00:32 +0000458 PD.push_front(new PathDiagnosticEventPiece(L, msg));
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek31061982009-03-31 23:00:32 +0000461 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000462 }
Ted Kremenek31061982009-03-31 23:00:32 +0000463};
464}
465
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000466static void HandleNotableSymbol(const ExplodedNode* N,
Ted Kremenek31061982009-03-31 23:00:32 +0000467 const Stmt* S,
468 SymbolRef Sym, BugReporter& BR,
469 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000471 const ExplodedNode* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek31061982009-03-31 23:00:32 +0000472 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek31061982009-03-31 23:00:32 +0000474 if (!PrevSt)
475 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Ted Kremenek31061982009-03-31 23:00:32 +0000477 // Look at the region bindings of the current state that map to the
478 // specified symbol. Are any of them not in the previous state?
479 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
480 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
481 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
482}
483
484namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000485class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000486: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Ted Kremenek31061982009-03-31 23:00:32 +0000488 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000489 const ExplodedNode* N;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000490 const Stmt* S;
Ted Kremenek31061982009-03-31 23:00:32 +0000491 GRBugReporter& BR;
492 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000495 ScanNotableSymbols(const ExplodedNode* n, const Stmt* s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000496 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000497 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Ted Kremenek31061982009-03-31 23:00:32 +0000499 bool HandleBinding(StoreManager& SMgr, Store store,
500 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek31061982009-03-31 23:00:32 +0000502 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek31061982009-03-31 23:00:32 +0000504 if (!ScanSym)
505 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek31061982009-03-31 23:00:32 +0000507 if (!BR.isNotable(ScanSym))
508 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenek31061982009-03-31 23:00:32 +0000510 if (AlreadyProcessed.count(ScanSym))
511 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremenek31061982009-03-31 23:00:32 +0000513 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenek31061982009-03-31 23:00:32 +0000515 HandleNotableSymbol(N, S, ScanSym, BR, PD);
516 return true;
517 }
518};
519} // end anonymous namespace
520
521//===----------------------------------------------------------------------===//
522// "Minimal" path diagnostic generation algorithm.
523//===----------------------------------------------------------------------===//
524
Ted Kremenek14856d72009-04-06 23:06:54 +0000525static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
526
Ted Kremenek31061982009-03-31 23:00:32 +0000527static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
528 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000529 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000530
Ted Kremenek31061982009-03-31 23:00:32 +0000531 SourceManager& SMgr = PDB.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000532 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000533 ? NULL : *(N->pred_begin());
534 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000535 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000536 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Ted Kremenek31061982009-03-31 23:00:32 +0000538 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
541 CFGBlock* Src = BE->getSrc();
542 CFGBlock* Dst = BE->getDst();
543 Stmt* T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 if (!T)
546 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek31061982009-03-31 23:00:32 +0000548 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek31061982009-03-31 23:00:32 +0000550 switch (T->getStmtClass()) {
551 default:
552 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenek31061982009-03-31 23:00:32 +0000554 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000555 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000556 const Stmt* S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek31061982009-03-31 23:00:32 +0000558 if (!S)
559 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek31061982009-03-31 23:00:32 +0000561 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000562 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000563 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenek31061982009-03-31 23:00:32 +0000565 os << "Control jumps to line "
566 << End.asLocation().getInstantiationLineNumber();
567 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
568 os.str()));
569 break;
570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
572 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000573 // Figure out what case arm we took.
574 std::string sbuf;
575 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenek31061982009-03-31 23:00:32 +0000577 if (Stmt* S = Dst->getLabel()) {
578 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Ted Kremenek31061982009-03-31 23:00:32 +0000580 switch (S->getStmtClass()) {
581 default:
582 os << "No cases match in the switch statement. "
583 "Control jumps to line "
584 << End.asLocation().getInstantiationLineNumber();
585 break;
586 case Stmt::DefaultStmtClass:
587 os << "Control jumps to the 'default' case at line "
588 << End.asLocation().getInstantiationLineNumber();
589 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Ted Kremenek31061982009-03-31 23:00:32 +0000591 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000592 os << "Control jumps to 'case ";
593 CaseStmt* Case = cast<CaseStmt>(S);
Ted Kremenek31061982009-03-31 23:00:32 +0000594 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000595
596 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000597 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Ted Kremenek31061982009-03-31 23:00:32 +0000599 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
600 // FIXME: Maybe this should be an assertion. Are there cases
601 // were it is not an EnumConstantDecl?
602 EnumConstantDecl* D =
603 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenek31061982009-03-31 23:00:32 +0000605 if (D) {
606 GetRawInt = false;
607 os << D->getNameAsString();
608 }
609 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000610
611 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000612 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000613
Ted Kremenek31061982009-03-31 23:00:32 +0000614 os << ":' at line "
615 << End.asLocation().getInstantiationLineNumber();
616 break;
617 }
618 }
619 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
620 os.str()));
621 }
622 else {
623 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000624 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000625 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
626 os.str()));
627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek31061982009-03-31 23:00:32 +0000629 break;
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Ted Kremenek31061982009-03-31 23:00:32 +0000632 case Stmt::BreakStmtClass:
633 case Stmt::ContinueStmtClass: {
634 std::string sbuf;
635 llvm::raw_string_ostream os(sbuf);
636 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
637 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
638 os.str()));
639 break;
640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek31061982009-03-31 23:00:32 +0000642 // Determine control-flow for ternary '?'.
643 case Stmt::ConditionalOperatorClass: {
644 std::string sbuf;
645 llvm::raw_string_ostream os(sbuf);
646 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremenek31061982009-03-31 23:00:32 +0000648 if (*(Src->succ_begin()+1) == Dst)
649 os << "false";
650 else
651 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Ted Kremenek31061982009-03-31 23:00:32 +0000655 if (const Stmt *S = End.asStmt())
656 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
659 os.str()));
660 break;
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek31061982009-03-31 23:00:32 +0000663 // Determine control-flow for short-circuited '&&' and '||'.
664 case Stmt::BinaryOperatorClass: {
665 if (!PDB.supportsLogicalOpControlFlow())
666 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Ted Kremenek31061982009-03-31 23:00:32 +0000668 BinaryOperator *B = cast<BinaryOperator>(T);
669 std::string sbuf;
670 llvm::raw_string_ostream os(sbuf);
671 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Ted Kremenek31061982009-03-31 23:00:32 +0000673 if (B->getOpcode() == BinaryOperator::LAnd) {
674 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek31061982009-03-31 23:00:32 +0000676 if (*(Src->succ_begin()+1) == Dst) {
677 os << "false";
678 PathDiagnosticLocation End(B->getLHS(), SMgr);
679 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
680 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
681 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000682 }
Ted Kremenek31061982009-03-31 23:00:32 +0000683 else {
684 os << "true";
685 PathDiagnosticLocation Start(B->getLHS(), SMgr);
686 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
687 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
688 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000689 }
Ted Kremenek31061982009-03-31 23:00:32 +0000690 }
691 else {
692 assert(B->getOpcode() == BinaryOperator::LOr);
693 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Ted Kremenek31061982009-03-31 23:00:32 +0000695 if (*(Src->succ_begin()+1) == Dst) {
696 os << "false";
697 PathDiagnosticLocation Start(B->getLHS(), SMgr);
698 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
699 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000700 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000701 }
702 else {
703 os << "true";
704 PathDiagnosticLocation End(B->getLHS(), SMgr);
705 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
706 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000707 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000708 }
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Ted Kremenek31061982009-03-31 23:00:32 +0000711 break;
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
714 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000715 if (*(Src->succ_begin()) == Dst) {
716 std::string sbuf;
717 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 os << "Loop condition is true. ";
720 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek31061982009-03-31 23:00:32 +0000722 if (const Stmt *S = End.asStmt())
723 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek31061982009-03-31 23:00:32 +0000725 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
726 os.str()));
727 }
728 else {
729 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenek31061982009-03-31 23:00:32 +0000731 if (const Stmt *S = End.asStmt())
732 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek31061982009-03-31 23:00:32 +0000734 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
735 "Loop condition is false. Exiting loop"));
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek31061982009-03-31 23:00:32 +0000738 break;
739 }
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek31061982009-03-31 23:00:32 +0000741 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000742 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000743 if (*(Src->succ_begin()+1) == Dst) {
744 std::string sbuf;
745 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Ted Kremenek31061982009-03-31 23:00:32 +0000747 os << "Loop condition is false. ";
748 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
749 if (const Stmt *S = End.asStmt())
750 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Ted Kremenek31061982009-03-31 23:00:32 +0000752 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
753 os.str()));
754 }
755 else {
756 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
757 if (const Stmt *S = End.asStmt())
758 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek31061982009-03-31 23:00:32 +0000760 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000761 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek31061982009-03-31 23:00:32 +0000764 break;
765 }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek31061982009-03-31 23:00:32 +0000767 case Stmt::IfStmtClass: {
768 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek31061982009-03-31 23:00:32 +0000770 if (const Stmt *S = End.asStmt())
771 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek31061982009-03-31 23:00:32 +0000773 if (*(Src->succ_begin()+1) == Dst)
774 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000775 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000776 else
Ted Kremenek31061982009-03-31 23:00:32 +0000777 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000778 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenek31061982009-03-31 23:00:32 +0000780 break;
781 }
782 }
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000785 if (NextNode) {
786 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
787 E = PDB.visitor_end(); I!=E; ++I) {
788 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB))
789 PD.push_front(p);
790 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
793 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000794 // Scan the region bindings, and see if a "notable" symbol has a new
795 // lval binding.
796 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
797 PDB.getStateManager().iterBindings(N->getState(), SNS);
798 }
799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Ted Kremenek14856d72009-04-06 23:06:54 +0000801 // After constructing the full PathDiagnostic, do a pass over it to compact
802 // PathDiagnosticPieces that occur within a macro.
803 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000804}
805
806//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000807// "Extensive" PathDiagnostic generation.
808//===----------------------------------------------------------------------===//
809
810static bool IsControlFlowExpr(const Stmt *S) {
811 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000813 if (!E)
814 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
816 E = E->IgnoreParenCasts();
817
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000818 if (isa<ConditionalOperator>(E))
819 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000821 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
822 if (B->isLogicalOp())
823 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000824
825 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000826}
827
Ted Kremenek14856d72009-04-06 23:06:54 +0000828namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000829class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000830 bool IsDead;
831public:
832 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
833 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000834
835 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000836 bool isDead() const { return IsDead; }
837};
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000839class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000840 std::vector<ContextLocation> CLocs;
841 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000842 PathDiagnostic &PD;
843 PathDiagnosticBuilder &PDB;
844 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000846 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek14856d72009-04-06 23:06:54 +0000848 bool containsLocation(const PathDiagnosticLocation &Container,
849 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek14856d72009-04-06 23:06:54 +0000851 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek9650cf32009-05-11 21:42:34 +0000853 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
854 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000855 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000856 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000857 while (1) {
858 // Adjust the location for some expressions that are best referenced
859 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000860 switch (S->getStmtClass()) {
861 default:
862 break;
863 case Stmt::ParenExprClass:
864 S = cast<ParenExpr>(S)->IgnoreParens();
865 firstCharOnly = true;
866 continue;
867 case Stmt::ConditionalOperatorClass:
868 S = cast<ConditionalOperator>(S)->getCond();
869 firstCharOnly = true;
870 continue;
871 case Stmt::ChooseExprClass:
872 S = cast<ChooseExpr>(S)->getCond();
873 firstCharOnly = true;
874 continue;
875 case Stmt::BinaryOperatorClass:
876 S = cast<BinaryOperator>(S)->getLHS();
877 firstCharOnly = true;
878 continue;
879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek9650cf32009-05-11 21:42:34 +0000881 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek9650cf32009-05-11 21:42:34 +0000884 if (S != Original)
885 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Ted Kremenek9650cf32009-05-11 21:42:34 +0000888 if (firstCharOnly)
889 L = PathDiagnosticLocation(L.asLocation());
890
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000891 return L;
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek14856d72009-04-06 23:06:54 +0000894 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000895 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000896 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000897 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000898 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000899 CLocs.pop_back();
900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
902 PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000903
904public:
905 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
906 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremeneka301a672009-04-22 18:16:20 +0000908 // If the PathDiagnostic already has pieces, add the enclosing statement
909 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000910 if (!PD.empty()) {
911 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Ted Kremenek14856d72009-04-06 23:06:54 +0000913 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000914 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000915 }
916 }
917
918 ~EdgeBuilder() {
919 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremeneka301a672009-04-22 18:16:20 +0000921 // Finally, add an initial edge from the start location of the first
922 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000923 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
924 if (const CompoundStmt *CS =
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000925 PDB.getCodeDecl().getCompoundBody())
Ted Kremeneka301a672009-04-22 18:16:20 +0000926 if (!CS->body_empty()) {
927 SourceLocation Loc = (*CS->body_begin())->getLocStart();
928 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
929 }
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek14856d72009-04-06 23:06:54 +0000931 }
932
933 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Ted Kremenek14856d72009-04-06 23:06:54 +0000935 void addEdge(const Stmt *S, bool alwaysAdd = false) {
936 addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd);
937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000939 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek14856d72009-04-06 23:06:54 +0000941 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000942 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000943};
Ted Kremenek14856d72009-04-06 23:06:54 +0000944} // end anonymous namespace
945
946
947PathDiagnosticLocation
948EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
949 if (const Stmt *S = L.asStmt()) {
950 if (IsControlFlowExpr(S))
951 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
953 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek14856d72009-04-06 23:06:54 +0000956 return L;
957}
958
959bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
960 const PathDiagnosticLocation &Containee) {
961
962 if (Container == Containee)
963 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Ted Kremenek14856d72009-04-06 23:06:54 +0000965 if (Container.asDecl())
966 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Ted Kremenek14856d72009-04-06 23:06:54 +0000968 if (const Stmt *S = Containee.asStmt())
969 if (const Stmt *ContainerS = Container.asStmt()) {
970 while (S) {
971 if (S == ContainerS)
972 return true;
973 S = PDB.getParent(S);
974 }
975 return false;
976 }
977
978 // Less accurate: compare using source ranges.
979 SourceRange ContainerR = Container.asRange();
980 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Ted Kremenek14856d72009-04-06 23:06:54 +0000982 SourceManager &SM = PDB.getSourceManager();
983 SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin());
984 SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd());
985 SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin());
986 SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Ted Kremenek14856d72009-04-06 23:06:54 +0000988 unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg);
989 unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd);
990 unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg);
991 unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Ted Kremenek14856d72009-04-06 23:06:54 +0000993 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000994 assert(ContaineeBegLine <= ContaineeEndLine);
995
Ted Kremenek14856d72009-04-06 23:06:54 +0000996 return (ContainerBegLine <= ContaineeBegLine &&
997 ContainerEndLine >= ContaineeEndLine &&
998 (ContainerBegLine != ContaineeBegLine ||
Mike Stump1eb44332009-09-09 15:08:12 +0000999 SM.getInstantiationColumnNumber(ContainerRBeg) <=
Ted Kremenek14856d72009-04-06 23:06:54 +00001000 SM.getInstantiationColumnNumber(ContaineeRBeg)) &&
1001 (ContainerEndLine != ContaineeEndLine ||
1002 SM.getInstantiationColumnNumber(ContainerREnd) >=
1003 SM.getInstantiationColumnNumber(ContainerREnd)));
1004}
1005
1006PathDiagnosticLocation
1007EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) {
1008 if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt()))
1009 return PathDiagnosticLocation(E->IgnoreParenCasts(),
1010 PDB.getSourceManager());
1011 return L;
1012}
1013
1014void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1015 if (!PrevLoc.isValid()) {
1016 PrevLoc = NewLoc;
1017 return;
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001020 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1021 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001023 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001024 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek14856d72009-04-06 23:06:54 +00001026 // FIXME: Ignore intra-macro edges for now.
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001027 if (NewLocClean.asLocation().getInstantiationLoc() ==
1028 PrevLocClean.asLocation().getInstantiationLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001029 return;
1030
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001031 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1032 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001033}
1034
1035void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Ted Kremeneka301a672009-04-22 18:16:20 +00001037 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1038 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Ted Kremenek14856d72009-04-06 23:06:54 +00001040 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1041
1042 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001043 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremenek14856d72009-04-06 23:06:54 +00001045 // Is the top location context the same as the one for the new location?
1046 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001047 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001048 if (IsConsumedExpr(TopContextLoc) &&
1049 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001050 TopContextLoc.markDead();
1051
Ted Kremenek14856d72009-04-06 23:06:54 +00001052 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001053 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001054
1055 return;
1056 }
1057
1058 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001059 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001060 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001062 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001063 CLocs.push_back(ContextLocation(CLoc, true));
1064 return;
1065 }
1066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremenek14856d72009-04-06 23:06:54 +00001068 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001069 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001070 }
1071
1072 // Context does not contain the location. Flush it.
1073 popLocation();
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001076 // If we reach here, there is no enclosing context. Just add the edge.
1077 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001078}
1079
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001080bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1081 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1082 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001084 return false;
1085}
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremeneke1baed32009-05-05 23:13:38 +00001087void EdgeBuilder::addExtendedContext(const Stmt *S) {
1088 if (!S)
1089 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
1091 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001092 while (Parent) {
1093 if (isa<CompoundStmt>(Parent))
1094 Parent = PDB.getParent(Parent);
1095 else
1096 break;
1097 }
1098
1099 if (Parent) {
1100 switch (Parent->getStmtClass()) {
1101 case Stmt::DoStmtClass:
1102 case Stmt::ObjCAtSynchronizedStmtClass:
1103 addContext(Parent);
1104 default:
1105 break;
1106 }
1107 }
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Ted Kremeneke1baed32009-05-05 23:13:38 +00001109 addContext(S);
1110}
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Ted Kremenek14856d72009-04-06 23:06:54 +00001112void EdgeBuilder::addContext(const Stmt *S) {
1113 if (!S)
1114 return;
1115
1116 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Ted Kremenek14856d72009-04-06 23:06:54 +00001118 while (!CLocs.empty()) {
1119 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1120
1121 // Is the top location context the same as the one for the new location?
1122 if (TopContextLoc == L)
1123 return;
1124
1125 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001126 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001127 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001128 }
1129
1130 // Context does not contain the location. Flush it.
1131 popLocation();
1132 }
1133
1134 CLocs.push_back(L);
1135}
1136
1137static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1138 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001139 const ExplodedNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +00001140
1141
Ted Kremenek14856d72009-04-06 23:06:54 +00001142 EdgeBuilder EB(PD, PDB);
1143
Mike Stump1eb44332009-09-09 15:08:12 +00001144 const ExplodedNode* NextNode = N->pred_empty()
Ted Kremenek14856d72009-04-06 23:06:54 +00001145 ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001146 while (NextNode) {
1147 N = NextNode;
1148 NextNode = GetPredecessorNode(N);
1149 ProgramPoint P = N->getLocation();
1150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 do {
1152 // Block edges.
1153 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1154 const CFGBlock &Blk = *BE->getSrc();
1155 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001157 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001158 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001159 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001160 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001162 if (!Term) {
1163 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1164 CS = dyn_cast<CompoundStmt>(FS->getBody());
1165 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001166 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001167 }
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001169 PathDiagnosticEventPiece *p =
1170 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001171 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001173 EB.addEdge(p->getLocation(), true);
1174 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001176 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001177 PathDiagnosticLocation BL(CS->getRBracLoc(),
1178 PDB.getSourceManager());
1179 BL = PathDiagnosticLocation(BL.asLocation());
1180 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001181 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001184 if (Term)
1185 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001187 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001188 }
1189
Mike Stump1eb44332009-09-09 15:08:12 +00001190 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001191 if (const Stmt* S = BE->getFirstStmt()) {
1192 if (IsControlFlowExpr(S)) {
1193 // Add the proper context for '&&', '||', and '?'.
1194 EB.addContext(S);
1195 }
1196 else
1197 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1198 }
1199
1200 break;
1201 }
1202 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001204 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001205 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Ted Kremenek8966bc12009-05-06 21:39:49 +00001207 for (BugReporterContext::visitor_iterator I = PDB.visitor_begin(),
1208 E = PDB.visitor_end(); I!=E; ++I) {
1209 if (PathDiagnosticPiece* p = (*I)->VisitNode(N, NextNode, PDB)) {
1210 const PathDiagnosticLocation &Loc = p->getLocation();
1211 EB.addEdge(Loc, true);
1212 PD.push_front(p);
1213 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001214 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001217 }
1218}
1219
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001220//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001221// Methods for BugType and subclasses.
1222//===----------------------------------------------------------------------===//
Ted Kremenek90b6acf2009-09-14 20:40:59 +00001223BugType::~BugType() {
1224 // Free up the equivalence class objects. Observe that we get a pointer to
1225 // the object first before incrementing the iterator, as destroying the
1226 // node before doing so means we will read from freed memory.
1227 for (iterator I = begin(), E = end(); I !=E; ) {
1228 BugReportEquivClass *EQ = &*I;
1229 ++I;
1230 delete EQ;
1231 }
1232}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001233void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001234
Ted Kremenekcf118d42009-02-04 23:49:09 +00001235//===----------------------------------------------------------------------===//
1236// Methods for BugReport and subclasses.
1237//===----------------------------------------------------------------------===//
1238BugReport::~BugReport() {}
1239RangedBugReport::~RangedBugReport() {}
1240
Mike Stump1eb44332009-09-09 15:08:12 +00001241const Stmt* BugReport::getStmt() const {
1242 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001243 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenekcf118d42009-02-04 23:49:09 +00001245 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001246 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001247 if (BE->getBlock() == &Exit)
1248 S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001249 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001250 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001251 S = GetStmt(ProgP);
1252
1253 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001254}
1255
1256PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00001257BugReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001258 const ExplodedNode* EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001260 const Stmt* S = getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Ted Kremenek61f3e052008-04-03 04:42:52 +00001262 if (!S)
1263 return NULL;
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001264
Ted Kremenekde7161f2008-04-03 18:00:37 +00001265 const SourceRange *Beg, *End;
Mike Stump1eb44332009-09-09 15:08:12 +00001266 getRanges(Beg, End);
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001267 PathDiagnosticLocation L(S, BRC.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001269 // Only add the statement itself as a range if we didn't specify any
1270 // special ranges for this report.
1271 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription(),
1272 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001274 for (; Beg != End; ++Beg)
1275 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Ted Kremenek61f3e052008-04-03 04:42:52 +00001277 return P;
1278}
1279
Mike Stump1eb44332009-09-09 15:08:12 +00001280void BugReport::getRanges(const SourceRange*& beg, const SourceRange*& end) {
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001281 if (const Expr* E = dyn_cast_or_null<Expr>(getStmt())) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001282 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001283 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001284 beg = &R;
1285 end = beg+1;
1286 }
1287 else
1288 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001289}
1290
Mike Stump1eb44332009-09-09 15:08:12 +00001291SourceLocation BugReport::getLocation() const {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001292 if (EndNode)
Ted Kremenek5f85e172009-07-22 22:35:28 +00001293 if (const Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001294 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001295 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001296 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001297 // For binary operators, return the location of the operator.
1298 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1299 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001300
Ted Kremenekcf118d42009-02-04 23:49:09 +00001301 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001302 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001303
1304 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001305}
1306
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001307PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode* N,
1308 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001309 BugReporterContext &BRC) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001310 return NULL;
1311}
1312
Ted Kremenekcf118d42009-02-04 23:49:09 +00001313//===----------------------------------------------------------------------===//
1314// Methods for BugReporter and subclasses.
1315//===----------------------------------------------------------------------===//
1316
1317BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001318 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001319}
1320
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001321GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001322BugReporterData::~BugReporterData() {}
1323
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001324ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001325
1326GRStateManager&
1327GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1328
1329BugReporter::~BugReporter() { FlushReports(); }
1330
1331void BugReporter::FlushReports() {
1332 if (BugTypes.isEmpty())
1333 return;
1334
1335 // First flush the warnings for each BugType. This may end up creating new
1336 // warnings and new BugTypes. Because ImmutableSet is a functional data
1337 // structure, we do not need to worry about the iterators being invalidated.
1338 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
1339 const_cast<BugType*>(*I)->FlushReports(*this);
1340
1341 // Iterate through BugTypes a second time. BugTypes may have been updated
1342 // with new BugType objects and new warnings.
1343 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
1344 BugType *BT = const_cast<BugType*>(*I);
1345
1346 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1347 SetTy& EQClasses = BT->EQClasses;
1348
1349 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1350 BugReportEquivClass& EQ = *EI;
1351 FlushReport(EQ);
1352 }
Mike Stump1eb44332009-09-09 15:08:12 +00001353
1354 // Delete the BugType object.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001355 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +00001356 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001357
1358 // Remove all references to the BugType objects.
1359 BugTypes = F.GetEmptySet();
1360}
1361
1362//===----------------------------------------------------------------------===//
1363// PathDiagnostics generation.
1364//===----------------------------------------------------------------------===//
1365
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001366static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001367 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001368MakeReportGraph(const ExplodedGraph* G,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001369 const ExplodedNode** NStart,
1370 const ExplodedNode** NEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Ted Kremenekcf118d42009-02-04 23:49:09 +00001372 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001373 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001374 // error node unless there are two or more error nodes with the same minimum
1375 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001376 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001377 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001378
1379 llvm::DenseMap<const void*, const void*> InverseMap;
1380 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Ted Kremenekcf118d42009-02-04 23:49:09 +00001382 // Create owning pointers for GTrim and NMap just to ensure that they are
1383 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001384 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001385 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenekcf118d42009-02-04 23:49:09 +00001387 // Find the (first) error node in the trimmed graph. We just need to consult
1388 // the node map (NMap) which maps from nodes in the original graph to nodes
1389 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001390
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001391 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001392 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001393 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001394
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001395 for (const ExplodedNode** I = NStart; I != NEnd; ++I)
1396 if (const ExplodedNode *N = NMap->getMappedNode(*I)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001397 unsigned NodeIndex = (I - NStart) / sizeof(*I);
1398 WS.push(N);
1399 IndexMap[*I] = NodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001400 }
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Ted Kremenek938332c2009-05-16 01:11:58 +00001402 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001403
1404 // Create a new (third!) graph with a single path. This is the graph
1405 // that will be returned to the caller.
Zhongxing Xucc025532009-08-25 03:33:41 +00001406 ExplodedGraph *GNew = new ExplodedGraph(GTrim->getContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Ted Kremenek10aa5542009-03-12 23:41:59 +00001408 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001409 // to the root node, and then construct a new graph that contains only
1410 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001411 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001413 unsigned cnt = 0;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001414 const ExplodedNode* Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001416 while (!WS.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001417 const ExplodedNode* Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001418 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001420 if (Visited.find(Node) != Visited.end())
1421 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001423 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001425 if (Node->pred_empty()) {
1426 Root = Node;
1427 break;
1428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001430 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001431 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001432 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenek938332c2009-05-16 01:11:58 +00001435 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek10aa5542009-03-12 23:41:59 +00001437 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001438 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001439 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001440 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001441 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001443 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001444 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001445 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001446 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001448 // Create the equivalent node in the new graph with the same state
1449 // and location.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001450 ExplodedNode* NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001452 // Store the mapping to the original node.
1453 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1454 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001455 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001457 // Link up the new node with the previous node.
1458 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001459 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001461 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001463 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001464 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001465 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001466 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001467 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001468 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001469 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 // Find the next successor node. We choose the node that is marked
1473 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001474 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1475 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001476 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001478 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001480 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001482 if (I == Visited.end())
1483 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001485 if (!N || I->second < MinVal) {
1486 N = *SI;
1487 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001488 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001489 }
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek938332c2009-05-16 01:11:58 +00001491 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001492 }
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek938332c2009-05-16 01:11:58 +00001494 assert(First);
1495
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001496 return std::make_pair(std::make_pair(GNew, BM),
1497 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001498}
1499
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001500/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1501/// and collapses PathDiagosticPieces that are expanded by macros.
1502static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1503 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1504 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001506 typedef std::vector<PathDiagnosticPiece*>
1507 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001509 MacroStackTy MacroStack;
1510 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001512 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1513 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001514 const FullSourceLoc Loc = I->getLocation().asLocation();
1515
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001516 // Determine the instantiation location, which is the location we group
1517 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001518 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001519 SM.getInstantiationLoc(Loc) :
1520 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001522 if (Loc.isFileID()) {
1523 MacroStack.clear();
1524 Pieces.push_back(&*I);
1525 continue;
1526 }
1527
1528 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001530 // Is the PathDiagnosticPiece within the same macro group?
1531 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1532 MacroStack.back().first->push_back(&*I);
1533 continue;
1534 }
1535
1536 // We aren't in the same group. Are we descending into a new macro
1537 // or are part of an old one?
1538 PathDiagnosticMacroPiece *MacroGroup = 0;
1539
1540 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
1541 SM.getInstantiationLoc(Loc) :
1542 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001544 // Walk the entire macro stack.
1545 while (!MacroStack.empty()) {
1546 if (InstantiationLoc == MacroStack.back().second) {
1547 MacroGroup = MacroStack.back().first;
1548 break;
1549 }
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001551 if (ParentInstantiationLoc == MacroStack.back().second) {
1552 MacroGroup = MacroStack.back().first;
1553 break;
1554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001556 MacroStack.pop_back();
1557 }
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001559 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1560 // Create a new macro group and add it to the stack.
1561 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1562
1563 if (MacroGroup)
1564 MacroGroup->push_back(NewGroup);
1565 else {
1566 assert(InstantiationLoc.isFileID());
1567 Pieces.push_back(NewGroup);
1568 }
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001570 MacroGroup = NewGroup;
1571 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1572 }
1573
1574 // Finally, add the PathDiagnosticPiece to the group.
1575 MacroGroup->push_back(&*I);
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001578 // Now take the pieces and construct a new PathDiagnostic.
1579 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001581 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1582 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1583 if (!MP->containsEvent()) {
1584 delete MP;
1585 continue;
1586 }
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001588 PD.push_back(*I);
1589 }
1590}
1591
Ted Kremenek7dc86642009-03-31 20:22:36 +00001592void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001593 BugReportEquivClass& EQ) {
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001595 std::vector<const ExplodedNode*> Nodes;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Ted Kremenekcf118d42009-02-04 23:49:09 +00001597 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001598 const ExplodedNode* N = I->getEndNode();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001599 if (N) Nodes.push_back(N);
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenekcf118d42009-02-04 23:49:09 +00001602 if (Nodes.empty())
1603 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001605 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001606 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001607 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001608 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek7dc86642009-03-31 20:22:36 +00001609 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenekcf118d42009-02-04 23:49:09 +00001611 // Find the BugReport with the original location.
1612 BugReport *R = 0;
1613 unsigned i = 0;
1614 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
1615 if (i == GPair.second.second) { R = *I; break; }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Ted Kremenekcf118d42009-02-04 23:49:09 +00001617 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001619 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001620 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001621 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001622
1623 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001624 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek8966bc12009-05-06 21:39:49 +00001626 if (PathDiagnosticPiece* Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001627 PD.push_back(Piece);
1628 else
1629 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001631 R->registerInitialVisitors(PDB, N);
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Ted Kremenek7dc86642009-03-31 20:22:36 +00001633 switch (PDB.getGenerationScheme()) {
1634 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001635 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001636 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001637 case PathDiagnosticClient::Minimal:
1638 GenerateMinimalPathDiagnostic(PD, PDB, N);
1639 break;
1640 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001641}
1642
Ted Kremenekcf118d42009-02-04 23:49:09 +00001643void BugReporter::Register(BugType *BT) {
1644 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001645}
1646
Mike Stump1eb44332009-09-09 15:08:12 +00001647void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001648 // Compute the bug report's hash to determine its equivalence class.
1649 llvm::FoldingSetNodeID ID;
1650 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001651
1652 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001653 BugType& BT = R->getBugType();
1654 Register(&BT);
1655 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +00001656 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1657
Ted Kremenekcf118d42009-02-04 23:49:09 +00001658 if (!EQ) {
1659 EQ = new BugReportEquivClass(R);
1660 BT.EQClasses.InsertNode(EQ, InsertPos);
1661 }
1662 else
1663 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001664}
1665
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001666
1667//===----------------------------------------------------------------------===//
1668// Emitting reports in equivalence classes.
1669//===----------------------------------------------------------------------===//
1670
1671namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001672struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001673 const ExplodedNode *N;
1674 ExplodedNode::const_succ_iterator I, E;
1675
1676 FRIEC_WLItem(const ExplodedNode *n)
1677 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1678};
1679}
1680
1681static BugReport *FindReportInEquivalenceClass(BugReportEquivClass& EQ) {
1682 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1683 assert(I != E);
1684 BugReport *R = *I;
1685 BugType& BT = R->getBugType();
1686
1687 if (!BT.isSuppressOnSink())
1688 return R;
1689
1690 // For bug reports that should be suppressed when all paths are post-dominated
1691 // by a sink node, iterate through the reports in the equivalence class
1692 // until we find one that isn't post-dominated (if one exists). We use a
1693 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1694 // this as a recursive function, but we don't want to risk blowing out the
1695 // stack for very long paths.
1696 for (; I != E; ++I) {
1697 R = *I;
1698 const ExplodedNode *N = R->getEndNode();
1699
1700 if (!N)
1701 continue;
1702
1703 if (N->isSink()) {
1704 assert(false &&
1705 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
1706 return R;
1707 }
1708
1709 if (N->succ_empty())
1710 return R;
1711
1712 // At this point we know that 'N' is not a sink and it has at least one
1713 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1714 typedef FRIEC_WLItem WLItem;
1715 typedef llvm::SmallVector<WLItem, 10> DFSWorkList;
1716 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1717
1718 DFSWorkList WL;
1719 WL.push_back(N);
1720 Visited[N] = 1;
1721
1722 while (!WL.empty()) {
1723 WLItem &WI = WL.back();
1724 assert(!WI.N->succ_empty());
1725
1726 for (; WI.I != WI.E; ++WI.I) {
1727 const ExplodedNode *Succ = *WI.I;
1728 // End-of-path node?
1729 if (Succ->succ_empty()) {
1730 // If we found an end-of-path node that is not a sink, then return
1731 // this report.
1732 if (!Succ->isSink())
1733 return R;
1734
1735 // Found a sink? Continue on to the next successor.
1736 continue;
1737 }
1738
1739 // Mark the successor as visited. If it hasn't been explored,
1740 // enqueue it to the DFS worklist.
1741 unsigned &mark = Visited[Succ];
1742 if (!mark) {
1743 mark = 1;
1744 WL.push_back(Succ);
1745 break;
1746 }
1747 }
1748
1749 if (&WL.back() == &WI)
1750 WL.pop_back();
1751 }
1752 }
1753
Ted Kremenek6b0c6eb2009-09-15 03:28:00 +00001754 // If we reach here, the end nodes for all reports in the equivalence
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001755 // class are post-dominated by a sink node.
1756 return NULL;
1757}
1758
Ted Kremeneke0a58072009-09-18 22:37:37 +00001759
1760//===----------------------------------------------------------------------===//
1761// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1762// uses global state, which eventually should go elsewhere.
1763//===----------------------------------------------------------------------===//
1764namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001765class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001766 llvm::FoldingSetNodeID ID;
1767public:
1768 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1769 ID.AddString(R->getBugType().getName());
1770 ID.AddString(R->getBugType().getCategory());
1771 ID.AddString(R->getDescription());
1772 ID.AddInteger(R->getLocation().getRawEncoding());
1773 PD->Profile(ID);
1774 }
1775
1776 void Profile(llvm::FoldingSetNodeID &id) {
1777 id = ID;
1778 }
1779
1780 llvm::FoldingSetNodeID &getID() { return ID; }
1781};
1782}
1783
1784static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1785 // FIXME: Eventually this diagnostic cache should reside in something
1786 // like AnalysisManager instead of being a static variable. This is
1787 // really unsafe in the long term.
1788 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1789 static DiagnosticCache DC;
1790
1791 void *InsertPos;
1792 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1793
1794 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1795 delete Item;
1796 return true;
1797 }
1798
1799 DC.InsertNode(Item, InsertPos);
1800 return false;
1801}
1802
Ted Kremenekcf118d42009-02-04 23:49:09 +00001803void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001804 BugReport *R = FindReportInEquivalenceClass(EQ);
1805
1806 if (!R)
1807 return;
1808
Ted Kremenekd49967f2009-04-29 21:58:13 +00001809 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Ted Kremenekcf118d42009-02-04 23:49:09 +00001811 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001812 // Probably doesn't make a difference in practice.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001813 BugType& BT = R->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Ted Kremenekd49967f2009-04-29 21:58:13 +00001815 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001816 D(new PathDiagnostic(R->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001817 !PD || PD->useVerboseDescription()
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001818 ? R->getDescription() : R->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001819 BT.getCategory()));
1820
Ted Kremenekcf118d42009-02-04 23:49:09 +00001821 GeneratePathDiagnostic(*D.get(), EQ);
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Ted Kremeneke0a58072009-09-18 22:37:37 +00001823 if (IsCachedDiagnostic(R, D.get()))
1824 return;
1825
Ted Kremenek072192b2008-04-30 23:47:44 +00001826 // Get the meta data.
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001827 std::pair<const char**, const char**> Meta = R->getExtraDescriptiveText();
1828 for (const char** s = Meta.first; s != Meta.second; ++s)
1829 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001830
Ted Kremenek3148eb42009-01-24 00:55:43 +00001831 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001832 const SourceRange *Beg = 0, *End = 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001833 R->getRanges(Beg, End);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001834 Diagnostic& Diag = getDiagnostic();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001835 FullSourceLoc L(R->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001836
1837 // Search the description for '%', as that will be interpretted as a
1838 // format character by FormatDiagnostics.
1839 llvm::StringRef desc = R->getShortDescription();
1840 unsigned ErrorDiag;
1841 {
1842 llvm::SmallString<512> TmpStr;
1843 llvm::raw_svector_ostream Out(TmpStr);
1844 for (llvm::StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
1845 if (*I == '%')
1846 Out << "%%";
1847 else
1848 Out << *I;
1849
1850 Out.flush();
1851 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1852 }
Ted Kremenek57202072008-07-14 17:40:50 +00001853
Ted Kremenek3148eb42009-01-24 00:55:43 +00001854 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001855 default: assert(0 && "Don't handle this many ranges yet!");
1856 case 0: Diag.Report(L, ErrorDiag); break;
1857 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1858 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1859 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001860 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001861
1862 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1863 if (!PD)
1864 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
1866 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001867 PathDiagnosticPiece* piece =
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001868 new PathDiagnosticEventPiece(L, R->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001869
Ted Kremenek3148eb42009-01-24 00:55:43 +00001870 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1871 D->push_back(piece);
1872 }
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Ted Kremenek3148eb42009-01-24 00:55:43 +00001874 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001875}
Ted Kremenek57202072008-07-14 17:40:50 +00001876
Benjamin Kramerf0171732009-11-29 18:27:55 +00001877void BugReporter::EmitBasicReport(llvm::StringRef name, llvm::StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001878 SourceLocation Loc,
1879 SourceRange* RBeg, unsigned NumRanges) {
1880 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1881}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001882
Benjamin Kramerf0171732009-11-29 18:27:55 +00001883void BugReporter::EmitBasicReport(llvm::StringRef name,
1884 llvm::StringRef category,
1885 llvm::StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001886 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001887
Ted Kremenekcf118d42009-02-04 23:49:09 +00001888 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1889 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001890 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001891 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1892 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1893 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001894}