blob: cf1d1b774dbaa6eb0c182acd4bee1a64a528d132 [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
34static inline Stmt* GetStmt(const ProgramPoint& P) {
35 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
36 return PS->getStmt();
37 }
38 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
39 return BE->getSrc()->getTerminator();
40 }
41 else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
42 return BE->getFirstStmt();
43 }
44
45 assert (false && "Unsupported ProgramPoint.");
46 return NULL;
47}
48
Ted Kremenek706e3cf2008-04-07 23:35:17 +000049static inline Stmt* GetStmt(const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +000050 if (B->empty())
Ted Kremenek2673c9f2008-04-25 19:01:27 +000051 return const_cast<Stmt*>(B->getTerminator());
Ted Kremenek2673c9f2008-04-25 19:01:27 +000052 else
53 return (*B)[0];
Ted Kremenek706e3cf2008-04-07 23:35:17 +000054}
55
Ted Kremenek3148eb42009-01-24 00:55:43 +000056static inline const ExplodedNode<GRState>*
57GetNextNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000058 return N->pred_empty() ? NULL : *(N->pred_begin());
59}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000060
Ted Kremenek3148eb42009-01-24 00:55:43 +000061static Stmt* GetLastStmt(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000062 assert (isa<BlockEntrance>(N->getLocation()));
63
64 for (N = GetNextNode(N); N; N = GetNextNode(N)) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000065 ProgramPoint P = N->getLocation();
Ted Kremenek3148eb42009-01-24 00:55:43 +000066 if (PostStmt* PS = dyn_cast<PostStmt>(&P)) return PS->getStmt();
Ted Kremenekbd7efa82008-04-17 23:44:37 +000067 }
68
69 return NULL;
70}
71
Ted Kremenek3148eb42009-01-24 00:55:43 +000072static inline Stmt* GetStmt(const ExplodedNode<GRState>* N) {
73 ProgramPoint ProgP = N->getLocation();
74 return isa<BlockEntrance>(ProgP) ? GetLastStmt(N) : GetStmt(ProgP);
75}
76
Ted Kremenek297308e2009-02-10 23:56:07 +000077static void ExecutionContinues(llvm::raw_string_ostream& os,
78 SourceManager& SMgr,
79 const Stmt* S) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000080 if (!S)
81 return;
Ted Kremenek143ca222008-05-06 18:11:09 +000082
83 // Slow, but probably doesn't matter.
Ted Kremenek297308e2009-02-10 23:56:07 +000084 if (os.str().empty()) os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +000085
86 os << "Execution continues on line "
Ted Kremenek297308e2009-02-10 23:56:07 +000087 << SMgr.getInstantiationLineNumber(S->getLocStart()) << '.';
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000088}
Ted Kremenek143ca222008-05-06 18:11:09 +000089
Ted Kremenek297308e2009-02-10 23:56:07 +000090static inline void ExecutionContinues(llvm::raw_string_ostream& os,
Ted Kremenek143ca222008-05-06 18:11:09 +000091 SourceManager& SMgr,
Ted Kremenek3148eb42009-01-24 00:55:43 +000092 const ExplodedNode<GRState>* N) {
Ted Kremenek143ca222008-05-06 18:11:09 +000093 ExecutionContinues(os, SMgr, GetStmt(N->getLocation()));
94}
95
Ted Kremenek297308e2009-02-10 23:56:07 +000096static inline void ExecutionContinues(llvm::raw_string_ostream& os,
Ted Kremenek143ca222008-05-06 18:11:09 +000097 SourceManager& SMgr,
98 const CFGBlock* B) {
Ted Kremenek143ca222008-05-06 18:11:09 +000099 ExecutionContinues(os, SMgr, GetStmt(B));
100}
101
Ted Kremenekcf118d42009-02-04 23:49:09 +0000102//===----------------------------------------------------------------------===//
103// Methods for BugType and subclasses.
104//===----------------------------------------------------------------------===//
105BugType::~BugType() {}
106void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000107
Ted Kremenekcf118d42009-02-04 23:49:09 +0000108//===----------------------------------------------------------------------===//
109// Methods for BugReport and subclasses.
110//===----------------------------------------------------------------------===//
111BugReport::~BugReport() {}
112RangedBugReport::~RangedBugReport() {}
113
114Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000115 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000116 Stmt *S = NULL;
117
Ted Kremenekcf118d42009-02-04 23:49:09 +0000118 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
119 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetLastStmt(EndNode);
120 }
121 if (!S) S = GetStmt(ProgP);
122
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000123 return S;
124}
125
126PathDiagnosticPiece*
127BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000128 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000129
130 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000131
132 if (!S)
133 return NULL;
134
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000135 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
136 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000137
Ted Kremenekde7161f2008-04-03 18:00:37 +0000138 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000139 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000140
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000141 for (; Beg != End; ++Beg)
142 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000143
144 return P;
145}
146
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000147void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
148 const SourceRange*& end) {
149
150 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
151 R = E->getSourceRange();
152 beg = &R;
153 end = beg+1;
154 }
155 else
156 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000157}
158
Ted Kremenekcf118d42009-02-04 23:49:09 +0000159SourceLocation BugReport::getLocation() const {
160 if (EndNode)
161 if (Stmt* S = GetStmt(EndNode))
162 return S->getLocStart();
163
164 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000165}
166
Ted Kremenek3148eb42009-01-24 00:55:43 +0000167PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
168 const ExplodedNode<GRState>* PrevN,
169 const ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +0000170 BugReporter& BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000171 return NULL;
172}
173
Ted Kremenekcf118d42009-02-04 23:49:09 +0000174//===----------------------------------------------------------------------===//
175// Methods for BugReporter and subclasses.
176//===----------------------------------------------------------------------===//
177
178BugReportEquivClass::~BugReportEquivClass() {
179 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
180}
181
182GRBugReporter::~GRBugReporter() { FlushReports(); }
183BugReporterData::~BugReporterData() {}
184
185ExplodedGraph<GRState>&
186GRBugReporter::getGraph() { return Eng.getGraph(); }
187
188GRStateManager&
189GRBugReporter::getStateManager() { return Eng.getStateManager(); }
190
191BugReporter::~BugReporter() { FlushReports(); }
192
193void BugReporter::FlushReports() {
194 if (BugTypes.isEmpty())
195 return;
196
197 // First flush the warnings for each BugType. This may end up creating new
198 // warnings and new BugTypes. Because ImmutableSet is a functional data
199 // structure, we do not need to worry about the iterators being invalidated.
200 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
201 const_cast<BugType*>(*I)->FlushReports(*this);
202
203 // Iterate through BugTypes a second time. BugTypes may have been updated
204 // with new BugType objects and new warnings.
205 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
206 BugType *BT = const_cast<BugType*>(*I);
207
208 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
209 SetTy& EQClasses = BT->EQClasses;
210
211 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
212 BugReportEquivClass& EQ = *EI;
213 FlushReport(EQ);
214 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000215
Ted Kremenekcf118d42009-02-04 23:49:09 +0000216 // Delete the BugType object. This will also delete the equivalence
217 // classes.
218 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000219 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000220
221 // Remove all references to the BugType objects.
222 BugTypes = F.GetEmptySet();
223}
224
225//===----------------------------------------------------------------------===//
226// PathDiagnostics generation.
227//===----------------------------------------------------------------------===//
228
229static std::pair<ExplodedGraph<GRState>*,
230 std::pair<ExplodedNode<GRState>*, unsigned> >
231MakeReportGraph(const ExplodedGraph<GRState>* G,
232 const ExplodedNode<GRState>** NStart,
233 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000234
Ted Kremenekcf118d42009-02-04 23:49:09 +0000235 // Create the trimmed graph. It will contain the shortest paths from the
236 // error nodes to the root. In the new graph we should only have one
237 // error node unless there are two or more error nodes with the same minimum
238 // path length.
239 ExplodedGraph<GRState>* GTrim;
240 InterExplodedGraphMap<GRState>* NMap;
241 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd);
242
243 // Create owning pointers for GTrim and NMap just to ensure that they are
244 // released when this function exists.
245 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
246 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
247
248 // Find the (first) error node in the trimmed graph. We just need to consult
249 // the node map (NMap) which maps from nodes in the original graph to nodes
250 // in the new graph.
251 const ExplodedNode<GRState>* N = 0;
252 unsigned NodeIndex = 0;
253
254 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
255 if ((N = NMap->getMappedNode(*I))) {
256 NodeIndex = (I - NStart) / sizeof(*I);
257 break;
258 }
259
260 assert(N && "No error node found in the trimmed graph.");
261
262 // Create a new (third!) graph with a single path. This is the graph
263 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000264 ExplodedGraph<GRState> *GNew =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000265 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
266 GTrim->getContext());
267
268 // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000269 // to the root node, and then construct a new graph that contains only
270 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000271 llvm::DenseMap<const void*,unsigned> Visited;
272 llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000273 WS.push_back(N);
274 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000275 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000276
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000277 while (!WS.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000278 const ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000279 WS.pop_back();
280
281 if (Visited.find(Node) != Visited.end())
282 continue;
283
284 Visited[Node] = cnt++;
285
286 if (Node->pred_empty()) {
287 Root = Node;
288 break;
289 }
290
Ted Kremenek3148eb42009-01-24 00:55:43 +0000291 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000292 E=Node->pred_end(); I!=E; ++I)
293 WS.push_back(*I);
294 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000295
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000296 assert (Root);
297
298 // Now walk from the root down the DFS path, always taking the successor
299 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000300 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000301
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000302 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000303 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000304 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000305 assert (I != Visited.end());
306
307 // Create the equivalent node in the new graph with the same state
308 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000309 ExplodedNode<GRState>* NewN =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000310 GNew->getNode(N->getLocation(), N->getState());
311
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000312 // Link up the new node with the previous node.
313 if (Last)
314 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000315
316 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000317
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000318 // Are we at the final node?
319 if (I->second == 0) {
320 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000321 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000322 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000323
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000324 // Find the next successor node. We choose the node that is marked
325 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000326 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
327 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000328 N = 0;
329
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000330 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000331
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000332 I = Visited.find(*SI);
333
334 if (I == Visited.end())
335 continue;
336
337 if (!N || I->second < MinVal) {
338 N = *SI;
339 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000340 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000341 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000342
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000343 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000344 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000345
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000346 assert (First);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000347 return std::make_pair(GNew, std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000348}
349
Ted Kremenek3148eb42009-01-24 00:55:43 +0000350static const VarDecl*
351GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
352 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000353
354 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
355
356 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000357
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000358 if (!isa<PostStmt>(P))
359 continue;
360
361 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000362
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000363 if (!DR)
364 continue;
365
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000366 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000367
368 if (X != Y)
369 continue;
370
371 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
372
373 if (!VD)
374 continue;
375
376 return VD;
377 }
378
379 return 0;
380}
381
Ted Kremenek9e240492008-10-04 05:50:14 +0000382namespace {
383class VISIBILITY_HIDDEN NotableSymbolHandler
384 : public StoreManager::BindingsHandler {
385
Ted Kremenek2dabd432008-12-05 02:27:51 +0000386 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000387 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000388 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000389 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000390 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000391 PathDiagnostic& PD;
392 BugReporter& BR;
393
394public:
395
Ted Kremenek3148eb42009-01-24 00:55:43 +0000396 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
397 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000398 PathDiagnostic& pd, BugReporter& br)
399 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
400
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000401 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000402
Ted Kremenek2dabd432008-12-05 02:27:51 +0000403 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000404
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000405 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000406 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000407 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000408 ScanSym = SV->getSymbol();
409 else
410 return true;
411
412 if (ScanSym != Sym)
413 return true;
414
415 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000416 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000417
418 if (X == V) // Same binding?
419 return true;
420
421 // Different binding. Only handle assignments for now. We don't pull
422 // this check out of the loop because we will eventually handle other
423 // cases.
424
425 VarDecl *VD = 0;
426
Ted Kremenek3148eb42009-01-24 00:55:43 +0000427 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000428 if (!B->isAssignmentOp())
429 return true;
430
431 // What variable did we assign to?
432 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
433
434 if (!DR)
435 return true;
436
437 VD = dyn_cast<VarDecl>(DR->getDecl());
438 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000439 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000440 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
441 // assume that each DeclStmt has a single Decl. This invariant
442 // holds by contruction in the CFG.
443 VD = dyn_cast<VarDecl>(*DS->decl_begin());
444 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000445
446 if (!VD)
447 return true;
448
449 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000450 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000451
452 if (!MostRecent)
453 return true;
454
455 // Create the diagnostic.
456
457 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
458
459 if (VD->getType()->isPointerLikeType()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000460 std::string msg = "'" + std::string(VD->getNameAsString()) +
461 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000462
463 PD.push_front(new PathDiagnosticPiece(L, msg));
464 }
465
466 return true;
467 }
468};
469}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000470
Ted Kremenek3148eb42009-01-24 00:55:43 +0000471static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
472 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000473 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000474 PathDiagnostic& PD) {
475
Ted Kremenek3148eb42009-01-24 00:55:43 +0000476 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000477 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000478
479 if (!PrevSt)
480 return;
481
Ted Kremenek9e240492008-10-04 05:50:14 +0000482 // Look at the region bindings of the current state that map to the
483 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000484 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000485 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
486 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
487}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000488
Ted Kremenek9e240492008-10-04 05:50:14 +0000489namespace {
490class VISIBILITY_HIDDEN ScanNotableSymbols
491 : public StoreManager::BindingsHandler {
492
Ted Kremenek2dabd432008-12-05 02:27:51 +0000493 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000494 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000495 Stmt* S;
496 GRBugReporter& BR;
497 PathDiagnostic& PD;
498
499public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000500 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000501 PathDiagnostic& pd)
502 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000503
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000504 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000505 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000506
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000507 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000508 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000509 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000510 ScanSym = SV->getSymbol();
511 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000512 return true;
513
514 assert (ScanSym.isInitialized());
515
516 if (!BR.isNotable(ScanSym))
517 return true;
518
519 if (AlreadyProcessed.count(ScanSym))
520 return true;
521
522 AlreadyProcessed.insert(ScanSym);
523
524 HandleNotableSymbol(N, S, ScanSym, BR, PD);
525 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000526 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000527};
528} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000529
Ted Kremenekc0959972008-07-02 21:24:01 +0000530void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000531 BugReportEquivClass& EQ) {
532
533 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000534
Ted Kremenekcf118d42009-02-04 23:49:09 +0000535 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
536 const ExplodedNode<GRState>* N = I->getEndNode();
537 if (N) Nodes.push_back(N);
538 }
539
540 if (Nodes.empty())
541 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000542
543 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000544 // node to a root.
545 const std::pair<ExplodedGraph<GRState>*,
546 std::pair<ExplodedNode<GRState>*, unsigned> >&
547 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000548
Ted Kremenekcf118d42009-02-04 23:49:09 +0000549 // Find the BugReport with the original location.
550 BugReport *R = 0;
551 unsigned i = 0;
552 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
553 if (i == GPair.second.second) { R = *I; break; }
554
555 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000556
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000557 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000558 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000559
Ted Kremenekcf118d42009-02-04 23:49:09 +0000560 // Start building the path diagnostic...
561 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000562 PD.push_back(Piece);
563 else
564 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000565
Ted Kremenek3148eb42009-01-24 00:55:43 +0000566 const ExplodedNode<GRState>* NextNode = N->pred_empty()
567 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000568
Ted Kremenekc0959972008-07-02 21:24:01 +0000569 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000570 SourceManager& SMgr = Ctx.getSourceManager();
571
Ted Kremenek6837faa2008-04-09 00:20:43 +0000572 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000573
Ted Kremenek3148eb42009-01-24 00:55:43 +0000574 const ExplodedNode<GRState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000575 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000576 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000577
578 ProgramPoint P = N->getLocation();
579
580 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
581
582 CFGBlock* Src = BE->getSrc();
583 CFGBlock* Dst = BE->getDst();
584
585 Stmt* T = Src->getTerminator();
586
587 if (!T)
588 continue;
589
590 FullSourceLoc L(T->getLocStart(), SMgr);
591
592 switch (T->getStmtClass()) {
593 default:
594 break;
595
596 case Stmt::GotoStmtClass:
597 case Stmt::IndirectGotoStmtClass: {
598
599 Stmt* S = GetStmt(LastNode->getLocation());
600
601 if (!S)
602 continue;
603
Ted Kremenek297308e2009-02-10 23:56:07 +0000604 std::string sbuf;
605 llvm::raw_string_ostream os(sbuf);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000606
607 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000608 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000609
610 PD.push_front(new PathDiagnosticPiece(L, os.str()));
611 break;
612 }
613
Ted Kremenek297308e2009-02-10 23:56:07 +0000614 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000615 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000616 std::string sbuf;
617 llvm::raw_string_ostream os(sbuf);
Ted Kremenek5a429952008-04-23 23:35:07 +0000618
619 if (Stmt* S = Dst->getLabel())
620 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000621 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000622 os << "No cases match in the switch statement. "
623 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000624 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000625 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000626 case Stmt::DefaultStmtClass:
627 os << "Control jumps to the 'default' case at line "
628 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
629 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000630
631 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000632 os << "Control jumps to 'case ";
633
Ted Kremenek5a429952008-04-23 23:35:07 +0000634 CaseStmt* Case = cast<CaseStmt>(S);
635 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000636
Ted Kremenek5a429952008-04-23 23:35:07 +0000637 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000638
Ted Kremenek5a429952008-04-23 23:35:07 +0000639 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000640
Ted Kremenek5a429952008-04-23 23:35:07 +0000641 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
642
643 // FIXME: Maybe this should be an assertion. Are there cases
644 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000645 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000646 if (D) {
647 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000648 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000649 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000650 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000651
652 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000653
Ted Kremenek5a429952008-04-23 23:35:07 +0000654 // Not an enum.
655 Expr* CondE = cast<SwitchStmt>(T)->getCond();
656 unsigned bits = Ctx.getTypeSize(CondE->getType());
657 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000658
Ted Kremenek5a429952008-04-23 23:35:07 +0000659 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
660 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000661 continue;
662 }
663
Ted Kremenek297308e2009-02-10 23:56:07 +0000664 os << V;
665 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000666
Ted Kremenek61f3e052008-04-03 04:42:52 +0000667 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000668 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000669
670 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000671 }
672 }
Ted Kremenek56783922008-04-25 01:29:56 +0000673 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000674 os << "'Default' branch taken. ";
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000675 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000676 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000677
678 PD.push_front(new PathDiagnosticPiece(L, os.str()));
679 break;
680 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000681
682 case Stmt::BreakStmtClass:
683 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000684 std::string sbuf;
685 llvm::raw_string_ostream os(sbuf);
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000686 ExecutionContinues(os, SMgr, LastNode);
687 PD.push_front(new PathDiagnosticPiece(L, os.str()));
688 break;
689 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000690
691 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000692 std::string sbuf;
693 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000694 os << "'?' condition evaluates to ";
695
696 if (*(Src->succ_begin()+1) == Dst)
697 os << "false.";
698 else
699 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000700
Ted Kremenek297308e2009-02-10 23:56:07 +0000701 PD.push_front(new PathDiagnosticPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000702 break;
703 }
704
705 case Stmt::DoStmtClass: {
706
707 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000708 std::string sbuf;
709 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000710
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000711 os << "Loop condition is true. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000712 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000713
714 PD.push_front(new PathDiagnosticPiece(L, os.str()));
715 }
716 else
717 PD.push_front(new PathDiagnosticPiece(L,
718 "Loop condition is false. Exiting loop."));
719
720 break;
721 }
722
Ted Kremenek61f3e052008-04-03 04:42:52 +0000723 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000724 case Stmt::ForStmtClass: {
725
726 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000727 std::string sbuf;
728 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000729
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000730 os << "Loop condition is false. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000731 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek297308e2009-02-10 23:56:07 +0000732
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000733 PD.push_front(new PathDiagnosticPiece(L, os.str()));
734 }
735 else
736 PD.push_front(new PathDiagnosticPiece(L,
737 "Loop condition is true. Entering loop body."));
738
739 break;
740 }
741
Ted Kremenek297308e2009-02-10 23:56:07 +0000742 case Stmt::IfStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000743 if (*(Src->succ_begin()+1) == Dst)
744 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
745 else
746 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
747
748 break;
749 }
750 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000751 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000752
Ted Kremenekcf118d42009-02-04 23:49:09 +0000753 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000754 PD.push_front(p);
755
Ted Kremenek9e240492008-10-04 05:50:14 +0000756 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
757 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000758 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000759 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
760 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000761 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000762 }
763}
764
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000765
Ted Kremenekcf118d42009-02-04 23:49:09 +0000766void BugReporter::Register(BugType *BT) {
767 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000768}
769
Ted Kremenekcf118d42009-02-04 23:49:09 +0000770void BugReporter::EmitReport(BugReport* R) {
771 // Compute the bug report's hash to determine its equivalence class.
772 llvm::FoldingSetNodeID ID;
773 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000774
Ted Kremenekcf118d42009-02-04 23:49:09 +0000775 // Lookup the equivance class. If there isn't one, create it.
776 BugType& BT = R->getBugType();
777 Register(&BT);
778 void *InsertPos;
779 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
780
781 if (!EQ) {
782 EQ = new BugReportEquivClass(R);
783 BT.EQClasses.InsertNode(EQ, InsertPos);
784 }
785 else
786 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000787}
788
Ted Kremenekcf118d42009-02-04 23:49:09 +0000789void BugReporter::FlushReport(BugReportEquivClass& EQ) {
790 assert(!EQ.Reports.empty());
791 BugReport &R = **EQ.begin();
792
793 // FIXME: Make sure we use the 'R' for the path that was actually used.
794 // Probably doesn't make a difference in practice.
795 BugType& BT = R.getBugType();
796
797 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
798 R.getDescription(),
799 BT.getCategory()));
800 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000801
802 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000803 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000804 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000805
Ted Kremenek3148eb42009-01-24 00:55:43 +0000806 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000807 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000808 const SourceRange *Beg = 0, *End = 0;
809 R.getRanges(*this, Beg, End);
810 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000811 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000812 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
813 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000814
Ted Kremenek3148eb42009-01-24 00:55:43 +0000815 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000816 default: assert(0 && "Don't handle this many ranges yet!");
817 case 0: Diag.Report(L, ErrorDiag); break;
818 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
819 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
820 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000821 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000822
823 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
824 if (!PD)
825 return;
826
827 if (D->empty()) {
828 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
829 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
830 D->push_back(piece);
831 }
832
833 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000834}
Ted Kremenek57202072008-07-14 17:40:50 +0000835
Ted Kremenek8c036c72008-09-20 04:23:38 +0000836void BugReporter::EmitBasicReport(const char* name, const char* str,
837 SourceLocation Loc,
838 SourceRange* RBeg, unsigned NumRanges) {
839 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
840}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000841
Ted Kremenek8c036c72008-09-20 04:23:38 +0000842void BugReporter::EmitBasicReport(const char* name, const char* category,
843 const char* str, SourceLocation Loc,
844 SourceRange* RBeg, unsigned NumRanges) {
845
Ted Kremenekcf118d42009-02-04 23:49:09 +0000846 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
847 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000848 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000849 RangedBugReport *R = new DiagBugReport(*BT, str, L);
850 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
851 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000852}