blob: c42218e506c9c6124b26235a6374c34738294007 [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#include <sstream>
28
29using namespace clang;
30
Ted Kremenekcf118d42009-02-04 23:49:09 +000031//===----------------------------------------------------------------------===//
32// static functions.
33//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35static inline Stmt* GetStmt(const ProgramPoint& P) {
36 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
37 return PS->getStmt();
38 }
39 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
40 return BE->getSrc()->getTerminator();
41 }
42 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
43 return BE->getFirstStmt();
44 }
45
46 assert (false && "Unsupported ProgramPoint.");
47 return NULL;
48}
49
Ted Kremenek706e3cf2008-04-07 23:35:17 +000050static inline Stmt* GetStmt(const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +000051 if (B->empty())
Ted Kremenek2673c9f2008-04-25 19:01:27 +000052 return const_cast<Stmt*>(B->getTerminator());
Ted Kremenek2673c9f2008-04-25 19:01:27 +000053 else
54 return (*B)[0];
Ted Kremenek706e3cf2008-04-07 23:35:17 +000055}
56
Ted Kremenek3148eb42009-01-24 00:55:43 +000057static inline const ExplodedNode<GRState>*
58GetNextNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000059 return N->pred_empty() ? NULL : *(N->pred_begin());
60}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000061
Ted Kremenek3148eb42009-01-24 00:55:43 +000062static Stmt* GetLastStmt(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000063 assert (isa<BlockEntrance>(N->getLocation()));
64
65 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000066 ProgramPoint P = N->getLocation();
Ted Kremenek3148eb42009-01-24 00:55:43 +000067 if (PostStmt* PS = dyn_cast<PostStmt>(&P)) return PS->getStmt();
Ted Kremenekbd7efa82008-04-17 23:44:37 +000068 }
69
70 return NULL;
71}
72
Ted Kremenek3148eb42009-01-24 00:55:43 +000073static inline Stmt* GetStmt(const ExplodedNode<GRState>* N) {
74 ProgramPoint ProgP = N->getLocation();
75 return isa<BlockEntrance>(ProgP) ? GetLastStmt(N) : GetStmt(ProgP);
76}
77
Ted Kremenek143ca222008-05-06 18:11:09 +000078static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr,
Ted Kremenek3148eb42009-01-24 00:55:43 +000079 const Stmt* S) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000080
81 if (!S)
82 return;
Ted Kremenek143ca222008-05-06 18:11:09 +000083
84 // Slow, but probably doesn't matter.
85 if (os.str().empty())
86 os << ' ';
87
88 os << "Execution continues on line "
Ted Kremenekcf118d42009-02-04 23:49:09 +000089 << SMgr.getInstantiationLineNumber(S->getLocStart()) << '.';
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000090}
Ted Kremenek143ca222008-05-06 18:11:09 +000091
Ted Kremenek143ca222008-05-06 18:11:09 +000092static inline void ExecutionContinues(std::ostringstream& os,
93 SourceManager& SMgr,
Ted Kremenek3148eb42009-01-24 00:55:43 +000094 const ExplodedNode<GRState>* N) {
Ted Kremenek143ca222008-05-06 18:11:09 +000095 ExecutionContinues(os, SMgr, GetStmt(N->getLocation()));
96}
97
98static inline void ExecutionContinues(std::ostringstream& os,
99 SourceManager& SMgr,
100 const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +0000101 ExecutionContinues(os, SMgr, GetStmt(B));
102}
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)) {
121 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetLastStmt(EndNode);
122 }
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();
154 beg = &R;
155 end = beg+1;
156 }
157 else
158 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000159}
160
Ted Kremenekcf118d42009-02-04 23:49:09 +0000161SourceLocation BugReport::getLocation() const {
162 if (EndNode)
163 if (Stmt* S = GetStmt(EndNode))
164 return S->getLocStart();
165
166 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000167}
168
Ted Kremenek3148eb42009-01-24 00:55:43 +0000169PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
170 const ExplodedNode<GRState>* PrevN,
171 const ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000172 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000173 return NULL;
174}
175
Ted Kremenekcf118d42009-02-04 23:49:09 +0000176//===----------------------------------------------------------------------===//
177// Methods for BugReporter and subclasses.
178//===----------------------------------------------------------------------===//
179
180BugReportEquivClass::~BugReportEquivClass() {
181 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
182}
183
184GRBugReporter::~GRBugReporter() { FlushReports(); }
185BugReporterData::~BugReporterData() {}
186
187ExplodedGraph<GRState>&
188GRBugReporter::getGraph() { return Eng.getGraph(); }
189
190GRStateManager&
191GRBugReporter::getStateManager() { return Eng.getStateManager(); }
192
193BugReporter::~BugReporter() { FlushReports(); }
194
195void BugReporter::FlushReports() {
196 if (BugTypes.isEmpty())
197 return;
198
199 // First flush the warnings for each BugType. This may end up creating new
200 // warnings and new BugTypes. Because ImmutableSet is a functional data
201 // structure, we do not need to worry about the iterators being invalidated.
202 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
203 const_cast<BugType*>(*I)->FlushReports(*this);
204
205 // Iterate through BugTypes a second time. BugTypes may have been updated
206 // with new BugType objects and new warnings.
207 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
208 BugType *BT = const_cast<BugType*>(*I);
209
210 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
211 SetTy& EQClasses = BT->EQClasses;
212
213 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
214 BugReportEquivClass& EQ = *EI;
215 FlushReport(EQ);
216 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000217
Ted Kremenekcf118d42009-02-04 23:49:09 +0000218 // Delete the BugType object. This will also delete the equivalence
219 // classes.
220 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000221 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000222
223 // Remove all references to the BugType objects.
224 BugTypes = F.GetEmptySet();
225}
226
227//===----------------------------------------------------------------------===//
228// PathDiagnostics generation.
229//===----------------------------------------------------------------------===//
230
231static std::pair<ExplodedGraph<GRState>*,
232 std::pair<ExplodedNode<GRState>*, unsigned> >
233MakeReportGraph(const ExplodedGraph<GRState>* G,
234 const ExplodedNode<GRState>** NStart,
235 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000236
Ted Kremenekcf118d42009-02-04 23:49:09 +0000237 // Create the trimmed graph. It will contain the shortest paths from the
238 // error nodes to the root. In the new graph we should only have one
239 // error node unless there are two or more error nodes with the same minimum
240 // path length.
241 ExplodedGraph<GRState>* GTrim;
242 InterExplodedGraphMap<GRState>* NMap;
243 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd);
244
245 // Create owning pointers for GTrim and NMap just to ensure that they are
246 // released when this function exists.
247 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
248 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
249
250 // Find the (first) error node in the trimmed graph. We just need to consult
251 // the node map (NMap) which maps from nodes in the original graph to nodes
252 // in the new graph.
253 const ExplodedNode<GRState>* N = 0;
254 unsigned NodeIndex = 0;
255
256 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
257 if ((N = NMap->getMappedNode(*I))) {
258 NodeIndex = (I - NStart) / sizeof(*I);
259 break;
260 }
261
262 assert(N && "No error node found in the trimmed graph.");
263
264 // Create a new (third!) graph with a single path. This is the graph
265 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000266 ExplodedGraph<GRState> *GNew =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000267 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
268 GTrim->getContext());
269
270 // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000271 // to the root node, and then construct a new graph that contains only
272 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000273 llvm::DenseMap<const void*,unsigned> Visited;
274 llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000275 WS.push_back(N);
276 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000277 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000278
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000279 while (!WS.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000280 const ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000281 WS.pop_back();
282
283 if (Visited.find(Node) != Visited.end())
284 continue;
285
286 Visited[Node] = cnt++;
287
288 if (Node->pred_empty()) {
289 Root = Node;
290 break;
291 }
292
Ted Kremenek3148eb42009-01-24 00:55:43 +0000293 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000294 E=Node->pred_end(); I!=E; ++I)
295 WS.push_back(*I);
296 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000297
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000298 assert (Root);
299
300 // Now walk from the root down the DFS path, always taking the successor
301 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000302 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000303
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000304 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000305 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000306 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000307 assert (I != Visited.end());
308
309 // Create the equivalent node in the new graph with the same state
310 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000311 ExplodedNode<GRState>* NewN =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000312 GNew->getNode(N->getLocation(), N->getState());
313
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000314 // Link up the new node with the previous node.
315 if (Last)
316 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000317
318 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000319
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000320 // Are we at the final node?
321 if (I->second == 0) {
322 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000323 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000324 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000325
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000326 // Find the next successor node. We choose the node that is marked
327 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000328 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
329 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000330 N = 0;
331
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000332 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000333
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000334 I = Visited.find(*SI);
335
336 if (I == Visited.end())
337 continue;
338
339 if (!N || I->second < MinVal) {
340 N = *SI;
341 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000342 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000343 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000344
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000345 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000346 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000347
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000348 assert (First);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000349 return std::make_pair(GNew, std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000350}
351
Ted Kremenek3148eb42009-01-24 00:55:43 +0000352static const VarDecl*
353GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
354 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000355
356 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
357
358 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000359
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000360 if (!isa<PostStmt>(P))
361 continue;
362
363 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000364
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000365 if (!DR)
366 continue;
367
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000368 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000369
370 if (X != Y)
371 continue;
372
373 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
374
375 if (!VD)
376 continue;
377
378 return VD;
379 }
380
381 return 0;
382}
383
Ted Kremenek9e240492008-10-04 05:50:14 +0000384namespace {
385class VISIBILITY_HIDDEN NotableSymbolHandler
386 : public StoreManager::BindingsHandler {
387
Ted Kremenek2dabd432008-12-05 02:27:51 +0000388 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000389 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000390 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000391 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000392 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000393 PathDiagnostic& PD;
394 BugReporter& BR;
395
396public:
397
Ted Kremenek3148eb42009-01-24 00:55:43 +0000398 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
399 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000400 PathDiagnostic& pd, BugReporter& br)
401 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
402
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000403 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000404
Ted Kremenek2dabd432008-12-05 02:27:51 +0000405 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000406
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000407 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000408 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000409 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000410 ScanSym = SV->getSymbol();
411 else
412 return true;
413
414 if (ScanSym != Sym)
415 return true;
416
417 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000418 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000419
420 if (X == V) // Same binding?
421 return true;
422
423 // Different binding. Only handle assignments for now. We don't pull
424 // this check out of the loop because we will eventually handle other
425 // cases.
426
427 VarDecl *VD = 0;
428
Ted Kremenek3148eb42009-01-24 00:55:43 +0000429 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000430 if (!B->isAssignmentOp())
431 return true;
432
433 // What variable did we assign to?
434 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
435
436 if (!DR)
437 return true;
438
439 VD = dyn_cast<VarDecl>(DR->getDecl());
440 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000441 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000442 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
443 // assume that each DeclStmt has a single Decl. This invariant
444 // holds by contruction in the CFG.
445 VD = dyn_cast<VarDecl>(*DS->decl_begin());
446 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000447
448 if (!VD)
449 return true;
450
451 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000452 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000453
454 if (!MostRecent)
455 return true;
456
457 // Create the diagnostic.
458
459 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
460
461 if (VD->getType()->isPointerLikeType()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000462 std::string msg = "'" + std::string(VD->getNameAsString()) +
463 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000464
465 PD.push_front(new PathDiagnosticPiece(L, msg));
466 }
467
468 return true;
469 }
470};
471}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000472
Ted Kremenek3148eb42009-01-24 00:55:43 +0000473static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
474 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000475 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000476 PathDiagnostic& PD) {
477
Ted Kremenek3148eb42009-01-24 00:55:43 +0000478 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000479 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000480
481 if (!PrevSt)
482 return;
483
Ted Kremenek9e240492008-10-04 05:50:14 +0000484 // Look at the region bindings of the current state that map to the
485 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000486 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000487 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
488 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
489}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000490
Ted Kremenek9e240492008-10-04 05:50:14 +0000491namespace {
492class VISIBILITY_HIDDEN ScanNotableSymbols
493 : public StoreManager::BindingsHandler {
494
Ted Kremenek2dabd432008-12-05 02:27:51 +0000495 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000496 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000497 Stmt* S;
498 GRBugReporter& BR;
499 PathDiagnostic& PD;
500
501public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000502 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000503 PathDiagnostic& pd)
504 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000505
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000506 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000507 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000508
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000509 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000510 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000511 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000512 ScanSym = SV->getSymbol();
513 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000514 return true;
515
516 assert (ScanSym.isInitialized());
517
518 if (!BR.isNotable(ScanSym))
519 return true;
520
521 if (AlreadyProcessed.count(ScanSym))
522 return true;
523
524 AlreadyProcessed.insert(ScanSym);
525
526 HandleNotableSymbol(N, S, ScanSym, BR, PD);
527 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000528 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000529};
530} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000531
Ted Kremenekc0959972008-07-02 21:24:01 +0000532void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000533 BugReportEquivClass& EQ) {
534
535 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000536
Ted Kremenekcf118d42009-02-04 23:49:09 +0000537 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
538 const ExplodedNode<GRState>* N = I->getEndNode();
539 if (N) Nodes.push_back(N);
540 }
541
542 if (Nodes.empty())
543 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000544
545 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000546 // node to a root.
547 const std::pair<ExplodedGraph<GRState>*,
548 std::pair<ExplodedNode<GRState>*, unsigned> >&
549 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000550
Ted Kremenekcf118d42009-02-04 23:49:09 +0000551 // Find the BugReport with the original location.
552 BugReport *R = 0;
553 unsigned i = 0;
554 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
555 if (i == GPair.second.second) { R = *I; break; }
556
557 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000558
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000559 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000560 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000561
Ted Kremenekcf118d42009-02-04 23:49:09 +0000562 // Start building the path diagnostic...
563 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000564 PD.push_back(Piece);
565 else
566 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000567
Ted Kremenek3148eb42009-01-24 00:55:43 +0000568 const ExplodedNode<GRState>* NextNode = N->pred_empty()
569 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000570
Ted Kremenekc0959972008-07-02 21:24:01 +0000571 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000572 SourceManager& SMgr = Ctx.getSourceManager();
573
Ted Kremenek6837faa2008-04-09 00:20:43 +0000574 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000575
Ted Kremenek3148eb42009-01-24 00:55:43 +0000576 const ExplodedNode<GRState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000577 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000578 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000579
580 ProgramPoint P = N->getLocation();
581
582 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
583
584 CFGBlock* Src = BE->getSrc();
585 CFGBlock* Dst = BE->getDst();
586
587 Stmt* T = Src->getTerminator();
588
589 if (!T)
590 continue;
591
592 FullSourceLoc L(T->getLocStart(), SMgr);
593
594 switch (T->getStmtClass()) {
595 default:
596 break;
597
598 case Stmt::GotoStmtClass:
599 case Stmt::IndirectGotoStmtClass: {
600
601 Stmt* S = GetStmt(LastNode->getLocation());
602
603 if (!S)
604 continue;
605
606 std::ostringstream os;
607
608 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000609 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000610
611 PD.push_front(new PathDiagnosticPiece(L, os.str()));
612 break;
613 }
614
615 case Stmt::SwitchStmtClass: {
616
617 // Figure out what case arm we took.
Ted Kremenek5a429952008-04-23 23:35:07 +0000618
Ted Kremenek61f3e052008-04-03 04:42:52 +0000619 std::ostringstream os;
Ted Kremenek5a429952008-04-23 23:35:07 +0000620
621 if (Stmt* S = Dst->getLabel())
622 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000623 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000624 os << "No cases match in the switch statement. "
625 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000626 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000627 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000628 case Stmt::DefaultStmtClass:
629 os << "Control jumps to the 'default' case at line "
630 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
631 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000632
633 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000634 os << "Control jumps to 'case ";
635
Ted Kremenek5a429952008-04-23 23:35:07 +0000636 CaseStmt* Case = cast<CaseStmt>(S);
637 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000638
Ted Kremenek5a429952008-04-23 23:35:07 +0000639 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000640
Ted Kremenek5a429952008-04-23 23:35:07 +0000641 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000642
Ted Kremenek5a429952008-04-23 23:35:07 +0000643 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
644
645 // FIXME: Maybe this should be an assertion. Are there cases
646 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000647 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000648 if (D) {
649 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000650 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000651 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000652 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000653
654 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000655
Ted Kremenek5a429952008-04-23 23:35:07 +0000656 // Not an enum.
657 Expr* CondE = cast<SwitchStmt>(T)->getCond();
658 unsigned bits = Ctx.getTypeSize(CondE->getType());
659 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000660
Ted Kremenek5a429952008-04-23 23:35:07 +0000661 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
662 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000663 continue;
664 }
665
Chris Lattner405674c2008-08-23 22:23:37 +0000666 llvm::raw_os_ostream OS(os);
667 OS << V;
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000668 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000669
Ted Kremenek61f3e052008-04-03 04:42:52 +0000670 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000671 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000672
673 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000674 }
675 }
Ted Kremenek56783922008-04-25 01:29:56 +0000676 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000677 os << "'Default' branch taken. ";
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000678 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000679 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000680
681 PD.push_front(new PathDiagnosticPiece(L, os.str()));
682 break;
683 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000684
685 case Stmt::BreakStmtClass:
686 case Stmt::ContinueStmtClass: {
687 std::ostringstream os;
688 ExecutionContinues(os, SMgr, LastNode);
689 PD.push_front(new PathDiagnosticPiece(L, os.str()));
690 break;
691 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000692
693 case Stmt::ConditionalOperatorClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000694
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000695 std::ostringstream os;
696 os << "'?' condition evaluates to ";
697
698 if (*(Src->succ_begin()+1) == Dst)
699 os << "false.";
700 else
701 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000702
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000703 PD.push_front(new PathDiagnosticPiece(L, os.str()));
704
705 break;
706 }
707
708 case Stmt::DoStmtClass: {
709
710 if (*(Src->succ_begin()) == Dst) {
711
712 std::ostringstream os;
713
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000714 os << "Loop condition is true. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000715 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000716
717 PD.push_front(new PathDiagnosticPiece(L, os.str()));
718 }
719 else
720 PD.push_front(new PathDiagnosticPiece(L,
721 "Loop condition is false. Exiting loop."));
722
723 break;
724 }
725
Ted Kremenek61f3e052008-04-03 04:42:52 +0000726 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000727 case Stmt::ForStmtClass: {
728
729 if (*(Src->succ_begin()+1) == Dst) {
730
731 std::ostringstream os;
732
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000733 os << "Loop condition is false. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000734 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000735
736 PD.push_front(new PathDiagnosticPiece(L, os.str()));
737 }
738 else
739 PD.push_front(new PathDiagnosticPiece(L,
740 "Loop condition is true. Entering loop body."));
741
742 break;
743 }
744
Ted Kremenek61f3e052008-04-03 04:42:52 +0000745 case Stmt::IfStmtClass: {
746
747 if (*(Src->succ_begin()+1) == Dst)
748 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
749 else
750 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
751
752 break;
753 }
754 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000755 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000756
Ted Kremenekcf118d42009-02-04 23:49:09 +0000757 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000758 PD.push_front(p);
759
Ted Kremenek9e240492008-10-04 05:50:14 +0000760 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
761 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000762 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000763 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
764 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000765 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000766 }
767}
768
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000769
Ted Kremenekcf118d42009-02-04 23:49:09 +0000770void BugReporter::Register(BugType *BT) {
771 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000772}
773
Ted Kremenekcf118d42009-02-04 23:49:09 +0000774void BugReporter::EmitReport(BugReport* R) {
775 // Compute the bug report's hash to determine its equivalence class.
776 llvm::FoldingSetNodeID ID;
777 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000778
Ted Kremenekcf118d42009-02-04 23:49:09 +0000779 // Lookup the equivance class. If there isn't one, create it.
780 BugType& BT = R->getBugType();
781 Register(&BT);
782 void *InsertPos;
783 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
784
785 if (!EQ) {
786 EQ = new BugReportEquivClass(R);
787 BT.EQClasses.InsertNode(EQ, InsertPos);
788 }
789 else
790 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000791}
792
Ted Kremenekcf118d42009-02-04 23:49:09 +0000793void BugReporter::FlushReport(BugReportEquivClass& EQ) {
794 assert(!EQ.Reports.empty());
795 BugReport &R = **EQ.begin();
796
797 // FIXME: Make sure we use the 'R' for the path that was actually used.
798 // Probably doesn't make a difference in practice.
799 BugType& BT = R.getBugType();
800
801 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
802 R.getDescription(),
803 BT.getCategory()));
804 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000805
806 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000807 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000808 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000809
Ted Kremenek3148eb42009-01-24 00:55:43 +0000810 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000811 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000812 const SourceRange *Beg = 0, *End = 0;
813 R.getRanges(*this, Beg, End);
814 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000815 FullSourceLoc L(R.getLocation(), getSourceManager());
816 const std::string &msg = PD ? R.getBugType().getName() : R.getDescription();
817 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, msg.c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000818
Ted Kremenek3148eb42009-01-24 00:55:43 +0000819 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000820 default: assert(0 && "Don't handle this many ranges yet!");
821 case 0: Diag.Report(L, ErrorDiag); break;
822 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
823 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
824 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000825 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000826
827 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
828 if (!PD)
829 return;
830
831 if (D->empty()) {
832 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
833 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
834 D->push_back(piece);
835 }
836
837 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000838}
Ted Kremenek57202072008-07-14 17:40:50 +0000839
Ted Kremenek8c036c72008-09-20 04:23:38 +0000840void BugReporter::EmitBasicReport(const char* name, const char* str,
841 SourceLocation Loc,
842 SourceRange* RBeg, unsigned NumRanges) {
843 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
844}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000845
Ted Kremenek8c036c72008-09-20 04:23:38 +0000846void BugReporter::EmitBasicReport(const char* name, const char* category,
847 const char* str, SourceLocation Loc,
848 SourceRange* RBeg, unsigned NumRanges) {
849
Ted Kremenekcf118d42009-02-04 23:49:09 +0000850 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
851 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000852 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000853 RangedBugReport *R = new DiagBugReport(*BT, str, L);
854 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
855 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000856}