blob: a8cb6a9107aac32ebe89948067e1f16d83982989 [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
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000422 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000423
Ted Kremenek2dabd432008-12-05 02:27:51 +0000424 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000425
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000426 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000427 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000428 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000429 ScanSym = SV->getSymbol();
430 else
431 return true;
432
433 if (ScanSym != Sym)
434 return true;
435
436 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000437 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000438
439 if (X == V) // Same binding?
440 return true;
441
442 // Different binding. Only handle assignments for now. We don't pull
443 // this check out of the loop because we will eventually handle other
444 // cases.
445
446 VarDecl *VD = 0;
447
Ted Kremenek3148eb42009-01-24 00:55:43 +0000448 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000449 if (!B->isAssignmentOp())
450 return true;
451
452 // What variable did we assign to?
453 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
454
455 if (!DR)
456 return true;
457
458 VD = dyn_cast<VarDecl>(DR->getDecl());
459 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000460 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000461 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
462 // assume that each DeclStmt has a single Decl. This invariant
463 // holds by contruction in the CFG.
464 VD = dyn_cast<VarDecl>(*DS->decl_begin());
465 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000466
467 if (!VD)
468 return true;
469
470 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000471 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000472
473 if (!MostRecent)
474 return true;
475
476 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000477 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
478
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000479 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000480 std::string msg = "'" + std::string(VD->getNameAsString()) +
481 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000482
483 PD.push_front(new PathDiagnosticPiece(L, msg));
484 }
485
486 return true;
487 }
488};
489}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000490
Ted Kremenek3148eb42009-01-24 00:55:43 +0000491static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
492 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000493 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000494 PathDiagnostic& PD) {
495
Ted Kremenek3148eb42009-01-24 00:55:43 +0000496 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000497 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000498
499 if (!PrevSt)
500 return;
501
Ted Kremenek9e240492008-10-04 05:50:14 +0000502 // Look at the region bindings of the current state that map to the
503 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000504 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000505 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
506 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
507}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000508
Ted Kremenek9e240492008-10-04 05:50:14 +0000509namespace {
510class VISIBILITY_HIDDEN ScanNotableSymbols
511 : public StoreManager::BindingsHandler {
512
Ted Kremenek2dabd432008-12-05 02:27:51 +0000513 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000514 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000515 Stmt* S;
516 GRBugReporter& BR;
517 PathDiagnostic& PD;
518
519public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000520 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000521 PathDiagnostic& pd)
522 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000523
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000524 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000525 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000526
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000527 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000528 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000529 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000530 ScanSym = SV->getSymbol();
531 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000532 return true;
533
534 assert (ScanSym.isInitialized());
535
536 if (!BR.isNotable(ScanSym))
537 return true;
538
539 if (AlreadyProcessed.count(ScanSym))
540 return true;
541
542 AlreadyProcessed.insert(ScanSym);
543
544 HandleNotableSymbol(N, S, ScanSym, BR, PD);
545 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000546 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000547};
548} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000549
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000550namespace {
551class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
552 NodeBackMap& M;
553public:
554 NodeMapClosure(NodeBackMap *m) : M(*m) {}
555 ~NodeMapClosure() {}
556
557 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
558 NodeBackMap::iterator I = M.find(N);
559 return I == M.end() ? 0 : I->second;
560 }
561};
562}
563
Ted Kremenekc0959972008-07-02 21:24:01 +0000564void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000565 BugReportEquivClass& EQ) {
566
567 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000568
Ted Kremenekcf118d42009-02-04 23:49:09 +0000569 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
570 const ExplodedNode<GRState>* N = I->getEndNode();
571 if (N) Nodes.push_back(N);
572 }
573
574 if (Nodes.empty())
575 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000576
577 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000578 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000579 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000580 std::pair<ExplodedNode<GRState>*, unsigned> >&
581 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000582
Ted Kremenekcf118d42009-02-04 23:49:09 +0000583 // Find the BugReport with the original location.
584 BugReport *R = 0;
585 unsigned i = 0;
586 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
587 if (i == GPair.second.second) { R = *I; break; }
588
589 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000590
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000591 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
592 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000593 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000594
Ted Kremenekcf118d42009-02-04 23:49:09 +0000595 // Start building the path diagnostic...
596 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000597 PD.push_back(Piece);
598 else
599 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000600
Ted Kremenek3148eb42009-01-24 00:55:43 +0000601 const ExplodedNode<GRState>* NextNode = N->pred_empty()
602 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000603
Ted Kremenekc0959972008-07-02 21:24:01 +0000604 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000605 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000606 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000607
Ted Kremenek6837faa2008-04-09 00:20:43 +0000608 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000609 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000610 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000611
612 ProgramPoint P = N->getLocation();
613
614 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
615
616 CFGBlock* Src = BE->getSrc();
617 CFGBlock* Dst = BE->getDst();
618
619 Stmt* T = Src->getTerminator();
620
621 if (!T)
622 continue;
623
624 FullSourceLoc L(T->getLocStart(), SMgr);
625
626 switch (T->getStmtClass()) {
627 default:
628 break;
629
630 case Stmt::GotoStmtClass:
631 case Stmt::IndirectGotoStmtClass: {
632
Ted Kremenekb697b102009-02-23 22:44:26 +0000633 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000634
635 if (!S)
636 continue;
637
Ted Kremenek297308e2009-02-10 23:56:07 +0000638 std::string sbuf;
639 llvm::raw_string_ostream os(sbuf);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000640
641 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000642 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000643
644 PD.push_front(new PathDiagnosticPiece(L, os.str()));
645 break;
646 }
647
Ted Kremenek297308e2009-02-10 23:56:07 +0000648 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000649 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000650 std::string sbuf;
651 llvm::raw_string_ostream os(sbuf);
Ted Kremenek5a429952008-04-23 23:35:07 +0000652
653 if (Stmt* S = Dst->getLabel())
654 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000655 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000656 os << "No cases match in the switch statement. "
657 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000658 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000659 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000660 case Stmt::DefaultStmtClass:
661 os << "Control jumps to the 'default' case at line "
662 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
663 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000664
665 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000666 os << "Control jumps to 'case ";
667
Ted Kremenek5a429952008-04-23 23:35:07 +0000668 CaseStmt* Case = cast<CaseStmt>(S);
669 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000670
Ted Kremenek5a429952008-04-23 23:35:07 +0000671 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000672
Ted Kremenek5a429952008-04-23 23:35:07 +0000673 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000674
Ted Kremenek5a429952008-04-23 23:35:07 +0000675 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
676
677 // FIXME: Maybe this should be an assertion. Are there cases
678 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000679 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000680 if (D) {
681 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000682 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000683 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000684 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000685
686 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000687
Ted Kremenek5a429952008-04-23 23:35:07 +0000688 // Not an enum.
689 Expr* CondE = cast<SwitchStmt>(T)->getCond();
690 unsigned bits = Ctx.getTypeSize(CondE->getType());
691 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000692
Ted Kremenek5a429952008-04-23 23:35:07 +0000693 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
694 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000695 continue;
696 }
697
Ted Kremenek297308e2009-02-10 23:56:07 +0000698 os << V;
699 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000700
Ted Kremenek61f3e052008-04-03 04:42:52 +0000701 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000702 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000703
704 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000705 }
706 }
Ted Kremenek56783922008-04-25 01:29:56 +0000707 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000708 os << "'Default' branch taken. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000709 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek56783922008-04-25 01:29:56 +0000710 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000711
712 PD.push_front(new PathDiagnosticPiece(L, os.str()));
713 break;
714 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000715
716 case Stmt::BreakStmtClass:
717 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000718 std::string sbuf;
719 llvm::raw_string_ostream os(sbuf);
Ted Kremenekb479dad2009-02-23 23:13:51 +0000720 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000721 PD.push_front(new PathDiagnosticPiece(L, os.str()));
722 break;
723 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000724
725 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000726 std::string sbuf;
727 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000728 os << "'?' condition evaluates to ";
729
730 if (*(Src->succ_begin()+1) == Dst)
731 os << "false.";
732 else
733 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000734
Ted Kremenek297308e2009-02-10 23:56:07 +0000735 PD.push_front(new PathDiagnosticPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000736 break;
737 }
738
739 case Stmt::DoStmtClass: {
740
741 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000742 std::string sbuf;
743 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000744
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000745 os << "Loop condition is true. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000746 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000747
748 PD.push_front(new PathDiagnosticPiece(L, os.str()));
749 }
750 else
751 PD.push_front(new PathDiagnosticPiece(L,
752 "Loop condition is false. Exiting loop."));
753
754 break;
755 }
756
Ted Kremenek61f3e052008-04-03 04:42:52 +0000757 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000758 case Stmt::ForStmtClass: {
759
760 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000761 std::string sbuf;
762 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000763
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000764 os << "Loop condition is false. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000765 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek297308e2009-02-10 23:56:07 +0000766
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000767 PD.push_front(new PathDiagnosticPiece(L, os.str()));
768 }
769 else
770 PD.push_front(new PathDiagnosticPiece(L,
771 "Loop condition is true. Entering loop body."));
772
773 break;
774 }
775
Ted Kremenek297308e2009-02-10 23:56:07 +0000776 case Stmt::IfStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000777 if (*(Src->succ_begin()+1) == Dst)
778 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
779 else
780 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
781
782 break;
783 }
784 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000785 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000786
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000787 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
788 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000789 PD.push_front(p);
790
Ted Kremenek9e240492008-10-04 05:50:14 +0000791 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
792 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000793 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000794 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
795 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000796 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000797 }
798}
799
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000800
Ted Kremenekcf118d42009-02-04 23:49:09 +0000801void BugReporter::Register(BugType *BT) {
802 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000803}
804
Ted Kremenekcf118d42009-02-04 23:49:09 +0000805void BugReporter::EmitReport(BugReport* R) {
806 // Compute the bug report's hash to determine its equivalence class.
807 llvm::FoldingSetNodeID ID;
808 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000809
Ted Kremenekcf118d42009-02-04 23:49:09 +0000810 // Lookup the equivance class. If there isn't one, create it.
811 BugType& BT = R->getBugType();
812 Register(&BT);
813 void *InsertPos;
814 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
815
816 if (!EQ) {
817 EQ = new BugReportEquivClass(R);
818 BT.EQClasses.InsertNode(EQ, InsertPos);
819 }
820 else
821 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000822}
823
Ted Kremenekcf118d42009-02-04 23:49:09 +0000824void BugReporter::FlushReport(BugReportEquivClass& EQ) {
825 assert(!EQ.Reports.empty());
826 BugReport &R = **EQ.begin();
827
828 // FIXME: Make sure we use the 'R' for the path that was actually used.
829 // Probably doesn't make a difference in practice.
830 BugType& BT = R.getBugType();
831
832 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
833 R.getDescription(),
834 BT.getCategory()));
835 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000836
837 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000838 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000839 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000840
Ted Kremenek3148eb42009-01-24 00:55:43 +0000841 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000842 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000843 const SourceRange *Beg = 0, *End = 0;
844 R.getRanges(*this, Beg, End);
845 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000846 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000847 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
848 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000849
Ted Kremenek3148eb42009-01-24 00:55:43 +0000850 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000851 default: assert(0 && "Don't handle this many ranges yet!");
852 case 0: Diag.Report(L, ErrorDiag); break;
853 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
854 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
855 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000856 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000857
858 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
859 if (!PD)
860 return;
861
862 if (D->empty()) {
863 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
864 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
865 D->push_back(piece);
866 }
867
868 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000869}
Ted Kremenek57202072008-07-14 17:40:50 +0000870
Ted Kremenek8c036c72008-09-20 04:23:38 +0000871void BugReporter::EmitBasicReport(const char* name, const char* str,
872 SourceLocation Loc,
873 SourceRange* RBeg, unsigned NumRanges) {
874 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
875}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000876
Ted Kremenek8c036c72008-09-20 04:23:38 +0000877void BugReporter::EmitBasicReport(const char* name, const char* category,
878 const char* str, SourceLocation Loc,
879 SourceRange* RBeg, unsigned NumRanges) {
880
Ted Kremenekcf118d42009-02-04 23:49:09 +0000881 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
882 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000883 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000884 RangedBugReport *R = new DiagBugReport(*BT, str, L);
885 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
886 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000887}