blob: 09a4f2b45bdb1f91afd1d45e4d9e12017cf5b695 [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//===----------------------------------------------------------------------===//
86
Ted Kremenek297308e2009-02-10 23:56:07 +000087static void ExecutionContinues(llvm::raw_string_ostream& os,
88 SourceManager& SMgr,
89 const Stmt* S) {
Ted Kremenek143ca222008-05-06 18:11:09 +000090 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +000091 if (os.str().empty())
92 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +000093
Ted Kremenekb697b102009-02-23 22:44:26 +000094 if (S)
95 os << "Execution continues on line "
96 << SMgr.getInstantiationLineNumber(S->getLocStart()) << '.';
97 else
98 os << "Execution jumps to the end of the function.";
Ted Kremenekbb77e9b2008-05-01 22:50:36 +000099}
Ted Kremenek143ca222008-05-06 18:11:09 +0000100
Ted Kremenek297308e2009-02-10 23:56:07 +0000101static inline void ExecutionContinues(llvm::raw_string_ostream& os,
Ted Kremenek143ca222008-05-06 18:11:09 +0000102 SourceManager& SMgr,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000103 const ExplodedNode<GRState>* N) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000104 ExecutionContinues(os, SMgr, GetNextStmt(N));
Ted Kremenek143ca222008-05-06 18:11:09 +0000105}
106
Ted Kremenekcf118d42009-02-04 23:49:09 +0000107//===----------------------------------------------------------------------===//
108// Methods for BugType and subclasses.
109//===----------------------------------------------------------------------===//
110BugType::~BugType() {}
111void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000112
Ted Kremenekcf118d42009-02-04 23:49:09 +0000113//===----------------------------------------------------------------------===//
114// Methods for BugReport and subclasses.
115//===----------------------------------------------------------------------===//
116BugReport::~BugReport() {}
117RangedBugReport::~RangedBugReport() {}
118
119Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000120 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000121 Stmt *S = NULL;
122
Ted Kremenekcf118d42009-02-04 23:49:09 +0000123 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000124 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000125 }
126 if (!S) S = GetStmt(ProgP);
127
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000128 return S;
129}
130
131PathDiagnosticPiece*
132BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000133 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000134
135 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000136
137 if (!S)
138 return NULL;
139
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000140 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
141 PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000142
Ted Kremenekde7161f2008-04-03 18:00:37 +0000143 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000144 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000146 for (; Beg != End; ++Beg)
147 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000148
149 return P;
150}
151
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000152void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
153 const SourceRange*& end) {
154
155 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
156 R = E->getSourceRange();
157 beg = &R;
158 end = beg+1;
159 }
160 else
161 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000162}
163
Ted Kremenekcf118d42009-02-04 23:49:09 +0000164SourceLocation BugReport::getLocation() const {
165 if (EndNode)
Ted Kremenekb697b102009-02-23 22:44:26 +0000166 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode))
Ted Kremenekcf118d42009-02-04 23:49:09 +0000167 return S->getLocStart();
168
169 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000170}
171
Ted Kremenek3148eb42009-01-24 00:55:43 +0000172PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
173 const ExplodedNode<GRState>* PrevN,
174 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000175 BugReporter& BR,
176 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000177 return NULL;
178}
179
Ted Kremenekcf118d42009-02-04 23:49:09 +0000180//===----------------------------------------------------------------------===//
181// Methods for BugReporter and subclasses.
182//===----------------------------------------------------------------------===//
183
184BugReportEquivClass::~BugReportEquivClass() {
185 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
186}
187
188GRBugReporter::~GRBugReporter() { FlushReports(); }
189BugReporterData::~BugReporterData() {}
190
191ExplodedGraph<GRState>&
192GRBugReporter::getGraph() { return Eng.getGraph(); }
193
194GRStateManager&
195GRBugReporter::getStateManager() { return Eng.getStateManager(); }
196
197BugReporter::~BugReporter() { FlushReports(); }
198
199void BugReporter::FlushReports() {
200 if (BugTypes.isEmpty())
201 return;
202
203 // First flush the warnings for each BugType. This may end up creating new
204 // warnings and new BugTypes. Because ImmutableSet is a functional data
205 // structure, we do not need to worry about the iterators being invalidated.
206 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
207 const_cast<BugType*>(*I)->FlushReports(*this);
208
209 // Iterate through BugTypes a second time. BugTypes may have been updated
210 // with new BugType objects and new warnings.
211 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
212 BugType *BT = const_cast<BugType*>(*I);
213
214 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
215 SetTy& EQClasses = BT->EQClasses;
216
217 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
218 BugReportEquivClass& EQ = *EI;
219 FlushReport(EQ);
220 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000221
Ted Kremenekcf118d42009-02-04 23:49:09 +0000222 // Delete the BugType object. This will also delete the equivalence
223 // classes.
224 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000225 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000226
227 // Remove all references to the BugType objects.
228 BugTypes = F.GetEmptySet();
229}
230
231//===----------------------------------------------------------------------===//
232// PathDiagnostics generation.
233//===----------------------------------------------------------------------===//
234
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000235typedef llvm::DenseMap<const ExplodedNode<GRState>*,
236 const ExplodedNode<GRState>*> NodeBackMap;
237
238static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000239 std::pair<ExplodedNode<GRState>*, unsigned> >
240MakeReportGraph(const ExplodedGraph<GRState>* G,
241 const ExplodedNode<GRState>** NStart,
242 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000243
Ted Kremenekcf118d42009-02-04 23:49:09 +0000244 // Create the trimmed graph. It will contain the shortest paths from the
245 // error nodes to the root. In the new graph we should only have one
246 // error node unless there are two or more error nodes with the same minimum
247 // path length.
248 ExplodedGraph<GRState>* GTrim;
249 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000250
251 llvm::DenseMap<const void*, const void*> InverseMap;
252 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000253
254 // Create owning pointers for GTrim and NMap just to ensure that they are
255 // released when this function exists.
256 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
257 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
258
259 // Find the (first) error node in the trimmed graph. We just need to consult
260 // the node map (NMap) which maps from nodes in the original graph to nodes
261 // in the new graph.
262 const ExplodedNode<GRState>* N = 0;
263 unsigned NodeIndex = 0;
264
265 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
266 if ((N = NMap->getMappedNode(*I))) {
267 NodeIndex = (I - NStart) / sizeof(*I);
268 break;
269 }
270
271 assert(N && "No error node found in the trimmed graph.");
272
273 // Create a new (third!) graph with a single path. This is the graph
274 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000275 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000276 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
277 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000278
279 // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000280 // to the root node, and then construct a new graph that contains only
281 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000282 llvm::DenseMap<const void*,unsigned> Visited;
283 llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000284 WS.push_back(N);
285 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000286 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000287
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000288 while (!WS.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000289 const ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000290 WS.pop_back();
291
292 if (Visited.find(Node) != Visited.end())
293 continue;
294
295 Visited[Node] = cnt++;
296
297 if (Node->pred_empty()) {
298 Root = Node;
299 break;
300 }
301
Ted Kremenek3148eb42009-01-24 00:55:43 +0000302 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000303 E=Node->pred_end(); I!=E; ++I)
304 WS.push_back(*I);
305 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000306
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000307 assert (Root);
308
309 // Now walk from the root down the DFS path, always taking the successor
310 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000311 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000312 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000313
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000314 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000315 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000316 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000317 assert (I != Visited.end());
318
319 // Create the equivalent node in the new graph with the same state
320 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000321 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000322 GNew->getNode(N->getLocation(), N->getState());
323
324 // Store the mapping to the original node.
325 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
326 assert(IMitr != InverseMap.end() && "No mapping to original node.");
327 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000328
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000329 // Link up the new node with the previous node.
330 if (Last)
331 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000332
333 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000334
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000335 // Are we at the final node?
336 if (I->second == 0) {
337 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000338 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000339 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000340
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000341 // Find the next successor node. We choose the node that is marked
342 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000343 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
344 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000345 N = 0;
346
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000347 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000348
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000349 I = Visited.find(*SI);
350
351 if (I == Visited.end())
352 continue;
353
354 if (!N || I->second < MinVal) {
355 N = *SI;
356 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000357 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000358 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000359
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000360 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000361 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000362
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000363 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000364 return std::make_pair(std::make_pair(GNew, BM),
365 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000366}
367
Ted Kremenek3148eb42009-01-24 00:55:43 +0000368static const VarDecl*
369GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
370 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000371
372 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
373
374 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000375
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000376 if (!isa<PostStmt>(P))
377 continue;
378
379 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000380
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000381 if (!DR)
382 continue;
383
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000384 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000385
386 if (X != Y)
387 continue;
388
389 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
390
391 if (!VD)
392 continue;
393
394 return VD;
395 }
396
397 return 0;
398}
399
Ted Kremenek9e240492008-10-04 05:50:14 +0000400namespace {
401class VISIBILITY_HIDDEN NotableSymbolHandler
402 : public StoreManager::BindingsHandler {
403
Ted Kremenek2dabd432008-12-05 02:27:51 +0000404 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000405 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000406 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000407 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000408 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000409 PathDiagnostic& PD;
410 BugReporter& BR;
411
412public:
413
Ted Kremenek3148eb42009-01-24 00:55:43 +0000414 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
415 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000416 PathDiagnostic& pd, BugReporter& br)
417 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
418
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000419 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000420
Ted Kremenek2dabd432008-12-05 02:27:51 +0000421 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000422
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000423 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000424 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000425 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000426 ScanSym = SV->getSymbol();
427 else
428 return true;
429
430 if (ScanSym != Sym)
431 return true;
432
433 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000434 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000435
436 if (X == V) // Same binding?
437 return true;
438
439 // Different binding. Only handle assignments for now. We don't pull
440 // this check out of the loop because we will eventually handle other
441 // cases.
442
443 VarDecl *VD = 0;
444
Ted Kremenek3148eb42009-01-24 00:55:43 +0000445 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000446 if (!B->isAssignmentOp())
447 return true;
448
449 // What variable did we assign to?
450 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
451
452 if (!DR)
453 return true;
454
455 VD = dyn_cast<VarDecl>(DR->getDecl());
456 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000457 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000458 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
459 // assume that each DeclStmt has a single Decl. This invariant
460 // holds by contruction in the CFG.
461 VD = dyn_cast<VarDecl>(*DS->decl_begin());
462 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000463
464 if (!VD)
465 return true;
466
467 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000468 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000469
470 if (!MostRecent)
471 return true;
472
473 // Create the diagnostic.
474
475 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
476
477 if (VD->getType()->isPointerLikeType()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000478 std::string msg = "'" + std::string(VD->getNameAsString()) +
479 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000480
481 PD.push_front(new PathDiagnosticPiece(L, msg));
482 }
483
484 return true;
485 }
486};
487}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000488
Ted Kremenek3148eb42009-01-24 00:55:43 +0000489static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
490 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000491 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000492 PathDiagnostic& PD) {
493
Ted Kremenek3148eb42009-01-24 00:55:43 +0000494 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000495 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000496
497 if (!PrevSt)
498 return;
499
Ted Kremenek9e240492008-10-04 05:50:14 +0000500 // Look at the region bindings of the current state that map to the
501 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000502 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000503 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
504 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
505}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000506
Ted Kremenek9e240492008-10-04 05:50:14 +0000507namespace {
508class VISIBILITY_HIDDEN ScanNotableSymbols
509 : public StoreManager::BindingsHandler {
510
Ted Kremenek2dabd432008-12-05 02:27:51 +0000511 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000512 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000513 Stmt* S;
514 GRBugReporter& BR;
515 PathDiagnostic& PD;
516
517public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000518 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000519 PathDiagnostic& pd)
520 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000521
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000522 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000523 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000524
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000525 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000526 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000527 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000528 ScanSym = SV->getSymbol();
529 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000530 return true;
531
532 assert (ScanSym.isInitialized());
533
534 if (!BR.isNotable(ScanSym))
535 return true;
536
537 if (AlreadyProcessed.count(ScanSym))
538 return true;
539
540 AlreadyProcessed.insert(ScanSym);
541
542 HandleNotableSymbol(N, S, ScanSym, BR, PD);
543 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000544 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000545};
546} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000547
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000548namespace {
549class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
550 NodeBackMap& M;
551public:
552 NodeMapClosure(NodeBackMap *m) : M(*m) {}
553 ~NodeMapClosure() {}
554
555 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
556 NodeBackMap::iterator I = M.find(N);
557 return I == M.end() ? 0 : I->second;
558 }
559};
560}
561
Ted Kremenekc0959972008-07-02 21:24:01 +0000562void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000563 BugReportEquivClass& EQ) {
564
565 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000566
Ted Kremenekcf118d42009-02-04 23:49:09 +0000567 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
568 const ExplodedNode<GRState>* N = I->getEndNode();
569 if (N) Nodes.push_back(N);
570 }
571
572 if (Nodes.empty())
573 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000574
575 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000576 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000577 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000578 std::pair<ExplodedNode<GRState>*, unsigned> >&
579 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000580
Ted Kremenekcf118d42009-02-04 23:49:09 +0000581 // Find the BugReport with the original location.
582 BugReport *R = 0;
583 unsigned i = 0;
584 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
585 if (i == GPair.second.second) { R = *I; break; }
586
587 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000588
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000589 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
590 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000591 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000592
Ted Kremenekcf118d42009-02-04 23:49:09 +0000593 // Start building the path diagnostic...
594 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000595 PD.push_back(Piece);
596 else
597 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000598
Ted Kremenek3148eb42009-01-24 00:55:43 +0000599 const ExplodedNode<GRState>* NextNode = N->pred_empty()
600 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000601
Ted Kremenekc0959972008-07-02 21:24:01 +0000602 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000603 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000604 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000605
Ted Kremenek6837faa2008-04-09 00:20:43 +0000606 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000607 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000608 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000609
610 ProgramPoint P = N->getLocation();
611
612 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
613
614 CFGBlock* Src = BE->getSrc();
615 CFGBlock* Dst = BE->getDst();
616
617 Stmt* T = Src->getTerminator();
618
619 if (!T)
620 continue;
621
622 FullSourceLoc L(T->getLocStart(), SMgr);
623
624 switch (T->getStmtClass()) {
625 default:
626 break;
627
628 case Stmt::GotoStmtClass:
629 case Stmt::IndirectGotoStmtClass: {
630
Ted Kremenekb697b102009-02-23 22:44:26 +0000631 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000632
633 if (!S)
634 continue;
635
Ted Kremenek297308e2009-02-10 23:56:07 +0000636 std::string sbuf;
637 llvm::raw_string_ostream os(sbuf);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000638
639 os << "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000640 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000641
642 PD.push_front(new PathDiagnosticPiece(L, os.str()));
643 break;
644 }
645
Ted Kremenek297308e2009-02-10 23:56:07 +0000646 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000647 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000648 std::string sbuf;
649 llvm::raw_string_ostream os(sbuf);
Ted Kremenek5a429952008-04-23 23:35:07 +0000650
651 if (Stmt* S = Dst->getLabel())
652 switch (S->getStmtClass()) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000653 default:
Ted Kremenek3ddc4d52008-12-20 01:41:43 +0000654 os << "No cases match in the switch statement. "
655 "Control jumps to line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000656 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000657 break;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000658 case Stmt::DefaultStmtClass:
659 os << "Control jumps to the 'default' case at line "
660 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
661 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000662
663 case Stmt::CaseStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000664 os << "Control jumps to 'case ";
665
Ted Kremenek5a429952008-04-23 23:35:07 +0000666 CaseStmt* Case = cast<CaseStmt>(S);
667 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000668
Ted Kremenek5a429952008-04-23 23:35:07 +0000669 // Determine if it is an enum.
Ted Kremenek61f3e052008-04-03 04:42:52 +0000670
Ted Kremenek5a429952008-04-23 23:35:07 +0000671 bool GetRawInt = true;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000672
Ted Kremenek5a429952008-04-23 23:35:07 +0000673 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
674
675 // FIXME: Maybe this should be an assertion. Are there cases
676 // were it is not an EnumConstantDecl?
Chris Lattner470e5fc2008-11-18 06:07:40 +0000677 EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000678 if (D) {
679 GetRawInt = false;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000680 os << D->getNameAsString();
Ted Kremenek5a429952008-04-23 23:35:07 +0000681 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000682 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000683
684 if (GetRawInt) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000685
Ted Kremenek5a429952008-04-23 23:35:07 +0000686 // Not an enum.
687 Expr* CondE = cast<SwitchStmt>(T)->getCond();
688 unsigned bits = Ctx.getTypeSize(CondE->getType());
689 llvm::APSInt V(bits, false);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000690
Ted Kremenek5a429952008-04-23 23:35:07 +0000691 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
692 assert (false && "Case condition must be constant.");
Ted Kremenek61f3e052008-04-03 04:42:52 +0000693 continue;
694 }
695
Ted Kremenek297308e2009-02-10 23:56:07 +0000696 os << V;
697 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000698
Ted Kremenek61f3e052008-04-03 04:42:52 +0000699 os << ":' at line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000700 << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000701
702 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000703 }
704 }
Ted Kremenek56783922008-04-25 01:29:56 +0000705 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000706 os << "'Default' branch taken. ";
Ted Kremenekb697b102009-02-23 22:44:26 +0000707 ExecutionContinues(os, SMgr, N);
Ted Kremenek56783922008-04-25 01:29:56 +0000708 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000709
710 PD.push_front(new PathDiagnosticPiece(L, os.str()));
711 break;
712 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000713
714 case Stmt::BreakStmtClass:
715 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000716 std::string sbuf;
717 llvm::raw_string_ostream os(sbuf);
Ted Kremenekb697b102009-02-23 22:44:26 +0000718 ExecutionContinues(os, SMgr, N);
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000719 PD.push_front(new PathDiagnosticPiece(L, os.str()));
720 break;
721 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000722
723 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000724 std::string sbuf;
725 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000726 os << "'?' condition evaluates to ";
727
728 if (*(Src->succ_begin()+1) == Dst)
729 os << "false.";
730 else
731 os << "true.";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000732
Ted Kremenek297308e2009-02-10 23:56:07 +0000733 PD.push_front(new PathDiagnosticPiece(L, os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000734 break;
735 }
736
737 case Stmt::DoStmtClass: {
738
739 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000740 std::string sbuf;
741 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000742
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000743 os << "Loop condition is true. ";
Ted Kremenekb697b102009-02-23 22:44:26 +0000744 ExecutionContinues(os, SMgr, N);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000745
746 PD.push_front(new PathDiagnosticPiece(L, os.str()));
747 }
748 else
749 PD.push_front(new PathDiagnosticPiece(L,
750 "Loop condition is false. Exiting loop."));
751
752 break;
753 }
754
Ted Kremenek61f3e052008-04-03 04:42:52 +0000755 case Stmt::WhileStmtClass:
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000756 case Stmt::ForStmtClass: {
757
758 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000759 std::string sbuf;
760 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000761
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000762 os << "Loop condition is false. ";
Ted Kremenekb697b102009-02-23 22:44:26 +0000763 ExecutionContinues(os, SMgr, N);
Ted Kremenek297308e2009-02-10 23:56:07 +0000764
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000765 PD.push_front(new PathDiagnosticPiece(L, os.str()));
766 }
767 else
768 PD.push_front(new PathDiagnosticPiece(L,
769 "Loop condition is true. Entering loop body."));
770
771 break;
772 }
773
Ted Kremenek297308e2009-02-10 23:56:07 +0000774 case Stmt::IfStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000775 if (*(Src->succ_begin()+1) == Dst)
776 PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
777 else
778 PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
779
780 break;
781 }
782 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000783 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000784
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000785 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
786 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000787 PD.push_front(p);
788
Ted Kremenek9e240492008-10-04 05:50:14 +0000789 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
790 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000791 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000792 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
793 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000794 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000795 }
796}
797
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000798
Ted Kremenekcf118d42009-02-04 23:49:09 +0000799void BugReporter::Register(BugType *BT) {
800 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000801}
802
Ted Kremenekcf118d42009-02-04 23:49:09 +0000803void BugReporter::EmitReport(BugReport* R) {
804 // Compute the bug report's hash to determine its equivalence class.
805 llvm::FoldingSetNodeID ID;
806 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000807
Ted Kremenekcf118d42009-02-04 23:49:09 +0000808 // Lookup the equivance class. If there isn't one, create it.
809 BugType& BT = R->getBugType();
810 Register(&BT);
811 void *InsertPos;
812 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
813
814 if (!EQ) {
815 EQ = new BugReportEquivClass(R);
816 BT.EQClasses.InsertNode(EQ, InsertPos);
817 }
818 else
819 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000820}
821
Ted Kremenekcf118d42009-02-04 23:49:09 +0000822void BugReporter::FlushReport(BugReportEquivClass& EQ) {
823 assert(!EQ.Reports.empty());
824 BugReport &R = **EQ.begin();
825
826 // FIXME: Make sure we use the 'R' for the path that was actually used.
827 // Probably doesn't make a difference in practice.
828 BugType& BT = R.getBugType();
829
830 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
831 R.getDescription(),
832 BT.getCategory()));
833 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000834
835 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000836 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000837 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000838
Ted Kremenek3148eb42009-01-24 00:55:43 +0000839 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000840 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000841 const SourceRange *Beg = 0, *End = 0;
842 R.getRanges(*this, Beg, End);
843 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000844 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000845 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
846 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000847
Ted Kremenek3148eb42009-01-24 00:55:43 +0000848 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000849 default: assert(0 && "Don't handle this many ranges yet!");
850 case 0: Diag.Report(L, ErrorDiag); break;
851 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
852 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
853 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000854 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000855
856 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
857 if (!PD)
858 return;
859
860 if (D->empty()) {
861 PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription());
862 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
863 D->push_back(piece);
864 }
865
866 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000867}
Ted Kremenek57202072008-07-14 17:40:50 +0000868
Ted Kremenek8c036c72008-09-20 04:23:38 +0000869void BugReporter::EmitBasicReport(const char* name, const char* str,
870 SourceLocation Loc,
871 SourceRange* RBeg, unsigned NumRanges) {
872 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
873}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000874
Ted Kremenek8c036c72008-09-20 04:23:38 +0000875void BugReporter::EmitBasicReport(const char* name, const char* category,
876 const char* str, SourceLocation Loc,
877 SourceRange* RBeg, unsigned NumRanges) {
878
Ted Kremenekcf118d42009-02-04 23:49:09 +0000879 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
880 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +0000881 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000882 RangedBugReport *R = new DiagBugReport(*BT, str, L);
883 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
884 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +0000885}