blob: a89542d4c11688c68da7e01b72b6637b4d6b3125 [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.
471
472 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
473
474 if (VD->getType()->isPointerLikeType()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000475 std::string msg = "'" + std::string(VD->getNameAsString()) +
476 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000477
478 PD.push_front(new PathDiagnosticPiece(L, msg));
479 }
480
481 return true;
482 }
483};
484}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000485
Ted Kremenek3148eb42009-01-24 00:55:43 +0000486static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
487 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000488 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000489 PathDiagnostic& PD) {
490
Ted Kremenek3148eb42009-01-24 00:55:43 +0000491 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000492 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000493
494 if (!PrevSt)
495 return;
496
Ted Kremenek9e240492008-10-04 05:50:14 +0000497 // Look at the region bindings of the current state that map to the
498 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000499 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000500 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
501 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
502}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000503
Ted Kremenek9e240492008-10-04 05:50:14 +0000504namespace {
505class VISIBILITY_HIDDEN ScanNotableSymbols
506 : public StoreManager::BindingsHandler {
507
Ted Kremenek2dabd432008-12-05 02:27:51 +0000508 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000509 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000510 Stmt* S;
511 GRBugReporter& BR;
512 PathDiagnostic& PD;
513
514public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000515 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000516 PathDiagnostic& pd)
517 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000518
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000519 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000520 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000521
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000522 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000523 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000524 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000525 ScanSym = SV->getSymbol();
526 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000527 return true;
528
529 assert (ScanSym.isInitialized());
530
531 if (!BR.isNotable(ScanSym))
532 return true;
533
534 if (AlreadyProcessed.count(ScanSym))
535 return true;
536
537 AlreadyProcessed.insert(ScanSym);
538
539 HandleNotableSymbol(N, S, ScanSym, BR, PD);
540 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000541 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000542};
543} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000544
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000545namespace {
546class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
547 NodeBackMap& M;
548public:
549 NodeMapClosure(NodeBackMap *m) : M(*m) {}
550 ~NodeMapClosure() {}
551
552 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
553 NodeBackMap::iterator I = M.find(N);
554 return I == M.end() ? 0 : I->second;
555 }
556};
557}
558
Ted Kremenekc0959972008-07-02 21:24:01 +0000559void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000560 BugReportEquivClass& EQ) {
561
562 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000563
Ted Kremenekcf118d42009-02-04 23:49:09 +0000564 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
565 const ExplodedNode<GRState>* N = I->getEndNode();
566 if (N) Nodes.push_back(N);
567 }
568
569 if (Nodes.empty())
570 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000571
572 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000573 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000574 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000575 std::pair<ExplodedNode<GRState>*, unsigned> >&
576 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000577
Ted Kremenekcf118d42009-02-04 23:49:09 +0000578 // Find the BugReport with the original location.
579 BugReport *R = 0;
580 unsigned i = 0;
581 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
582 if (i == GPair.second.second) { R = *I; break; }
583
584 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000585
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000586 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
587 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000588 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000589
Ted Kremenekcf118d42009-02-04 23:49:09 +0000590 // Start building the path diagnostic...
591 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000592 PD.push_back(Piece);
593 else
594 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000595
Ted Kremenek3148eb42009-01-24 00:55:43 +0000596 const ExplodedNode<GRState>* NextNode = N->pred_empty()
597 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000598
Ted Kremenekc0959972008-07-02 21:24:01 +0000599 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000600 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000601 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000602
Ted Kremenek6837faa2008-04-09 00:20:43 +0000603 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000604 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000605 NextNode = GetPredecessorNode(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
Ted Kremenekb697b102009-02-23 22:44:26 +0000628 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000629
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 Kremenekb479dad2009-02-23 23:13:51 +0000704 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
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 Kremenekb479dad2009-02-23 23:13:51 +0000715 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000716 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 Kremenekb479dad2009-02-23 23:13:51 +0000741 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
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 Kremenekb479dad2009-02-23 23:13:51 +0000760 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
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}