blob: 01a74ced223558b50767a41758994440522057e4 [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
11// PathDiagnostics for analyses based on GRSimpleVals.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/CFG.h"
21#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000023#include "clang/Analysis/ProgramPoint.h"
24#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000026#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000027#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000028#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000029#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000030
31using namespace clang;
32
Ted Kremenekcf118d42009-02-04 23:49:09 +000033//===----------------------------------------------------------------------===//
34// static functions.
35//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000036
Ted Kremenekb697b102009-02-23 22:44:26 +000037static inline Stmt* GetStmt(ProgramPoint P) {
38 if (const PostStmt* PS = dyn_cast<PostStmt>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000039 return PS->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000040 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000041 return BE->getSrc()->getTerminator();
Ted Kremenek61f3e052008-04-03 04:42:52 +000042
Ted Kremenekb697b102009-02-23 22:44:26 +000043 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000044}
45
Ted Kremenek3148eb42009-01-24 00:55:43 +000046static inline const ExplodedNode<GRState>*
Ted Kremenekb697b102009-02-23 22:44:26 +000047GetPredecessorNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000048 return N->pred_empty() ? NULL : *(N->pred_begin());
49}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000050
Ted Kremenekb697b102009-02-23 22:44:26 +000051static inline const ExplodedNode<GRState>*
52GetSuccessorNode(const ExplodedNode<GRState>* N) {
53 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000054}
55
Ted Kremenekb697b102009-02-23 22:44:26 +000056static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) {
57 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
58 if (Stmt *S = GetStmt(N->getLocation()))
59 return S;
60
61 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000062}
63
Ted Kremenekb697b102009-02-23 22:44:26 +000064static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) {
65 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
66 if (Stmt *S = GetStmt(N->getLocation()))
67 return S;
68
69 return 0;
70}
71
72static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
73 if (Stmt *S = GetStmt(N->getLocation()))
74 return S;
75
76 return GetPreviousStmt(N);
77}
78
79static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
80 if (Stmt *S = GetStmt(N->getLocation()))
81 return S;
82
83 return GetNextStmt(N);
84}
85
86//===----------------------------------------------------------------------===//
87// Diagnostics for 'execution continues on line XXX'.
88//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +000089
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000090namespace {
91class VISIBILITY_HIDDEN PathDiagnosticBuilder {
92 SourceManager &SMgr;
93 const Decl& CodeDecl;
94 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +000095 llvm::OwningPtr<ParentMap> PM;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000096public:
97 PathDiagnosticBuilder(SourceManager &smgr, const Decl& codedecl,
98 PathDiagnosticClient *pdc)
99 : SMgr(smgr), CodeDecl(codedecl), PDC(pdc) {}
100
Ted Kremenek00605e02009-03-27 20:55:39 +0000101 PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N);
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000102
Ted Kremenek00605e02009-03-27 20:55:39 +0000103 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
104 const ExplodedNode<GRState>* N);
105
106 ParentMap& getParentMap() {
107 if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody()));
108 return *PM.get();
109 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000110
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000111 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
112
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000113 bool supportsLogicalOpControlFlow() const {
114 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
115 }
116};
117} // end anonymous namespace
118
Ted Kremenek00605e02009-03-27 20:55:39 +0000119PathDiagnosticLocation
120PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) {
121 if (Stmt *S = GetNextStmt(N))
122 return PathDiagnosticLocation(S, SMgr);
123
124 return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000125}
126
Ted Kremenek00605e02009-03-27 20:55:39 +0000127PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000128PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
129 const ExplodedNode<GRState>* N) {
130
Ted Kremenek143ca222008-05-06 18:11:09 +0000131 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000132 if (os.str().empty())
133 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000134
Ted Kremenek00605e02009-03-27 20:55:39 +0000135 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000136
Ted Kremenek00605e02009-03-27 20:55:39 +0000137 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000138 os << "Execution continues on line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000139 << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000140 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000141 os << "Execution jumps to the end of the "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000142 << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000143
144 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000145}
146
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000147PathDiagnosticLocation
148PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
149 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
150 ParentMap &P = getParentMap();
151 while (isa<Expr>(S)) {
152 const Stmt *Parent = P.getParent(S);
153
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000154 if (!Parent)
155 break;
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000156
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000157 switch (Parent->getStmtClass()) {
158 case Stmt::CompoundStmtClass:
159 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000160 return PathDiagnosticLocation(S, SMgr);
161 case Stmt::ChooseExprClass:
162 // Similar to '?' if we are referring to condition, just have the edge
163 // point to the entire choose expression.
164 if (cast<ChooseExpr>(Parent)->getCond() == S)
165 return PathDiagnosticLocation(Parent, SMgr);
166 else
167 return PathDiagnosticLocation(S, SMgr);
168 case Stmt::ConditionalOperatorClass:
169 // For '?', if we are referring to condition, just have the edge point
170 // to the entire '?' expression.
171 if (cast<ConditionalOperator>(Parent)->getCond() == S)
172 return PathDiagnosticLocation(Parent, SMgr);
173 else
174 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000175 case Stmt::DoStmtClass:
176 if (cast<DoStmt>(Parent)->getCond() != S)
177 return PathDiagnosticLocation(S, SMgr);
178 break;
179 case Stmt::ForStmtClass:
180 if (cast<ForStmt>(Parent)->getBody() == S)
181 return PathDiagnosticLocation(S, SMgr);
182 break;
183 case Stmt::IfStmtClass:
184 if (cast<IfStmt>(Parent)->getCond() != S)
185 return PathDiagnosticLocation(S, SMgr);
186 break;
187 case Stmt::ObjCForCollectionStmtClass:
188 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
189 return PathDiagnosticLocation(S, SMgr);
190 break;
191 case Stmt::WhileStmtClass:
192 if (cast<WhileStmt>(Parent)->getCond() != S)
193 return PathDiagnosticLocation(S, SMgr);
194 break;
195 default:
196 break;
197 }
198
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000199 S = Parent;
200 }
201
202 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
203 return PathDiagnosticLocation(S, SMgr);
204}
205
Ted Kremenekcf118d42009-02-04 23:49:09 +0000206//===----------------------------------------------------------------------===//
207// Methods for BugType and subclasses.
208//===----------------------------------------------------------------------===//
209BugType::~BugType() {}
210void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000211
Ted Kremenekcf118d42009-02-04 23:49:09 +0000212//===----------------------------------------------------------------------===//
213// Methods for BugReport and subclasses.
214//===----------------------------------------------------------------------===//
215BugReport::~BugReport() {}
216RangedBugReport::~RangedBugReport() {}
217
218Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000219 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000220 Stmt *S = NULL;
221
Ted Kremenekcf118d42009-02-04 23:49:09 +0000222 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000223 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000224 }
225 if (!S) S = GetStmt(ProgP);
226
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000227 return S;
228}
229
230PathDiagnosticPiece*
231BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000232 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000233
234 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000235
236 if (!S)
237 return NULL;
238
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000239 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000240 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000241
Ted Kremenekde7161f2008-04-03 18:00:37 +0000242 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000243 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000244
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000245 for (; Beg != End; ++Beg)
246 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000247
248 return P;
249}
250
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000251void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
252 const SourceRange*& end) {
253
254 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
255 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000256 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000257 beg = &R;
258 end = beg+1;
259 }
260 else
261 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000262}
263
Ted Kremenekcf118d42009-02-04 23:49:09 +0000264SourceLocation BugReport::getLocation() const {
265 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000266 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
267 // For member expressions, return the location of the '.' or '->'.
268 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
269 return ME->getMemberLoc();
270
Ted Kremenekcf118d42009-02-04 23:49:09 +0000271 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000272 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000273
274 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000275}
276
Ted Kremenek3148eb42009-01-24 00:55:43 +0000277PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
278 const ExplodedNode<GRState>* PrevN,
279 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000280 BugReporter& BR,
281 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000282 return NULL;
283}
284
Ted Kremenekcf118d42009-02-04 23:49:09 +0000285//===----------------------------------------------------------------------===//
286// Methods for BugReporter and subclasses.
287//===----------------------------------------------------------------------===//
288
289BugReportEquivClass::~BugReportEquivClass() {
290 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
291}
292
293GRBugReporter::~GRBugReporter() { FlushReports(); }
294BugReporterData::~BugReporterData() {}
295
296ExplodedGraph<GRState>&
297GRBugReporter::getGraph() { return Eng.getGraph(); }
298
299GRStateManager&
300GRBugReporter::getStateManager() { return Eng.getStateManager(); }
301
302BugReporter::~BugReporter() { FlushReports(); }
303
304void BugReporter::FlushReports() {
305 if (BugTypes.isEmpty())
306 return;
307
308 // First flush the warnings for each BugType. This may end up creating new
309 // warnings and new BugTypes. Because ImmutableSet is a functional data
310 // structure, we do not need to worry about the iterators being invalidated.
311 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
312 const_cast<BugType*>(*I)->FlushReports(*this);
313
314 // Iterate through BugTypes a second time. BugTypes may have been updated
315 // with new BugType objects and new warnings.
316 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
317 BugType *BT = const_cast<BugType*>(*I);
318
319 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
320 SetTy& EQClasses = BT->EQClasses;
321
322 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
323 BugReportEquivClass& EQ = *EI;
324 FlushReport(EQ);
325 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000326
Ted Kremenekcf118d42009-02-04 23:49:09 +0000327 // Delete the BugType object. This will also delete the equivalence
328 // classes.
329 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000330 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000331
332 // Remove all references to the BugType objects.
333 BugTypes = F.GetEmptySet();
334}
335
336//===----------------------------------------------------------------------===//
337// PathDiagnostics generation.
338//===----------------------------------------------------------------------===//
339
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000340typedef llvm::DenseMap<const ExplodedNode<GRState>*,
341 const ExplodedNode<GRState>*> NodeBackMap;
342
343static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000344 std::pair<ExplodedNode<GRState>*, unsigned> >
345MakeReportGraph(const ExplodedGraph<GRState>* G,
346 const ExplodedNode<GRState>** NStart,
347 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000348
Ted Kremenekcf118d42009-02-04 23:49:09 +0000349 // Create the trimmed graph. It will contain the shortest paths from the
350 // error nodes to the root. In the new graph we should only have one
351 // error node unless there are two or more error nodes with the same minimum
352 // path length.
353 ExplodedGraph<GRState>* GTrim;
354 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000355
356 llvm::DenseMap<const void*, const void*> InverseMap;
357 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000358
359 // Create owning pointers for GTrim and NMap just to ensure that they are
360 // released when this function exists.
361 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
362 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
363
364 // Find the (first) error node in the trimmed graph. We just need to consult
365 // the node map (NMap) which maps from nodes in the original graph to nodes
366 // in the new graph.
367 const ExplodedNode<GRState>* N = 0;
368 unsigned NodeIndex = 0;
369
370 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
371 if ((N = NMap->getMappedNode(*I))) {
372 NodeIndex = (I - NStart) / sizeof(*I);
373 break;
374 }
375
376 assert(N && "No error node found in the trimmed graph.");
377
378 // Create a new (third!) graph with a single path. This is the graph
379 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000380 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000381 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
382 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000383
Ted Kremenek10aa5542009-03-12 23:41:59 +0000384 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000385 // to the root node, and then construct a new graph that contains only
386 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000387 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000388 std::queue<const ExplodedNode<GRState>*> WS;
389 WS.push(N);
390
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000391 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000392 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000393
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000394 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000395 const ExplodedNode<GRState>* Node = WS.front();
396 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000397
398 if (Visited.find(Node) != Visited.end())
399 continue;
400
401 Visited[Node] = cnt++;
402
403 if (Node->pred_empty()) {
404 Root = Node;
405 break;
406 }
407
Ted Kremenek3148eb42009-01-24 00:55:43 +0000408 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000409 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000410 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000411 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000412
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000413 assert (Root);
414
Ted Kremenek10aa5542009-03-12 23:41:59 +0000415 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000416 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000417 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000418 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000419
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000420 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000421 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000422 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000423 assert (I != Visited.end());
424
425 // Create the equivalent node in the new graph with the same state
426 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000427 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000428 GNew->getNode(N->getLocation(), N->getState());
429
430 // Store the mapping to the original node.
431 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
432 assert(IMitr != InverseMap.end() && "No mapping to original node.");
433 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000434
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000435 // Link up the new node with the previous node.
436 if (Last)
437 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000438
439 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000440
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000441 // Are we at the final node?
442 if (I->second == 0) {
443 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000444 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000445 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000446
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000447 // Find the next successor node. We choose the node that is marked
448 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000449 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
450 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000451 N = 0;
452
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000453 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000454
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000455 I = Visited.find(*SI);
456
457 if (I == Visited.end())
458 continue;
459
460 if (!N || I->second < MinVal) {
461 N = *SI;
462 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000463 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000464 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000465
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000466 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000467 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000468
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000469 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000470 return std::make_pair(std::make_pair(GNew, BM),
471 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000472}
473
Ted Kremenek3148eb42009-01-24 00:55:43 +0000474static const VarDecl*
475GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
476 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000477
478 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
479
480 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000481
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000482 if (!isa<PostStmt>(P))
483 continue;
484
485 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000486
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000487 if (!DR)
488 continue;
489
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000490 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000491
492 if (X != Y)
493 continue;
494
495 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
496
497 if (!VD)
498 continue;
499
500 return VD;
501 }
502
503 return 0;
504}
505
Ted Kremenek9e240492008-10-04 05:50:14 +0000506namespace {
507class VISIBILITY_HIDDEN NotableSymbolHandler
508 : public StoreManager::BindingsHandler {
509
Ted Kremenek2dabd432008-12-05 02:27:51 +0000510 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000511 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000512 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000513 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000514 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000515 PathDiagnostic& PD;
516 BugReporter& BR;
517
518public:
519
Ted Kremenek3148eb42009-01-24 00:55:43 +0000520 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
521 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000522 PathDiagnostic& pd, BugReporter& br)
523 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
524
Ted Kremenekbe912242009-03-05 16:31:07 +0000525 bool HandleBinding(StoreManager& SMgr, Store store,
526 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000527
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000528 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000529
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000530 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000531 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000532 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000533 ScanSym = SV->getSymbol();
534 else
535 return true;
536
537 if (ScanSym != Sym)
538 return true;
539
540 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000541 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000542
543 if (X == V) // Same binding?
544 return true;
545
546 // Different binding. Only handle assignments for now. We don't pull
547 // this check out of the loop because we will eventually handle other
548 // cases.
549
550 VarDecl *VD = 0;
551
Ted Kremenek3148eb42009-01-24 00:55:43 +0000552 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000553 if (!B->isAssignmentOp())
554 return true;
555
556 // What variable did we assign to?
557 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
558
559 if (!DR)
560 return true;
561
562 VD = dyn_cast<VarDecl>(DR->getDecl());
563 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000564 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000565 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
566 // assume that each DeclStmt has a single Decl. This invariant
567 // holds by contruction in the CFG.
568 VD = dyn_cast<VarDecl>(*DS->decl_begin());
569 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000570
571 if (!VD)
572 return true;
573
574 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000575 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000576
577 if (!MostRecent)
578 return true;
579
580 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000581 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
582
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000583 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000584 std::string msg = "'" + std::string(VD->getNameAsString()) +
585 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000586
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000587 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000588 }
589
590 return true;
591 }
592};
593}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000594
Ted Kremenek3148eb42009-01-24 00:55:43 +0000595static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
596 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000597 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000598 PathDiagnostic& PD) {
599
Ted Kremenek3148eb42009-01-24 00:55:43 +0000600 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000601 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000602
603 if (!PrevSt)
604 return;
605
Ted Kremenek9e240492008-10-04 05:50:14 +0000606 // Look at the region bindings of the current state that map to the
607 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000608 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000609 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
610 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
611}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000612
Ted Kremenek9e240492008-10-04 05:50:14 +0000613namespace {
614class VISIBILITY_HIDDEN ScanNotableSymbols
615 : public StoreManager::BindingsHandler {
616
Ted Kremenek2dabd432008-12-05 02:27:51 +0000617 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000618 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000619 Stmt* S;
620 GRBugReporter& BR;
621 PathDiagnostic& PD;
622
623public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000624 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000625 PathDiagnostic& pd)
626 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000627
Ted Kremenekbe912242009-03-05 16:31:07 +0000628 bool HandleBinding(StoreManager& SMgr, Store store,
629 const MemRegion* R, SVal V) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000630 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000631
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000632 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000633 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000634 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000635 ScanSym = SV->getSymbol();
636 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000637 return true;
638
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000639 assert (ScanSym);
Ted Kremenek9e240492008-10-04 05:50:14 +0000640
641 if (!BR.isNotable(ScanSym))
642 return true;
643
644 if (AlreadyProcessed.count(ScanSym))
645 return true;
646
647 AlreadyProcessed.insert(ScanSym);
648
649 HandleNotableSymbol(N, S, ScanSym, BR, PD);
650 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000651 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000652};
653} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000654
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000655namespace {
656class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
657 NodeBackMap& M;
658public:
659 NodeMapClosure(NodeBackMap *m) : M(*m) {}
660 ~NodeMapClosure() {}
661
662 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
663 NodeBackMap::iterator I = M.find(N);
664 return I == M.end() ? 0 : I->second;
665 }
666};
667}
668
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000669/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
670/// and collapses PathDiagosticPieces that are expanded by macros.
671static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
672 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
673 MacroStackTy;
674
675 typedef std::vector<PathDiagnosticPiece*>
676 PiecesTy;
677
678 MacroStackTy MacroStack;
679 PiecesTy Pieces;
680
681 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
682 // Get the location of the PathDiagnosticPiece.
683 const FullSourceLoc Loc = I->getLocation();
684
685 // Determine the instantiation location, which is the location we group
686 // related PathDiagnosticPieces.
687 SourceLocation InstantiationLoc = Loc.isMacroID() ?
688 SM.getInstantiationLoc(Loc) :
689 SourceLocation();
690
691 if (Loc.isFileID()) {
692 MacroStack.clear();
693 Pieces.push_back(&*I);
694 continue;
695 }
696
697 assert(Loc.isMacroID());
698
699 // Is the PathDiagnosticPiece within the same macro group?
700 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
701 MacroStack.back().first->push_back(&*I);
702 continue;
703 }
704
705 // We aren't in the same group. Are we descending into a new macro
706 // or are part of an old one?
707 PathDiagnosticMacroPiece *MacroGroup = 0;
708
709 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
710 SM.getInstantiationLoc(Loc) :
711 SourceLocation();
712
713 // Walk the entire macro stack.
714 while (!MacroStack.empty()) {
715 if (InstantiationLoc == MacroStack.back().second) {
716 MacroGroup = MacroStack.back().first;
717 break;
718 }
719
720 if (ParentInstantiationLoc == MacroStack.back().second) {
721 MacroGroup = MacroStack.back().first;
722 break;
723 }
724
725 MacroStack.pop_back();
726 }
727
728 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
729 // Create a new macro group and add it to the stack.
730 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
731
732 if (MacroGroup)
733 MacroGroup->push_back(NewGroup);
734 else {
735 assert(InstantiationLoc.isFileID());
736 Pieces.push_back(NewGroup);
737 }
738
739 MacroGroup = NewGroup;
740 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
741 }
742
743 // Finally, add the PathDiagnosticPiece to the group.
744 MacroGroup->push_back(&*I);
745 }
746
747 // Now take the pieces and construct a new PathDiagnostic.
748 PD.resetPath(false);
749
750 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
751 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
752 if (!MP->containsEvent()) {
753 delete MP;
754 continue;
755 }
756
757 PD.push_back(*I);
758 }
759}
760
Ted Kremenekc0959972008-07-02 21:24:01 +0000761void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000762 BugReportEquivClass& EQ) {
763
764 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000765
Ted Kremenekcf118d42009-02-04 23:49:09 +0000766 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
767 const ExplodedNode<GRState>* N = I->getEndNode();
768 if (N) Nodes.push_back(N);
769 }
770
771 if (Nodes.empty())
772 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000773
774 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000775 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000776 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000777 std::pair<ExplodedNode<GRState>*, unsigned> >&
778 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000779
Ted Kremenekcf118d42009-02-04 23:49:09 +0000780 // Find the BugReport with the original location.
781 BugReport *R = 0;
782 unsigned i = 0;
783 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
784 if (i == GPair.second.second) { R = *I; break; }
785
786 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000787
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000788 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
789 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000790 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000791
Ted Kremenekcf118d42009-02-04 23:49:09 +0000792 // Start building the path diagnostic...
793 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000794 PD.push_back(Piece);
795 else
796 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000797
Ted Kremenek3148eb42009-01-24 00:55:43 +0000798 const ExplodedNode<GRState>* NextNode = N->pred_empty()
799 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000800
Ted Kremenekc0959972008-07-02 21:24:01 +0000801 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000802 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000803 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000804 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
805 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000806
Ted Kremenek6837faa2008-04-09 00:20:43 +0000807 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000808 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000809 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000810
811 ProgramPoint P = N->getLocation();
812
813 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000814 CFGBlock* Src = BE->getSrc();
815 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000816 Stmt* T = Src->getTerminator();
817
818 if (!T)
819 continue;
820
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000821 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000822
823 switch (T->getStmtClass()) {
824 default:
825 break;
826
827 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000828 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000829 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000830
831 if (!S)
832 continue;
833
Ted Kremenek297308e2009-02-10 23:56:07 +0000834 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000835 llvm::raw_string_ostream os(sbuf);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000836 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000837
Ted Kremenek00605e02009-03-27 20:55:39 +0000838 os << "Control jumps to line "
839 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000840 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
841 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000842 break;
843 }
844
Ted Kremenek297308e2009-02-10 23:56:07 +0000845 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000846 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000847 std::string sbuf;
848 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000849
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000850 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000851 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000852
Ted Kremenek5a429952008-04-23 23:35:07 +0000853 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000854 default:
855 os << "No cases match in the switch statement. "
856 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000857 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000858 break;
859 case Stmt::DefaultStmtClass:
860 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000861 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000862 break;
863
864 case Stmt::CaseStmtClass: {
865 os << "Control jumps to 'case ";
866 CaseStmt* Case = cast<CaseStmt>(S);
867 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
868
869 // Determine if it is an enum.
870 bool GetRawInt = true;
871
872 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
873 // FIXME: Maybe this should be an assertion. Are there cases
874 // were it is not an EnumConstantDecl?
875 EnumConstantDecl* D =
876 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000877
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000878 if (D) {
879 GetRawInt = false;
880 os << D->getNameAsString();
881 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000882 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000883
884 if (GetRawInt) {
885
886 // Not an enum.
887 Expr* CondE = cast<SwitchStmt>(T)->getCond();
888 unsigned bits = Ctx.getTypeSize(CondE->getType());
889 llvm::APSInt V(bits, false);
890
891 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
892 assert (false && "Case condition must be constant.");
893 continue;
894 }
895
896 os << V;
897 }
898
Ted Kremenek00605e02009-03-27 20:55:39 +0000899 os << ":' at line "
900 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000901 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000902 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000903 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000904 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
905 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000906 }
Ted Kremenek56783922008-04-25 01:29:56 +0000907 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000908 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000909 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
910 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
911 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000912 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000913
Ted Kremenek61f3e052008-04-03 04:42:52 +0000914 break;
915 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000916
917 case Stmt::BreakStmtClass:
918 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000919 std::string sbuf;
920 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000921 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000922 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
923 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000924 break;
925 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000926
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000927 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000928 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000929 std::string sbuf;
930 llvm::raw_string_ostream os(sbuf);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000931 os << "'?' condition is ";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000932
933 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000934 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000935 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000936 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000937
Ted Kremenek00605e02009-03-27 20:55:39 +0000938 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000939
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000940 if (const Stmt *S = End.asStmt())
941 End = PDB.getEnclosingStmtLocation(S);
942
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000943 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
944 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000945 break;
946 }
947
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000948 // Determine control-flow for short-circuited '&&' and '||'.
949 case Stmt::BinaryOperatorClass: {
950 if (!PDB.supportsLogicalOpControlFlow())
951 break;
952
953 BinaryOperator *B = cast<BinaryOperator>(T);
954 std::string sbuf;
955 llvm::raw_string_ostream os(sbuf);
956 os << "Left side of '";
957
958 if (B->getOpcode() == BinaryOperator::LAnd) {
959 os << "&&";
960 }
961 else {
962 assert(B->getOpcode() == BinaryOperator::LOr);
963 os << "||";
964 }
965
966 os << "' is ";
967 if (*(Src->succ_begin()+1) == Dst)
968 os << (B->getOpcode() == BinaryOperator::LAnd
969 ? "false" : "true");
970 else
971 os << (B->getOpcode() == BinaryOperator::LAnd
972 ? "true" : "false");
973
974 PathDiagnosticLocation Start(B->getLHS(), SMgr);
975 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
976
977 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
978 os.str()));
979 break;
980 }
981
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000982 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000983 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000984 std::string sbuf;
985 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000986
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000987 os << "Loop condition is true. ";
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000988 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
989
990 if (const Stmt *S = End.asStmt())
991 End = PDB.getEnclosingStmtLocation(S);
992
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000993 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
994 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000995 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000996 else {
Ted Kremenek00605e02009-03-27 20:55:39 +0000997 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000998
999 if (const Stmt *S = End.asStmt())
1000 End = PDB.getEnclosingStmtLocation(S);
1001
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001002 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1003 "Loop condition is false. Exiting loop"));
1004 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001005
1006 break;
1007 }
1008
Ted Kremenek61f3e052008-04-03 04:42:52 +00001009 case Stmt::WhileStmtClass:
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001010 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001011 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001012 std::string sbuf;
1013 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001014
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001015 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +00001016 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001017 if (const Stmt *S = End.asStmt())
1018 End = PDB.getEnclosingStmtLocation(S);
1019
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001020 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1021 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001022 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001023 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001024 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001025 if (const Stmt *S = End.asStmt())
1026 End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001027
1028 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1029 "Loop condition is true. Entering loop body"));
1030 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001031
1032 break;
1033 }
1034
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001035 case Stmt::IfStmtClass: {
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001036 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1037
1038 if (const Stmt *S = End.asStmt())
1039 End = PDB.getEnclosingStmtLocation(S);
1040
Ted Kremenek61f3e052008-04-03 04:42:52 +00001041 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001042 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1043 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +00001044 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001045 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1046 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +00001047
1048 break;
1049 }
1050 }
Ted Kremenek6837faa2008-04-09 00:20:43 +00001051 }
Ted Kremenek5a429952008-04-23 23:35:07 +00001052
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001053 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
1054 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001055 PD.push_front(p);
1056
Ted Kremenek9e240492008-10-04 05:50:14 +00001057 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
1058 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001059 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +00001060 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
1061 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001062 }
Ted Kremenek61f3e052008-04-03 04:42:52 +00001063 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001064
1065 // After constructing the full PathDiagnostic, do a pass over it to compact
1066 // PathDiagnosticPieces that occur within a macro.
1067 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001068}
1069
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001070
Ted Kremenekcf118d42009-02-04 23:49:09 +00001071void BugReporter::Register(BugType *BT) {
1072 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001073}
1074
Ted Kremenekcf118d42009-02-04 23:49:09 +00001075void BugReporter::EmitReport(BugReport* R) {
1076 // Compute the bug report's hash to determine its equivalence class.
1077 llvm::FoldingSetNodeID ID;
1078 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001079
Ted Kremenekcf118d42009-02-04 23:49:09 +00001080 // Lookup the equivance class. If there isn't one, create it.
1081 BugType& BT = R->getBugType();
1082 Register(&BT);
1083 void *InsertPos;
1084 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1085
1086 if (!EQ) {
1087 EQ = new BugReportEquivClass(R);
1088 BT.EQClasses.InsertNode(EQ, InsertPos);
1089 }
1090 else
1091 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001092}
1093
Ted Kremenekcf118d42009-02-04 23:49:09 +00001094void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1095 assert(!EQ.Reports.empty());
1096 BugReport &R = **EQ.begin();
1097
1098 // FIXME: Make sure we use the 'R' for the path that was actually used.
1099 // Probably doesn't make a difference in practice.
1100 BugType& BT = R.getBugType();
1101
1102 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1103 R.getDescription(),
1104 BT.getCategory()));
1105 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001106
1107 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001108 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001109 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001110
Ted Kremenek3148eb42009-01-24 00:55:43 +00001111 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001112 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001113 const SourceRange *Beg = 0, *End = 0;
1114 R.getRanges(*this, Beg, End);
1115 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001116 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001117 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1118 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001119
Ted Kremenek3148eb42009-01-24 00:55:43 +00001120 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001121 default: assert(0 && "Don't handle this many ranges yet!");
1122 case 0: Diag.Report(L, ErrorDiag); break;
1123 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1124 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1125 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001126 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001127
1128 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1129 if (!PD)
1130 return;
1131
1132 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001133 PathDiagnosticPiece* piece =
1134 new PathDiagnosticEventPiece(L, R.getDescription());
1135
Ted Kremenek3148eb42009-01-24 00:55:43 +00001136 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1137 D->push_back(piece);
1138 }
1139
1140 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001141}
Ted Kremenek57202072008-07-14 17:40:50 +00001142
Ted Kremenek8c036c72008-09-20 04:23:38 +00001143void BugReporter::EmitBasicReport(const char* name, const char* str,
1144 SourceLocation Loc,
1145 SourceRange* RBeg, unsigned NumRanges) {
1146 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1147}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001148
Ted Kremenek8c036c72008-09-20 04:23:38 +00001149void BugReporter::EmitBasicReport(const char* name, const char* category,
1150 const char* str, SourceLocation Loc,
1151 SourceRange* RBeg, unsigned NumRanges) {
1152
Ted Kremenekcf118d42009-02-04 23:49:09 +00001153 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1154 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001155 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001156 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1157 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1158 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001159}