blob: ac3d96191aad3572facb33da46193c303e9f2d19 [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"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000023#include "clang/Analysis/ProgramPoint.h"
24#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000026#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000027#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000028#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000029#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000030
31using namespace clang;
32
Ted Kremenekcf118d42009-02-04 23:49:09 +000033//===----------------------------------------------------------------------===//
34// static functions.
35//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000036
Ted Kremenekb697b102009-02-23 22:44:26 +000037static inline Stmt* GetStmt(ProgramPoint P) {
38 if (const PostStmt* PS = dyn_cast<PostStmt>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000039 return PS->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000040 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000041 return BE->getSrc()->getTerminator();
Ted Kremenek61f3e052008-04-03 04:42:52 +000042
Ted Kremenekb697b102009-02-23 22:44:26 +000043 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000044}
45
Ted Kremenek3148eb42009-01-24 00:55:43 +000046static inline const ExplodedNode<GRState>*
Ted Kremenekb697b102009-02-23 22:44:26 +000047GetPredecessorNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000048 return N->pred_empty() ? NULL : *(N->pred_begin());
49}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000050
Ted Kremenekb697b102009-02-23 22:44:26 +000051static inline const ExplodedNode<GRState>*
52GetSuccessorNode(const ExplodedNode<GRState>* N) {
53 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000054}
55
Ted Kremenekb697b102009-02-23 22:44:26 +000056static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) {
57 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
58 if (Stmt *S = GetStmt(N->getLocation()))
59 return S;
60
61 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000062}
63
Ted Kremenekb697b102009-02-23 22:44:26 +000064static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) {
65 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
66 if (Stmt *S = GetStmt(N->getLocation()))
67 return S;
68
69 return 0;
70}
71
72static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
73 if (Stmt *S = GetStmt(N->getLocation()))
74 return S;
75
76 return GetPreviousStmt(N);
77}
78
79static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
80 if (Stmt *S = GetStmt(N->getLocation()))
81 return S;
82
83 return GetNextStmt(N);
84}
85
86//===----------------------------------------------------------------------===//
87// Diagnostics for 'execution continues on line XXX'.
88//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +000089
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000090namespace {
91class VISIBILITY_HIDDEN PathDiagnosticBuilder {
92 SourceManager &SMgr;
93 const Decl& CodeDecl;
94 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +000095 llvm::OwningPtr<ParentMap> PM;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000096public:
97 PathDiagnosticBuilder(SourceManager &smgr, const Decl& codedecl,
98 PathDiagnosticClient *pdc)
99 : SMgr(smgr), CodeDecl(codedecl), PDC(pdc) {}
100
Ted Kremenek00605e02009-03-27 20:55:39 +0000101 PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N);
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000102
Ted Kremenek00605e02009-03-27 20:55:39 +0000103 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
104 const ExplodedNode<GRState>* N);
105
106 ParentMap& getParentMap() {
107 if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody()));
108 return *PM.get();
109 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000110
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000111 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
112
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000113 bool supportsLogicalOpControlFlow() const {
114 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
115 }
116};
117} // end anonymous namespace
118
Ted Kremenek00605e02009-03-27 20:55:39 +0000119PathDiagnosticLocation
120PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) {
121 if (Stmt *S = GetNextStmt(N))
122 return PathDiagnosticLocation(S, SMgr);
123
124 return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000125}
126
Ted Kremenek00605e02009-03-27 20:55:39 +0000127PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000128PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
129 const ExplodedNode<GRState>* N) {
130
Ted Kremenek143ca222008-05-06 18:11:09 +0000131 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000132 if (os.str().empty())
133 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000134
Ted Kremenek00605e02009-03-27 20:55:39 +0000135 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000136
Ted Kremenek00605e02009-03-27 20:55:39 +0000137 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000138 os << "Execution continues on line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000139 << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000140 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000141 os << "Execution jumps to the end of the "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000142 << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000143
144 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000145}
146
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000147PathDiagnosticLocation
148PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
149 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
150 ParentMap &P = getParentMap();
151 while (isa<Expr>(S)) {
152 const Stmt *Parent = P.getParent(S);
153
154 if (!Parent || isa<CompoundStmt>(Parent) || isa<StmtExpr>(Parent))
155 return PathDiagnosticLocation(S, SMgr);
156
157 S = Parent;
158 }
159
160 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
161 return PathDiagnosticLocation(S, SMgr);
162}
163
Ted Kremenekcf118d42009-02-04 23:49:09 +0000164//===----------------------------------------------------------------------===//
165// Methods for BugType and subclasses.
166//===----------------------------------------------------------------------===//
167BugType::~BugType() {}
168void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000169
Ted Kremenekcf118d42009-02-04 23:49:09 +0000170//===----------------------------------------------------------------------===//
171// Methods for BugReport and subclasses.
172//===----------------------------------------------------------------------===//
173BugReport::~BugReport() {}
174RangedBugReport::~RangedBugReport() {}
175
176Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000177 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000178 Stmt *S = NULL;
179
Ted Kremenekcf118d42009-02-04 23:49:09 +0000180 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000181 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000182 }
183 if (!S) S = GetStmt(ProgP);
184
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000185 return S;
186}
187
188PathDiagnosticPiece*
189BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000190 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000191
192 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000193
194 if (!S)
195 return NULL;
196
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000197 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000198 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000199
Ted Kremenekde7161f2008-04-03 18:00:37 +0000200 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000201 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000202
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000203 for (; Beg != End; ++Beg)
204 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000205
206 return P;
207}
208
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000209void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
210 const SourceRange*& end) {
211
212 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
213 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000214 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000215 beg = &R;
216 end = beg+1;
217 }
218 else
219 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000220}
221
Ted Kremenekcf118d42009-02-04 23:49:09 +0000222SourceLocation BugReport::getLocation() const {
223 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000224 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
225 // For member expressions, return the location of the '.' or '->'.
226 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
227 return ME->getMemberLoc();
228
Ted Kremenekcf118d42009-02-04 23:49:09 +0000229 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000230 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000231
232 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000233}
234
Ted Kremenek3148eb42009-01-24 00:55:43 +0000235PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
236 const ExplodedNode<GRState>* PrevN,
237 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000238 BugReporter& BR,
239 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000240 return NULL;
241}
242
Ted Kremenekcf118d42009-02-04 23:49:09 +0000243//===----------------------------------------------------------------------===//
244// Methods for BugReporter and subclasses.
245//===----------------------------------------------------------------------===//
246
247BugReportEquivClass::~BugReportEquivClass() {
248 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
249}
250
251GRBugReporter::~GRBugReporter() { FlushReports(); }
252BugReporterData::~BugReporterData() {}
253
254ExplodedGraph<GRState>&
255GRBugReporter::getGraph() { return Eng.getGraph(); }
256
257GRStateManager&
258GRBugReporter::getStateManager() { return Eng.getStateManager(); }
259
260BugReporter::~BugReporter() { FlushReports(); }
261
262void BugReporter::FlushReports() {
263 if (BugTypes.isEmpty())
264 return;
265
266 // First flush the warnings for each BugType. This may end up creating new
267 // warnings and new BugTypes. Because ImmutableSet is a functional data
268 // structure, we do not need to worry about the iterators being invalidated.
269 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
270 const_cast<BugType*>(*I)->FlushReports(*this);
271
272 // Iterate through BugTypes a second time. BugTypes may have been updated
273 // with new BugType objects and new warnings.
274 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
275 BugType *BT = const_cast<BugType*>(*I);
276
277 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
278 SetTy& EQClasses = BT->EQClasses;
279
280 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
281 BugReportEquivClass& EQ = *EI;
282 FlushReport(EQ);
283 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000284
Ted Kremenekcf118d42009-02-04 23:49:09 +0000285 // Delete the BugType object. This will also delete the equivalence
286 // classes.
287 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000288 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000289
290 // Remove all references to the BugType objects.
291 BugTypes = F.GetEmptySet();
292}
293
294//===----------------------------------------------------------------------===//
295// PathDiagnostics generation.
296//===----------------------------------------------------------------------===//
297
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000298typedef llvm::DenseMap<const ExplodedNode<GRState>*,
299 const ExplodedNode<GRState>*> NodeBackMap;
300
301static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000302 std::pair<ExplodedNode<GRState>*, unsigned> >
303MakeReportGraph(const ExplodedGraph<GRState>* G,
304 const ExplodedNode<GRState>** NStart,
305 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000306
Ted Kremenekcf118d42009-02-04 23:49:09 +0000307 // Create the trimmed graph. It will contain the shortest paths from the
308 // error nodes to the root. In the new graph we should only have one
309 // error node unless there are two or more error nodes with the same minimum
310 // path length.
311 ExplodedGraph<GRState>* GTrim;
312 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000313
314 llvm::DenseMap<const void*, const void*> InverseMap;
315 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000316
317 // Create owning pointers for GTrim and NMap just to ensure that they are
318 // released when this function exists.
319 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
320 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
321
322 // Find the (first) error node in the trimmed graph. We just need to consult
323 // the node map (NMap) which maps from nodes in the original graph to nodes
324 // in the new graph.
325 const ExplodedNode<GRState>* N = 0;
326 unsigned NodeIndex = 0;
327
328 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
329 if ((N = NMap->getMappedNode(*I))) {
330 NodeIndex = (I - NStart) / sizeof(*I);
331 break;
332 }
333
334 assert(N && "No error node found in the trimmed graph.");
335
336 // Create a new (third!) graph with a single path. This is the graph
337 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000338 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000339 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
340 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000341
Ted Kremenek10aa5542009-03-12 23:41:59 +0000342 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000343 // to the root node, and then construct a new graph that contains only
344 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000345 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000346 std::queue<const ExplodedNode<GRState>*> WS;
347 WS.push(N);
348
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000349 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000350 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000351
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000352 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000353 const ExplodedNode<GRState>* Node = WS.front();
354 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000355
356 if (Visited.find(Node) != Visited.end())
357 continue;
358
359 Visited[Node] = cnt++;
360
361 if (Node->pred_empty()) {
362 Root = Node;
363 break;
364 }
365
Ted Kremenek3148eb42009-01-24 00:55:43 +0000366 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000367 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000368 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000369 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000370
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000371 assert (Root);
372
Ted Kremenek10aa5542009-03-12 23:41:59 +0000373 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000374 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000375 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000376 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000377
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000378 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000379 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000380 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000381 assert (I != Visited.end());
382
383 // Create the equivalent node in the new graph with the same state
384 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000385 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000386 GNew->getNode(N->getLocation(), N->getState());
387
388 // Store the mapping to the original node.
389 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
390 assert(IMitr != InverseMap.end() && "No mapping to original node.");
391 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000392
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000393 // Link up the new node with the previous node.
394 if (Last)
395 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000396
397 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000398
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000399 // Are we at the final node?
400 if (I->second == 0) {
401 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000402 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000403 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000404
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000405 // Find the next successor node. We choose the node that is marked
406 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000407 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
408 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000409 N = 0;
410
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000411 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000412
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000413 I = Visited.find(*SI);
414
415 if (I == Visited.end())
416 continue;
417
418 if (!N || I->second < MinVal) {
419 N = *SI;
420 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000421 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000422 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000423
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000424 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000425 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000426
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000427 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000428 return std::make_pair(std::make_pair(GNew, BM),
429 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000430}
431
Ted Kremenek3148eb42009-01-24 00:55:43 +0000432static const VarDecl*
433GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
434 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000435
436 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
437
438 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000439
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000440 if (!isa<PostStmt>(P))
441 continue;
442
443 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000444
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000445 if (!DR)
446 continue;
447
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000448 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000449
450 if (X != Y)
451 continue;
452
453 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
454
455 if (!VD)
456 continue;
457
458 return VD;
459 }
460
461 return 0;
462}
463
Ted Kremenek9e240492008-10-04 05:50:14 +0000464namespace {
465class VISIBILITY_HIDDEN NotableSymbolHandler
466 : public StoreManager::BindingsHandler {
467
Ted Kremenek2dabd432008-12-05 02:27:51 +0000468 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000469 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000470 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000471 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000472 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000473 PathDiagnostic& PD;
474 BugReporter& BR;
475
476public:
477
Ted Kremenek3148eb42009-01-24 00:55:43 +0000478 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
479 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000480 PathDiagnostic& pd, BugReporter& br)
481 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
482
Ted Kremenekbe912242009-03-05 16:31:07 +0000483 bool HandleBinding(StoreManager& SMgr, Store store,
484 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000485
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000486 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000487
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000488 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000489 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000490 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000491 ScanSym = SV->getSymbol();
492 else
493 return true;
494
495 if (ScanSym != Sym)
496 return true;
497
498 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000499 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000500
501 if (X == V) // Same binding?
502 return true;
503
504 // Different binding. Only handle assignments for now. We don't pull
505 // this check out of the loop because we will eventually handle other
506 // cases.
507
508 VarDecl *VD = 0;
509
Ted Kremenek3148eb42009-01-24 00:55:43 +0000510 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000511 if (!B->isAssignmentOp())
512 return true;
513
514 // What variable did we assign to?
515 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
516
517 if (!DR)
518 return true;
519
520 VD = dyn_cast<VarDecl>(DR->getDecl());
521 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000522 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000523 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
524 // assume that each DeclStmt has a single Decl. This invariant
525 // holds by contruction in the CFG.
526 VD = dyn_cast<VarDecl>(*DS->decl_begin());
527 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000528
529 if (!VD)
530 return true;
531
532 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000533 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000534
535 if (!MostRecent)
536 return true;
537
538 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000539 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
540
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000541 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000542 std::string msg = "'" + std::string(VD->getNameAsString()) +
543 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000544
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000545 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000546 }
547
548 return true;
549 }
550};
551}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000552
Ted Kremenek3148eb42009-01-24 00:55:43 +0000553static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
554 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000555 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000556 PathDiagnostic& PD) {
557
Ted Kremenek3148eb42009-01-24 00:55:43 +0000558 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000559 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000560
561 if (!PrevSt)
562 return;
563
Ted Kremenek9e240492008-10-04 05:50:14 +0000564 // Look at the region bindings of the current state that map to the
565 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000566 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000567 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
568 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
569}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000570
Ted Kremenek9e240492008-10-04 05:50:14 +0000571namespace {
572class VISIBILITY_HIDDEN ScanNotableSymbols
573 : public StoreManager::BindingsHandler {
574
Ted Kremenek2dabd432008-12-05 02:27:51 +0000575 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000576 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000577 Stmt* S;
578 GRBugReporter& BR;
579 PathDiagnostic& PD;
580
581public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000582 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000583 PathDiagnostic& pd)
584 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000585
Ted Kremenekbe912242009-03-05 16:31:07 +0000586 bool HandleBinding(StoreManager& SMgr, Store store,
587 const MemRegion* R, SVal V) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000588 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000589
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000590 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000591 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000592 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000593 ScanSym = SV->getSymbol();
594 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000595 return true;
596
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000597 assert (ScanSym);
Ted Kremenek9e240492008-10-04 05:50:14 +0000598
599 if (!BR.isNotable(ScanSym))
600 return true;
601
602 if (AlreadyProcessed.count(ScanSym))
603 return true;
604
605 AlreadyProcessed.insert(ScanSym);
606
607 HandleNotableSymbol(N, S, ScanSym, BR, PD);
608 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000609 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000610};
611} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000612
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000613namespace {
614class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
615 NodeBackMap& M;
616public:
617 NodeMapClosure(NodeBackMap *m) : M(*m) {}
618 ~NodeMapClosure() {}
619
620 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
621 NodeBackMap::iterator I = M.find(N);
622 return I == M.end() ? 0 : I->second;
623 }
624};
625}
626
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000627/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
628/// and collapses PathDiagosticPieces that are expanded by macros.
629static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
630 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
631 MacroStackTy;
632
633 typedef std::vector<PathDiagnosticPiece*>
634 PiecesTy;
635
636 MacroStackTy MacroStack;
637 PiecesTy Pieces;
638
639 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
640 // Get the location of the PathDiagnosticPiece.
641 const FullSourceLoc Loc = I->getLocation();
642
643 // Determine the instantiation location, which is the location we group
644 // related PathDiagnosticPieces.
645 SourceLocation InstantiationLoc = Loc.isMacroID() ?
646 SM.getInstantiationLoc(Loc) :
647 SourceLocation();
648
649 if (Loc.isFileID()) {
650 MacroStack.clear();
651 Pieces.push_back(&*I);
652 continue;
653 }
654
655 assert(Loc.isMacroID());
656
657 // Is the PathDiagnosticPiece within the same macro group?
658 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
659 MacroStack.back().first->push_back(&*I);
660 continue;
661 }
662
663 // We aren't in the same group. Are we descending into a new macro
664 // or are part of an old one?
665 PathDiagnosticMacroPiece *MacroGroup = 0;
666
667 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
668 SM.getInstantiationLoc(Loc) :
669 SourceLocation();
670
671 // Walk the entire macro stack.
672 while (!MacroStack.empty()) {
673 if (InstantiationLoc == MacroStack.back().second) {
674 MacroGroup = MacroStack.back().first;
675 break;
676 }
677
678 if (ParentInstantiationLoc == MacroStack.back().second) {
679 MacroGroup = MacroStack.back().first;
680 break;
681 }
682
683 MacroStack.pop_back();
684 }
685
686 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
687 // Create a new macro group and add it to the stack.
688 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
689
690 if (MacroGroup)
691 MacroGroup->push_back(NewGroup);
692 else {
693 assert(InstantiationLoc.isFileID());
694 Pieces.push_back(NewGroup);
695 }
696
697 MacroGroup = NewGroup;
698 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
699 }
700
701 // Finally, add the PathDiagnosticPiece to the group.
702 MacroGroup->push_back(&*I);
703 }
704
705 // Now take the pieces and construct a new PathDiagnostic.
706 PD.resetPath(false);
707
708 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
709 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
710 if (!MP->containsEvent()) {
711 delete MP;
712 continue;
713 }
714
715 PD.push_back(*I);
716 }
717}
718
Ted Kremenekc0959972008-07-02 21:24:01 +0000719void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000720 BugReportEquivClass& EQ) {
721
722 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000723
Ted Kremenekcf118d42009-02-04 23:49:09 +0000724 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
725 const ExplodedNode<GRState>* N = I->getEndNode();
726 if (N) Nodes.push_back(N);
727 }
728
729 if (Nodes.empty())
730 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000731
732 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000733 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000734 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000735 std::pair<ExplodedNode<GRState>*, unsigned> >&
736 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000737
Ted Kremenekcf118d42009-02-04 23:49:09 +0000738 // Find the BugReport with the original location.
739 BugReport *R = 0;
740 unsigned i = 0;
741 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
742 if (i == GPair.second.second) { R = *I; break; }
743
744 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000745
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000746 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
747 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000748 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000749
Ted Kremenekcf118d42009-02-04 23:49:09 +0000750 // Start building the path diagnostic...
751 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000752 PD.push_back(Piece);
753 else
754 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000755
Ted Kremenek3148eb42009-01-24 00:55:43 +0000756 const ExplodedNode<GRState>* NextNode = N->pred_empty()
757 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000758
Ted Kremenekc0959972008-07-02 21:24:01 +0000759 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000760 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000761 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000762 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
763 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000764
Ted Kremenek6837faa2008-04-09 00:20:43 +0000765 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000766 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000767 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000768
769 ProgramPoint P = N->getLocation();
770
771 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000772 CFGBlock* Src = BE->getSrc();
773 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000774 Stmt* T = Src->getTerminator();
775
776 if (!T)
777 continue;
778
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000779 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000780
781 switch (T->getStmtClass()) {
782 default:
783 break;
784
785 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000786 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000787 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000788
789 if (!S)
790 continue;
791
Ted Kremenek297308e2009-02-10 23:56:07 +0000792 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000793 llvm::raw_string_ostream os(sbuf);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000794 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000795
Ted Kremenek00605e02009-03-27 20:55:39 +0000796 os << "Control jumps to line "
797 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000798 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
799 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000800 break;
801 }
802
Ted Kremenek297308e2009-02-10 23:56:07 +0000803 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000804 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000805 std::string sbuf;
806 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000807
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000808 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000809 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000810
Ted Kremenek5a429952008-04-23 23:35:07 +0000811 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000812 default:
813 os << "No cases match in the switch statement. "
814 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000815 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000816 break;
817 case Stmt::DefaultStmtClass:
818 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000819 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000820 break;
821
822 case Stmt::CaseStmtClass: {
823 os << "Control jumps to 'case ";
824 CaseStmt* Case = cast<CaseStmt>(S);
825 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
826
827 // Determine if it is an enum.
828 bool GetRawInt = true;
829
830 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
831 // FIXME: Maybe this should be an assertion. Are there cases
832 // were it is not an EnumConstantDecl?
833 EnumConstantDecl* D =
834 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000835
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000836 if (D) {
837 GetRawInt = false;
838 os << D->getNameAsString();
839 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000840 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000841
842 if (GetRawInt) {
843
844 // Not an enum.
845 Expr* CondE = cast<SwitchStmt>(T)->getCond();
846 unsigned bits = Ctx.getTypeSize(CondE->getType());
847 llvm::APSInt V(bits, false);
848
849 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
850 assert (false && "Case condition must be constant.");
851 continue;
852 }
853
854 os << V;
855 }
856
Ted Kremenek00605e02009-03-27 20:55:39 +0000857 os << ":' at line "
858 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000859 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000860 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000861 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000862 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
863 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000864 }
Ted Kremenek56783922008-04-25 01:29:56 +0000865 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000866 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000867 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
868 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
869 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000870 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000871
Ted Kremenek61f3e052008-04-03 04:42:52 +0000872 break;
873 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000874
875 case Stmt::BreakStmtClass:
876 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000877 std::string sbuf;
878 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000879 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000880 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
881 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000882 break;
883 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000884
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000885 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000886 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000887 std::string sbuf;
888 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000889 os << "'?' condition evaluates to ";
890
891 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000892 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000893 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000894 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000895
Ted Kremenek00605e02009-03-27 20:55:39 +0000896 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000897
898 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
899 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000900 break;
901 }
902
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000903 // Determine control-flow for short-circuited '&&' and '||'.
904 case Stmt::BinaryOperatorClass: {
905 if (!PDB.supportsLogicalOpControlFlow())
906 break;
907
908 BinaryOperator *B = cast<BinaryOperator>(T);
909 std::string sbuf;
910 llvm::raw_string_ostream os(sbuf);
911 os << "Left side of '";
912
913 if (B->getOpcode() == BinaryOperator::LAnd) {
914 os << "&&";
915 }
916 else {
917 assert(B->getOpcode() == BinaryOperator::LOr);
918 os << "||";
919 }
920
921 os << "' is ";
922 if (*(Src->succ_begin()+1) == Dst)
923 os << (B->getOpcode() == BinaryOperator::LAnd
924 ? "false" : "true");
925 else
926 os << (B->getOpcode() == BinaryOperator::LAnd
927 ? "true" : "false");
928
929 PathDiagnosticLocation Start(B->getLHS(), SMgr);
930 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
931
932 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
933 os.str()));
934 break;
935 }
936
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000937 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000938 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000939 std::string sbuf;
940 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000941
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000942 os << "Loop condition is true. ";
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000943 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
944
945 if (const Stmt *S = End.asStmt())
946 End = PDB.getEnclosingStmtLocation(S);
947
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000948 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
949 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000950 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000951 else {
Ted Kremenek00605e02009-03-27 20:55:39 +0000952 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000953
954 if (const Stmt *S = End.asStmt())
955 End = PDB.getEnclosingStmtLocation(S);
956
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000957 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
958 "Loop condition is false. Exiting loop"));
959 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000960
961 break;
962 }
963
Ted Kremenek61f3e052008-04-03 04:42:52 +0000964 case Stmt::WhileStmtClass:
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000965 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000966 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000967 std::string sbuf;
968 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000969
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000970 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000971 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000972 if (const Stmt *S = End.asStmt())
973 End = PDB.getEnclosingStmtLocation(S);
974
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000975 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
976 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000977 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000978 else {
Ted Kremenek00605e02009-03-27 20:55:39 +0000979 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000980 if (const Stmt *S = End.asStmt())
981 End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000982
983 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
984 "Loop condition is true. Entering loop body"));
985 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000986
987 break;
988 }
989
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000990 case Stmt::IfStmtClass: {
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000991 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
992
993 if (const Stmt *S = End.asStmt())
994 End = PDB.getEnclosingStmtLocation(S);
995
Ted Kremenek61f3e052008-04-03 04:42:52 +0000996 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000997 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
998 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +0000999 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001000 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1001 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +00001002
1003 break;
1004 }
1005 }
Ted Kremenek6837faa2008-04-09 00:20:43 +00001006 }
Ted Kremenek5a429952008-04-23 23:35:07 +00001007
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001008 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
1009 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001010 PD.push_front(p);
1011
Ted Kremenek9e240492008-10-04 05:50:14 +00001012 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
1013 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001014 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +00001015 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
1016 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001017 }
Ted Kremenek61f3e052008-04-03 04:42:52 +00001018 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001019
1020 // After constructing the full PathDiagnostic, do a pass over it to compact
1021 // PathDiagnosticPieces that occur within a macro.
1022 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001023}
1024
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001025
Ted Kremenekcf118d42009-02-04 23:49:09 +00001026void BugReporter::Register(BugType *BT) {
1027 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001028}
1029
Ted Kremenekcf118d42009-02-04 23:49:09 +00001030void BugReporter::EmitReport(BugReport* R) {
1031 // Compute the bug report's hash to determine its equivalence class.
1032 llvm::FoldingSetNodeID ID;
1033 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001034
Ted Kremenekcf118d42009-02-04 23:49:09 +00001035 // Lookup the equivance class. If there isn't one, create it.
1036 BugType& BT = R->getBugType();
1037 Register(&BT);
1038 void *InsertPos;
1039 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1040
1041 if (!EQ) {
1042 EQ = new BugReportEquivClass(R);
1043 BT.EQClasses.InsertNode(EQ, InsertPos);
1044 }
1045 else
1046 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001047}
1048
Ted Kremenekcf118d42009-02-04 23:49:09 +00001049void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1050 assert(!EQ.Reports.empty());
1051 BugReport &R = **EQ.begin();
1052
1053 // FIXME: Make sure we use the 'R' for the path that was actually used.
1054 // Probably doesn't make a difference in practice.
1055 BugType& BT = R.getBugType();
1056
1057 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1058 R.getDescription(),
1059 BT.getCategory()));
1060 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001061
1062 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001063 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001064 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001065
Ted Kremenek3148eb42009-01-24 00:55:43 +00001066 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001067 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001068 const SourceRange *Beg = 0, *End = 0;
1069 R.getRanges(*this, Beg, End);
1070 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001071 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001072 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1073 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001074
Ted Kremenek3148eb42009-01-24 00:55:43 +00001075 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001076 default: assert(0 && "Don't handle this many ranges yet!");
1077 case 0: Diag.Report(L, ErrorDiag); break;
1078 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1079 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1080 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001081 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001082
1083 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1084 if (!PD)
1085 return;
1086
1087 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001088 PathDiagnosticPiece* piece =
1089 new PathDiagnosticEventPiece(L, R.getDescription());
1090
Ted Kremenek3148eb42009-01-24 00:55:43 +00001091 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1092 D->push_back(piece);
1093 }
1094
1095 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001096}
Ted Kremenek57202072008-07-14 17:40:50 +00001097
Ted Kremenek8c036c72008-09-20 04:23:38 +00001098void BugReporter::EmitBasicReport(const char* name, const char* str,
1099 SourceLocation Loc,
1100 SourceRange* RBeg, unsigned NumRanges) {
1101 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1102}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001103
Ted Kremenek8c036c72008-09-20 04:23:38 +00001104void BugReporter::EmitBasicReport(const char* name, const char* category,
1105 const char* str, SourceLocation Loc,
1106 SourceRange* RBeg, unsigned NumRanges) {
1107
Ted Kremenekcf118d42009-02-04 23:49:09 +00001108 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1109 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001110 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001111 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1112 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1113 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001114}