blob: 88887b1d6c75ea538f0335cdf29e6d73d78ea8b4 [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());
138 PathDiagnosticPiece* P = new PathDiagnosticPiece(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
484 PD.push_front(new PathDiagnosticPiece(L, msg));
485 }
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 Kremenekc0959972008-07-02 21:24:01 +0000566void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000567 BugReportEquivClass& EQ) {
568
569 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000570
Ted Kremenekcf118d42009-02-04 23:49:09 +0000571 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
572 const ExplodedNode<GRState>* N = I->getEndNode();
573 if (N) Nodes.push_back(N);
574 }
575
576 if (Nodes.empty())
577 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000578
579 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000580 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000581 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000582 std::pair<ExplodedNode<GRState>*, unsigned> >&
583 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000584
Ted Kremenekcf118d42009-02-04 23:49:09 +0000585 // Find the BugReport with the original location.
586 BugReport *R = 0;
587 unsigned i = 0;
588 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
589 if (i == GPair.second.second) { R = *I; break; }
590
591 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000592
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000593 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
594 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000595 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000596
Ted Kremenekcf118d42009-02-04 23:49:09 +0000597 // Start building the path diagnostic...
598 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000599 PD.push_back(Piece);
600 else
601 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000602
Ted Kremenek3148eb42009-01-24 00:55:43 +0000603 const ExplodedNode<GRState>* NextNode = N->pred_empty()
604 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000605
Ted Kremenekc0959972008-07-02 21:24:01 +0000606 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000607 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000608 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000609
Ted Kremenek6837faa2008-04-09 00:20:43 +0000610 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000611 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000612 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000613
614 ProgramPoint P = N->getLocation();
615
616 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
617
618 CFGBlock* Src = BE->getSrc();
619 CFGBlock* Dst = BE->getDst();
620
621 Stmt* T = Src->getTerminator();
622
623 if (!T)
624 continue;
625
626 FullSourceLoc L(T->getLocStart(), SMgr);
627
628 switch (T->getStmtClass()) {
629 default:
630 break;
631
632 case Stmt::GotoStmtClass:
633 case Stmt::IndirectGotoStmtClass: {
634
Ted Kremenekb697b102009-02-23 22:44:26 +0000635 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000636
637 if (!S)
638 continue;
639
Ted Kremenek297308e2009-02-10 23:56:07 +0000640 std::string sbuf;
641 llvm::raw_string_ostream os(sbuf);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000642
643 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000644 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000645
Ted Kremenek025fedc2009-03-02 21:41:18 +0000646 PD.push_front(new PathDiagnosticPiece(L, os.str(),
647 PathDiagnosticPiece::ControlFlow));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000648 break;
649 }
650
Ted Kremenek297308e2009-02-10 23:56:07 +0000651 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000652 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000653 std::string sbuf;
654 llvm::raw_string_ostream os(sbuf);
Ted Kremenek5a429952008-04-23 23:35:07 +0000655
656 if (Stmt* S = Dst->getLabel())
657 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000658 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000659 os << "No cases match in the switch statement. "
660 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000661 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000662 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000663 case Stmt::DefaultStmtClass:
664 os << "Control jumps to the 'default' case at line "
665 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
666 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000667
668 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000669 os << "Control jumps to 'case ";
670
Ted Kremenek5a429952008-04-23 23:35:07 +0000671 CaseStmt* Case = cast<CaseStmt>(S);
672 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000673
Ted Kremenek5a429952008-04-23 23:35:07 +0000674 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000675
Ted Kremenek5a429952008-04-23 23:35:07 +0000676 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000677
Ted Kremenek5a429952008-04-23 23:35:07 +0000678 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
679
680 // FIXME: Maybe this should be an assertion. Are there cases
681 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000682 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000683 if (D) {
684 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000685 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000686 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000687 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000688
689 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000690
Ted Kremenek5a429952008-04-23 23:35:07 +0000691 // Not an enum.
692 Expr* CondE = cast<SwitchStmt>(T)->getCond();
693 unsigned bits = Ctx.getTypeSize(CondE->getType());
694 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000695
Ted Kremenek5a429952008-04-23 23:35:07 +0000696 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
697 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000698 continue;
699 }
700
Ted Kremenek297308e2009-02-10 23:56:07 +0000701 os << V;
702 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000703
Ted Kremenek61f3e052008-04-03 04:42:52 +0000704 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000705 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000706
707 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000708 }
709 }
Ted Kremenek56783922008-04-25 01:29:56 +0000710 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000711 os << "'Default' branch taken. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000712 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek56783922008-04-25 01:29:56 +0000713 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000714
Ted Kremenek025fedc2009-03-02 21:41:18 +0000715 PD.push_front(new PathDiagnosticPiece(L, os.str(),
716 PathDiagnosticPiece::ControlFlow));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000717 break;
718 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000719
720 case Stmt::BreakStmtClass:
721 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000722 std::string sbuf;
723 llvm::raw_string_ostream os(sbuf);
Ted Kremenekb479dad2009-02-23 23:13:51 +0000724 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek025fedc2009-03-02 21:41:18 +0000725 PD.push_front(new PathDiagnosticPiece(L, os.str(),
726 PathDiagnosticPiece::ControlFlow));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000727 break;
728 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000729
730 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000731 std::string sbuf;
732 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000733 os << "'?' condition evaluates to ";
734
735 if (*(Src->succ_begin()+1) == Dst)
736 os << "false.";
737 else
738 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000739
Ted Kremenek025fedc2009-03-02 21:41:18 +0000740 PD.push_front(new PathDiagnosticPiece(L, os.str(),
741 PathDiagnosticPiece::ControlFlow));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000742 break;
743 }
744
745 case Stmt::DoStmtClass: {
746
747 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000748 std::string sbuf;
749 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000750
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000751 os << "Loop condition is true. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000752 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000753
Ted Kremenek025fedc2009-03-02 21:41:18 +0000754 PD.push_front(new PathDiagnosticPiece(L, os.str(),
755 PathDiagnosticPiece::ControlFlow));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000756 }
757 else
758 PD.push_front(new PathDiagnosticPiece(L,
Ted Kremenek025fedc2009-03-02 21:41:18 +0000759 "Loop condition is false. Exiting loop.",
760 PathDiagnosticPiece::ControlFlow));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000761
762 break;
763 }
764
Ted Kremenek61f3e052008-04-03 04:42:52 +0000765 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000766 case Stmt::ForStmtClass: {
767
768 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000769 std::string sbuf;
770 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000771
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000772 os << "Loop condition is false. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000773 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek297308e2009-02-10 23:56:07 +0000774
Ted Kremenek025fedc2009-03-02 21:41:18 +0000775 PD.push_front(new PathDiagnosticPiece(L, os.str(),
776 PathDiagnosticPiece::ControlFlow));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000777 }
778 else
779 PD.push_front(new PathDiagnosticPiece(L,
Ted Kremenek025fedc2009-03-02 21:41:18 +0000780 "Loop condition is true. Entering loop body.",
781 PathDiagnosticPiece::ControlFlow));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000782
783 break;
784 }
785
Ted Kremenek297308e2009-02-10 23:56:07 +0000786 case Stmt::IfStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000787 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek025fedc2009-03-02 21:41:18 +0000788 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch.",
789 PathDiagnosticPiece::ControlFlow));
790 else
791 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch.",
792 PathDiagnosticPiece::ControlFlow));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000793
794 break;
795 }
796 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000797 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000798
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000799 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
800 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000801 PD.push_front(p);
802
Ted Kremenek9e240492008-10-04 05:50:14 +0000803 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
804 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000805 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000806 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
807 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000808 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000809 }
810}
811
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000812
Ted Kremenekcf118d42009-02-04 23:49:09 +0000813void BugReporter::Register(BugType *BT) {
814 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000815}
816
Ted Kremenekcf118d42009-02-04 23:49:09 +0000817void BugReporter::EmitReport(BugReport* R) {
818 // Compute the bug report's hash to determine its equivalence class.
819 llvm::FoldingSetNodeID ID;
820 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000821
Ted Kremenekcf118d42009-02-04 23:49:09 +0000822 // Lookup the equivance class. If there isn't one, create it.
823 BugType& BT = R->getBugType();
824 Register(&BT);
825 void *InsertPos;
826 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
827
828 if (!EQ) {
829 EQ = new BugReportEquivClass(R);
830 BT.EQClasses.InsertNode(EQ, InsertPos);
831 }
832 else
833 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000834}
835
Ted Kremenekcf118d42009-02-04 23:49:09 +0000836void BugReporter::FlushReport(BugReportEquivClass& EQ) {
837 assert(!EQ.Reports.empty());
838 BugReport &R = **EQ.begin();
839
840 // FIXME: Make sure we use the 'R' for the path that was actually used.
841 // Probably doesn't make a difference in practice.
842 BugType& BT = R.getBugType();
843
844 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
845 R.getDescription(),
846 BT.getCategory()));
847 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000848
849 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000850 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000851 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000852
Ted Kremenek3148eb42009-01-24 00:55:43 +0000853 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000854 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000855 const SourceRange *Beg = 0, *End = 0;
856 R.getRanges(*this, Beg, End);
857 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000858 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000859 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
860 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000861
Ted Kremenek3148eb42009-01-24 00:55:43 +0000862 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000863 default: assert(0 && "Don't handle this many ranges yet!");
864 case 0: Diag.Report(L, ErrorDiag); break;
865 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
866 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
867 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000868 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000869
870 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
871 if (!PD)
872 return;
873
874 if (D->empty()) {
875 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
876 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
877 D->push_back(piece);
878 }
879
880 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000881}
Ted Kremenek57202072008-07-14 17:40:50 +0000882
Ted Kremenek8c036c72008-09-20 04:23:38 +0000883void BugReporter::EmitBasicReport(const char* name, const char* str,
884 SourceLocation Loc,
885 SourceRange* RBeg, unsigned NumRanges) {
886 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
887}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000888
Ted Kremenek8c036c72008-09-20 04:23:38 +0000889void BugReporter::EmitBasicReport(const char* name, const char* category,
890 const char* str, SourceLocation Loc,
891 SourceRange* RBeg, unsigned NumRanges) {
892
Ted Kremenekcf118d42009-02-04 23:49:09 +0000893 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
894 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000895 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000896 RangedBugReport *R = new DiagBugReport(*BT, str, L);
897 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
898 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000899}