blob: e66b619bac2b0562c07f09330b010560dc8ac143 [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 Kremenek10aa5542009-03-12 23:41:59 +000027#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000028
29using namespace clang;
30
Ted Kremenekcf118d42009-02-04 23:49:09 +000031//===----------------------------------------------------------------------===//
32// static functions.
33//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
Ted Kremenekb697b102009-02-23 22:44:26 +000035static inline Stmt* GetStmt(ProgramPoint P) {
36 if (const PostStmt* PS = dyn_cast<PostStmt>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000037 return PS->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000038 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000039 return BE->getSrc()->getTerminator();
Ted Kremenek61f3e052008-04-03 04:42:52 +000040
Ted Kremenekb697b102009-02-23 22:44:26 +000041 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000042}
43
Ted Kremenek3148eb42009-01-24 00:55:43 +000044static inline const ExplodedNode<GRState>*
Ted Kremenekb697b102009-02-23 22:44:26 +000045GetPredecessorNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000046 return N->pred_empty() ? NULL : *(N->pred_begin());
47}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000048
Ted Kremenekb697b102009-02-23 22:44:26 +000049static inline const ExplodedNode<GRState>*
50GetSuccessorNode(const ExplodedNode<GRState>* N) {
51 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000052}
53
Ted Kremenekb697b102009-02-23 22:44:26 +000054static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) {
55 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
56 if (Stmt *S = GetStmt(N->getLocation()))
57 return S;
58
59 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000060}
61
Ted Kremenekb697b102009-02-23 22:44:26 +000062static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) {
63 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
64 if (Stmt *S = GetStmt(N->getLocation()))
65 return S;
66
67 return 0;
68}
69
70static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
71 if (Stmt *S = GetStmt(N->getLocation()))
72 return S;
73
74 return GetPreviousStmt(N);
75}
76
77static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
78 if (Stmt *S = GetStmt(N->getLocation()))
79 return S;
80
81 return GetNextStmt(N);
82}
83
84//===----------------------------------------------------------------------===//
85// Diagnostics for 'execution continues on line XXX'.
86//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +000087
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000088namespace {
89class VISIBILITY_HIDDEN PathDiagnosticBuilder {
90 SourceManager &SMgr;
91 const Decl& CodeDecl;
92 PathDiagnosticClient *PDC;
93public:
94 PathDiagnosticBuilder(SourceManager &smgr, const Decl& codedecl,
95 PathDiagnosticClient *pdc)
96 : SMgr(smgr), CodeDecl(codedecl), PDC(pdc) {}
97
98 FullSourceLoc ExecutionContinues(const ExplodedNode<GRState>* N,
99 bool* OutHasStmt = 0);
100
101 FullSourceLoc ExecutionContinues(llvm::raw_string_ostream& os,
102 const ExplodedNode<GRState>* N);
103
104 bool supportsLogicalOpControlFlow() const {
105 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
106 }
107};
108} // end anonymous namespace
109
110FullSourceLoc
111PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N,
112 bool* OutHasStmt) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000113 if (Stmt *S = GetNextStmt(N)) {
114 if (OutHasStmt) *OutHasStmt = true;
Ted Kremenek6f002042009-03-26 23:12:02 +0000115 return FullSourceLoc(S->getLocStart(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000116 }
117 else {
118 if (OutHasStmt) *OutHasStmt = false;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000119 return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000120 }
121}
122
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000123FullSourceLoc
124PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
125 const ExplodedNode<GRState>* N) {
126
Ted Kremenek143ca222008-05-06 18:11:09 +0000127 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000128 if (os.str().empty())
129 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000130
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000131 bool hasStmt;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000132 FullSourceLoc Loc = ExecutionContinues(N, &hasStmt);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000133
134 if (hasStmt)
Ted Kremenekb697b102009-02-23 22:44:26 +0000135 os << "Execution continues on line "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000136 << SMgr.getInstantiationLineNumber(Loc) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000137 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000138 os << "Execution jumps to the end of the "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000139 << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000140
141 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000142}
143
Ted Kremenekcf118d42009-02-04 23:49:09 +0000144//===----------------------------------------------------------------------===//
145// Methods for BugType and subclasses.
146//===----------------------------------------------------------------------===//
147BugType::~BugType() {}
148void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000149
Ted Kremenekcf118d42009-02-04 23:49:09 +0000150//===----------------------------------------------------------------------===//
151// Methods for BugReport and subclasses.
152//===----------------------------------------------------------------------===//
153BugReport::~BugReport() {}
154RangedBugReport::~RangedBugReport() {}
155
156Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000157 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000158 Stmt *S = NULL;
159
Ted Kremenekcf118d42009-02-04 23:49:09 +0000160 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000161 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000162 }
163 if (!S) S = GetStmt(ProgP);
164
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000165 return S;
166}
167
168PathDiagnosticPiece*
169BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000170 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000171
172 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000173
174 if (!S)
175 return NULL;
176
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000177 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000178 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000179
Ted Kremenekde7161f2008-04-03 18:00:37 +0000180 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000181 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000182
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000183 for (; Beg != End; ++Beg)
184 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000185
186 return P;
187}
188
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000189void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
190 const SourceRange*& end) {
191
192 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
193 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000194 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000195 beg = &R;
196 end = beg+1;
197 }
198 else
199 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000200}
201
Ted Kremenekcf118d42009-02-04 23:49:09 +0000202SourceLocation BugReport::getLocation() const {
203 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000204 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
205 // For member expressions, return the location of the '.' or '->'.
206 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
207 return ME->getMemberLoc();
208
Ted Kremenekcf118d42009-02-04 23:49:09 +0000209 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000210 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000211
212 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000213}
214
Ted Kremenek3148eb42009-01-24 00:55:43 +0000215PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
216 const ExplodedNode<GRState>* PrevN,
217 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000218 BugReporter& BR,
219 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000220 return NULL;
221}
222
Ted Kremenekcf118d42009-02-04 23:49:09 +0000223//===----------------------------------------------------------------------===//
224// Methods for BugReporter and subclasses.
225//===----------------------------------------------------------------------===//
226
227BugReportEquivClass::~BugReportEquivClass() {
228 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
229}
230
231GRBugReporter::~GRBugReporter() { FlushReports(); }
232BugReporterData::~BugReporterData() {}
233
234ExplodedGraph<GRState>&
235GRBugReporter::getGraph() { return Eng.getGraph(); }
236
237GRStateManager&
238GRBugReporter::getStateManager() { return Eng.getStateManager(); }
239
240BugReporter::~BugReporter() { FlushReports(); }
241
242void BugReporter::FlushReports() {
243 if (BugTypes.isEmpty())
244 return;
245
246 // First flush the warnings for each BugType. This may end up creating new
247 // warnings and new BugTypes. Because ImmutableSet is a functional data
248 // structure, we do not need to worry about the iterators being invalidated.
249 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
250 const_cast<BugType*>(*I)->FlushReports(*this);
251
252 // Iterate through BugTypes a second time. BugTypes may have been updated
253 // with new BugType objects and new warnings.
254 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
255 BugType *BT = const_cast<BugType*>(*I);
256
257 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
258 SetTy& EQClasses = BT->EQClasses;
259
260 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
261 BugReportEquivClass& EQ = *EI;
262 FlushReport(EQ);
263 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000264
Ted Kremenekcf118d42009-02-04 23:49:09 +0000265 // Delete the BugType object. This will also delete the equivalence
266 // classes.
267 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000268 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000269
270 // Remove all references to the BugType objects.
271 BugTypes = F.GetEmptySet();
272}
273
274//===----------------------------------------------------------------------===//
275// PathDiagnostics generation.
276//===----------------------------------------------------------------------===//
277
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000278typedef llvm::DenseMap<const ExplodedNode<GRState>*,
279 const ExplodedNode<GRState>*> NodeBackMap;
280
281static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000282 std::pair<ExplodedNode<GRState>*, unsigned> >
283MakeReportGraph(const ExplodedGraph<GRState>* G,
284 const ExplodedNode<GRState>** NStart,
285 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000286
Ted Kremenekcf118d42009-02-04 23:49:09 +0000287 // Create the trimmed graph. It will contain the shortest paths from the
288 // error nodes to the root. In the new graph we should only have one
289 // error node unless there are two or more error nodes with the same minimum
290 // path length.
291 ExplodedGraph<GRState>* GTrim;
292 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000293
294 llvm::DenseMap<const void*, const void*> InverseMap;
295 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000296
297 // Create owning pointers for GTrim and NMap just to ensure that they are
298 // released when this function exists.
299 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
300 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
301
302 // Find the (first) error node in the trimmed graph. We just need to consult
303 // the node map (NMap) which maps from nodes in the original graph to nodes
304 // in the new graph.
305 const ExplodedNode<GRState>* N = 0;
306 unsigned NodeIndex = 0;
307
308 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
309 if ((N = NMap->getMappedNode(*I))) {
310 NodeIndex = (I - NStart) / sizeof(*I);
311 break;
312 }
313
314 assert(N && "No error node found in the trimmed graph.");
315
316 // Create a new (third!) graph with a single path. This is the graph
317 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000318 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000319 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
320 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000321
Ted Kremenek10aa5542009-03-12 23:41:59 +0000322 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000323 // to the root node, and then construct a new graph that contains only
324 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000325 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000326 std::queue<const ExplodedNode<GRState>*> WS;
327 WS.push(N);
328
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000329 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000330 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000331
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000332 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000333 const ExplodedNode<GRState>* Node = WS.front();
334 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000335
336 if (Visited.find(Node) != Visited.end())
337 continue;
338
339 Visited[Node] = cnt++;
340
341 if (Node->pred_empty()) {
342 Root = Node;
343 break;
344 }
345
Ted Kremenek3148eb42009-01-24 00:55:43 +0000346 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000347 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000348 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000349 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000350
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000351 assert (Root);
352
Ted Kremenek10aa5542009-03-12 23:41:59 +0000353 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000354 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000355 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000356 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000357
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000358 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000359 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000360 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000361 assert (I != Visited.end());
362
363 // Create the equivalent node in the new graph with the same state
364 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000365 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000366 GNew->getNode(N->getLocation(), N->getState());
367
368 // Store the mapping to the original node.
369 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
370 assert(IMitr != InverseMap.end() && "No mapping to original node.");
371 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000372
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000373 // Link up the new node with the previous node.
374 if (Last)
375 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000376
377 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000378
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000379 // Are we at the final node?
380 if (I->second == 0) {
381 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000382 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000383 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000384
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000385 // Find the next successor node. We choose the node that is marked
386 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000387 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
388 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000389 N = 0;
390
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000391 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000392
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000393 I = Visited.find(*SI);
394
395 if (I == Visited.end())
396 continue;
397
398 if (!N || I->second < MinVal) {
399 N = *SI;
400 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000401 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000402 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000403
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000404 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000405 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000406
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000407 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000408 return std::make_pair(std::make_pair(GNew, BM),
409 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000410}
411
Ted Kremenek3148eb42009-01-24 00:55:43 +0000412static const VarDecl*
413GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
414 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000415
416 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
417
418 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000419
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000420 if (!isa<PostStmt>(P))
421 continue;
422
423 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000424
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000425 if (!DR)
426 continue;
427
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000428 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000429
430 if (X != Y)
431 continue;
432
433 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
434
435 if (!VD)
436 continue;
437
438 return VD;
439 }
440
441 return 0;
442}
443
Ted Kremenek9e240492008-10-04 05:50:14 +0000444namespace {
445class VISIBILITY_HIDDEN NotableSymbolHandler
446 : public StoreManager::BindingsHandler {
447
Ted Kremenek2dabd432008-12-05 02:27:51 +0000448 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000449 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000450 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000451 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000452 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000453 PathDiagnostic& PD;
454 BugReporter& BR;
455
456public:
457
Ted Kremenek3148eb42009-01-24 00:55:43 +0000458 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
459 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000460 PathDiagnostic& pd, BugReporter& br)
461 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
462
Ted Kremenekbe912242009-03-05 16:31:07 +0000463 bool HandleBinding(StoreManager& SMgr, Store store,
464 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000465
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000466 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000467
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000468 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000469 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000470 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000471 ScanSym = SV->getSymbol();
472 else
473 return true;
474
475 if (ScanSym != Sym)
476 return true;
477
478 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000479 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000480
481 if (X == V) // Same binding?
482 return true;
483
484 // Different binding. Only handle assignments for now. We don't pull
485 // this check out of the loop because we will eventually handle other
486 // cases.
487
488 VarDecl *VD = 0;
489
Ted Kremenek3148eb42009-01-24 00:55:43 +0000490 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000491 if (!B->isAssignmentOp())
492 return true;
493
494 // What variable did we assign to?
495 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
496
497 if (!DR)
498 return true;
499
500 VD = dyn_cast<VarDecl>(DR->getDecl());
501 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000502 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000503 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
504 // assume that each DeclStmt has a single Decl. This invariant
505 // holds by contruction in the CFG.
506 VD = dyn_cast<VarDecl>(*DS->decl_begin());
507 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000508
509 if (!VD)
510 return true;
511
512 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000513 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000514
515 if (!MostRecent)
516 return true;
517
518 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000519 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
520
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000521 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000522 std::string msg = "'" + std::string(VD->getNameAsString()) +
523 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000524
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000525 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000526 }
527
528 return true;
529 }
530};
531}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000532
Ted Kremenek3148eb42009-01-24 00:55:43 +0000533static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
534 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000535 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000536 PathDiagnostic& PD) {
537
Ted Kremenek3148eb42009-01-24 00:55:43 +0000538 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000539 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000540
541 if (!PrevSt)
542 return;
543
Ted Kremenek9e240492008-10-04 05:50:14 +0000544 // Look at the region bindings of the current state that map to the
545 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000546 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000547 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
548 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
549}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000550
Ted Kremenek9e240492008-10-04 05:50:14 +0000551namespace {
552class VISIBILITY_HIDDEN ScanNotableSymbols
553 : public StoreManager::BindingsHandler {
554
Ted Kremenek2dabd432008-12-05 02:27:51 +0000555 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000556 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000557 Stmt* S;
558 GRBugReporter& BR;
559 PathDiagnostic& PD;
560
561public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000562 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000563 PathDiagnostic& pd)
564 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000565
Ted Kremenekbe912242009-03-05 16:31:07 +0000566 bool HandleBinding(StoreManager& SMgr, Store store,
567 const MemRegion* R, SVal V) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000568 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000569
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000570 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000571 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000572 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000573 ScanSym = SV->getSymbol();
574 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000575 return true;
576
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000577 assert (ScanSym);
Ted Kremenek9e240492008-10-04 05:50:14 +0000578
579 if (!BR.isNotable(ScanSym))
580 return true;
581
582 if (AlreadyProcessed.count(ScanSym))
583 return true;
584
585 AlreadyProcessed.insert(ScanSym);
586
587 HandleNotableSymbol(N, S, ScanSym, BR, PD);
588 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000589 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000590};
591} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000592
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000593namespace {
594class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
595 NodeBackMap& M;
596public:
597 NodeMapClosure(NodeBackMap *m) : M(*m) {}
598 ~NodeMapClosure() {}
599
600 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
601 NodeBackMap::iterator I = M.find(N);
602 return I == M.end() ? 0 : I->second;
603 }
604};
605}
606
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000607/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
608/// and collapses PathDiagosticPieces that are expanded by macros.
609static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
610 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
611 MacroStackTy;
612
613 typedef std::vector<PathDiagnosticPiece*>
614 PiecesTy;
615
616 MacroStackTy MacroStack;
617 PiecesTy Pieces;
618
619 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
620 // Get the location of the PathDiagnosticPiece.
621 const FullSourceLoc Loc = I->getLocation();
622
623 // Determine the instantiation location, which is the location we group
624 // related PathDiagnosticPieces.
625 SourceLocation InstantiationLoc = Loc.isMacroID() ?
626 SM.getInstantiationLoc(Loc) :
627 SourceLocation();
628
629 if (Loc.isFileID()) {
630 MacroStack.clear();
631 Pieces.push_back(&*I);
632 continue;
633 }
634
635 assert(Loc.isMacroID());
636
637 // Is the PathDiagnosticPiece within the same macro group?
638 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
639 MacroStack.back().first->push_back(&*I);
640 continue;
641 }
642
643 // We aren't in the same group. Are we descending into a new macro
644 // or are part of an old one?
645 PathDiagnosticMacroPiece *MacroGroup = 0;
646
647 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
648 SM.getInstantiationLoc(Loc) :
649 SourceLocation();
650
651 // Walk the entire macro stack.
652 while (!MacroStack.empty()) {
653 if (InstantiationLoc == MacroStack.back().second) {
654 MacroGroup = MacroStack.back().first;
655 break;
656 }
657
658 if (ParentInstantiationLoc == MacroStack.back().second) {
659 MacroGroup = MacroStack.back().first;
660 break;
661 }
662
663 MacroStack.pop_back();
664 }
665
666 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
667 // Create a new macro group and add it to the stack.
668 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
669
670 if (MacroGroup)
671 MacroGroup->push_back(NewGroup);
672 else {
673 assert(InstantiationLoc.isFileID());
674 Pieces.push_back(NewGroup);
675 }
676
677 MacroGroup = NewGroup;
678 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
679 }
680
681 // Finally, add the PathDiagnosticPiece to the group.
682 MacroGroup->push_back(&*I);
683 }
684
685 // Now take the pieces and construct a new PathDiagnostic.
686 PD.resetPath(false);
687
688 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
689 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
690 if (!MP->containsEvent()) {
691 delete MP;
692 continue;
693 }
694
695 PD.push_back(*I);
696 }
697}
698
Ted Kremenekc0959972008-07-02 21:24:01 +0000699void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000700 BugReportEquivClass& EQ) {
701
702 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000703
Ted Kremenekcf118d42009-02-04 23:49:09 +0000704 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
705 const ExplodedNode<GRState>* N = I->getEndNode();
706 if (N) Nodes.push_back(N);
707 }
708
709 if (Nodes.empty())
710 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000711
712 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000713 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000714 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000715 std::pair<ExplodedNode<GRState>*, unsigned> >&
716 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000717
Ted Kremenekcf118d42009-02-04 23:49:09 +0000718 // Find the BugReport with the original location.
719 BugReport *R = 0;
720 unsigned i = 0;
721 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
722 if (i == GPair.second.second) { R = *I; break; }
723
724 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000725
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000726 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
727 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000728 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000729
Ted Kremenekcf118d42009-02-04 23:49:09 +0000730 // Start building the path diagnostic...
731 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000732 PD.push_back(Piece);
733 else
734 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000735
Ted Kremenek3148eb42009-01-24 00:55:43 +0000736 const ExplodedNode<GRState>* NextNode = N->pred_empty()
737 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000738
Ted Kremenekc0959972008-07-02 21:24:01 +0000739 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000740 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000741 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000742 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
743 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000744
Ted Kremenek6837faa2008-04-09 00:20:43 +0000745 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000746 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000747 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000748
749 ProgramPoint P = N->getLocation();
750
751 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000752 CFGBlock* Src = BE->getSrc();
753 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000754 Stmt* T = Src->getTerminator();
755
756 if (!T)
757 continue;
758
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000759 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000760
761 switch (T->getStmtClass()) {
762 default:
763 break;
764
765 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000766 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000767 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000768
769 if (!S)
770 continue;
771
Ted Kremenek297308e2009-02-10 23:56:07 +0000772 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000773 llvm::raw_string_ostream os(sbuf);
Ted Kremenek6f002042009-03-26 23:12:02 +0000774 FullSourceLoc End(S->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000775
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000776 os << "Control jumps to line " << SMgr.getInstantiationLineNumber(End);
777 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
778 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000779 break;
780 }
781
Ted Kremenek297308e2009-02-10 23:56:07 +0000782 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000783 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000784 std::string sbuf;
785 llvm::raw_string_ostream os(sbuf);
Ted Kremenek6f002042009-03-26 23:12:02 +0000786 FullSourceLoc End;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000787
788 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek6f002042009-03-26 23:12:02 +0000789 End = FullSourceLoc(S->getLocStart(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000790
Ted Kremenek5a429952008-04-23 23:35:07 +0000791 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000792 default:
793 os << "No cases match in the switch statement. "
794 "Control jumps to line "
795 << SMgr.getInstantiationLineNumber(End);
796 break;
797 case Stmt::DefaultStmtClass:
798 os << "Control jumps to the 'default' case at line "
799 << SMgr.getInstantiationLineNumber(End);
800 break;
801
802 case Stmt::CaseStmtClass: {
803 os << "Control jumps to 'case ";
804 CaseStmt* Case = cast<CaseStmt>(S);
805 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
806
807 // Determine if it is an enum.
808 bool GetRawInt = true;
809
810 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
811 // FIXME: Maybe this should be an assertion. Are there cases
812 // were it is not an EnumConstantDecl?
813 EnumConstantDecl* D =
814 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000815
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000816 if (D) {
817 GetRawInt = false;
818 os << D->getNameAsString();
819 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000820 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000821
822 if (GetRawInt) {
823
824 // Not an enum.
825 Expr* CondE = cast<SwitchStmt>(T)->getCond();
826 unsigned bits = Ctx.getTypeSize(CondE->getType());
827 llvm::APSInt V(bits, false);
828
829 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
830 assert (false && "Case condition must be constant.");
831 continue;
832 }
833
834 os << V;
835 }
836
837 os << ":' at line " << SMgr.getInstantiationLineNumber(End);
838 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000839 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000840 }
841 }
Ted Kremenek56783922008-04-25 01:29:56 +0000842 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000843 os << "'Default' branch taken. ";
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000844 End = PDB.ExecutionContinues(os, N);
Ted Kremenek56783922008-04-25 01:29:56 +0000845 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000846
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000847 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
848 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000849 break;
850 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000851
852 case Stmt::BreakStmtClass:
853 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000854 std::string sbuf;
855 llvm::raw_string_ostream os(sbuf);
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000856 FullSourceLoc End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000857 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
858 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000859 break;
860 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000861
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000862 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000863 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000864 std::string sbuf;
865 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000866 os << "'?' condition evaluates to ";
867
868 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000869 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000870 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000871 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000872
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000873 FullSourceLoc End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000874
875 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
876 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000877 break;
878 }
879
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000880 // Determine control-flow for short-circuited '&&' and '||'.
881 case Stmt::BinaryOperatorClass: {
882 if (!PDB.supportsLogicalOpControlFlow())
883 break;
884
885 BinaryOperator *B = cast<BinaryOperator>(T);
886 std::string sbuf;
887 llvm::raw_string_ostream os(sbuf);
888 os << "Left side of '";
889
890 if (B->getOpcode() == BinaryOperator::LAnd) {
891 os << "&&";
892 }
893 else {
894 assert(B->getOpcode() == BinaryOperator::LOr);
895 os << "||";
896 }
897
898 os << "' is ";
899 if (*(Src->succ_begin()+1) == Dst)
900 os << (B->getOpcode() == BinaryOperator::LAnd
901 ? "false" : "true");
902 else
903 os << (B->getOpcode() == BinaryOperator::LAnd
904 ? "true" : "false");
905
906 PathDiagnosticLocation Start(B->getLHS(), SMgr);
907 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
908
909 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
910 os.str()));
911 break;
912 }
913
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000914 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000915 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000916 std::string sbuf;
917 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000918
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000919 os << "Loop condition is true. ";
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000920 FullSourceLoc End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000921 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
922 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000923 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000924 else {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000925 FullSourceLoc End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000926 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
927 "Loop condition is false. Exiting loop"));
928 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000929
930 break;
931 }
932
Ted Kremenek61f3e052008-04-03 04:42:52 +0000933 case Stmt::WhileStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000934 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000935 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000936 std::string sbuf;
937 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000938
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000939 os << "Loop condition is false. ";
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000940 FullSourceLoc End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000941 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
942 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000943 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000944 else {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000945 FullSourceLoc End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000946
947 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
948 "Loop condition is true. Entering loop body"));
949 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000950
951 break;
952 }
953
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000954 case Stmt::IfStmtClass: {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000955 FullSourceLoc End = PDB.ExecutionContinues(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000956 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000957 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
958 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +0000959 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000960 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
961 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000962
963 break;
964 }
965 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000966 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000967
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000968 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
969 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000970 PD.push_front(p);
971
Ted Kremenek9e240492008-10-04 05:50:14 +0000972 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
973 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000974 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000975 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
976 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000977 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000978 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000979
980 // After constructing the full PathDiagnostic, do a pass over it to compact
981 // PathDiagnosticPieces that occur within a macro.
982 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000983}
984
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000985
Ted Kremenekcf118d42009-02-04 23:49:09 +0000986void BugReporter::Register(BugType *BT) {
987 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000988}
989
Ted Kremenekcf118d42009-02-04 23:49:09 +0000990void BugReporter::EmitReport(BugReport* R) {
991 // Compute the bug report's hash to determine its equivalence class.
992 llvm::FoldingSetNodeID ID;
993 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000994
Ted Kremenekcf118d42009-02-04 23:49:09 +0000995 // Lookup the equivance class. If there isn't one, create it.
996 BugType& BT = R->getBugType();
997 Register(&BT);
998 void *InsertPos;
999 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1000
1001 if (!EQ) {
1002 EQ = new BugReportEquivClass(R);
1003 BT.EQClasses.InsertNode(EQ, InsertPos);
1004 }
1005 else
1006 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001007}
1008
Ted Kremenekcf118d42009-02-04 23:49:09 +00001009void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1010 assert(!EQ.Reports.empty());
1011 BugReport &R = **EQ.begin();
1012
1013 // FIXME: Make sure we use the 'R' for the path that was actually used.
1014 // Probably doesn't make a difference in practice.
1015 BugType& BT = R.getBugType();
1016
1017 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1018 R.getDescription(),
1019 BT.getCategory()));
1020 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001021
1022 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001023 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001024 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001025
Ted Kremenek3148eb42009-01-24 00:55:43 +00001026 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001027 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001028 const SourceRange *Beg = 0, *End = 0;
1029 R.getRanges(*this, Beg, End);
1030 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001031 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001032 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1033 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001034
Ted Kremenek3148eb42009-01-24 00:55:43 +00001035 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001036 default: assert(0 && "Don't handle this many ranges yet!");
1037 case 0: Diag.Report(L, ErrorDiag); break;
1038 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1039 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1040 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001041 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001042
1043 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1044 if (!PD)
1045 return;
1046
1047 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001048 PathDiagnosticPiece* piece =
1049 new PathDiagnosticEventPiece(L, R.getDescription());
1050
Ted Kremenek3148eb42009-01-24 00:55:43 +00001051 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1052 D->push_back(piece);
1053 }
1054
1055 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001056}
Ted Kremenek57202072008-07-14 17:40:50 +00001057
Ted Kremenek8c036c72008-09-20 04:23:38 +00001058void BugReporter::EmitBasicReport(const char* name, const char* str,
1059 SourceLocation Loc,
1060 SourceRange* RBeg, unsigned NumRanges) {
1061 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1062}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001063
Ted Kremenek8c036c72008-09-20 04:23:38 +00001064void BugReporter::EmitBasicReport(const char* name, const char* category,
1065 const char* str, SourceLocation Loc,
1066 SourceRange* RBeg, unsigned NumRanges) {
1067
Ted Kremenekcf118d42009-02-04 23:49:09 +00001068 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1069 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001070 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001071 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1072 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1073 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001074}