blob: 86ee484f36cfd80b196b8b687e12a19a75402768 [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"
22#include "clang/Analysis/ProgramPoint.h"
23#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000025#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000026#include "llvm/ADT/STLExtras.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000027
28using namespace clang;
29
Ted Kremenekcf118d42009-02-04 23:49:09 +000030//===----------------------------------------------------------------------===//
31// static functions.
32//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000033
Ted Kremenekb697b102009-02-23 22:44:26 +000034static inline Stmt* GetStmt(ProgramPoint P) {
35 if (const PostStmt* PS = dyn_cast<PostStmt>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000036 return PS->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000037 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000038 return BE->getSrc()->getTerminator();
Ted Kremenek61f3e052008-04-03 04:42:52 +000039
Ted Kremenekb697b102009-02-23 22:44:26 +000040 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000041}
42
Ted Kremenek3148eb42009-01-24 00:55:43 +000043static inline const ExplodedNode<GRState>*
Ted Kremenekb697b102009-02-23 22:44:26 +000044GetPredecessorNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000045 return N->pred_empty() ? NULL : *(N->pred_begin());
46}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000047
Ted Kremenekb697b102009-02-23 22:44:26 +000048static inline const ExplodedNode<GRState>*
49GetSuccessorNode(const ExplodedNode<GRState>* N) {
50 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000051}
52
Ted Kremenekb697b102009-02-23 22:44:26 +000053static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) {
54 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
55 if (Stmt *S = GetStmt(N->getLocation()))
56 return S;
57
58 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000059}
60
Ted Kremenekb697b102009-02-23 22:44:26 +000061static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) {
62 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
63 if (Stmt *S = GetStmt(N->getLocation()))
64 return S;
65
66 return 0;
67}
68
69static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
70 if (Stmt *S = GetStmt(N->getLocation()))
71 return S;
72
73 return GetPreviousStmt(N);
74}
75
76static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
77 if (Stmt *S = GetStmt(N->getLocation()))
78 return S;
79
80 return GetNextStmt(N);
81}
82
83//===----------------------------------------------------------------------===//
84// Diagnostics for 'execution continues on line XXX'.
85//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +000086
87static inline void ExecutionContinues(llvm::raw_string_ostream& os,
88 SourceManager& SMgr,
89 const ExplodedNode<GRState>* N,
90 const Decl& D) {
91
Ted Kremenek143ca222008-05-06 18:11:09 +000092 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +000093 if (os.str().empty())
94 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +000095
Ted Kremenekb479dad2009-02-23 23:13:51 +000096 if (Stmt *S = GetNextStmt(N))
Ted Kremenekb697b102009-02-23 22:44:26 +000097 os << "Execution continues on line "
Ted Kremenekb479dad2009-02-23 23:13:51 +000098 << SMgr.getInstantiationLineNumber(S->getLocStart()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +000099 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000100 os << "Execution jumps to the end of the "
101 << (isa<ObjCMethodDecl>(D) ? "method" : "function") << '.';
Ted Kremenek143ca222008-05-06 18:11:09 +0000102}
103
Ted Kremenekcf118d42009-02-04 23:49:09 +0000104//===----------------------------------------------------------------------===//
105// Methods for BugType and subclasses.
106//===----------------------------------------------------------------------===//
107BugType::~BugType() {}
108void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000109
Ted Kremenekcf118d42009-02-04 23:49:09 +0000110//===----------------------------------------------------------------------===//
111// Methods for BugReport and subclasses.
112//===----------------------------------------------------------------------===//
113BugReport::~BugReport() {}
114RangedBugReport::~RangedBugReport() {}
115
116Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000117 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000118 Stmt *S = NULL;
119
Ted Kremenekcf118d42009-02-04 23:49:09 +0000120 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000121 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000122 }
123 if (!S) S = GetStmt(ProgP);
124
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000125 return S;
126}
127
128PathDiagnosticPiece*
129BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000130 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000131
132 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000133
134 if (!S)
135 return NULL;
136
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000137 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000138 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000139
Ted Kremenekde7161f2008-04-03 18:00:37 +0000140 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000141 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000142
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000143 for (; Beg != End; ++Beg)
144 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000145
146 return P;
147}
148
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000149void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
150 const SourceRange*& end) {
151
152 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
153 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000154 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000155 beg = &R;
156 end = beg+1;
157 }
158 else
159 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000160}
161
Ted Kremenekcf118d42009-02-04 23:49:09 +0000162SourceLocation BugReport::getLocation() const {
163 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000164 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
165 // For member expressions, return the location of the '.' or '->'.
166 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
167 return ME->getMemberLoc();
168
Ted Kremenekcf118d42009-02-04 23:49:09 +0000169 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000170 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000171
172 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000173}
174
Ted Kremenek3148eb42009-01-24 00:55:43 +0000175PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
176 const ExplodedNode<GRState>* PrevN,
177 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000178 BugReporter& BR,
179 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000180 return NULL;
181}
182
Ted Kremenekcf118d42009-02-04 23:49:09 +0000183//===----------------------------------------------------------------------===//
184// Methods for BugReporter and subclasses.
185//===----------------------------------------------------------------------===//
186
187BugReportEquivClass::~BugReportEquivClass() {
188 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
189}
190
191GRBugReporter::~GRBugReporter() { FlushReports(); }
192BugReporterData::~BugReporterData() {}
193
194ExplodedGraph<GRState>&
195GRBugReporter::getGraph() { return Eng.getGraph(); }
196
197GRStateManager&
198GRBugReporter::getStateManager() { return Eng.getStateManager(); }
199
200BugReporter::~BugReporter() { FlushReports(); }
201
202void BugReporter::FlushReports() {
203 if (BugTypes.isEmpty())
204 return;
205
206 // First flush the warnings for each BugType. This may end up creating new
207 // warnings and new BugTypes. Because ImmutableSet is a functional data
208 // structure, we do not need to worry about the iterators being invalidated.
209 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
210 const_cast<BugType*>(*I)->FlushReports(*this);
211
212 // Iterate through BugTypes a second time. BugTypes may have been updated
213 // with new BugType objects and new warnings.
214 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
215 BugType *BT = const_cast<BugType*>(*I);
216
217 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
218 SetTy& EQClasses = BT->EQClasses;
219
220 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
221 BugReportEquivClass& EQ = *EI;
222 FlushReport(EQ);
223 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000224
Ted Kremenekcf118d42009-02-04 23:49:09 +0000225 // Delete the BugType object. This will also delete the equivalence
226 // classes.
227 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000228 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000229
230 // Remove all references to the BugType objects.
231 BugTypes = F.GetEmptySet();
232}
233
234//===----------------------------------------------------------------------===//
235// PathDiagnostics generation.
236//===----------------------------------------------------------------------===//
237
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000238typedef llvm::DenseMap<const ExplodedNode<GRState>*,
239 const ExplodedNode<GRState>*> NodeBackMap;
240
241static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000242 std::pair<ExplodedNode<GRState>*, unsigned> >
243MakeReportGraph(const ExplodedGraph<GRState>* G,
244 const ExplodedNode<GRState>** NStart,
245 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000246
Ted Kremenekcf118d42009-02-04 23:49:09 +0000247 // Create the trimmed graph. It will contain the shortest paths from the
248 // error nodes to the root. In the new graph we should only have one
249 // error node unless there are two or more error nodes with the same minimum
250 // path length.
251 ExplodedGraph<GRState>* GTrim;
252 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000253
254 llvm::DenseMap<const void*, const void*> InverseMap;
255 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000256
257 // Create owning pointers for GTrim and NMap just to ensure that they are
258 // released when this function exists.
259 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
260 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
261
262 // Find the (first) error node in the trimmed graph. We just need to consult
263 // the node map (NMap) which maps from nodes in the original graph to nodes
264 // in the new graph.
265 const ExplodedNode<GRState>* N = 0;
266 unsigned NodeIndex = 0;
267
268 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
269 if ((N = NMap->getMappedNode(*I))) {
270 NodeIndex = (I - NStart) / sizeof(*I);
271 break;
272 }
273
274 assert(N && "No error node found in the trimmed graph.");
275
276 // Create a new (third!) graph with a single path. This is the graph
277 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000278 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000279 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
280 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000281
282 // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000283 // to the root node, and then construct a new graph that contains only
284 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000285 llvm::DenseMap<const void*,unsigned> Visited;
286 llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000287 WS.push_back(N);
288 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000289 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000290
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000291 while (!WS.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000292 const ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000293 WS.pop_back();
294
295 if (Visited.find(Node) != Visited.end())
296 continue;
297
298 Visited[Node] = cnt++;
299
300 if (Node->pred_empty()) {
301 Root = Node;
302 break;
303 }
304
Ted Kremenek3148eb42009-01-24 00:55:43 +0000305 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000306 E=Node->pred_end(); I!=E; ++I)
307 WS.push_back(*I);
308 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000309
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000310 assert (Root);
311
312 // Now walk from the root down the DFS path, always taking the successor
313 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000314 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000315 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000316
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000317 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000318 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000319 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000320 assert (I != Visited.end());
321
322 // Create the equivalent node in the new graph with the same state
323 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000324 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000325 GNew->getNode(N->getLocation(), N->getState());
326
327 // Store the mapping to the original node.
328 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
329 assert(IMitr != InverseMap.end() && "No mapping to original node.");
330 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000331
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000332 // Link up the new node with the previous node.
333 if (Last)
334 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000335
336 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000337
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000338 // Are we at the final node?
339 if (I->second == 0) {
340 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000341 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000342 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000343
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000344 // Find the next successor node. We choose the node that is marked
345 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000346 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
347 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000348 N = 0;
349
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000350 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000351
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000352 I = Visited.find(*SI);
353
354 if (I == Visited.end())
355 continue;
356
357 if (!N || I->second < MinVal) {
358 N = *SI;
359 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000360 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000361 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000362
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000363 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000364 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000365
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000366 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000367 return std::make_pair(std::make_pair(GNew, BM),
368 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000369}
370
Ted Kremenek3148eb42009-01-24 00:55:43 +0000371static const VarDecl*
372GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
373 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000374
375 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
376
377 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000378
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000379 if (!isa<PostStmt>(P))
380 continue;
381
382 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000383
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000384 if (!DR)
385 continue;
386
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000387 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000388
389 if (X != Y)
390 continue;
391
392 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
393
394 if (!VD)
395 continue;
396
397 return VD;
398 }
399
400 return 0;
401}
402
Ted Kremenek9e240492008-10-04 05:50:14 +0000403namespace {
404class VISIBILITY_HIDDEN NotableSymbolHandler
405 : public StoreManager::BindingsHandler {
406
Ted Kremenek2dabd432008-12-05 02:27:51 +0000407 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000408 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000409 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000410 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000411 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000412 PathDiagnostic& PD;
413 BugReporter& BR;
414
415public:
416
Ted Kremenek3148eb42009-01-24 00:55:43 +0000417 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
418 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000419 PathDiagnostic& pd, BugReporter& br)
420 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
421
Ted Kremenekbe912242009-03-05 16:31:07 +0000422 bool HandleBinding(StoreManager& SMgr, Store store,
423 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000424
Ted Kremenek2dabd432008-12-05 02:27:51 +0000425 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000426
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000427 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000428 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000429 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000430 ScanSym = SV->getSymbol();
431 else
432 return true;
433
434 if (ScanSym != Sym)
435 return true;
436
437 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000438 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000439
440 if (X == V) // Same binding?
441 return true;
442
443 // Different binding. Only handle assignments for now. We don't pull
444 // this check out of the loop because we will eventually handle other
445 // cases.
446
447 VarDecl *VD = 0;
448
Ted Kremenek3148eb42009-01-24 00:55:43 +0000449 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000450 if (!B->isAssignmentOp())
451 return true;
452
453 // What variable did we assign to?
454 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
455
456 if (!DR)
457 return true;
458
459 VD = dyn_cast<VarDecl>(DR->getDecl());
460 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000461 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000462 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
463 // assume that each DeclStmt has a single Decl. This invariant
464 // holds by contruction in the CFG.
465 VD = dyn_cast<VarDecl>(*DS->decl_begin());
466 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000467
468 if (!VD)
469 return true;
470
471 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000472 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000473
474 if (!MostRecent)
475 return true;
476
477 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000478 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
479
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000480 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000481 std::string msg = "'" + std::string(VD->getNameAsString()) +
482 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000483
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000484 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000485 }
486
487 return true;
488 }
489};
490}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000491
Ted Kremenek3148eb42009-01-24 00:55:43 +0000492static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
493 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000494 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000495 PathDiagnostic& PD) {
496
Ted Kremenek3148eb42009-01-24 00:55:43 +0000497 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000498 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000499
500 if (!PrevSt)
501 return;
502
Ted Kremenek9e240492008-10-04 05:50:14 +0000503 // Look at the region bindings of the current state that map to the
504 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000505 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000506 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
507 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
508}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000509
Ted Kremenek9e240492008-10-04 05:50:14 +0000510namespace {
511class VISIBILITY_HIDDEN ScanNotableSymbols
512 : public StoreManager::BindingsHandler {
513
Ted Kremenek2dabd432008-12-05 02:27:51 +0000514 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000515 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000516 Stmt* S;
517 GRBugReporter& BR;
518 PathDiagnostic& PD;
519
520public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000521 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000522 PathDiagnostic& pd)
523 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000524
Ted Kremenekbe912242009-03-05 16:31:07 +0000525 bool HandleBinding(StoreManager& SMgr, Store store,
526 const MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000527 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000528
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000529 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000530 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000531 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000532 ScanSym = SV->getSymbol();
533 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000534 return true;
535
Ted Kremenek94c96982009-03-03 22:06:47 +0000536 assert (ScanSym.isValid());
Ted Kremenek9e240492008-10-04 05:50:14 +0000537
538 if (!BR.isNotable(ScanSym))
539 return true;
540
541 if (AlreadyProcessed.count(ScanSym))
542 return true;
543
544 AlreadyProcessed.insert(ScanSym);
545
546 HandleNotableSymbol(N, S, ScanSym, BR, PD);
547 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000548 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000549};
550} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000551
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000552namespace {
553class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
554 NodeBackMap& M;
555public:
556 NodeMapClosure(NodeBackMap *m) : M(*m) {}
557 ~NodeMapClosure() {}
558
559 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
560 NodeBackMap::iterator I = M.find(N);
561 return I == M.end() ? 0 : I->second;
562 }
563};
564}
565
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000566/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
567/// and collapses PathDiagosticPieces that are expanded by macros.
568static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
569 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
570 MacroStackTy;
571
572 typedef std::vector<PathDiagnosticPiece*>
573 PiecesTy;
574
575 MacroStackTy MacroStack;
576 PiecesTy Pieces;
577
578 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
579 // Get the location of the PathDiagnosticPiece.
580 const FullSourceLoc Loc = I->getLocation();
581
582 // Determine the instantiation location, which is the location we group
583 // related PathDiagnosticPieces.
584 SourceLocation InstantiationLoc = Loc.isMacroID() ?
585 SM.getInstantiationLoc(Loc) :
586 SourceLocation();
587
588 if (Loc.isFileID()) {
589 MacroStack.clear();
590 Pieces.push_back(&*I);
591 continue;
592 }
593
594 assert(Loc.isMacroID());
595
596 // Is the PathDiagnosticPiece within the same macro group?
597 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
598 MacroStack.back().first->push_back(&*I);
599 continue;
600 }
601
602 // We aren't in the same group. Are we descending into a new macro
603 // or are part of an old one?
604 PathDiagnosticMacroPiece *MacroGroup = 0;
605
606 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
607 SM.getInstantiationLoc(Loc) :
608 SourceLocation();
609
610 // Walk the entire macro stack.
611 while (!MacroStack.empty()) {
612 if (InstantiationLoc == MacroStack.back().second) {
613 MacroGroup = MacroStack.back().first;
614 break;
615 }
616
617 if (ParentInstantiationLoc == MacroStack.back().second) {
618 MacroGroup = MacroStack.back().first;
619 break;
620 }
621
622 MacroStack.pop_back();
623 }
624
625 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
626 // Create a new macro group and add it to the stack.
627 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
628
629 if (MacroGroup)
630 MacroGroup->push_back(NewGroup);
631 else {
632 assert(InstantiationLoc.isFileID());
633 Pieces.push_back(NewGroup);
634 }
635
636 MacroGroup = NewGroup;
637 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
638 }
639
640 // Finally, add the PathDiagnosticPiece to the group.
641 MacroGroup->push_back(&*I);
642 }
643
644 // Now take the pieces and construct a new PathDiagnostic.
645 PD.resetPath(false);
646
647 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
648 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
649 if (!MP->containsEvent()) {
650 delete MP;
651 continue;
652 }
653
654 PD.push_back(*I);
655 }
656}
657
Ted Kremenekc0959972008-07-02 21:24:01 +0000658void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000659 BugReportEquivClass& EQ) {
660
661 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000662
Ted Kremenekcf118d42009-02-04 23:49:09 +0000663 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
664 const ExplodedNode<GRState>* N = I->getEndNode();
665 if (N) Nodes.push_back(N);
666 }
667
668 if (Nodes.empty())
669 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000670
671 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000672 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000673 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000674 std::pair<ExplodedNode<GRState>*, unsigned> >&
675 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000676
Ted Kremenekcf118d42009-02-04 23:49:09 +0000677 // Find the BugReport with the original location.
678 BugReport *R = 0;
679 unsigned i = 0;
680 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
681 if (i == GPair.second.second) { R = *I; break; }
682
683 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000684
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000685 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
686 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000687 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000688
Ted Kremenekcf118d42009-02-04 23:49:09 +0000689 // Start building the path diagnostic...
690 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000691 PD.push_back(Piece);
692 else
693 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000694
Ted Kremenek3148eb42009-01-24 00:55:43 +0000695 const ExplodedNode<GRState>* NextNode = N->pred_empty()
696 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000697
Ted Kremenekc0959972008-07-02 21:24:01 +0000698 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000699 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000700 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000701
Ted Kremenek6837faa2008-04-09 00:20:43 +0000702 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000703 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000704 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000705
706 ProgramPoint P = N->getLocation();
707
708 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
709
710 CFGBlock* Src = BE->getSrc();
711 CFGBlock* Dst = BE->getDst();
712
713 Stmt* T = Src->getTerminator();
714
715 if (!T)
716 continue;
717
718 FullSourceLoc L(T->getLocStart(), SMgr);
719
720 switch (T->getStmtClass()) {
721 default:
722 break;
723
724 case Stmt::GotoStmtClass:
725 case Stmt::IndirectGotoStmtClass: {
726
Ted Kremenekb697b102009-02-23 22:44:26 +0000727 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000728
729 if (!S)
730 continue;
731
Ted Kremenek297308e2009-02-10 23:56:07 +0000732 std::string sbuf;
733 llvm::raw_string_ostream os(sbuf);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000734
735 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000736 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000737
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000738 PD.push_front(new PathDiagnosticControlFlowPiece(L, os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000739 break;
740 }
741
Ted Kremenek297308e2009-02-10 23:56:07 +0000742 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000743 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000744 std::string sbuf;
745 llvm::raw_string_ostream os(sbuf);
Ted Kremenek5a429952008-04-23 23:35:07 +0000746
747 if (Stmt* S = Dst->getLabel())
748 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000749 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000750 os << "No cases match in the switch statement. "
751 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000752 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000753 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000754 case Stmt::DefaultStmtClass:
755 os << "Control jumps to the 'default' case at line "
756 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
757 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000758
759 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000760 os << "Control jumps to 'case ";
761
Ted Kremenek5a429952008-04-23 23:35:07 +0000762 CaseStmt* Case = cast<CaseStmt>(S);
763 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000764
Ted Kremenek5a429952008-04-23 23:35:07 +0000765 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000766
Ted Kremenek5a429952008-04-23 23:35:07 +0000767 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000768
Ted Kremenek5a429952008-04-23 23:35:07 +0000769 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
770
771 // FIXME: Maybe this should be an assertion. Are there cases
772 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000773 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000774 if (D) {
775 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000776 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000777 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000778 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000779
780 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000781
Ted Kremenek5a429952008-04-23 23:35:07 +0000782 // Not an enum.
783 Expr* CondE = cast<SwitchStmt>(T)->getCond();
784 unsigned bits = Ctx.getTypeSize(CondE->getType());
785 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000786
Ted Kremenek5a429952008-04-23 23:35:07 +0000787 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
788 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000789 continue;
790 }
791
Ted Kremenek297308e2009-02-10 23:56:07 +0000792 os << V;
793 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000794
Ted Kremenek61f3e052008-04-03 04:42:52 +0000795 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000796 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000797
798 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000799 }
800 }
Ted Kremenek56783922008-04-25 01:29:56 +0000801 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000802 os << "'Default' branch taken. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000803 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek56783922008-04-25 01:29:56 +0000804 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000805
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000806 PD.push_front(new PathDiagnosticControlFlowPiece(L, os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000807 break;
808 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000809
810 case Stmt::BreakStmtClass:
811 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000812 std::string sbuf;
813 llvm::raw_string_ostream os(sbuf);
Ted Kremenekb479dad2009-02-23 23:13:51 +0000814 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000815 PD.push_front(new PathDiagnosticControlFlowPiece(L, os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000816 break;
817 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000818
819 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000820 std::string sbuf;
821 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000822 os << "'?' condition evaluates to ";
823
824 if (*(Src->succ_begin()+1) == Dst)
825 os << "false.";
826 else
827 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000828
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000829 PD.push_front(new PathDiagnosticControlFlowPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000830 break;
831 }
832
833 case Stmt::DoStmtClass: {
834
835 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000836 std::string sbuf;
837 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000838
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000839 os << "Loop condition is true. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000840 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000841
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000842 PD.push_front(new PathDiagnosticControlFlowPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000843 }
844 else
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000845 PD.push_front(new PathDiagnosticControlFlowPiece(L,
846 "Loop condition is false. Exiting loop."));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000847
848 break;
849 }
850
Ted Kremenek61f3e052008-04-03 04:42:52 +0000851 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000852 case Stmt::ForStmtClass: {
853
854 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000855 std::string sbuf;
856 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000857
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000858 os << "Loop condition is false. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000859 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek297308e2009-02-10 23:56:07 +0000860
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000861 PD.push_front(new PathDiagnosticControlFlowPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000862 }
863 else
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000864 PD.push_front(new PathDiagnosticControlFlowPiece(L,
865 "Loop condition is true. Entering loop body."));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000866
867 break;
868 }
869
Ted Kremenek297308e2009-02-10 23:56:07 +0000870 case Stmt::IfStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000871 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000872 PD.push_front(new PathDiagnosticControlFlowPiece(L,
873 "Taking false branch."));
Ted Kremenek025fedc2009-03-02 21:41:18 +0000874 else
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000875 PD.push_front(new PathDiagnosticControlFlowPiece(L,
876 "Taking true branch."));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000877
878 break;
879 }
880 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000881 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000882
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000883 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
884 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000885 PD.push_front(p);
886
Ted Kremenek9e240492008-10-04 05:50:14 +0000887 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
888 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000889 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000890 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
891 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000892 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000893 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000894
895 // After constructing the full PathDiagnostic, do a pass over it to compact
896 // PathDiagnosticPieces that occur within a macro.
897 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000898}
899
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000900
Ted Kremenekcf118d42009-02-04 23:49:09 +0000901void BugReporter::Register(BugType *BT) {
902 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000903}
904
Ted Kremenekcf118d42009-02-04 23:49:09 +0000905void BugReporter::EmitReport(BugReport* R) {
906 // Compute the bug report's hash to determine its equivalence class.
907 llvm::FoldingSetNodeID ID;
908 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000909
Ted Kremenekcf118d42009-02-04 23:49:09 +0000910 // Lookup the equivance class. If there isn't one, create it.
911 BugType& BT = R->getBugType();
912 Register(&BT);
913 void *InsertPos;
914 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
915
916 if (!EQ) {
917 EQ = new BugReportEquivClass(R);
918 BT.EQClasses.InsertNode(EQ, InsertPos);
919 }
920 else
921 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000922}
923
Ted Kremenekcf118d42009-02-04 23:49:09 +0000924void BugReporter::FlushReport(BugReportEquivClass& EQ) {
925 assert(!EQ.Reports.empty());
926 BugReport &R = **EQ.begin();
927
928 // FIXME: Make sure we use the 'R' for the path that was actually used.
929 // Probably doesn't make a difference in practice.
930 BugType& BT = R.getBugType();
931
932 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
933 R.getDescription(),
934 BT.getCategory()));
935 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000936
937 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000938 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000939 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000940
Ted Kremenek3148eb42009-01-24 00:55:43 +0000941 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000942 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000943 const SourceRange *Beg = 0, *End = 0;
944 R.getRanges(*this, Beg, End);
945 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000946 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000947 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
948 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000949
Ted Kremenek3148eb42009-01-24 00:55:43 +0000950 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000951 default: assert(0 && "Don't handle this many ranges yet!");
952 case 0: Diag.Report(L, ErrorDiag); break;
953 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
954 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
955 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000956 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000957
958 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
959 if (!PD)
960 return;
961
962 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000963 PathDiagnosticPiece* piece =
964 new PathDiagnosticEventPiece(L, R.getDescription());
965
Ted Kremenek3148eb42009-01-24 00:55:43 +0000966 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
967 D->push_back(piece);
968 }
969
970 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000971}
Ted Kremenek57202072008-07-14 17:40:50 +0000972
Ted Kremenek8c036c72008-09-20 04:23:38 +0000973void BugReporter::EmitBasicReport(const char* name, const char* str,
974 SourceLocation Loc,
975 SourceRange* RBeg, unsigned NumRanges) {
976 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
977}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000978
Ted Kremenek8c036c72008-09-20 04:23:38 +0000979void BugReporter::EmitBasicReport(const char* name, const char* category,
980 const char* str, SourceLocation Loc,
981 SourceRange* RBeg, unsigned NumRanges) {
982
Ted Kremenekcf118d42009-02-04 23:49:09 +0000983 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
984 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000985 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000986 RangedBugReport *R = new DiagBugReport(*BT, str, L);
987 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
988 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000989}