blob: 50ef3070ab0b55cab70c85818a51020dbbd28545 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
11// PathDiagnostics for analyses based on GRSimpleVals.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/CFG.h"
21#include "clang/AST/Expr.h"
22#include "clang/Analysis/ProgramPoint.h"
23#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000025#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000026#include "llvm/ADT/STLExtras.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000027
28using namespace clang;
29
Ted Kremenekcf118d42009-02-04 23:49:09 +000030//===----------------------------------------------------------------------===//
31// static functions.
32//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000033
Ted Kremenekb697b102009-02-23 22:44:26 +000034static inline Stmt* GetStmt(ProgramPoint P) {
35 if (const PostStmt* PS = dyn_cast<PostStmt>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000036 return PS->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000037 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000038 return BE->getSrc()->getTerminator();
Ted Kremenek61f3e052008-04-03 04:42:52 +000039
Ted Kremenekb697b102009-02-23 22:44:26 +000040 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000041}
42
Ted Kremenek3148eb42009-01-24 00:55:43 +000043static inline const ExplodedNode<GRState>*
Ted Kremenekb697b102009-02-23 22:44:26 +000044GetPredecessorNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000045 return N->pred_empty() ? NULL : *(N->pred_begin());
46}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000047
Ted Kremenekb697b102009-02-23 22:44:26 +000048static inline const ExplodedNode<GRState>*
49GetSuccessorNode(const ExplodedNode<GRState>* N) {
50 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000051}
52
Ted Kremenekb697b102009-02-23 22:44:26 +000053static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) {
54 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
55 if (Stmt *S = GetStmt(N->getLocation()))
56 return S;
57
58 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000059}
60
Ted Kremenekb697b102009-02-23 22:44:26 +000061static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) {
62 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
63 if (Stmt *S = GetStmt(N->getLocation()))
64 return S;
65
66 return 0;
67}
68
69static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
70 if (Stmt *S = GetStmt(N->getLocation()))
71 return S;
72
73 return GetPreviousStmt(N);
74}
75
76static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
77 if (Stmt *S = GetStmt(N->getLocation()))
78 return S;
79
80 return GetNextStmt(N);
81}
82
83//===----------------------------------------------------------------------===//
84// Diagnostics for 'execution continues on line XXX'.
85//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +000086
87static inline void ExecutionContinues(llvm::raw_string_ostream& os,
88 SourceManager& SMgr,
89 const ExplodedNode<GRState>* N,
90 const Decl& D) {
91
Ted Kremenek143ca222008-05-06 18:11:09 +000092 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +000093 if (os.str().empty())
94 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +000095
Ted Kremenekb479dad2009-02-23 23:13:51 +000096 if (Stmt *S = GetNextStmt(N))
Ted Kremenekb697b102009-02-23 22:44:26 +000097 os << "Execution continues on line "
Ted Kremenekb479dad2009-02-23 23:13:51 +000098 << SMgr.getInstantiationLineNumber(S->getLocStart()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +000099 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000100 os << "Execution jumps to the end of the "
101 << (isa<ObjCMethodDecl>(D) ? "method" : "function") << '.';
Ted Kremenek143ca222008-05-06 18:11:09 +0000102}
103
Ted Kremenekcf118d42009-02-04 23:49:09 +0000104//===----------------------------------------------------------------------===//
105// Methods for BugType and subclasses.
106//===----------------------------------------------------------------------===//
107BugType::~BugType() {}
108void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000109
Ted Kremenekcf118d42009-02-04 23:49:09 +0000110//===----------------------------------------------------------------------===//
111// Methods for BugReport and subclasses.
112//===----------------------------------------------------------------------===//
113BugReport::~BugReport() {}
114RangedBugReport::~RangedBugReport() {}
115
116Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000117 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000118 Stmt *S = NULL;
119
Ted Kremenekcf118d42009-02-04 23:49:09 +0000120 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000121 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000122 }
123 if (!S) S = GetStmt(ProgP);
124
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000125 return S;
126}
127
128PathDiagnosticPiece*
129BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000130 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000131
132 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000133
134 if (!S)
135 return NULL;
136
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000137 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
138 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000139
Ted Kremenekde7161f2008-04-03 18:00:37 +0000140 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000141 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000142
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000143 for (; Beg != End; ++Beg)
144 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000145
146 return P;
147}
148
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000149void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
150 const SourceRange*& end) {
151
152 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
153 R = E->getSourceRange();
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)
Ted Kremenekb697b102009-02-23 22:44:26 +0000163 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode))
Ted Kremenekcf118d42009-02-04 23:49:09 +0000164 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 Kremenekfe9e5432009-02-18 03:48:14 +0000172 BugReporter& BR,
173 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000174 return NULL;
175}
176
Ted Kremenekcf118d42009-02-04 23:49:09 +0000177//===----------------------------------------------------------------------===//
178// Methods for BugReporter and subclasses.
179//===----------------------------------------------------------------------===//
180
181BugReportEquivClass::~BugReportEquivClass() {
182 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
183}
184
185GRBugReporter::~GRBugReporter() { FlushReports(); }
186BugReporterData::~BugReporterData() {}
187
188ExplodedGraph<GRState>&
189GRBugReporter::getGraph() { return Eng.getGraph(); }
190
191GRStateManager&
192GRBugReporter::getStateManager() { return Eng.getStateManager(); }
193
194BugReporter::~BugReporter() { FlushReports(); }
195
196void BugReporter::FlushReports() {
197 if (BugTypes.isEmpty())
198 return;
199
200 // First flush the warnings for each BugType. This may end up creating new
201 // warnings and new BugTypes. Because ImmutableSet is a functional data
202 // structure, we do not need to worry about the iterators being invalidated.
203 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
204 const_cast<BugType*>(*I)->FlushReports(*this);
205
206 // Iterate through BugTypes a second time. BugTypes may have been updated
207 // with new BugType objects and new warnings.
208 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
209 BugType *BT = const_cast<BugType*>(*I);
210
211 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
212 SetTy& EQClasses = BT->EQClasses;
213
214 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
215 BugReportEquivClass& EQ = *EI;
216 FlushReport(EQ);
217 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000218
Ted Kremenekcf118d42009-02-04 23:49:09 +0000219 // Delete the BugType object. This will also delete the equivalence
220 // classes.
221 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000222 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000223
224 // Remove all references to the BugType objects.
225 BugTypes = F.GetEmptySet();
226}
227
228//===----------------------------------------------------------------------===//
229// PathDiagnostics generation.
230//===----------------------------------------------------------------------===//
231
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000232typedef llvm::DenseMap<const ExplodedNode<GRState>*,
233 const ExplodedNode<GRState>*> NodeBackMap;
234
235static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000236 std::pair<ExplodedNode<GRState>*, unsigned> >
237MakeReportGraph(const ExplodedGraph<GRState>* G,
238 const ExplodedNode<GRState>** NStart,
239 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000240
Ted Kremenekcf118d42009-02-04 23:49:09 +0000241 // Create the trimmed graph. It will contain the shortest paths from the
242 // error nodes to the root. In the new graph we should only have one
243 // error node unless there are two or more error nodes with the same minimum
244 // path length.
245 ExplodedGraph<GRState>* GTrim;
246 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000247
248 llvm::DenseMap<const void*, const void*> InverseMap;
249 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000250
251 // Create owning pointers for GTrim and NMap just to ensure that they are
252 // released when this function exists.
253 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
254 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
255
256 // Find the (first) error node in the trimmed graph. We just need to consult
257 // the node map (NMap) which maps from nodes in the original graph to nodes
258 // in the new graph.
259 const ExplodedNode<GRState>* N = 0;
260 unsigned NodeIndex = 0;
261
262 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
263 if ((N = NMap->getMappedNode(*I))) {
264 NodeIndex = (I - NStart) / sizeof(*I);
265 break;
266 }
267
268 assert(N && "No error node found in the trimmed graph.");
269
270 // Create a new (third!) graph with a single path. This is the graph
271 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000272 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000273 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
274 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000275
276 // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000277 // to the root node, and then construct a new graph that contains only
278 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000279 llvm::DenseMap<const void*,unsigned> Visited;
280 llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000281 WS.push_back(N);
282 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000283 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000284
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000285 while (!WS.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000286 const ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000287 WS.pop_back();
288
289 if (Visited.find(Node) != Visited.end())
290 continue;
291
292 Visited[Node] = cnt++;
293
294 if (Node->pred_empty()) {
295 Root = Node;
296 break;
297 }
298
Ted Kremenek3148eb42009-01-24 00:55:43 +0000299 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000300 E=Node->pred_end(); I!=E; ++I)
301 WS.push_back(*I);
302 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000303
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000304 assert (Root);
305
306 // Now walk from the root down the DFS path, always taking the successor
307 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000308 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000309 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000310
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000311 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000312 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000313 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000314 assert (I != Visited.end());
315
316 // Create the equivalent node in the new graph with the same state
317 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000318 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000319 GNew->getNode(N->getLocation(), N->getState());
320
321 // Store the mapping to the original node.
322 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
323 assert(IMitr != InverseMap.end() && "No mapping to original node.");
324 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000325
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000326 // Link up the new node with the previous node.
327 if (Last)
328 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000329
330 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000331
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000332 // Are we at the final node?
333 if (I->second == 0) {
334 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000335 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000336 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000337
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000338 // Find the next successor node. We choose the node that is marked
339 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000340 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
341 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000342 N = 0;
343
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000344 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000345
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000346 I = Visited.find(*SI);
347
348 if (I == Visited.end())
349 continue;
350
351 if (!N || I->second < MinVal) {
352 N = *SI;
353 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000354 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000355 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000356
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000357 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000358 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000359
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000360 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000361 return std::make_pair(std::make_pair(GNew, BM),
362 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000363}
364
Ted Kremenek3148eb42009-01-24 00:55:43 +0000365static const VarDecl*
366GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
367 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000368
369 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
370
371 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000372
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000373 if (!isa<PostStmt>(P))
374 continue;
375
376 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000377
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000378 if (!DR)
379 continue;
380
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000381 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000382
383 if (X != Y)
384 continue;
385
386 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
387
388 if (!VD)
389 continue;
390
391 return VD;
392 }
393
394 return 0;
395}
396
Ted Kremenek9e240492008-10-04 05:50:14 +0000397namespace {
398class VISIBILITY_HIDDEN NotableSymbolHandler
399 : public StoreManager::BindingsHandler {
400
Ted Kremenek2dabd432008-12-05 02:27:51 +0000401 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000402 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000403 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000404 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000405 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000406 PathDiagnostic& PD;
407 BugReporter& BR;
408
409public:
410
Ted Kremenek3148eb42009-01-24 00:55:43 +0000411 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
412 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000413 PathDiagnostic& pd, BugReporter& br)
414 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
415
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000416 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000417
Ted Kremenek2dabd432008-12-05 02:27:51 +0000418 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000419
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000420 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000421 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000422 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000423 ScanSym = SV->getSymbol();
424 else
425 return true;
426
427 if (ScanSym != Sym)
428 return true;
429
430 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000431 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000432
433 if (X == V) // Same binding?
434 return true;
435
436 // Different binding. Only handle assignments for now. We don't pull
437 // this check out of the loop because we will eventually handle other
438 // cases.
439
440 VarDecl *VD = 0;
441
Ted Kremenek3148eb42009-01-24 00:55:43 +0000442 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000443 if (!B->isAssignmentOp())
444 return true;
445
446 // What variable did we assign to?
447 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
448
449 if (!DR)
450 return true;
451
452 VD = dyn_cast<VarDecl>(DR->getDecl());
453 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000454 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000455 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
456 // assume that each DeclStmt has a single Decl. This invariant
457 // holds by contruction in the CFG.
458 VD = dyn_cast<VarDecl>(*DS->decl_begin());
459 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000460
461 if (!VD)
462 return true;
463
464 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000465 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000466
467 if (!MostRecent)
468 return true;
469
470 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000471 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
472
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000473 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000474 std::string msg = "'" + std::string(VD->getNameAsString()) +
475 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000476
477 PD.push_front(new PathDiagnosticPiece(L, msg));
478 }
479
480 return true;
481 }
482};
483}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000484
Ted Kremenek3148eb42009-01-24 00:55:43 +0000485static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
486 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000487 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000488 PathDiagnostic& PD) {
489
Ted Kremenek3148eb42009-01-24 00:55:43 +0000490 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000491 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000492
493 if (!PrevSt)
494 return;
495
Ted Kremenek9e240492008-10-04 05:50:14 +0000496 // Look at the region bindings of the current state that map to the
497 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000498 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000499 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
500 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
501}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000502
Ted Kremenek9e240492008-10-04 05:50:14 +0000503namespace {
504class VISIBILITY_HIDDEN ScanNotableSymbols
505 : public StoreManager::BindingsHandler {
506
Ted Kremenek2dabd432008-12-05 02:27:51 +0000507 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000508 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000509 Stmt* S;
510 GRBugReporter& BR;
511 PathDiagnostic& PD;
512
513public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000514 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000515 PathDiagnostic& pd)
516 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000517
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000518 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000519 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000520
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000521 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000522 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000523 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000524 ScanSym = SV->getSymbol();
525 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000526 return true;
527
528 assert (ScanSym.isInitialized());
529
530 if (!BR.isNotable(ScanSym))
531 return true;
532
533 if (AlreadyProcessed.count(ScanSym))
534 return true;
535
536 AlreadyProcessed.insert(ScanSym);
537
538 HandleNotableSymbol(N, S, ScanSym, BR, PD);
539 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000540 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000541};
542} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000543
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000544namespace {
545class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
546 NodeBackMap& M;
547public:
548 NodeMapClosure(NodeBackMap *m) : M(*m) {}
549 ~NodeMapClosure() {}
550
551 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
552 NodeBackMap::iterator I = M.find(N);
553 return I == M.end() ? 0 : I->second;
554 }
555};
556}
557
Ted Kremenekc0959972008-07-02 21:24:01 +0000558void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000559 BugReportEquivClass& EQ) {
560
561 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000562
Ted Kremenekcf118d42009-02-04 23:49:09 +0000563 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
564 const ExplodedNode<GRState>* N = I->getEndNode();
565 if (N) Nodes.push_back(N);
566 }
567
568 if (Nodes.empty())
569 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000570
571 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000572 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000573 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000574 std::pair<ExplodedNode<GRState>*, unsigned> >&
575 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000576
Ted Kremenekcf118d42009-02-04 23:49:09 +0000577 // Find the BugReport with the original location.
578 BugReport *R = 0;
579 unsigned i = 0;
580 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
581 if (i == GPair.second.second) { R = *I; break; }
582
583 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000584
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000585 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
586 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000587 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000588
Ted Kremenekcf118d42009-02-04 23:49:09 +0000589 // Start building the path diagnostic...
590 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000591 PD.push_back(Piece);
592 else
593 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000594
Ted Kremenek3148eb42009-01-24 00:55:43 +0000595 const ExplodedNode<GRState>* NextNode = N->pred_empty()
596 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000597
Ted Kremenekc0959972008-07-02 21:24:01 +0000598 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000599 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000600 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000601
Ted Kremenek6837faa2008-04-09 00:20:43 +0000602 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000603 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000604 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000605
606 ProgramPoint P = N->getLocation();
607
608 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
609
610 CFGBlock* Src = BE->getSrc();
611 CFGBlock* Dst = BE->getDst();
612
613 Stmt* T = Src->getTerminator();
614
615 if (!T)
616 continue;
617
618 FullSourceLoc L(T->getLocStart(), SMgr);
619
620 switch (T->getStmtClass()) {
621 default:
622 break;
623
624 case Stmt::GotoStmtClass:
625 case Stmt::IndirectGotoStmtClass: {
626
Ted Kremenekb697b102009-02-23 22:44:26 +0000627 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000628
629 if (!S)
630 continue;
631
Ted Kremenek297308e2009-02-10 23:56:07 +0000632 std::string sbuf;
633 llvm::raw_string_ostream os(sbuf);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000634
635 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000636 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000637
638 PD.push_front(new PathDiagnosticPiece(L, os.str()));
639 break;
640 }
641
Ted Kremenek297308e2009-02-10 23:56:07 +0000642 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000643 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000644 std::string sbuf;
645 llvm::raw_string_ostream os(sbuf);
Ted Kremenek5a429952008-04-23 23:35:07 +0000646
647 if (Stmt* S = Dst->getLabel())
648 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000649 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000650 os << "No cases match in the switch statement. "
651 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000652 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000653 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000654 case Stmt::DefaultStmtClass:
655 os << "Control jumps to the 'default' case at line "
656 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
657 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000658
659 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000660 os << "Control jumps to 'case ";
661
Ted Kremenek5a429952008-04-23 23:35:07 +0000662 CaseStmt* Case = cast<CaseStmt>(S);
663 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000664
Ted Kremenek5a429952008-04-23 23:35:07 +0000665 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000666
Ted Kremenek5a429952008-04-23 23:35:07 +0000667 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000668
Ted Kremenek5a429952008-04-23 23:35:07 +0000669 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
670
671 // FIXME: Maybe this should be an assertion. Are there cases
672 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000673 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000674 if (D) {
675 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000676 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000677 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000678 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000679
680 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000681
Ted Kremenek5a429952008-04-23 23:35:07 +0000682 // Not an enum.
683 Expr* CondE = cast<SwitchStmt>(T)->getCond();
684 unsigned bits = Ctx.getTypeSize(CondE->getType());
685 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000686
Ted Kremenek5a429952008-04-23 23:35:07 +0000687 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
688 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000689 continue;
690 }
691
Ted Kremenek297308e2009-02-10 23:56:07 +0000692 os << V;
693 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000694
Ted Kremenek61f3e052008-04-03 04:42:52 +0000695 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000696 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000697
698 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000699 }
700 }
Ted Kremenek56783922008-04-25 01:29:56 +0000701 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000702 os << "'Default' branch taken. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000703 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek56783922008-04-25 01:29:56 +0000704 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000705
706 PD.push_front(new PathDiagnosticPiece(L, os.str()));
707 break;
708 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000709
710 case Stmt::BreakStmtClass:
711 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000712 std::string sbuf;
713 llvm::raw_string_ostream os(sbuf);
Ted Kremenekb479dad2009-02-23 23:13:51 +0000714 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000715 PD.push_front(new PathDiagnosticPiece(L, os.str()));
716 break;
717 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000718
719 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000720 std::string sbuf;
721 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000722 os << "'?' condition evaluates to ";
723
724 if (*(Src->succ_begin()+1) == Dst)
725 os << "false.";
726 else
727 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000728
Ted Kremenek297308e2009-02-10 23:56:07 +0000729 PD.push_front(new PathDiagnosticPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000730 break;
731 }
732
733 case Stmt::DoStmtClass: {
734
735 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000736 std::string sbuf;
737 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000738
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000739 os << "Loop condition is true. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000740 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000741
742 PD.push_front(new PathDiagnosticPiece(L, os.str()));
743 }
744 else
745 PD.push_front(new PathDiagnosticPiece(L,
746 "Loop condition is false. Exiting loop."));
747
748 break;
749 }
750
Ted Kremenek61f3e052008-04-03 04:42:52 +0000751 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000752 case Stmt::ForStmtClass: {
753
754 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000755 std::string sbuf;
756 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000757
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000758 os << "Loop condition is false. ";
Ted Kremenekb479dad2009-02-23 23:13:51 +0000759 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek297308e2009-02-10 23:56:07 +0000760
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000761 PD.push_front(new PathDiagnosticPiece(L, os.str()));
762 }
763 else
764 PD.push_front(new PathDiagnosticPiece(L,
765 "Loop condition is true. Entering loop body."));
766
767 break;
768 }
769
Ted Kremenek297308e2009-02-10 23:56:07 +0000770 case Stmt::IfStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000771 if (*(Src->succ_begin()+1) == Dst)
772 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
773 else
774 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
775
776 break;
777 }
778 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000779 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000780
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000781 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
782 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000783 PD.push_front(p);
784
Ted Kremenek9e240492008-10-04 05:50:14 +0000785 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
786 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000787 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000788 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
789 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000790 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000791 }
792}
793
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000794
Ted Kremenekcf118d42009-02-04 23:49:09 +0000795void BugReporter::Register(BugType *BT) {
796 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000797}
798
Ted Kremenekcf118d42009-02-04 23:49:09 +0000799void BugReporter::EmitReport(BugReport* R) {
800 // Compute the bug report's hash to determine its equivalence class.
801 llvm::FoldingSetNodeID ID;
802 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000803
Ted Kremenekcf118d42009-02-04 23:49:09 +0000804 // Lookup the equivance class. If there isn't one, create it.
805 BugType& BT = R->getBugType();
806 Register(&BT);
807 void *InsertPos;
808 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
809
810 if (!EQ) {
811 EQ = new BugReportEquivClass(R);
812 BT.EQClasses.InsertNode(EQ, InsertPos);
813 }
814 else
815 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000816}
817
Ted Kremenekcf118d42009-02-04 23:49:09 +0000818void BugReporter::FlushReport(BugReportEquivClass& EQ) {
819 assert(!EQ.Reports.empty());
820 BugReport &R = **EQ.begin();
821
822 // FIXME: Make sure we use the 'R' for the path that was actually used.
823 // Probably doesn't make a difference in practice.
824 BugType& BT = R.getBugType();
825
826 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
827 R.getDescription(),
828 BT.getCategory()));
829 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000830
831 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000832 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000833 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000834
Ted Kremenek3148eb42009-01-24 00:55:43 +0000835 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000836 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000837 const SourceRange *Beg = 0, *End = 0;
838 R.getRanges(*this, Beg, End);
839 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000840 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000841 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
842 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000843
Ted Kremenek3148eb42009-01-24 00:55:43 +0000844 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000845 default: assert(0 && "Don't handle this many ranges yet!");
846 case 0: Diag.Report(L, ErrorDiag); break;
847 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
848 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
849 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000850 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000851
852 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
853 if (!PD)
854 return;
855
856 if (D->empty()) {
857 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
858 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
859 D->push_back(piece);
860 }
861
862 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000863}
Ted Kremenek57202072008-07-14 17:40:50 +0000864
Ted Kremenek8c036c72008-09-20 04:23:38 +0000865void BugReporter::EmitBasicReport(const char* name, const char* str,
866 SourceLocation Loc,
867 SourceRange* RBeg, unsigned NumRanges) {
868 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
869}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000870
Ted Kremenek8c036c72008-09-20 04:23:38 +0000871void BugReporter::EmitBasicReport(const char* name, const char* category,
872 const char* str, SourceLocation Loc,
873 SourceRange* RBeg, unsigned NumRanges) {
874
Ted Kremenekcf118d42009-02-04 23:49:09 +0000875 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
876 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000877 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000878 RangedBugReport *R = new DiagBugReport(*BT, str, L);
879 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
880 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000881}