blob: eb0b442bc15921f95b14fd2efc99ec98bc0c056c [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 Kremenekfe9e5432009-02-18 03:48:14 +0000170 BugReporter& BR,
171 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000172 return NULL;
173}
174
Ted Kremenekcf118d42009-02-04 23:49:09 +0000175//===----------------------------------------------------------------------===//
176// Methods for BugReporter and subclasses.
177//===----------------------------------------------------------------------===//
178
179BugReportEquivClass::~BugReportEquivClass() {
180 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
181}
182
183GRBugReporter::~GRBugReporter() { FlushReports(); }
184BugReporterData::~BugReporterData() {}
185
186ExplodedGraph<GRState>&
187GRBugReporter::getGraph() { return Eng.getGraph(); }
188
189GRStateManager&
190GRBugReporter::getStateManager() { return Eng.getStateManager(); }
191
192BugReporter::~BugReporter() { FlushReports(); }
193
194void BugReporter::FlushReports() {
195 if (BugTypes.isEmpty())
196 return;
197
198 // First flush the warnings for each BugType. This may end up creating new
199 // warnings and new BugTypes. Because ImmutableSet is a functional data
200 // structure, we do not need to worry about the iterators being invalidated.
201 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
202 const_cast<BugType*>(*I)->FlushReports(*this);
203
204 // Iterate through BugTypes a second time. BugTypes may have been updated
205 // with new BugType objects and new warnings.
206 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
207 BugType *BT = const_cast<BugType*>(*I);
208
209 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
210 SetTy& EQClasses = BT->EQClasses;
211
212 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
213 BugReportEquivClass& EQ = *EI;
214 FlushReport(EQ);
215 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000216
Ted Kremenekcf118d42009-02-04 23:49:09 +0000217 // Delete the BugType object. This will also delete the equivalence
218 // classes.
219 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000220 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000221
222 // Remove all references to the BugType objects.
223 BugTypes = F.GetEmptySet();
224}
225
226//===----------------------------------------------------------------------===//
227// PathDiagnostics generation.
228//===----------------------------------------------------------------------===//
229
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000230typedef llvm::DenseMap<const ExplodedNode<GRState>*,
231 const ExplodedNode<GRState>*> NodeBackMap;
232
233static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000234 std::pair<ExplodedNode<GRState>*, unsigned> >
235MakeReportGraph(const ExplodedGraph<GRState>* G,
236 const ExplodedNode<GRState>** NStart,
237 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000238
Ted Kremenekcf118d42009-02-04 23:49:09 +0000239 // Create the trimmed graph. It will contain the shortest paths from the
240 // error nodes to the root. In the new graph we should only have one
241 // error node unless there are two or more error nodes with the same minimum
242 // path length.
243 ExplodedGraph<GRState>* GTrim;
244 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000245
246 llvm::DenseMap<const void*, const void*> InverseMap;
247 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000248
249 // Create owning pointers for GTrim and NMap just to ensure that they are
250 // released when this function exists.
251 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
252 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
253
254 // Find the (first) error node in the trimmed graph. We just need to consult
255 // the node map (NMap) which maps from nodes in the original graph to nodes
256 // in the new graph.
257 const ExplodedNode<GRState>* N = 0;
258 unsigned NodeIndex = 0;
259
260 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
261 if ((N = NMap->getMappedNode(*I))) {
262 NodeIndex = (I - NStart) / sizeof(*I);
263 break;
264 }
265
266 assert(N && "No error node found in the trimmed graph.");
267
268 // Create a new (third!) graph with a single path. This is the graph
269 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000270 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000271 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
272 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000273
274 // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000275 // to the root node, and then construct a new graph that contains only
276 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000277 llvm::DenseMap<const void*,unsigned> Visited;
278 llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000279 WS.push_back(N);
280 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000281 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000282
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000283 while (!WS.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000284 const ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000285 WS.pop_back();
286
287 if (Visited.find(Node) != Visited.end())
288 continue;
289
290 Visited[Node] = cnt++;
291
292 if (Node->pred_empty()) {
293 Root = Node;
294 break;
295 }
296
Ted Kremenek3148eb42009-01-24 00:55:43 +0000297 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000298 E=Node->pred_end(); I!=E; ++I)
299 WS.push_back(*I);
300 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000301
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000302 assert (Root);
303
304 // Now walk from the root down the DFS path, always taking the successor
305 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000306 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000307 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000308
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000309 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000310 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000311 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000312 assert (I != Visited.end());
313
314 // Create the equivalent node in the new graph with the same state
315 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000316 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000317 GNew->getNode(N->getLocation(), N->getState());
318
319 // Store the mapping to the original node.
320 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
321 assert(IMitr != InverseMap.end() && "No mapping to original node.");
322 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000323
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000324 // Link up the new node with the previous node.
325 if (Last)
326 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000327
328 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000329
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000330 // Are we at the final node?
331 if (I->second == 0) {
332 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000333 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000334 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000335
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000336 // Find the next successor node. We choose the node that is marked
337 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000338 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
339 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000340 N = 0;
341
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000342 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000343
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000344 I = Visited.find(*SI);
345
346 if (I == Visited.end())
347 continue;
348
349 if (!N || I->second < MinVal) {
350 N = *SI;
351 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000352 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000353 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000354
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000355 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000356 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000357
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000358 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000359 return std::make_pair(std::make_pair(GNew, BM),
360 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000361}
362
Ted Kremenek3148eb42009-01-24 00:55:43 +0000363static const VarDecl*
364GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
365 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000366
367 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
368
369 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000370
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000371 if (!isa<PostStmt>(P))
372 continue;
373
374 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000375
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000376 if (!DR)
377 continue;
378
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000379 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000380
381 if (X != Y)
382 continue;
383
384 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
385
386 if (!VD)
387 continue;
388
389 return VD;
390 }
391
392 return 0;
393}
394
Ted Kremenek9e240492008-10-04 05:50:14 +0000395namespace {
396class VISIBILITY_HIDDEN NotableSymbolHandler
397 : public StoreManager::BindingsHandler {
398
Ted Kremenek2dabd432008-12-05 02:27:51 +0000399 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000400 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000401 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000402 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000403 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000404 PathDiagnostic& PD;
405 BugReporter& BR;
406
407public:
408
Ted Kremenek3148eb42009-01-24 00:55:43 +0000409 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
410 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000411 PathDiagnostic& pd, BugReporter& br)
412 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
413
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000414 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000415
Ted Kremenek2dabd432008-12-05 02:27:51 +0000416 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000417
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000418 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000419 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000420 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000421 ScanSym = SV->getSymbol();
422 else
423 return true;
424
425 if (ScanSym != Sym)
426 return true;
427
428 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000429 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000430
431 if (X == V) // Same binding?
432 return true;
433
434 // Different binding. Only handle assignments for now. We don't pull
435 // this check out of the loop because we will eventually handle other
436 // cases.
437
438 VarDecl *VD = 0;
439
Ted Kremenek3148eb42009-01-24 00:55:43 +0000440 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000441 if (!B->isAssignmentOp())
442 return true;
443
444 // What variable did we assign to?
445 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
446
447 if (!DR)
448 return true;
449
450 VD = dyn_cast<VarDecl>(DR->getDecl());
451 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000452 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000453 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
454 // assume that each DeclStmt has a single Decl. This invariant
455 // holds by contruction in the CFG.
456 VD = dyn_cast<VarDecl>(*DS->decl_begin());
457 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000458
459 if (!VD)
460 return true;
461
462 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000463 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000464
465 if (!MostRecent)
466 return true;
467
468 // Create the diagnostic.
469
470 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
471
472 if (VD->getType()->isPointerLikeType()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000473 std::string msg = "'" + std::string(VD->getNameAsString()) +
474 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000475
476 PD.push_front(new PathDiagnosticPiece(L, msg));
477 }
478
479 return true;
480 }
481};
482}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000483
Ted Kremenek3148eb42009-01-24 00:55:43 +0000484static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
485 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000486 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000487 PathDiagnostic& PD) {
488
Ted Kremenek3148eb42009-01-24 00:55:43 +0000489 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000490 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000491
492 if (!PrevSt)
493 return;
494
Ted Kremenek9e240492008-10-04 05:50:14 +0000495 // Look at the region bindings of the current state that map to the
496 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000497 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000498 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
499 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
500}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000501
Ted Kremenek9e240492008-10-04 05:50:14 +0000502namespace {
503class VISIBILITY_HIDDEN ScanNotableSymbols
504 : public StoreManager::BindingsHandler {
505
Ted Kremenek2dabd432008-12-05 02:27:51 +0000506 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000507 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000508 Stmt* S;
509 GRBugReporter& BR;
510 PathDiagnostic& PD;
511
512public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000513 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000514 PathDiagnostic& pd)
515 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000516
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000517 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000518 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000519
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000520 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000521 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000522 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000523 ScanSym = SV->getSymbol();
524 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000525 return true;
526
527 assert (ScanSym.isInitialized());
528
529 if (!BR.isNotable(ScanSym))
530 return true;
531
532 if (AlreadyProcessed.count(ScanSym))
533 return true;
534
535 AlreadyProcessed.insert(ScanSym);
536
537 HandleNotableSymbol(N, S, ScanSym, BR, PD);
538 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000539 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000540};
541} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000542
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000543namespace {
544class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
545 NodeBackMap& M;
546public:
547 NodeMapClosure(NodeBackMap *m) : M(*m) {}
548 ~NodeMapClosure() {}
549
550 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
551 NodeBackMap::iterator I = M.find(N);
552 return I == M.end() ? 0 : I->second;
553 }
554};
555}
556
Ted Kremenekc0959972008-07-02 21:24:01 +0000557void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000558 BugReportEquivClass& EQ) {
559
560 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000561
Ted Kremenekcf118d42009-02-04 23:49:09 +0000562 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
563 const ExplodedNode<GRState>* N = I->getEndNode();
564 if (N) Nodes.push_back(N);
565 }
566
567 if (Nodes.empty())
568 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000569
570 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000571 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000572 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000573 std::pair<ExplodedNode<GRState>*, unsigned> >&
574 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000575
Ted Kremenekcf118d42009-02-04 23:49:09 +0000576 // Find the BugReport with the original location.
577 BugReport *R = 0;
578 unsigned i = 0;
579 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
580 if (i == GPair.second.second) { R = *I; break; }
581
582 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000583
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000584 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
585 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000586 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000587
Ted Kremenekcf118d42009-02-04 23:49:09 +0000588 // Start building the path diagnostic...
589 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000590 PD.push_back(Piece);
591 else
592 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000593
Ted Kremenek3148eb42009-01-24 00:55:43 +0000594 const ExplodedNode<GRState>* NextNode = N->pred_empty()
595 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000596
Ted Kremenekc0959972008-07-02 21:24:01 +0000597 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000598 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000599 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000600
Ted Kremenek6837faa2008-04-09 00:20:43 +0000601 while (NextNode) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000602
Ted Kremenek3148eb42009-01-24 00:55:43 +0000603 const ExplodedNode<GRState>* LastNode = N;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000604 N = NextNode;
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000605 NextNode = GetNextNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000606
607 ProgramPoint P = N->getLocation();
608
609 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
610
611 CFGBlock* Src = BE->getSrc();
612 CFGBlock* Dst = BE->getDst();
613
614 Stmt* T = Src->getTerminator();
615
616 if (!T)
617 continue;
618
619 FullSourceLoc L(T->getLocStart(), SMgr);
620
621 switch (T->getStmtClass()) {
622 default:
623 break;
624
625 case Stmt::GotoStmtClass:
626 case Stmt::IndirectGotoStmtClass: {
627
628 Stmt* S = GetStmt(LastNode->getLocation());
629
630 if (!S)
631 continue;
632
Ted Kremenek297308e2009-02-10 23:56:07 +0000633 std::string sbuf;
634 llvm::raw_string_ostream os(sbuf);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000635
636 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000637 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000638
639 PD.push_front(new PathDiagnosticPiece(L, os.str()));
640 break;
641 }
642
Ted Kremenek297308e2009-02-10 23:56:07 +0000643 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000644 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000645 std::string sbuf;
646 llvm::raw_string_ostream os(sbuf);
Ted Kremenek5a429952008-04-23 23:35:07 +0000647
648 if (Stmt* S = Dst->getLabel())
649 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000650 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000651 os << "No cases match in the switch statement. "
652 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000653 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000654 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000655 case Stmt::DefaultStmtClass:
656 os << "Control jumps to the 'default' case at line "
657 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
658 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000659
660 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000661 os << "Control jumps to 'case ";
662
Ted Kremenek5a429952008-04-23 23:35:07 +0000663 CaseStmt* Case = cast<CaseStmt>(S);
664 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000665
Ted Kremenek5a429952008-04-23 23:35:07 +0000666 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000667
Ted Kremenek5a429952008-04-23 23:35:07 +0000668 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000669
Ted Kremenek5a429952008-04-23 23:35:07 +0000670 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
671
672 // FIXME: Maybe this should be an assertion. Are there cases
673 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000674 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000675 if (D) {
676 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000677 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000678 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000679 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000680
681 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000682
Ted Kremenek5a429952008-04-23 23:35:07 +0000683 // Not an enum.
684 Expr* CondE = cast<SwitchStmt>(T)->getCond();
685 unsigned bits = Ctx.getTypeSize(CondE->getType());
686 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000687
Ted Kremenek5a429952008-04-23 23:35:07 +0000688 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
689 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000690 continue;
691 }
692
Ted Kremenek297308e2009-02-10 23:56:07 +0000693 os << V;
694 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000695
Ted Kremenek61f3e052008-04-03 04:42:52 +0000696 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000697 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000698
699 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000700 }
701 }
Ted Kremenek56783922008-04-25 01:29:56 +0000702 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000703 os << "'Default' branch taken. ";
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000704 ExecutionContinues(os, SMgr, LastNode);
Ted Kremenek56783922008-04-25 01:29:56 +0000705 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000706
707 PD.push_front(new PathDiagnosticPiece(L, os.str()));
708 break;
709 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000710
711 case Stmt::BreakStmtClass:
712 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000713 std::string sbuf;
714 llvm::raw_string_ostream os(sbuf);
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000715 ExecutionContinues(os, SMgr, LastNode);
716 PD.push_front(new PathDiagnosticPiece(L, os.str()));
717 break;
718 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000719
720 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000721 std::string sbuf;
722 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000723 os << "'?' condition evaluates to ";
724
725 if (*(Src->succ_begin()+1) == Dst)
726 os << "false.";
727 else
728 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000729
Ted Kremenek297308e2009-02-10 23:56:07 +0000730 PD.push_front(new PathDiagnosticPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000731 break;
732 }
733
734 case Stmt::DoStmtClass: {
735
736 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000737 std::string sbuf;
738 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000739
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000740 os << "Loop condition is true. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000741 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000742
743 PD.push_front(new PathDiagnosticPiece(L, os.str()));
744 }
745 else
746 PD.push_front(new PathDiagnosticPiece(L,
747 "Loop condition is false. Exiting loop."));
748
749 break;
750 }
751
Ted Kremenek61f3e052008-04-03 04:42:52 +0000752 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000753 case Stmt::ForStmtClass: {
754
755 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000756 std::string sbuf;
757 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000758
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000759 os << "Loop condition is false. ";
Ted Kremenek143ca222008-05-06 18:11:09 +0000760 ExecutionContinues(os, SMgr, Dst);
Ted Kremenek297308e2009-02-10 23:56:07 +0000761
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000762 PD.push_front(new PathDiagnosticPiece(L, os.str()));
763 }
764 else
765 PD.push_front(new PathDiagnosticPiece(L,
766 "Loop condition is true. Entering loop body."));
767
768 break;
769 }
770
Ted Kremenek297308e2009-02-10 23:56:07 +0000771 case Stmt::IfStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000772 if (*(Src->succ_begin()+1) == Dst)
773 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
774 else
775 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
776
777 break;
778 }
779 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000780 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000781
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000782 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
783 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000784 PD.push_front(p);
785
Ted Kremenek9e240492008-10-04 05:50:14 +0000786 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
787 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000788 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000789 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
790 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000791 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000792 }
793}
794
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000795
Ted Kremenekcf118d42009-02-04 23:49:09 +0000796void BugReporter::Register(BugType *BT) {
797 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000798}
799
Ted Kremenekcf118d42009-02-04 23:49:09 +0000800void BugReporter::EmitReport(BugReport* R) {
801 // Compute the bug report's hash to determine its equivalence class.
802 llvm::FoldingSetNodeID ID;
803 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000804
Ted Kremenekcf118d42009-02-04 23:49:09 +0000805 // Lookup the equivance class. If there isn't one, create it.
806 BugType& BT = R->getBugType();
807 Register(&BT);
808 void *InsertPos;
809 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
810
811 if (!EQ) {
812 EQ = new BugReportEquivClass(R);
813 BT.EQClasses.InsertNode(EQ, InsertPos);
814 }
815 else
816 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000817}
818
Ted Kremenekcf118d42009-02-04 23:49:09 +0000819void BugReporter::FlushReport(BugReportEquivClass& EQ) {
820 assert(!EQ.Reports.empty());
821 BugReport &R = **EQ.begin();
822
823 // FIXME: Make sure we use the 'R' for the path that was actually used.
824 // Probably doesn't make a difference in practice.
825 BugType& BT = R.getBugType();
826
827 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
828 R.getDescription(),
829 BT.getCategory()));
830 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000831
832 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000833 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000834 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000835
Ted Kremenek3148eb42009-01-24 00:55:43 +0000836 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000837 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000838 const SourceRange *Beg = 0, *End = 0;
839 R.getRanges(*this, Beg, End);
840 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000841 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000842 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
843 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000844
Ted Kremenek3148eb42009-01-24 00:55:43 +0000845 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000846 default: assert(0 && "Don't handle this many ranges yet!");
847 case 0: Diag.Report(L, ErrorDiag); break;
848 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
849 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
850 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000851 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000852
853 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
854 if (!PD)
855 return;
856
857 if (D->empty()) {
858 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
859 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
860 D->push_back(piece);
861 }
862
863 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000864}
Ted Kremenek57202072008-07-14 17:40:50 +0000865
Ted Kremenek8c036c72008-09-20 04:23:38 +0000866void BugReporter::EmitBasicReport(const char* name, const char* str,
867 SourceLocation Loc,
868 SourceRange* RBeg, unsigned NumRanges) {
869 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
870}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000871
Ted Kremenek8c036c72008-09-20 04:23:38 +0000872void BugReporter::EmitBasicReport(const char* name, const char* category,
873 const char* str, SourceLocation Loc,
874 SourceRange* RBeg, unsigned NumRanges) {
875
Ted Kremenekcf118d42009-02-04 23:49:09 +0000876 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
877 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000878 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000879 RangedBugReport *R = new DiagBugReport(*BT, str, L);
880 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
881 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000882}