blob: 8a37d1a3c1d7a90a552c3894d48d845df1d8d171 [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))
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000066 if (Stmt *S = GetStmt(N->getLocation())) {
67 // Check if the statement is '?' or '&&'/'||'. These are "merges",
68 // not actual statement points.
69 switch (S->getStmtClass()) {
70 case Stmt::ChooseExprClass:
71 case Stmt::ConditionalOperatorClass: continue;
72 case Stmt::BinaryOperatorClass: {
73 BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode();
74 if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr)
75 continue;
76 break;
77 }
78 default:
79 break;
80 }
Ted Kremenekb697b102009-02-23 22:44:26 +000081 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000082 }
Ted Kremenekb697b102009-02-23 22:44:26 +000083
84 return 0;
85}
86
87static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
88 if (Stmt *S = GetStmt(N->getLocation()))
89 return S;
90
91 return GetPreviousStmt(N);
92}
93
94static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
95 if (Stmt *S = GetStmt(N->getLocation()))
96 return S;
97
98 return GetNextStmt(N);
99}
100
101//===----------------------------------------------------------------------===//
102// Diagnostics for 'execution continues on line XXX'.
103//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000104
Ted Kremenek7dc86642009-03-31 20:22:36 +0000105typedef llvm::DenseMap<const ExplodedNode<GRState>*,
106const ExplodedNode<GRState>*> NodeBackMap;
107
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000108namespace {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000109class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
110 NodeBackMap& M;
111public:
112 NodeMapClosure(NodeBackMap *m) : M(*m) {}
113 ~NodeMapClosure() {}
114
115 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
116 NodeBackMap::iterator I = M.find(N);
117 return I == M.end() ? 0 : I->second;
118 }
119};
120
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000121class VISIBILITY_HIDDEN PathDiagnosticBuilder {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000122 GRBugReporter &BR;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000123 SourceManager &SMgr;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000124 ExplodedGraph<GRState> *ReportGraph;
125 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000126 const Decl& CodeDecl;
127 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000128 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000129 NodeMapClosure NMC;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000130public:
Ted Kremenek7dc86642009-03-31 20:22:36 +0000131 PathDiagnosticBuilder(GRBugReporter &br, ExplodedGraph<GRState> *reportGraph,
132 BugReport *r, NodeBackMap *Backmap,
133 const Decl& codedecl, PathDiagnosticClient *pdc)
134 : BR(br), SMgr(BR.getSourceManager()), ReportGraph(reportGraph), R(r),
135 CodeDecl(codedecl), PDC(pdc), NMC(Backmap) {}
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000136
Ted Kremenek00605e02009-03-27 20:55:39 +0000137 PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N);
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000138
Ted Kremenek00605e02009-03-27 20:55:39 +0000139 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
140 const ExplodedNode<GRState>* N);
141
142 ParentMap& getParentMap() {
143 if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody()));
144 return *PM.get();
145 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000146
Ted Kremenek7dc86642009-03-31 20:22:36 +0000147 ExplodedGraph<GRState>& getGraph() { return *ReportGraph; }
148 NodeMapClosure& getNodeMapClosure() { return NMC; }
149 ASTContext& getContext() { return BR.getContext(); }
150 SourceManager& getSourceManager() { return SMgr; }
151 BugReport& getReport() { return *R; }
152 GRBugReporter& getBugReporter() { return BR; }
153 GRStateManager& getStateManager() { return BR.getStateManager(); }
154
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000155 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
156
Ted Kremenek7dc86642009-03-31 20:22:36 +0000157 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
158 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
159 }
160
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000161 bool supportsLogicalOpControlFlow() const {
162 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
163 }
164};
165} // end anonymous namespace
166
Ted Kremenek00605e02009-03-27 20:55:39 +0000167PathDiagnosticLocation
168PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) {
169 if (Stmt *S = GetNextStmt(N))
170 return PathDiagnosticLocation(S, SMgr);
171
172 return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000173}
174
Ted Kremenek00605e02009-03-27 20:55:39 +0000175PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000176PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
177 const ExplodedNode<GRState>* N) {
178
Ted Kremenek143ca222008-05-06 18:11:09 +0000179 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000180 if (os.str().empty())
181 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000182
Ted Kremenek00605e02009-03-27 20:55:39 +0000183 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000184
Ted Kremenek00605e02009-03-27 20:55:39 +0000185 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000186 os << "Execution continues on line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000187 << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000188 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000189 os << "Execution jumps to the end of the "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000190 << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000191
192 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000193}
194
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000195PathDiagnosticLocation
196PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
197 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
198 ParentMap &P = getParentMap();
199 while (isa<Expr>(S)) {
200 const Stmt *Parent = P.getParent(S);
201
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000202 if (!Parent)
203 break;
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000204
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000205 switch (Parent->getStmtClass()) {
206 case Stmt::CompoundStmtClass:
207 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000208 return PathDiagnosticLocation(S, SMgr);
209 case Stmt::ChooseExprClass:
210 // Similar to '?' if we are referring to condition, just have the edge
211 // point to the entire choose expression.
212 if (cast<ChooseExpr>(Parent)->getCond() == S)
213 return PathDiagnosticLocation(Parent, SMgr);
214 else
215 return PathDiagnosticLocation(S, SMgr);
216 case Stmt::ConditionalOperatorClass:
217 // For '?', if we are referring to condition, just have the edge point
218 // to the entire '?' expression.
219 if (cast<ConditionalOperator>(Parent)->getCond() == S)
220 return PathDiagnosticLocation(Parent, SMgr);
221 else
222 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000223 case Stmt::DoStmtClass:
224 if (cast<DoStmt>(Parent)->getCond() != S)
225 return PathDiagnosticLocation(S, SMgr);
226 break;
227 case Stmt::ForStmtClass:
228 if (cast<ForStmt>(Parent)->getBody() == S)
229 return PathDiagnosticLocation(S, SMgr);
230 break;
231 case Stmt::IfStmtClass:
232 if (cast<IfStmt>(Parent)->getCond() != S)
233 return PathDiagnosticLocation(S, SMgr);
234 break;
235 case Stmt::ObjCForCollectionStmtClass:
236 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
237 return PathDiagnosticLocation(S, SMgr);
238 break;
239 case Stmt::WhileStmtClass:
240 if (cast<WhileStmt>(Parent)->getCond() != S)
241 return PathDiagnosticLocation(S, SMgr);
242 break;
243 default:
244 break;
245 }
246
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000247 S = Parent;
248 }
249
250 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
251 return PathDiagnosticLocation(S, SMgr);
252}
253
Ted Kremenekcf118d42009-02-04 23:49:09 +0000254//===----------------------------------------------------------------------===//
255// Methods for BugType and subclasses.
256//===----------------------------------------------------------------------===//
257BugType::~BugType() {}
258void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000259
Ted Kremenekcf118d42009-02-04 23:49:09 +0000260//===----------------------------------------------------------------------===//
261// Methods for BugReport and subclasses.
262//===----------------------------------------------------------------------===//
263BugReport::~BugReport() {}
264RangedBugReport::~RangedBugReport() {}
265
266Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000267 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000268 Stmt *S = NULL;
269
Ted Kremenekcf118d42009-02-04 23:49:09 +0000270 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000271 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000272 }
273 if (!S) S = GetStmt(ProgP);
274
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000275 return S;
276}
277
278PathDiagnosticPiece*
279BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000280 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000281
282 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000283
284 if (!S)
285 return NULL;
286
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000287 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000288 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000289
Ted Kremenekde7161f2008-04-03 18:00:37 +0000290 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000291 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000292
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000293 for (; Beg != End; ++Beg)
294 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000295
296 return P;
297}
298
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000299void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
300 const SourceRange*& end) {
301
302 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
303 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000304 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000305 beg = &R;
306 end = beg+1;
307 }
308 else
309 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000310}
311
Ted Kremenekcf118d42009-02-04 23:49:09 +0000312SourceLocation BugReport::getLocation() const {
313 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000314 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
315 // For member expressions, return the location of the '.' or '->'.
316 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
317 return ME->getMemberLoc();
318
Ted Kremenekcf118d42009-02-04 23:49:09 +0000319 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000320 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000321
322 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000323}
324
Ted Kremenek3148eb42009-01-24 00:55:43 +0000325PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
326 const ExplodedNode<GRState>* PrevN,
327 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000328 BugReporter& BR,
329 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000330 return NULL;
331}
332
Ted Kremenekcf118d42009-02-04 23:49:09 +0000333//===----------------------------------------------------------------------===//
334// Methods for BugReporter and subclasses.
335//===----------------------------------------------------------------------===//
336
337BugReportEquivClass::~BugReportEquivClass() {
338 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
339}
340
341GRBugReporter::~GRBugReporter() { FlushReports(); }
342BugReporterData::~BugReporterData() {}
343
344ExplodedGraph<GRState>&
345GRBugReporter::getGraph() { return Eng.getGraph(); }
346
347GRStateManager&
348GRBugReporter::getStateManager() { return Eng.getStateManager(); }
349
350BugReporter::~BugReporter() { FlushReports(); }
351
352void BugReporter::FlushReports() {
353 if (BugTypes.isEmpty())
354 return;
355
356 // First flush the warnings for each BugType. This may end up creating new
357 // warnings and new BugTypes. Because ImmutableSet is a functional data
358 // structure, we do not need to worry about the iterators being invalidated.
359 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
360 const_cast<BugType*>(*I)->FlushReports(*this);
361
362 // Iterate through BugTypes a second time. BugTypes may have been updated
363 // with new BugType objects and new warnings.
364 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
365 BugType *BT = const_cast<BugType*>(*I);
366
367 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
368 SetTy& EQClasses = BT->EQClasses;
369
370 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
371 BugReportEquivClass& EQ = *EI;
372 FlushReport(EQ);
373 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000374
Ted Kremenekcf118d42009-02-04 23:49:09 +0000375 // Delete the BugType object. This will also delete the equivalence
376 // classes.
377 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000378 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000379
380 // Remove all references to the BugType objects.
381 BugTypes = F.GetEmptySet();
382}
383
384//===----------------------------------------------------------------------===//
385// PathDiagnostics generation.
386//===----------------------------------------------------------------------===//
387
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000388static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000389 std::pair<ExplodedNode<GRState>*, unsigned> >
390MakeReportGraph(const ExplodedGraph<GRState>* G,
391 const ExplodedNode<GRState>** NStart,
392 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000393
Ted Kremenekcf118d42009-02-04 23:49:09 +0000394 // Create the trimmed graph. It will contain the shortest paths from the
395 // error nodes to the root. In the new graph we should only have one
396 // error node unless there are two or more error nodes with the same minimum
397 // path length.
398 ExplodedGraph<GRState>* GTrim;
399 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000400
401 llvm::DenseMap<const void*, const void*> InverseMap;
402 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000403
404 // Create owning pointers for GTrim and NMap just to ensure that they are
405 // released when this function exists.
406 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
407 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
408
409 // Find the (first) error node in the trimmed graph. We just need to consult
410 // the node map (NMap) which maps from nodes in the original graph to nodes
411 // in the new graph.
412 const ExplodedNode<GRState>* N = 0;
413 unsigned NodeIndex = 0;
414
415 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
416 if ((N = NMap->getMappedNode(*I))) {
417 NodeIndex = (I - NStart) / sizeof(*I);
418 break;
419 }
420
421 assert(N && "No error node found in the trimmed graph.");
422
423 // Create a new (third!) graph with a single path. This is the graph
424 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000425 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000426 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
427 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000428
Ted Kremenek10aa5542009-03-12 23:41:59 +0000429 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000430 // to the root node, and then construct a new graph that contains only
431 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000432 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000433 std::queue<const ExplodedNode<GRState>*> WS;
434 WS.push(N);
435
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000436 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000437 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000438
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000439 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000440 const ExplodedNode<GRState>* Node = WS.front();
441 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000442
443 if (Visited.find(Node) != Visited.end())
444 continue;
445
446 Visited[Node] = cnt++;
447
448 if (Node->pred_empty()) {
449 Root = Node;
450 break;
451 }
452
Ted Kremenek3148eb42009-01-24 00:55:43 +0000453 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000454 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000455 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000456 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000457
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000458 assert (Root);
459
Ted Kremenek10aa5542009-03-12 23:41:59 +0000460 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000461 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000462 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000463 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000464
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000465 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000466 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000467 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000468 assert (I != Visited.end());
469
470 // Create the equivalent node in the new graph with the same state
471 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000472 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000473 GNew->getNode(N->getLocation(), N->getState());
474
475 // Store the mapping to the original node.
476 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
477 assert(IMitr != InverseMap.end() && "No mapping to original node.");
478 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000479
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000480 // Link up the new node with the previous node.
481 if (Last)
482 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000483
484 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000485
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000486 // Are we at the final node?
487 if (I->second == 0) {
488 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000489 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000490 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000491
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000492 // Find the next successor node. We choose the node that is marked
493 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000494 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
495 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000496 N = 0;
497
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000498 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000499
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000500 I = Visited.find(*SI);
501
502 if (I == Visited.end())
503 continue;
504
505 if (!N || I->second < MinVal) {
506 N = *SI;
507 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000508 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000509 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000510
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000511 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000512 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000513
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000514 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000515 return std::make_pair(std::make_pair(GNew, BM),
516 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000517}
518
Ted Kremenek3148eb42009-01-24 00:55:43 +0000519static const VarDecl*
520GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
521 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000522
523 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
524
525 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000526
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000527 if (!isa<PostStmt>(P))
528 continue;
529
530 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000531
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000532 if (!DR)
533 continue;
534
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000535 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000536
537 if (X != Y)
538 continue;
539
540 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
541
542 if (!VD)
543 continue;
544
545 return VD;
546 }
547
548 return 0;
549}
550
Ted Kremenek9e240492008-10-04 05:50:14 +0000551namespace {
552class VISIBILITY_HIDDEN NotableSymbolHandler
553 : public StoreManager::BindingsHandler {
554
Ted Kremenek2dabd432008-12-05 02:27:51 +0000555 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000556 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000557 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000558 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000559 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000560 PathDiagnostic& PD;
561 BugReporter& BR;
562
563public:
564
Ted Kremenek3148eb42009-01-24 00:55:43 +0000565 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
566 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000567 PathDiagnostic& pd, BugReporter& br)
568 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
569
Ted Kremenek0297ee02009-03-30 18:39:15 +0000570 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
571 SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000572
Ted Kremenek0297ee02009-03-30 18:39:15 +0000573 SymbolRef ScanSym = V.getAsSymbol();
574
Ted Kremenek9e240492008-10-04 05:50:14 +0000575 if (ScanSym != Sym)
576 return true;
577
578 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000579 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000580
581 if (X == V) // Same binding?
582 return true;
583
584 // Different binding. Only handle assignments for now. We don't pull
585 // this check out of the loop because we will eventually handle other
586 // cases.
587
588 VarDecl *VD = 0;
589
Ted Kremenek3148eb42009-01-24 00:55:43 +0000590 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000591 if (!B->isAssignmentOp())
592 return true;
593
594 // What variable did we assign to?
595 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
596
597 if (!DR)
598 return true;
599
600 VD = dyn_cast<VarDecl>(DR->getDecl());
601 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000602 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000603 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
604 // assume that each DeclStmt has a single Decl. This invariant
605 // holds by contruction in the CFG.
606 VD = dyn_cast<VarDecl>(*DS->decl_begin());
607 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000608
609 if (!VD)
610 return true;
611
612 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000613 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000614
615 if (!MostRecent)
616 return true;
617
618 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000619 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
620
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000621 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000622 std::string msg = "'" + std::string(VD->getNameAsString()) +
623 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000624
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000625 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000626 }
627
628 return true;
629 }
630};
631}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000632
Ted Kremenek3148eb42009-01-24 00:55:43 +0000633static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
634 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000635 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000636 PathDiagnostic& PD) {
637
Ted Kremenek3148eb42009-01-24 00:55:43 +0000638 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000639 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000640
641 if (!PrevSt)
642 return;
643
Ted Kremenek9e240492008-10-04 05:50:14 +0000644 // Look at the region bindings of the current state that map to the
645 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000646 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000647 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
648 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
649}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000650
Ted Kremenek9e240492008-10-04 05:50:14 +0000651namespace {
652class VISIBILITY_HIDDEN ScanNotableSymbols
653 : public StoreManager::BindingsHandler {
654
Ted Kremenek2dabd432008-12-05 02:27:51 +0000655 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000656 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000657 Stmt* S;
658 GRBugReporter& BR;
659 PathDiagnostic& PD;
660
661public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000662 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000663 PathDiagnostic& pd)
664 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000665
Ted Kremenekbe912242009-03-05 16:31:07 +0000666 bool HandleBinding(StoreManager& SMgr, Store store,
667 const MemRegion* R, SVal V) {
Ted Kremenek93e71452009-03-30 19:53:37 +0000668
669 SymbolRef ScanSym = V.getAsSymbol();
670
671 if (!ScanSym)
Ted Kremenek9e240492008-10-04 05:50:14 +0000672 return true;
673
Ted Kremenek9e240492008-10-04 05:50:14 +0000674 if (!BR.isNotable(ScanSym))
675 return true;
676
677 if (AlreadyProcessed.count(ScanSym))
678 return true;
679
680 AlreadyProcessed.insert(ScanSym);
681
682 HandleNotableSymbol(N, S, ScanSym, BR, PD);
683 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000684 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000685};
686} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000687
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000688/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
689/// and collapses PathDiagosticPieces that are expanded by macros.
690static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
691 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
692 MacroStackTy;
693
694 typedef std::vector<PathDiagnosticPiece*>
695 PiecesTy;
696
697 MacroStackTy MacroStack;
698 PiecesTy Pieces;
699
700 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
701 // Get the location of the PathDiagnosticPiece.
702 const FullSourceLoc Loc = I->getLocation();
703
704 // Determine the instantiation location, which is the location we group
705 // related PathDiagnosticPieces.
706 SourceLocation InstantiationLoc = Loc.isMacroID() ?
707 SM.getInstantiationLoc(Loc) :
708 SourceLocation();
709
710 if (Loc.isFileID()) {
711 MacroStack.clear();
712 Pieces.push_back(&*I);
713 continue;
714 }
715
716 assert(Loc.isMacroID());
717
718 // Is the PathDiagnosticPiece within the same macro group?
719 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
720 MacroStack.back().first->push_back(&*I);
721 continue;
722 }
723
724 // We aren't in the same group. Are we descending into a new macro
725 // or are part of an old one?
726 PathDiagnosticMacroPiece *MacroGroup = 0;
727
728 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
729 SM.getInstantiationLoc(Loc) :
730 SourceLocation();
731
732 // Walk the entire macro stack.
733 while (!MacroStack.empty()) {
734 if (InstantiationLoc == MacroStack.back().second) {
735 MacroGroup = MacroStack.back().first;
736 break;
737 }
738
739 if (ParentInstantiationLoc == MacroStack.back().second) {
740 MacroGroup = MacroStack.back().first;
741 break;
742 }
743
744 MacroStack.pop_back();
745 }
746
747 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
748 // Create a new macro group and add it to the stack.
749 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
750
751 if (MacroGroup)
752 MacroGroup->push_back(NewGroup);
753 else {
754 assert(InstantiationLoc.isFileID());
755 Pieces.push_back(NewGroup);
756 }
757
758 MacroGroup = NewGroup;
759 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
760 }
761
762 // Finally, add the PathDiagnosticPiece to the group.
763 MacroGroup->push_back(&*I);
764 }
765
766 // Now take the pieces and construct a new PathDiagnostic.
767 PD.resetPath(false);
768
769 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
770 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
771 if (!MP->containsEvent()) {
772 delete MP;
773 continue;
774 }
775
776 PD.push_back(*I);
777 }
778}
779
Ted Kremenek7dc86642009-03-31 20:22:36 +0000780static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
781 PathDiagnosticBuilder &PDB,
782 const ExplodedNode<GRState> *N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000783
Ted Kremenek7dc86642009-03-31 20:22:36 +0000784void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
785 BugReportEquivClass& EQ) {
786
787 std::vector<const ExplodedNode<GRState>*> Nodes;
788
Ted Kremenekcf118d42009-02-04 23:49:09 +0000789 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
790 const ExplodedNode<GRState>* N = I->getEndNode();
791 if (N) Nodes.push_back(N);
792 }
793
794 if (Nodes.empty())
795 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000796
797 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000798 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000799 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenek7dc86642009-03-31 20:22:36 +0000800 std::pair<ExplodedNode<GRState>*, unsigned> >&
801 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000802
Ted Kremenekcf118d42009-02-04 23:49:09 +0000803 // Find the BugReport with the original location.
804 BugReport *R = 0;
805 unsigned i = 0;
806 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
807 if (i == GPair.second.second) { R = *I; break; }
808
809 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000810
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000811 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
812 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000813 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000814
Ted Kremenekcf118d42009-02-04 23:49:09 +0000815 // Start building the path diagnostic...
816 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000817 PD.push_back(Piece);
818 else
819 return;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000820
821 PathDiagnosticBuilder PDB(*this, ReportGraph.get(), R, BackMap.get(),
822 getStateManager().getCodeDecl(),
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000823 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000824
Ted Kremenek7dc86642009-03-31 20:22:36 +0000825 switch (PDB.getGenerationScheme()) {
826 case PathDiagnosticClient::Extensive:
827 case PathDiagnosticClient::Minimal:
828 GenerateMinimalPathDiagnostic(PD, PDB, N);
829 break;
830 }
831
832 // After constructing the full PathDiagnostic, do a pass over it to compact
833 // PathDiagnosticPieces that occur within a macro.
834 CompactPathDiagnostic(PD, PDB.getSourceManager());
835}
836
837static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
838 PathDiagnosticBuilder &PDB,
839 const ExplodedNode<GRState> *N) {
840
841
842 ASTContext& Ctx = PDB.getContext();
843 SourceManager& SMgr = PDB.getSourceManager();
844 const ExplodedNode<GRState>* NextNode = N->pred_empty()
845 ? NULL : *(N->pred_begin());
846
Ted Kremenek6837faa2008-04-09 00:20:43 +0000847 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000848 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000849 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000850
851 ProgramPoint P = N->getLocation();
852
853 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000854 CFGBlock* Src = BE->getSrc();
855 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000856 Stmt* T = Src->getTerminator();
857
858 if (!T)
859 continue;
860
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000861 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000862
863 switch (T->getStmtClass()) {
864 default:
865 break;
866
867 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000868 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000869 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000870
871 if (!S)
872 continue;
873
Ted Kremenek297308e2009-02-10 23:56:07 +0000874 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000875 llvm::raw_string_ostream os(sbuf);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000876 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000877
Ted Kremenek00605e02009-03-27 20:55:39 +0000878 os << "Control jumps to line "
879 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000880 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
881 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000882 break;
883 }
884
Ted Kremenek297308e2009-02-10 23:56:07 +0000885 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000886 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000887 std::string sbuf;
888 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000889
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000890 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000891 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000892
Ted Kremenek5a429952008-04-23 23:35:07 +0000893 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000894 default:
895 os << "No cases match in the switch statement. "
896 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000897 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000898 break;
899 case Stmt::DefaultStmtClass:
900 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000901 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000902 break;
903
904 case Stmt::CaseStmtClass: {
905 os << "Control jumps to 'case ";
906 CaseStmt* Case = cast<CaseStmt>(S);
907 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
908
909 // Determine if it is an enum.
910 bool GetRawInt = true;
911
912 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
913 // FIXME: Maybe this should be an assertion. Are there cases
914 // were it is not an EnumConstantDecl?
915 EnumConstantDecl* D =
916 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000917
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000918 if (D) {
919 GetRawInt = false;
920 os << D->getNameAsString();
921 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000922 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000923
924 if (GetRawInt) {
925
926 // Not an enum.
927 Expr* CondE = cast<SwitchStmt>(T)->getCond();
928 unsigned bits = Ctx.getTypeSize(CondE->getType());
929 llvm::APSInt V(bits, false);
930
931 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
932 assert (false && "Case condition must be constant.");
933 continue;
934 }
935
936 os << V;
937 }
938
Ted Kremenek00605e02009-03-27 20:55:39 +0000939 os << ":' at line "
940 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000941 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000942 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000943 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000944 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
945 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000946 }
Ted Kremenek56783922008-04-25 01:29:56 +0000947 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000948 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000949 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
950 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
951 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000952 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000953
Ted Kremenek61f3e052008-04-03 04:42:52 +0000954 break;
955 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000956
957 case Stmt::BreakStmtClass:
958 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000959 std::string sbuf;
960 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000961 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000962 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
963 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000964 break;
965 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000966
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000967 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000968 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000969 std::string sbuf;
970 llvm::raw_string_ostream os(sbuf);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000971 os << "'?' condition is ";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000972
973 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000974 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000975 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000976 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000977
Ted Kremenek00605e02009-03-27 20:55:39 +0000978 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000979
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000980 if (const Stmt *S = End.asStmt())
981 End = PDB.getEnclosingStmtLocation(S);
982
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000983 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
984 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000985 break;
986 }
987
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000988 // Determine control-flow for short-circuited '&&' and '||'.
989 case Stmt::BinaryOperatorClass: {
990 if (!PDB.supportsLogicalOpControlFlow())
991 break;
992
993 BinaryOperator *B = cast<BinaryOperator>(T);
994 std::string sbuf;
995 llvm::raw_string_ostream os(sbuf);
996 os << "Left side of '";
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000997
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000998 if (B->getOpcode() == BinaryOperator::LAnd) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000999 os << "&&" << "' is ";
1000
1001 if (*(Src->succ_begin()+1) == Dst) {
1002 os << "false";
1003 PathDiagnosticLocation End(B->getLHS(), SMgr);
1004 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
1005 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1006 os.str()));
1007 }
1008 else {
1009 os << "true";
1010 PathDiagnosticLocation Start(B->getLHS(), SMgr);
1011 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1012 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1013 os.str()));
1014 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +00001015 }
1016 else {
1017 assert(B->getOpcode() == BinaryOperator::LOr);
Ted Kremenekf5ab8e62009-03-28 17:33:57 +00001018 os << "||" << "' is ";
1019
1020 if (*(Src->succ_begin()+1) == Dst) {
1021 os << "false";
1022 PathDiagnosticLocation Start(B->getLHS(), SMgr);
1023 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1024 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1025 os.str()));
1026 }
1027 else {
1028 os << "true";
1029 PathDiagnosticLocation End(B->getLHS(), SMgr);
1030 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
1031 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1032 os.str()));
1033 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +00001034 }
1035
Ted Kremenekbabdd7b2009-03-27 05:06:10 +00001036 break;
1037 }
1038
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001039 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001040 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001041 std::string sbuf;
1042 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001043
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001044 os << "Loop condition is true. ";
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001045 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
1046
1047 if (const Stmt *S = End.asStmt())
1048 End = PDB.getEnclosingStmtLocation(S);
1049
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001050 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1051 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001052 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001053 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001054 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001055
1056 if (const Stmt *S = End.asStmt())
1057 End = PDB.getEnclosingStmtLocation(S);
1058
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001059 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1060 "Loop condition is false. Exiting loop"));
1061 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001062
1063 break;
1064 }
1065
Ted Kremenek61f3e052008-04-03 04:42:52 +00001066 case Stmt::WhileStmtClass:
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001067 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001068 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001069 std::string sbuf;
1070 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001071
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001072 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +00001073 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001074 if (const Stmt *S = End.asStmt())
1075 End = PDB.getEnclosingStmtLocation(S);
1076
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001077 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1078 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001079 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001080 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001081 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001082 if (const Stmt *S = End.asStmt())
1083 End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001084
1085 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1086 "Loop condition is true. Entering loop body"));
1087 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001088
1089 break;
1090 }
1091
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001092 case Stmt::IfStmtClass: {
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001093 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1094
1095 if (const Stmt *S = End.asStmt())
1096 End = PDB.getEnclosingStmtLocation(S);
1097
Ted Kremenek61f3e052008-04-03 04:42:52 +00001098 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001099 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1100 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +00001101 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001102 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1103 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +00001104
1105 break;
1106 }
1107 }
Ted Kremenek6837faa2008-04-09 00:20:43 +00001108 }
Ted Kremenek5a429952008-04-23 23:35:07 +00001109
Ted Kremenek7dc86642009-03-31 20:22:36 +00001110 if (PathDiagnosticPiece* p =
1111 PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(),
1112 PDB.getBugReporter(),
1113 PDB.getNodeMapClosure())) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001114 PD.push_front(p);
Ted Kremenek7dc86642009-03-31 20:22:36 +00001115 }
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001116
Ted Kremenek9e240492008-10-04 05:50:14 +00001117 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
1118 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001119 // lval binding.
Ted Kremenek7dc86642009-03-31 20:22:36 +00001120 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
1121 PDB.getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001122 }
Ted Kremenek61f3e052008-04-03 04:42:52 +00001123 }
1124}
1125
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001126
Ted Kremenekcf118d42009-02-04 23:49:09 +00001127void BugReporter::Register(BugType *BT) {
1128 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001129}
1130
Ted Kremenekcf118d42009-02-04 23:49:09 +00001131void BugReporter::EmitReport(BugReport* R) {
1132 // Compute the bug report's hash to determine its equivalence class.
1133 llvm::FoldingSetNodeID ID;
1134 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001135
Ted Kremenekcf118d42009-02-04 23:49:09 +00001136 // Lookup the equivance class. If there isn't one, create it.
1137 BugType& BT = R->getBugType();
1138 Register(&BT);
1139 void *InsertPos;
1140 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1141
1142 if (!EQ) {
1143 EQ = new BugReportEquivClass(R);
1144 BT.EQClasses.InsertNode(EQ, InsertPos);
1145 }
1146 else
1147 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001148}
1149
Ted Kremenekcf118d42009-02-04 23:49:09 +00001150void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1151 assert(!EQ.Reports.empty());
1152 BugReport &R = **EQ.begin();
1153
1154 // FIXME: Make sure we use the 'R' for the path that was actually used.
1155 // Probably doesn't make a difference in practice.
1156 BugType& BT = R.getBugType();
1157
1158 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1159 R.getDescription(),
1160 BT.getCategory()));
1161 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001162
1163 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001164 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001165 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001166
Ted Kremenek3148eb42009-01-24 00:55:43 +00001167 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001168 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001169 const SourceRange *Beg = 0, *End = 0;
1170 R.getRanges(*this, Beg, End);
1171 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001172 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001173 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1174 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001175
Ted Kremenek3148eb42009-01-24 00:55:43 +00001176 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001177 default: assert(0 && "Don't handle this many ranges yet!");
1178 case 0: Diag.Report(L, ErrorDiag); break;
1179 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1180 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1181 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001182 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001183
1184 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1185 if (!PD)
1186 return;
1187
1188 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001189 PathDiagnosticPiece* piece =
1190 new PathDiagnosticEventPiece(L, R.getDescription());
1191
Ted Kremenek3148eb42009-01-24 00:55:43 +00001192 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1193 D->push_back(piece);
1194 }
1195
1196 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001197}
Ted Kremenek57202072008-07-14 17:40:50 +00001198
Ted Kremenek8c036c72008-09-20 04:23:38 +00001199void BugReporter::EmitBasicReport(const char* name, const char* str,
1200 SourceLocation Loc,
1201 SourceRange* RBeg, unsigned NumRanges) {
1202 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1203}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001204
Ted Kremenek8c036c72008-09-20 04:23:38 +00001205void BugReporter::EmitBasicReport(const char* name, const char* category,
1206 const char* str, SourceLocation Loc,
1207 SourceRange* RBeg, unsigned NumRanges) {
1208
Ted Kremenekcf118d42009-02-04 23:49:09 +00001209 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1210 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001211 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001212 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1213 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1214 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001215}