blob: f5ba3275b9b95b2a54d2f6117f0272db247a235a [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 Kremenekbabdd7b2009-03-27 05:06:10 +0000105namespace {
106class VISIBILITY_HIDDEN PathDiagnosticBuilder {
107 SourceManager &SMgr;
108 const Decl& CodeDecl;
109 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000110 llvm::OwningPtr<ParentMap> PM;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000111public:
112 PathDiagnosticBuilder(SourceManager &smgr, const Decl& codedecl,
113 PathDiagnosticClient *pdc)
114 : SMgr(smgr), CodeDecl(codedecl), PDC(pdc) {}
115
Ted Kremenek00605e02009-03-27 20:55:39 +0000116 PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N);
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000117
Ted Kremenek00605e02009-03-27 20:55:39 +0000118 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
119 const ExplodedNode<GRState>* N);
120
121 ParentMap& getParentMap() {
122 if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody()));
123 return *PM.get();
124 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000125
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000126 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
127
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000128 bool supportsLogicalOpControlFlow() const {
129 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
130 }
131};
132} // end anonymous namespace
133
Ted Kremenek00605e02009-03-27 20:55:39 +0000134PathDiagnosticLocation
135PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) {
136 if (Stmt *S = GetNextStmt(N))
137 return PathDiagnosticLocation(S, SMgr);
138
139 return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000140}
141
Ted Kremenek00605e02009-03-27 20:55:39 +0000142PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000143PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
144 const ExplodedNode<GRState>* N) {
145
Ted Kremenek143ca222008-05-06 18:11:09 +0000146 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000147 if (os.str().empty())
148 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000149
Ted Kremenek00605e02009-03-27 20:55:39 +0000150 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000151
Ted Kremenek00605e02009-03-27 20:55:39 +0000152 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000153 os << "Execution continues on line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000154 << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000155 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000156 os << "Execution jumps to the end of the "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000157 << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000158
159 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000160}
161
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000162PathDiagnosticLocation
163PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
164 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
165 ParentMap &P = getParentMap();
166 while (isa<Expr>(S)) {
167 const Stmt *Parent = P.getParent(S);
168
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000169 if (!Parent)
170 break;
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000171
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000172 switch (Parent->getStmtClass()) {
173 case Stmt::CompoundStmtClass:
174 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000175 return PathDiagnosticLocation(S, SMgr);
176 case Stmt::ChooseExprClass:
177 // Similar to '?' if we are referring to condition, just have the edge
178 // point to the entire choose expression.
179 if (cast<ChooseExpr>(Parent)->getCond() == S)
180 return PathDiagnosticLocation(Parent, SMgr);
181 else
182 return PathDiagnosticLocation(S, SMgr);
183 case Stmt::ConditionalOperatorClass:
184 // For '?', if we are referring to condition, just have the edge point
185 // to the entire '?' expression.
186 if (cast<ConditionalOperator>(Parent)->getCond() == S)
187 return PathDiagnosticLocation(Parent, SMgr);
188 else
189 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000190 case Stmt::DoStmtClass:
191 if (cast<DoStmt>(Parent)->getCond() != S)
192 return PathDiagnosticLocation(S, SMgr);
193 break;
194 case Stmt::ForStmtClass:
195 if (cast<ForStmt>(Parent)->getBody() == S)
196 return PathDiagnosticLocation(S, SMgr);
197 break;
198 case Stmt::IfStmtClass:
199 if (cast<IfStmt>(Parent)->getCond() != S)
200 return PathDiagnosticLocation(S, SMgr);
201 break;
202 case Stmt::ObjCForCollectionStmtClass:
203 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
204 return PathDiagnosticLocation(S, SMgr);
205 break;
206 case Stmt::WhileStmtClass:
207 if (cast<WhileStmt>(Parent)->getCond() != S)
208 return PathDiagnosticLocation(S, SMgr);
209 break;
210 default:
211 break;
212 }
213
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000214 S = Parent;
215 }
216
217 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
218 return PathDiagnosticLocation(S, SMgr);
219}
220
Ted Kremenekcf118d42009-02-04 23:49:09 +0000221//===----------------------------------------------------------------------===//
222// Methods for BugType and subclasses.
223//===----------------------------------------------------------------------===//
224BugType::~BugType() {}
225void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000226
Ted Kremenekcf118d42009-02-04 23:49:09 +0000227//===----------------------------------------------------------------------===//
228// Methods for BugReport and subclasses.
229//===----------------------------------------------------------------------===//
230BugReport::~BugReport() {}
231RangedBugReport::~RangedBugReport() {}
232
233Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000234 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000235 Stmt *S = NULL;
236
Ted Kremenekcf118d42009-02-04 23:49:09 +0000237 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000238 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000239 }
240 if (!S) S = GetStmt(ProgP);
241
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000242 return S;
243}
244
245PathDiagnosticPiece*
246BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000247 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000248
249 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000250
251 if (!S)
252 return NULL;
253
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000254 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000255 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000256
Ted Kremenekde7161f2008-04-03 18:00:37 +0000257 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000258 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000259
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000260 for (; Beg != End; ++Beg)
261 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000262
263 return P;
264}
265
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000266void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
267 const SourceRange*& end) {
268
269 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
270 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000271 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000272 beg = &R;
273 end = beg+1;
274 }
275 else
276 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000277}
278
Ted Kremenekcf118d42009-02-04 23:49:09 +0000279SourceLocation BugReport::getLocation() const {
280 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000281 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
282 // For member expressions, return the location of the '.' or '->'.
283 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
284 return ME->getMemberLoc();
285
Ted Kremenekcf118d42009-02-04 23:49:09 +0000286 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000287 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000288
289 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000290}
291
Ted Kremenek3148eb42009-01-24 00:55:43 +0000292PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
293 const ExplodedNode<GRState>* PrevN,
294 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000295 BugReporter& BR,
296 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000297 return NULL;
298}
299
Ted Kremenekcf118d42009-02-04 23:49:09 +0000300//===----------------------------------------------------------------------===//
301// Methods for BugReporter and subclasses.
302//===----------------------------------------------------------------------===//
303
304BugReportEquivClass::~BugReportEquivClass() {
305 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
306}
307
308GRBugReporter::~GRBugReporter() { FlushReports(); }
309BugReporterData::~BugReporterData() {}
310
311ExplodedGraph<GRState>&
312GRBugReporter::getGraph() { return Eng.getGraph(); }
313
314GRStateManager&
315GRBugReporter::getStateManager() { return Eng.getStateManager(); }
316
317BugReporter::~BugReporter() { FlushReports(); }
318
319void BugReporter::FlushReports() {
320 if (BugTypes.isEmpty())
321 return;
322
323 // First flush the warnings for each BugType. This may end up creating new
324 // warnings and new BugTypes. Because ImmutableSet is a functional data
325 // structure, we do not need to worry about the iterators being invalidated.
326 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
327 const_cast<BugType*>(*I)->FlushReports(*this);
328
329 // Iterate through BugTypes a second time. BugTypes may have been updated
330 // with new BugType objects and new warnings.
331 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
332 BugType *BT = const_cast<BugType*>(*I);
333
334 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
335 SetTy& EQClasses = BT->EQClasses;
336
337 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
338 BugReportEquivClass& EQ = *EI;
339 FlushReport(EQ);
340 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000341
Ted Kremenekcf118d42009-02-04 23:49:09 +0000342 // Delete the BugType object. This will also delete the equivalence
343 // classes.
344 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000345 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000346
347 // Remove all references to the BugType objects.
348 BugTypes = F.GetEmptySet();
349}
350
351//===----------------------------------------------------------------------===//
352// PathDiagnostics generation.
353//===----------------------------------------------------------------------===//
354
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000355typedef llvm::DenseMap<const ExplodedNode<GRState>*,
356 const ExplodedNode<GRState>*> NodeBackMap;
357
358static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000359 std::pair<ExplodedNode<GRState>*, unsigned> >
360MakeReportGraph(const ExplodedGraph<GRState>* G,
361 const ExplodedNode<GRState>** NStart,
362 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000363
Ted Kremenekcf118d42009-02-04 23:49:09 +0000364 // Create the trimmed graph. It will contain the shortest paths from the
365 // error nodes to the root. In the new graph we should only have one
366 // error node unless there are two or more error nodes with the same minimum
367 // path length.
368 ExplodedGraph<GRState>* GTrim;
369 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000370
371 llvm::DenseMap<const void*, const void*> InverseMap;
372 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000373
374 // Create owning pointers for GTrim and NMap just to ensure that they are
375 // released when this function exists.
376 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
377 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
378
379 // Find the (first) error node in the trimmed graph. We just need to consult
380 // the node map (NMap) which maps from nodes in the original graph to nodes
381 // in the new graph.
382 const ExplodedNode<GRState>* N = 0;
383 unsigned NodeIndex = 0;
384
385 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
386 if ((N = NMap->getMappedNode(*I))) {
387 NodeIndex = (I - NStart) / sizeof(*I);
388 break;
389 }
390
391 assert(N && "No error node found in the trimmed graph.");
392
393 // Create a new (third!) graph with a single path. This is the graph
394 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000395 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000396 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
397 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000398
Ted Kremenek10aa5542009-03-12 23:41:59 +0000399 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000400 // to the root node, and then construct a new graph that contains only
401 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000402 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000403 std::queue<const ExplodedNode<GRState>*> WS;
404 WS.push(N);
405
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000406 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000407 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000408
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000409 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000410 const ExplodedNode<GRState>* Node = WS.front();
411 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000412
413 if (Visited.find(Node) != Visited.end())
414 continue;
415
416 Visited[Node] = cnt++;
417
418 if (Node->pred_empty()) {
419 Root = Node;
420 break;
421 }
422
Ted Kremenek3148eb42009-01-24 00:55:43 +0000423 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000424 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000425 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000426 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000427
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000428 assert (Root);
429
Ted Kremenek10aa5542009-03-12 23:41:59 +0000430 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000431 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000432 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000433 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000434
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000435 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000436 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000437 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000438 assert (I != Visited.end());
439
440 // Create the equivalent node in the new graph with the same state
441 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000442 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000443 GNew->getNode(N->getLocation(), N->getState());
444
445 // Store the mapping to the original node.
446 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
447 assert(IMitr != InverseMap.end() && "No mapping to original node.");
448 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000449
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000450 // Link up the new node with the previous node.
451 if (Last)
452 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000453
454 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000455
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000456 // Are we at the final node?
457 if (I->second == 0) {
458 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000459 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000460 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000461
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000462 // Find the next successor node. We choose the node that is marked
463 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000464 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
465 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000466 N = 0;
467
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000468 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000469
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000470 I = Visited.find(*SI);
471
472 if (I == Visited.end())
473 continue;
474
475 if (!N || I->second < MinVal) {
476 N = *SI;
477 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000478 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000479 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000480
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000481 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000482 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000483
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000484 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000485 return std::make_pair(std::make_pair(GNew, BM),
486 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000487}
488
Ted Kremenek3148eb42009-01-24 00:55:43 +0000489static const VarDecl*
490GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
491 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000492
493 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
494
495 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000496
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000497 if (!isa<PostStmt>(P))
498 continue;
499
500 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000501
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000502 if (!DR)
503 continue;
504
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000505 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000506
507 if (X != Y)
508 continue;
509
510 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
511
512 if (!VD)
513 continue;
514
515 return VD;
516 }
517
518 return 0;
519}
520
Ted Kremenek9e240492008-10-04 05:50:14 +0000521namespace {
522class VISIBILITY_HIDDEN NotableSymbolHandler
523 : public StoreManager::BindingsHandler {
524
Ted Kremenek2dabd432008-12-05 02:27:51 +0000525 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000526 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000527 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000528 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000529 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000530 PathDiagnostic& PD;
531 BugReporter& BR;
532
533public:
534
Ted Kremenek3148eb42009-01-24 00:55:43 +0000535 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
536 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000537 PathDiagnostic& pd, BugReporter& br)
538 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
539
Ted Kremenekbe912242009-03-05 16:31:07 +0000540 bool HandleBinding(StoreManager& SMgr, Store store,
541 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000542
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000543 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000544
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000545 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000546 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000547 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000548 ScanSym = SV->getSymbol();
549 else
550 return true;
551
552 if (ScanSym != Sym)
553 return true;
554
555 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000556 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000557
558 if (X == V) // Same binding?
559 return true;
560
561 // Different binding. Only handle assignments for now. We don't pull
562 // this check out of the loop because we will eventually handle other
563 // cases.
564
565 VarDecl *VD = 0;
566
Ted Kremenek3148eb42009-01-24 00:55:43 +0000567 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000568 if (!B->isAssignmentOp())
569 return true;
570
571 // What variable did we assign to?
572 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
573
574 if (!DR)
575 return true;
576
577 VD = dyn_cast<VarDecl>(DR->getDecl());
578 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000579 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000580 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
581 // assume that each DeclStmt has a single Decl. This invariant
582 // holds by contruction in the CFG.
583 VD = dyn_cast<VarDecl>(*DS->decl_begin());
584 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000585
586 if (!VD)
587 return true;
588
589 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000590 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000591
592 if (!MostRecent)
593 return true;
594
595 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000596 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
597
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000598 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000599 std::string msg = "'" + std::string(VD->getNameAsString()) +
600 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000601
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000602 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000603 }
604
605 return true;
606 }
607};
608}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000609
Ted Kremenek3148eb42009-01-24 00:55:43 +0000610static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
611 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000612 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000613 PathDiagnostic& PD) {
614
Ted Kremenek3148eb42009-01-24 00:55:43 +0000615 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000616 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000617
618 if (!PrevSt)
619 return;
620
Ted Kremenek9e240492008-10-04 05:50:14 +0000621 // Look at the region bindings of the current state that map to the
622 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000623 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000624 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
625 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
626}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000627
Ted Kremenek9e240492008-10-04 05:50:14 +0000628namespace {
629class VISIBILITY_HIDDEN ScanNotableSymbols
630 : public StoreManager::BindingsHandler {
631
Ted Kremenek2dabd432008-12-05 02:27:51 +0000632 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000633 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000634 Stmt* S;
635 GRBugReporter& BR;
636 PathDiagnostic& PD;
637
638public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000639 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000640 PathDiagnostic& pd)
641 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000642
Ted Kremenekbe912242009-03-05 16:31:07 +0000643 bool HandleBinding(StoreManager& SMgr, Store store,
644 const MemRegion* R, SVal V) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000645 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000646
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000647 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000648 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000649 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000650 ScanSym = SV->getSymbol();
651 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000652 return true;
653
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000654 assert (ScanSym);
Ted Kremenek9e240492008-10-04 05:50:14 +0000655
656 if (!BR.isNotable(ScanSym))
657 return true;
658
659 if (AlreadyProcessed.count(ScanSym))
660 return true;
661
662 AlreadyProcessed.insert(ScanSym);
663
664 HandleNotableSymbol(N, S, ScanSym, BR, PD);
665 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000666 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000667};
668} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000669
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000670namespace {
671class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
672 NodeBackMap& M;
673public:
674 NodeMapClosure(NodeBackMap *m) : M(*m) {}
675 ~NodeMapClosure() {}
676
677 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
678 NodeBackMap::iterator I = M.find(N);
679 return I == M.end() ? 0 : I->second;
680 }
681};
682}
683
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000684/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
685/// and collapses PathDiagosticPieces that are expanded by macros.
686static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
687 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
688 MacroStackTy;
689
690 typedef std::vector<PathDiagnosticPiece*>
691 PiecesTy;
692
693 MacroStackTy MacroStack;
694 PiecesTy Pieces;
695
696 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
697 // Get the location of the PathDiagnosticPiece.
698 const FullSourceLoc Loc = I->getLocation();
699
700 // Determine the instantiation location, which is the location we group
701 // related PathDiagnosticPieces.
702 SourceLocation InstantiationLoc = Loc.isMacroID() ?
703 SM.getInstantiationLoc(Loc) :
704 SourceLocation();
705
706 if (Loc.isFileID()) {
707 MacroStack.clear();
708 Pieces.push_back(&*I);
709 continue;
710 }
711
712 assert(Loc.isMacroID());
713
714 // Is the PathDiagnosticPiece within the same macro group?
715 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
716 MacroStack.back().first->push_back(&*I);
717 continue;
718 }
719
720 // We aren't in the same group. Are we descending into a new macro
721 // or are part of an old one?
722 PathDiagnosticMacroPiece *MacroGroup = 0;
723
724 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
725 SM.getInstantiationLoc(Loc) :
726 SourceLocation();
727
728 // Walk the entire macro stack.
729 while (!MacroStack.empty()) {
730 if (InstantiationLoc == MacroStack.back().second) {
731 MacroGroup = MacroStack.back().first;
732 break;
733 }
734
735 if (ParentInstantiationLoc == MacroStack.back().second) {
736 MacroGroup = MacroStack.back().first;
737 break;
738 }
739
740 MacroStack.pop_back();
741 }
742
743 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
744 // Create a new macro group and add it to the stack.
745 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
746
747 if (MacroGroup)
748 MacroGroup->push_back(NewGroup);
749 else {
750 assert(InstantiationLoc.isFileID());
751 Pieces.push_back(NewGroup);
752 }
753
754 MacroGroup = NewGroup;
755 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
756 }
757
758 // Finally, add the PathDiagnosticPiece to the group.
759 MacroGroup->push_back(&*I);
760 }
761
762 // Now take the pieces and construct a new PathDiagnostic.
763 PD.resetPath(false);
764
765 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
766 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
767 if (!MP->containsEvent()) {
768 delete MP;
769 continue;
770 }
771
772 PD.push_back(*I);
773 }
774}
775
Ted Kremenekc0959972008-07-02 21:24:01 +0000776void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000777 BugReportEquivClass& EQ) {
778
779 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000780
Ted Kremenekcf118d42009-02-04 23:49:09 +0000781 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
782 const ExplodedNode<GRState>* N = I->getEndNode();
783 if (N) Nodes.push_back(N);
784 }
785
786 if (Nodes.empty())
787 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000788
789 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000790 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000791 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000792 std::pair<ExplodedNode<GRState>*, unsigned> >&
793 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000794
Ted Kremenekcf118d42009-02-04 23:49:09 +0000795 // Find the BugReport with the original location.
796 BugReport *R = 0;
797 unsigned i = 0;
798 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
799 if (i == GPair.second.second) { R = *I; break; }
800
801 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000802
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000803 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
804 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000805 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000806
Ted Kremenekcf118d42009-02-04 23:49:09 +0000807 // Start building the path diagnostic...
808 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000809 PD.push_back(Piece);
810 else
811 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000812
Ted Kremenek3148eb42009-01-24 00:55:43 +0000813 const ExplodedNode<GRState>* NextNode = N->pred_empty()
814 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000815
Ted Kremenekc0959972008-07-02 21:24:01 +0000816 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000817 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000818 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000819 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
820 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000821
Ted Kremenek6837faa2008-04-09 00:20:43 +0000822 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000823 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000824 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000825
826 ProgramPoint P = N->getLocation();
827
828 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000829 CFGBlock* Src = BE->getSrc();
830 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000831 Stmt* T = Src->getTerminator();
832
833 if (!T)
834 continue;
835
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000836 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000837
838 switch (T->getStmtClass()) {
839 default:
840 break;
841
842 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000843 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000844 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000845
846 if (!S)
847 continue;
848
Ted Kremenek297308e2009-02-10 23:56:07 +0000849 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000850 llvm::raw_string_ostream os(sbuf);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000851 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000852
Ted Kremenek00605e02009-03-27 20:55:39 +0000853 os << "Control jumps to line "
854 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000855 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
856 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000857 break;
858 }
859
Ted Kremenek297308e2009-02-10 23:56:07 +0000860 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000861 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000862 std::string sbuf;
863 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000864
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000865 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000866 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000867
Ted Kremenek5a429952008-04-23 23:35:07 +0000868 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000869 default:
870 os << "No cases match in the switch statement. "
871 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000872 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000873 break;
874 case Stmt::DefaultStmtClass:
875 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000876 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000877 break;
878
879 case Stmt::CaseStmtClass: {
880 os << "Control jumps to 'case ";
881 CaseStmt* Case = cast<CaseStmt>(S);
882 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
883
884 // Determine if it is an enum.
885 bool GetRawInt = true;
886
887 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
888 // FIXME: Maybe this should be an assertion. Are there cases
889 // were it is not an EnumConstantDecl?
890 EnumConstantDecl* D =
891 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000892
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000893 if (D) {
894 GetRawInt = false;
895 os << D->getNameAsString();
896 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000897 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000898
899 if (GetRawInt) {
900
901 // Not an enum.
902 Expr* CondE = cast<SwitchStmt>(T)->getCond();
903 unsigned bits = Ctx.getTypeSize(CondE->getType());
904 llvm::APSInt V(bits, false);
905
906 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
907 assert (false && "Case condition must be constant.");
908 continue;
909 }
910
911 os << V;
912 }
913
Ted Kremenek00605e02009-03-27 20:55:39 +0000914 os << ":' at line "
915 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000916 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000917 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000918 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000919 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
920 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000921 }
Ted Kremenek56783922008-04-25 01:29:56 +0000922 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000923 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000924 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
925 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
926 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000927 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000928
Ted Kremenek61f3e052008-04-03 04:42:52 +0000929 break;
930 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000931
932 case Stmt::BreakStmtClass:
933 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000934 std::string sbuf;
935 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000936 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000937 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
938 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000939 break;
940 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000941
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000942 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000943 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000944 std::string sbuf;
945 llvm::raw_string_ostream os(sbuf);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000946 os << "'?' condition is ";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000947
948 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000949 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000950 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000951 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000952
Ted Kremenek00605e02009-03-27 20:55:39 +0000953 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000954
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000955 if (const Stmt *S = End.asStmt())
956 End = PDB.getEnclosingStmtLocation(S);
957
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000958 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
959 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000960 break;
961 }
962
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000963 // Determine control-flow for short-circuited '&&' and '||'.
964 case Stmt::BinaryOperatorClass: {
965 if (!PDB.supportsLogicalOpControlFlow())
966 break;
967
968 BinaryOperator *B = cast<BinaryOperator>(T);
969 std::string sbuf;
970 llvm::raw_string_ostream os(sbuf);
971 os << "Left side of '";
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000972
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000973 if (B->getOpcode() == BinaryOperator::LAnd) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000974 os << "&&" << "' is ";
975
976 if (*(Src->succ_begin()+1) == Dst) {
977 os << "false";
978 PathDiagnosticLocation End(B->getLHS(), SMgr);
979 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
980 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
981 os.str()));
982 }
983 else {
984 os << "true";
985 PathDiagnosticLocation Start(B->getLHS(), SMgr);
986 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
987 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
988 os.str()));
989 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000990 }
991 else {
992 assert(B->getOpcode() == BinaryOperator::LOr);
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000993 os << "||" << "' is ";
994
995 if (*(Src->succ_begin()+1) == Dst) {
996 os << "false";
997 PathDiagnosticLocation Start(B->getLHS(), SMgr);
998 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
999 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1000 os.str()));
1001 }
1002 else {
1003 os << "true";
1004 PathDiagnosticLocation End(B->getLHS(), SMgr);
1005 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
1006 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1007 os.str()));
1008 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +00001009 }
1010
Ted Kremenekbabdd7b2009-03-27 05:06:10 +00001011 break;
1012 }
1013
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001014 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001015 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001016 std::string sbuf;
1017 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001018
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001019 os << "Loop condition is true. ";
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001020 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
1021
1022 if (const Stmt *S = End.asStmt())
1023 End = PDB.getEnclosingStmtLocation(S);
1024
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001025 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1026 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001027 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001028 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001029 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001030
1031 if (const Stmt *S = End.asStmt())
1032 End = PDB.getEnclosingStmtLocation(S);
1033
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001034 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1035 "Loop condition is false. Exiting loop"));
1036 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001037
1038 break;
1039 }
1040
Ted Kremenek61f3e052008-04-03 04:42:52 +00001041 case Stmt::WhileStmtClass:
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001042 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001043 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001044 std::string sbuf;
1045 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001046
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001047 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +00001048 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001049 if (const Stmt *S = End.asStmt())
1050 End = PDB.getEnclosingStmtLocation(S);
1051
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001052 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1053 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001054 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001055 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001056 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001057 if (const Stmt *S = End.asStmt())
1058 End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001059
1060 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1061 "Loop condition is true. Entering loop body"));
1062 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001063
1064 break;
1065 }
1066
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001067 case Stmt::IfStmtClass: {
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001068 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1069
1070 if (const Stmt *S = End.asStmt())
1071 End = PDB.getEnclosingStmtLocation(S);
1072
Ted Kremenek61f3e052008-04-03 04:42:52 +00001073 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001074 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1075 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +00001076 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001077 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1078 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +00001079
1080 break;
1081 }
1082 }
Ted Kremenek6837faa2008-04-09 00:20:43 +00001083 }
Ted Kremenek5a429952008-04-23 23:35:07 +00001084
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001085 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
1086 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001087 PD.push_front(p);
1088
Ted Kremenek9e240492008-10-04 05:50:14 +00001089 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
1090 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001091 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +00001092 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
1093 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001094 }
Ted Kremenek61f3e052008-04-03 04:42:52 +00001095 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001096
1097 // After constructing the full PathDiagnostic, do a pass over it to compact
1098 // PathDiagnosticPieces that occur within a macro.
1099 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001100}
1101
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001102
Ted Kremenekcf118d42009-02-04 23:49:09 +00001103void BugReporter::Register(BugType *BT) {
1104 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001105}
1106
Ted Kremenekcf118d42009-02-04 23:49:09 +00001107void BugReporter::EmitReport(BugReport* R) {
1108 // Compute the bug report's hash to determine its equivalence class.
1109 llvm::FoldingSetNodeID ID;
1110 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001111
Ted Kremenekcf118d42009-02-04 23:49:09 +00001112 // Lookup the equivance class. If there isn't one, create it.
1113 BugType& BT = R->getBugType();
1114 Register(&BT);
1115 void *InsertPos;
1116 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1117
1118 if (!EQ) {
1119 EQ = new BugReportEquivClass(R);
1120 BT.EQClasses.InsertNode(EQ, InsertPos);
1121 }
1122 else
1123 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001124}
1125
Ted Kremenekcf118d42009-02-04 23:49:09 +00001126void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1127 assert(!EQ.Reports.empty());
1128 BugReport &R = **EQ.begin();
1129
1130 // FIXME: Make sure we use the 'R' for the path that was actually used.
1131 // Probably doesn't make a difference in practice.
1132 BugType& BT = R.getBugType();
1133
1134 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1135 R.getDescription(),
1136 BT.getCategory()));
1137 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001138
1139 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001140 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001141 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001142
Ted Kremenek3148eb42009-01-24 00:55:43 +00001143 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001144 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001145 const SourceRange *Beg = 0, *End = 0;
1146 R.getRanges(*this, Beg, End);
1147 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001148 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001149 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1150 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001151
Ted Kremenek3148eb42009-01-24 00:55:43 +00001152 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001153 default: assert(0 && "Don't handle this many ranges yet!");
1154 case 0: Diag.Report(L, ErrorDiag); break;
1155 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1156 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1157 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001158 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001159
1160 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1161 if (!PD)
1162 return;
1163
1164 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001165 PathDiagnosticPiece* piece =
1166 new PathDiagnosticEventPiece(L, R.getDescription());
1167
Ted Kremenek3148eb42009-01-24 00:55:43 +00001168 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1169 D->push_back(piece);
1170 }
1171
1172 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001173}
Ted Kremenek57202072008-07-14 17:40:50 +00001174
Ted Kremenek8c036c72008-09-20 04:23:38 +00001175void BugReporter::EmitBasicReport(const char* name, const char* str,
1176 SourceLocation Loc,
1177 SourceRange* RBeg, unsigned NumRanges) {
1178 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1179}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001180
Ted Kremenek8c036c72008-09-20 04:23:38 +00001181void BugReporter::EmitBasicReport(const char* name, const char* category,
1182 const char* str, SourceLocation Loc,
1183 SourceRange* RBeg, unsigned NumRanges) {
1184
Ted Kremenekcf118d42009-02-04 23:49:09 +00001185 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1186 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001187 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001188 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1189 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1190 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001191}