blob: 8719be7fddd424ac283df469225d31ee281be165 [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
Ted Kremenek082cb8d2009-03-12 18:41:53 +000087static SourceLocation ExecutionContinues(SourceManager& SMgr,
88 const ExplodedNode<GRState>* N,
89 const Decl& D,
90 bool* OutHasStmt = 0) {
91 if (Stmt *S = GetNextStmt(N)) {
92 if (OutHasStmt) *OutHasStmt = true;
93 return S->getLocStart();
94 }
95 else {
96 if (OutHasStmt) *OutHasStmt = false;
97 return D.getBody()->getRBracLoc();
98 }
99}
100
101static SourceLocation ExecutionContinues(llvm::raw_string_ostream& os,
102 SourceManager& SMgr,
103 const ExplodedNode<GRState>* N,
104 const Decl& D) {
Ted Kremenekb479dad2009-02-23 23:13:51 +0000105
Ted Kremenek143ca222008-05-06 18:11:09 +0000106 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000107 if (os.str().empty())
108 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000109
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000110 bool hasStmt;
111 SourceLocation Loc = ExecutionContinues(SMgr, N, D, &hasStmt);
112
113 if (hasStmt)
Ted Kremenekb697b102009-02-23 22:44:26 +0000114 os << "Execution continues on line "
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000115 << SMgr.getInstantiationLineNumber(Loc) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000116 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000117 os << "Execution jumps to the end of the "
118 << (isa<ObjCMethodDecl>(D) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000119
120 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000121}
122
Ted Kremenekcf118d42009-02-04 23:49:09 +0000123//===----------------------------------------------------------------------===//
124// Methods for BugType and subclasses.
125//===----------------------------------------------------------------------===//
126BugType::~BugType() {}
127void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000128
Ted Kremenekcf118d42009-02-04 23:49:09 +0000129//===----------------------------------------------------------------------===//
130// Methods for BugReport and subclasses.
131//===----------------------------------------------------------------------===//
132BugReport::~BugReport() {}
133RangedBugReport::~RangedBugReport() {}
134
135Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000136 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000137 Stmt *S = NULL;
138
Ted Kremenekcf118d42009-02-04 23:49:09 +0000139 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000140 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000141 }
142 if (!S) S = GetStmt(ProgP);
143
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000144 return S;
145}
146
147PathDiagnosticPiece*
148BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000149 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000150
151 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000152
153 if (!S)
154 return NULL;
155
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000156 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000157 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000158
Ted Kremenekde7161f2008-04-03 18:00:37 +0000159 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000160 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000161
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000162 for (; Beg != End; ++Beg)
163 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000164
165 return P;
166}
167
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000168void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
169 const SourceRange*& end) {
170
171 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
172 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000173 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000174 beg = &R;
175 end = beg+1;
176 }
177 else
178 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000179}
180
Ted Kremenekcf118d42009-02-04 23:49:09 +0000181SourceLocation BugReport::getLocation() const {
182 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000183 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
184 // For member expressions, return the location of the '.' or '->'.
185 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
186 return ME->getMemberLoc();
187
Ted Kremenekcf118d42009-02-04 23:49:09 +0000188 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000189 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000190
191 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000192}
193
Ted Kremenek3148eb42009-01-24 00:55:43 +0000194PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
195 const ExplodedNode<GRState>* PrevN,
196 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000197 BugReporter& BR,
198 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000199 return NULL;
200}
201
Ted Kremenekcf118d42009-02-04 23:49:09 +0000202//===----------------------------------------------------------------------===//
203// Methods for BugReporter and subclasses.
204//===----------------------------------------------------------------------===//
205
206BugReportEquivClass::~BugReportEquivClass() {
207 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
208}
209
210GRBugReporter::~GRBugReporter() { FlushReports(); }
211BugReporterData::~BugReporterData() {}
212
213ExplodedGraph<GRState>&
214GRBugReporter::getGraph() { return Eng.getGraph(); }
215
216GRStateManager&
217GRBugReporter::getStateManager() { return Eng.getStateManager(); }
218
219BugReporter::~BugReporter() { FlushReports(); }
220
221void BugReporter::FlushReports() {
222 if (BugTypes.isEmpty())
223 return;
224
225 // First flush the warnings for each BugType. This may end up creating new
226 // warnings and new BugTypes. Because ImmutableSet is a functional data
227 // structure, we do not need to worry about the iterators being invalidated.
228 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
229 const_cast<BugType*>(*I)->FlushReports(*this);
230
231 // Iterate through BugTypes a second time. BugTypes may have been updated
232 // with new BugType objects and new warnings.
233 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
234 BugType *BT = const_cast<BugType*>(*I);
235
236 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
237 SetTy& EQClasses = BT->EQClasses;
238
239 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
240 BugReportEquivClass& EQ = *EI;
241 FlushReport(EQ);
242 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000243
Ted Kremenekcf118d42009-02-04 23:49:09 +0000244 // Delete the BugType object. This will also delete the equivalence
245 // classes.
246 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000247 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000248
249 // Remove all references to the BugType objects.
250 BugTypes = F.GetEmptySet();
251}
252
253//===----------------------------------------------------------------------===//
254// PathDiagnostics generation.
255//===----------------------------------------------------------------------===//
256
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000257typedef llvm::DenseMap<const ExplodedNode<GRState>*,
258 const ExplodedNode<GRState>*> NodeBackMap;
259
260static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000261 std::pair<ExplodedNode<GRState>*, unsigned> >
262MakeReportGraph(const ExplodedGraph<GRState>* G,
263 const ExplodedNode<GRState>** NStart,
264 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000265
Ted Kremenekcf118d42009-02-04 23:49:09 +0000266 // Create the trimmed graph. It will contain the shortest paths from the
267 // error nodes to the root. In the new graph we should only have one
268 // error node unless there are two or more error nodes with the same minimum
269 // path length.
270 ExplodedGraph<GRState>* GTrim;
271 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000272
273 llvm::DenseMap<const void*, const void*> InverseMap;
274 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000275
276 // Create owning pointers for GTrim and NMap just to ensure that they are
277 // released when this function exists.
278 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
279 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
280
281 // Find the (first) error node in the trimmed graph. We just need to consult
282 // the node map (NMap) which maps from nodes in the original graph to nodes
283 // in the new graph.
284 const ExplodedNode<GRState>* N = 0;
285 unsigned NodeIndex = 0;
286
287 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
288 if ((N = NMap->getMappedNode(*I))) {
289 NodeIndex = (I - NStart) / sizeof(*I);
290 break;
291 }
292
293 assert(N && "No error node found in the trimmed graph.");
294
295 // Create a new (third!) graph with a single path. This is the graph
296 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000297 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000298 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
299 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000300
301 // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000302 // to the root node, and then construct a new graph that contains only
303 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000304 llvm::DenseMap<const void*,unsigned> Visited;
305 llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000306 WS.push_back(N);
307 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000308 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000309
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000310 while (!WS.empty()) {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000311 const ExplodedNode<GRState>* Node = WS.back();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000312 WS.pop_back();
313
314 if (Visited.find(Node) != Visited.end())
315 continue;
316
317 Visited[Node] = cnt++;
318
319 if (Node->pred_empty()) {
320 Root = Node;
321 break;
322 }
323
Ted Kremenek3148eb42009-01-24 00:55:43 +0000324 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000325 E=Node->pred_end(); I!=E; ++I)
326 WS.push_back(*I);
327 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000328
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000329 assert (Root);
330
331 // Now walk from the root down the DFS path, always taking the successor
332 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000333 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000334 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000335
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000336 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000337 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000338 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000339 assert (I != Visited.end());
340
341 // Create the equivalent node in the new graph with the same state
342 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000343 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000344 GNew->getNode(N->getLocation(), N->getState());
345
346 // Store the mapping to the original node.
347 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
348 assert(IMitr != InverseMap.end() && "No mapping to original node.");
349 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000350
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000351 // Link up the new node with the previous node.
352 if (Last)
353 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000354
355 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000356
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000357 // Are we at the final node?
358 if (I->second == 0) {
359 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000360 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000361 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000362
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000363 // Find the next successor node. We choose the node that is marked
364 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000365 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
366 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000367 N = 0;
368
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000369 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000370
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000371 I = Visited.find(*SI);
372
373 if (I == Visited.end())
374 continue;
375
376 if (!N || I->second < MinVal) {
377 N = *SI;
378 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000379 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000380 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000381
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000382 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000383 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000384
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000385 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000386 return std::make_pair(std::make_pair(GNew, BM),
387 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000388}
389
Ted Kremenek3148eb42009-01-24 00:55:43 +0000390static const VarDecl*
391GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
392 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000393
394 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
395
396 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000397
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000398 if (!isa<PostStmt>(P))
399 continue;
400
401 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000402
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000403 if (!DR)
404 continue;
405
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000406 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000407
408 if (X != Y)
409 continue;
410
411 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
412
413 if (!VD)
414 continue;
415
416 return VD;
417 }
418
419 return 0;
420}
421
Ted Kremenek9e240492008-10-04 05:50:14 +0000422namespace {
423class VISIBILITY_HIDDEN NotableSymbolHandler
424 : public StoreManager::BindingsHandler {
425
Ted Kremenek2dabd432008-12-05 02:27:51 +0000426 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000427 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000428 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000429 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000430 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000431 PathDiagnostic& PD;
432 BugReporter& BR;
433
434public:
435
Ted Kremenek3148eb42009-01-24 00:55:43 +0000436 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
437 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000438 PathDiagnostic& pd, BugReporter& br)
439 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
440
Ted Kremenekbe912242009-03-05 16:31:07 +0000441 bool HandleBinding(StoreManager& SMgr, Store store,
442 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000443
Ted Kremenek2dabd432008-12-05 02:27:51 +0000444 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000445
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000446 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000447 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000448 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000449 ScanSym = SV->getSymbol();
450 else
451 return true;
452
453 if (ScanSym != Sym)
454 return true;
455
456 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000457 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000458
459 if (X == V) // Same binding?
460 return true;
461
462 // Different binding. Only handle assignments for now. We don't pull
463 // this check out of the loop because we will eventually handle other
464 // cases.
465
466 VarDecl *VD = 0;
467
Ted Kremenek3148eb42009-01-24 00:55:43 +0000468 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000469 if (!B->isAssignmentOp())
470 return true;
471
472 // What variable did we assign to?
473 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
474
475 if (!DR)
476 return true;
477
478 VD = dyn_cast<VarDecl>(DR->getDecl());
479 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000480 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000481 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
482 // assume that each DeclStmt has a single Decl. This invariant
483 // holds by contruction in the CFG.
484 VD = dyn_cast<VarDecl>(*DS->decl_begin());
485 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000486
487 if (!VD)
488 return true;
489
490 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000491 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000492
493 if (!MostRecent)
494 return true;
495
496 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000497 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
498
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000499 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000500 std::string msg = "'" + std::string(VD->getNameAsString()) +
501 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000502
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000503 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000504 }
505
506 return true;
507 }
508};
509}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000510
Ted Kremenek3148eb42009-01-24 00:55:43 +0000511static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
512 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000513 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000514 PathDiagnostic& PD) {
515
Ted Kremenek3148eb42009-01-24 00:55:43 +0000516 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000517 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000518
519 if (!PrevSt)
520 return;
521
Ted Kremenek9e240492008-10-04 05:50:14 +0000522 // Look at the region bindings of the current state that map to the
523 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000524 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000525 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
526 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
527}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000528
Ted Kremenek9e240492008-10-04 05:50:14 +0000529namespace {
530class VISIBILITY_HIDDEN ScanNotableSymbols
531 : public StoreManager::BindingsHandler {
532
Ted Kremenek2dabd432008-12-05 02:27:51 +0000533 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000534 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000535 Stmt* S;
536 GRBugReporter& BR;
537 PathDiagnostic& PD;
538
539public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000540 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000541 PathDiagnostic& pd)
542 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000543
Ted Kremenekbe912242009-03-05 16:31:07 +0000544 bool HandleBinding(StoreManager& SMgr, Store store,
545 const MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000546 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000547
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000548 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000549 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000550 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000551 ScanSym = SV->getSymbol();
552 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000553 return true;
554
Ted Kremenek94c96982009-03-03 22:06:47 +0000555 assert (ScanSym.isValid());
Ted Kremenek9e240492008-10-04 05:50:14 +0000556
557 if (!BR.isNotable(ScanSym))
558 return true;
559
560 if (AlreadyProcessed.count(ScanSym))
561 return true;
562
563 AlreadyProcessed.insert(ScanSym);
564
565 HandleNotableSymbol(N, S, ScanSym, BR, PD);
566 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000567 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000568};
569} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000570
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000571namespace {
572class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
573 NodeBackMap& M;
574public:
575 NodeMapClosure(NodeBackMap *m) : M(*m) {}
576 ~NodeMapClosure() {}
577
578 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
579 NodeBackMap::iterator I = M.find(N);
580 return I == M.end() ? 0 : I->second;
581 }
582};
583}
584
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000585/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
586/// and collapses PathDiagosticPieces that are expanded by macros.
587static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
588 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
589 MacroStackTy;
590
591 typedef std::vector<PathDiagnosticPiece*>
592 PiecesTy;
593
594 MacroStackTy MacroStack;
595 PiecesTy Pieces;
596
597 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
598 // Get the location of the PathDiagnosticPiece.
599 const FullSourceLoc Loc = I->getLocation();
600
601 // Determine the instantiation location, which is the location we group
602 // related PathDiagnosticPieces.
603 SourceLocation InstantiationLoc = Loc.isMacroID() ?
604 SM.getInstantiationLoc(Loc) :
605 SourceLocation();
606
607 if (Loc.isFileID()) {
608 MacroStack.clear();
609 Pieces.push_back(&*I);
610 continue;
611 }
612
613 assert(Loc.isMacroID());
614
615 // Is the PathDiagnosticPiece within the same macro group?
616 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
617 MacroStack.back().first->push_back(&*I);
618 continue;
619 }
620
621 // We aren't in the same group. Are we descending into a new macro
622 // or are part of an old one?
623 PathDiagnosticMacroPiece *MacroGroup = 0;
624
625 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
626 SM.getInstantiationLoc(Loc) :
627 SourceLocation();
628
629 // Walk the entire macro stack.
630 while (!MacroStack.empty()) {
631 if (InstantiationLoc == MacroStack.back().second) {
632 MacroGroup = MacroStack.back().first;
633 break;
634 }
635
636 if (ParentInstantiationLoc == MacroStack.back().second) {
637 MacroGroup = MacroStack.back().first;
638 break;
639 }
640
641 MacroStack.pop_back();
642 }
643
644 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
645 // Create a new macro group and add it to the stack.
646 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
647
648 if (MacroGroup)
649 MacroGroup->push_back(NewGroup);
650 else {
651 assert(InstantiationLoc.isFileID());
652 Pieces.push_back(NewGroup);
653 }
654
655 MacroGroup = NewGroup;
656 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
657 }
658
659 // Finally, add the PathDiagnosticPiece to the group.
660 MacroGroup->push_back(&*I);
661 }
662
663 // Now take the pieces and construct a new PathDiagnostic.
664 PD.resetPath(false);
665
666 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
667 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
668 if (!MP->containsEvent()) {
669 delete MP;
670 continue;
671 }
672
673 PD.push_back(*I);
674 }
675}
676
Ted Kremenekc0959972008-07-02 21:24:01 +0000677void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000678 BugReportEquivClass& EQ) {
679
680 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000681
Ted Kremenekcf118d42009-02-04 23:49:09 +0000682 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
683 const ExplodedNode<GRState>* N = I->getEndNode();
684 if (N) Nodes.push_back(N);
685 }
686
687 if (Nodes.empty())
688 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000689
690 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000691 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000692 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000693 std::pair<ExplodedNode<GRState>*, unsigned> >&
694 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000695
Ted Kremenekcf118d42009-02-04 23:49:09 +0000696 // Find the BugReport with the original location.
697 BugReport *R = 0;
698 unsigned i = 0;
699 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
700 if (i == GPair.second.second) { R = *I; break; }
701
702 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000703
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000704 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
705 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000706 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000707
Ted Kremenekcf118d42009-02-04 23:49:09 +0000708 // Start building the path diagnostic...
709 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000710 PD.push_back(Piece);
711 else
712 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000713
Ted Kremenek3148eb42009-01-24 00:55:43 +0000714 const ExplodedNode<GRState>* NextNode = N->pred_empty()
715 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000716
Ted Kremenekc0959972008-07-02 21:24:01 +0000717 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000718 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000719 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000720
Ted Kremenek6837faa2008-04-09 00:20:43 +0000721 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000722 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000723 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000724
725 ProgramPoint P = N->getLocation();
726
727 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000728 CFGBlock* Src = BE->getSrc();
729 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000730 Stmt* T = Src->getTerminator();
731
732 if (!T)
733 continue;
734
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000735 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000736
737 switch (T->getStmtClass()) {
738 default:
739 break;
740
741 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000742 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000743 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000744
745 if (!S)
746 continue;
747
Ted Kremenek297308e2009-02-10 23:56:07 +0000748 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000749 llvm::raw_string_ostream os(sbuf);
750 SourceLocation End = S->getLocStart();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000751
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000752 os << "Control jumps to line " << SMgr.getInstantiationLineNumber(End);
753 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
754 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000755 break;
756 }
757
Ted Kremenek297308e2009-02-10 23:56:07 +0000758 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000759 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000760 std::string sbuf;
761 llvm::raw_string_ostream os(sbuf);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000762 SourceLocation End;
763
764 if (Stmt* S = Dst->getLabel()) {
765 End = S->getLocStart();
766
Ted Kremenek5a429952008-04-23 23:35:07 +0000767 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000768 default:
769 os << "No cases match in the switch statement. "
770 "Control jumps to line "
771 << SMgr.getInstantiationLineNumber(End);
772 break;
773 case Stmt::DefaultStmtClass:
774 os << "Control jumps to the 'default' case at line "
775 << SMgr.getInstantiationLineNumber(End);
776 break;
777
778 case Stmt::CaseStmtClass: {
779 os << "Control jumps to 'case ";
780 CaseStmt* Case = cast<CaseStmt>(S);
781 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
782
783 // Determine if it is an enum.
784 bool GetRawInt = true;
785
786 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
787 // FIXME: Maybe this should be an assertion. Are there cases
788 // were it is not an EnumConstantDecl?
789 EnumConstantDecl* D =
790 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000791
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000792 if (D) {
793 GetRawInt = false;
794 os << D->getNameAsString();
795 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000796 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000797
798 if (GetRawInt) {
799
800 // Not an enum.
801 Expr* CondE = cast<SwitchStmt>(T)->getCond();
802 unsigned bits = Ctx.getTypeSize(CondE->getType());
803 llvm::APSInt V(bits, false);
804
805 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
806 assert (false && "Case condition must be constant.");
807 continue;
808 }
809
810 os << V;
811 }
812
813 os << ":' at line " << SMgr.getInstantiationLineNumber(End);
814 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000815 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000816 }
817 }
Ted Kremenek56783922008-04-25 01:29:56 +0000818 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000819 os << "'Default' branch taken. ";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000820 End = ExecutionContinues(os, SMgr, N,
821 getStateManager().getCodeDecl());
Ted Kremenek56783922008-04-25 01:29:56 +0000822 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000823
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000824 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
825 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000826 break;
827 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000828
829 case Stmt::BreakStmtClass:
830 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000831 std::string sbuf;
832 llvm::raw_string_ostream os(sbuf);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000833 SourceLocation End = ExecutionContinues(os, SMgr, N,
834 getStateManager().getCodeDecl());
835 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
836 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000837 break;
838 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000839
840 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000841 std::string sbuf;
842 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000843 os << "'?' condition evaluates to ";
844
845 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000846 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000847 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000848 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000849
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000850 SourceLocation End =
851 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
852
853 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
854 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000855 break;
856 }
857
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000858 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000859 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000860 std::string sbuf;
861 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000862
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000863 os << "Loop condition is true. ";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000864 SourceLocation End =
865 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
866 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
867 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000868 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000869 else {
870 SourceLocation End =
871 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
872 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
873 "Loop condition is false. Exiting loop"));
874 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000875
876 break;
877 }
878
Ted Kremenek61f3e052008-04-03 04:42:52 +0000879 case Stmt::WhileStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000880 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000881 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000882 std::string sbuf;
883 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000884
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000885 os << "Loop condition is false. ";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000886 SourceLocation End =
887 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek297308e2009-02-10 23:56:07 +0000888
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000889 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
890 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000891 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000892 else {
893 SourceLocation End =
894 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
895
896 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
897 "Loop condition is true. Entering loop body"));
898 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000899
900 break;
901 }
902
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000903 case Stmt::IfStmtClass: {
904 SourceLocation End =
905 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
906
Ted Kremenek61f3e052008-04-03 04:42:52 +0000907 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000908 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
909 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +0000910 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000911 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
912 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000913
914 break;
915 }
916 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000917 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000918
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000919 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
920 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000921 PD.push_front(p);
922
Ted Kremenek9e240492008-10-04 05:50:14 +0000923 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
924 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000925 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000926 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
927 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000928 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000929 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000930
931 // After constructing the full PathDiagnostic, do a pass over it to compact
932 // PathDiagnosticPieces that occur within a macro.
933 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000934}
935
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000936
Ted Kremenekcf118d42009-02-04 23:49:09 +0000937void BugReporter::Register(BugType *BT) {
938 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000939}
940
Ted Kremenekcf118d42009-02-04 23:49:09 +0000941void BugReporter::EmitReport(BugReport* R) {
942 // Compute the bug report's hash to determine its equivalence class.
943 llvm::FoldingSetNodeID ID;
944 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000945
Ted Kremenekcf118d42009-02-04 23:49:09 +0000946 // Lookup the equivance class. If there isn't one, create it.
947 BugType& BT = R->getBugType();
948 Register(&BT);
949 void *InsertPos;
950 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
951
952 if (!EQ) {
953 EQ = new BugReportEquivClass(R);
954 BT.EQClasses.InsertNode(EQ, InsertPos);
955 }
956 else
957 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000958}
959
Ted Kremenekcf118d42009-02-04 23:49:09 +0000960void BugReporter::FlushReport(BugReportEquivClass& EQ) {
961 assert(!EQ.Reports.empty());
962 BugReport &R = **EQ.begin();
963
964 // FIXME: Make sure we use the 'R' for the path that was actually used.
965 // Probably doesn't make a difference in practice.
966 BugType& BT = R.getBugType();
967
968 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
969 R.getDescription(),
970 BT.getCategory()));
971 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000972
973 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000974 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000975 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000976
Ted Kremenek3148eb42009-01-24 00:55:43 +0000977 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000978 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000979 const SourceRange *Beg = 0, *End = 0;
980 R.getRanges(*this, Beg, End);
981 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000982 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000983 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
984 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000985
Ted Kremenek3148eb42009-01-24 00:55:43 +0000986 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000987 default: assert(0 && "Don't handle this many ranges yet!");
988 case 0: Diag.Report(L, ErrorDiag); break;
989 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
990 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
991 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000992 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000993
994 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
995 if (!PD)
996 return;
997
998 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000999 PathDiagnosticPiece* piece =
1000 new PathDiagnosticEventPiece(L, R.getDescription());
1001
Ted Kremenek3148eb42009-01-24 00:55:43 +00001002 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1003 D->push_back(piece);
1004 }
1005
1006 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001007}
Ted Kremenek57202072008-07-14 17:40:50 +00001008
Ted Kremenek8c036c72008-09-20 04:23:38 +00001009void BugReporter::EmitBasicReport(const char* name, const char* str,
1010 SourceLocation Loc,
1011 SourceRange* RBeg, unsigned NumRanges) {
1012 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1013}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001014
Ted Kremenek8c036c72008-09-20 04:23:38 +00001015void BugReporter::EmitBasicReport(const char* name, const char* category,
1016 const char* str, SourceLocation Loc,
1017 SourceRange* RBeg, unsigned NumRanges) {
1018
Ted Kremenekcf118d42009-02-04 23:49:09 +00001019 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1020 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001021 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001022 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1023 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1024 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001025}