blob: a240062ef7199a10676a5f354ef8976d2171028f [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 Kremenek0297ee02009-03-30 18:39:15 +0000540 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
541 SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000542
Ted Kremenek0297ee02009-03-30 18:39:15 +0000543 SymbolRef ScanSym = V.getAsSymbol();
544
Ted Kremenek9e240492008-10-04 05:50:14 +0000545 if (ScanSym != Sym)
546 return true;
547
548 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000549 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000550
551 if (X == V) // Same binding?
552 return true;
553
554 // Different binding. Only handle assignments for now. We don't pull
555 // this check out of the loop because we will eventually handle other
556 // cases.
557
558 VarDecl *VD = 0;
559
Ted Kremenek3148eb42009-01-24 00:55:43 +0000560 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000561 if (!B->isAssignmentOp())
562 return true;
563
564 // What variable did we assign to?
565 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
566
567 if (!DR)
568 return true;
569
570 VD = dyn_cast<VarDecl>(DR->getDecl());
571 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000572 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000573 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
574 // assume that each DeclStmt has a single Decl. This invariant
575 // holds by contruction in the CFG.
576 VD = dyn_cast<VarDecl>(*DS->decl_begin());
577 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000578
579 if (!VD)
580 return true;
581
582 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000583 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000584
585 if (!MostRecent)
586 return true;
587
588 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000589 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
590
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000591 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000592 std::string msg = "'" + std::string(VD->getNameAsString()) +
593 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000594
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000595 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000596 }
597
598 return true;
599 }
600};
601}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000602
Ted Kremenek3148eb42009-01-24 00:55:43 +0000603static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
604 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000605 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000606 PathDiagnostic& PD) {
607
Ted Kremenek3148eb42009-01-24 00:55:43 +0000608 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000609 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000610
611 if (!PrevSt)
612 return;
613
Ted Kremenek9e240492008-10-04 05:50:14 +0000614 // Look at the region bindings of the current state that map to the
615 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000616 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000617 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
618 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
619}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000620
Ted Kremenek9e240492008-10-04 05:50:14 +0000621namespace {
622class VISIBILITY_HIDDEN ScanNotableSymbols
623 : public StoreManager::BindingsHandler {
624
Ted Kremenek2dabd432008-12-05 02:27:51 +0000625 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000626 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000627 Stmt* S;
628 GRBugReporter& BR;
629 PathDiagnostic& PD;
630
631public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000632 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000633 PathDiagnostic& pd)
634 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000635
Ted Kremenekbe912242009-03-05 16:31:07 +0000636 bool HandleBinding(StoreManager& SMgr, Store store,
637 const MemRegion* R, SVal V) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000638 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000639
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000640 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000641 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000642 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000643 ScanSym = SV->getSymbol();
644 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000645 return true;
646
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000647 assert (ScanSym);
Ted Kremenek9e240492008-10-04 05:50:14 +0000648
649 if (!BR.isNotable(ScanSym))
650 return true;
651
652 if (AlreadyProcessed.count(ScanSym))
653 return true;
654
655 AlreadyProcessed.insert(ScanSym);
656
657 HandleNotableSymbol(N, S, ScanSym, BR, PD);
658 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000659 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000660};
661} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000662
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000663namespace {
664class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
665 NodeBackMap& M;
666public:
667 NodeMapClosure(NodeBackMap *m) : M(*m) {}
668 ~NodeMapClosure() {}
669
670 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
671 NodeBackMap::iterator I = M.find(N);
672 return I == M.end() ? 0 : I->second;
673 }
674};
675}
676
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000677/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
678/// and collapses PathDiagosticPieces that are expanded by macros.
679static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
680 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
681 MacroStackTy;
682
683 typedef std::vector<PathDiagnosticPiece*>
684 PiecesTy;
685
686 MacroStackTy MacroStack;
687 PiecesTy Pieces;
688
689 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
690 // Get the location of the PathDiagnosticPiece.
691 const FullSourceLoc Loc = I->getLocation();
692
693 // Determine the instantiation location, which is the location we group
694 // related PathDiagnosticPieces.
695 SourceLocation InstantiationLoc = Loc.isMacroID() ?
696 SM.getInstantiationLoc(Loc) :
697 SourceLocation();
698
699 if (Loc.isFileID()) {
700 MacroStack.clear();
701 Pieces.push_back(&*I);
702 continue;
703 }
704
705 assert(Loc.isMacroID());
706
707 // Is the PathDiagnosticPiece within the same macro group?
708 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
709 MacroStack.back().first->push_back(&*I);
710 continue;
711 }
712
713 // We aren't in the same group. Are we descending into a new macro
714 // or are part of an old one?
715 PathDiagnosticMacroPiece *MacroGroup = 0;
716
717 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
718 SM.getInstantiationLoc(Loc) :
719 SourceLocation();
720
721 // Walk the entire macro stack.
722 while (!MacroStack.empty()) {
723 if (InstantiationLoc == MacroStack.back().second) {
724 MacroGroup = MacroStack.back().first;
725 break;
726 }
727
728 if (ParentInstantiationLoc == MacroStack.back().second) {
729 MacroGroup = MacroStack.back().first;
730 break;
731 }
732
733 MacroStack.pop_back();
734 }
735
736 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
737 // Create a new macro group and add it to the stack.
738 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
739
740 if (MacroGroup)
741 MacroGroup->push_back(NewGroup);
742 else {
743 assert(InstantiationLoc.isFileID());
744 Pieces.push_back(NewGroup);
745 }
746
747 MacroGroup = NewGroup;
748 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
749 }
750
751 // Finally, add the PathDiagnosticPiece to the group.
752 MacroGroup->push_back(&*I);
753 }
754
755 // Now take the pieces and construct a new PathDiagnostic.
756 PD.resetPath(false);
757
758 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
759 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
760 if (!MP->containsEvent()) {
761 delete MP;
762 continue;
763 }
764
765 PD.push_back(*I);
766 }
767}
768
Ted Kremenekc0959972008-07-02 21:24:01 +0000769void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000770 BugReportEquivClass& EQ) {
771
772 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000773
Ted Kremenekcf118d42009-02-04 23:49:09 +0000774 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
775 const ExplodedNode<GRState>* N = I->getEndNode();
776 if (N) Nodes.push_back(N);
777 }
778
779 if (Nodes.empty())
780 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000781
782 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000783 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000784 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000785 std::pair<ExplodedNode<GRState>*, unsigned> >&
786 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000787
Ted Kremenekcf118d42009-02-04 23:49:09 +0000788 // Find the BugReport with the original location.
789 BugReport *R = 0;
790 unsigned i = 0;
791 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
792 if (i == GPair.second.second) { R = *I; break; }
793
794 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000795
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000796 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
797 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000798 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000799
Ted Kremenekcf118d42009-02-04 23:49:09 +0000800 // Start building the path diagnostic...
801 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000802 PD.push_back(Piece);
803 else
804 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000805
Ted Kremenek3148eb42009-01-24 00:55:43 +0000806 const ExplodedNode<GRState>* NextNode = N->pred_empty()
807 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000808
Ted Kremenekc0959972008-07-02 21:24:01 +0000809 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000810 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000811 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000812 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
813 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000814
Ted Kremenek6837faa2008-04-09 00:20:43 +0000815 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000816 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000817 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000818
819 ProgramPoint P = N->getLocation();
820
821 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000822 CFGBlock* Src = BE->getSrc();
823 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000824 Stmt* T = Src->getTerminator();
825
826 if (!T)
827 continue;
828
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000829 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000830
831 switch (T->getStmtClass()) {
832 default:
833 break;
834
835 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000836 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000837 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000838
839 if (!S)
840 continue;
841
Ted Kremenek297308e2009-02-10 23:56:07 +0000842 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000843 llvm::raw_string_ostream os(sbuf);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000844 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000845
Ted Kremenek00605e02009-03-27 20:55:39 +0000846 os << "Control jumps to line "
847 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000848 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
849 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000850 break;
851 }
852
Ted Kremenek297308e2009-02-10 23:56:07 +0000853 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000854 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000855 std::string sbuf;
856 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000857
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000858 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000859 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000860
Ted Kremenek5a429952008-04-23 23:35:07 +0000861 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000862 default:
863 os << "No cases match in the switch statement. "
864 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000865 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000866 break;
867 case Stmt::DefaultStmtClass:
868 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000869 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000870 break;
871
872 case Stmt::CaseStmtClass: {
873 os << "Control jumps to 'case ";
874 CaseStmt* Case = cast<CaseStmt>(S);
875 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
876
877 // Determine if it is an enum.
878 bool GetRawInt = true;
879
880 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
881 // FIXME: Maybe this should be an assertion. Are there cases
882 // were it is not an EnumConstantDecl?
883 EnumConstantDecl* D =
884 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000885
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000886 if (D) {
887 GetRawInt = false;
888 os << D->getNameAsString();
889 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000890 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000891
892 if (GetRawInt) {
893
894 // Not an enum.
895 Expr* CondE = cast<SwitchStmt>(T)->getCond();
896 unsigned bits = Ctx.getTypeSize(CondE->getType());
897 llvm::APSInt V(bits, false);
898
899 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
900 assert (false && "Case condition must be constant.");
901 continue;
902 }
903
904 os << V;
905 }
906
Ted Kremenek00605e02009-03-27 20:55:39 +0000907 os << ":' at line "
908 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000909 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000910 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000911 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000912 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
913 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000914 }
Ted Kremenek56783922008-04-25 01:29:56 +0000915 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000916 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000917 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
918 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
919 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000920 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000921
Ted Kremenek61f3e052008-04-03 04:42:52 +0000922 break;
923 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000924
925 case Stmt::BreakStmtClass:
926 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000927 std::string sbuf;
928 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000929 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000930 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
931 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000932 break;
933 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000934
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000935 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000936 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000937 std::string sbuf;
938 llvm::raw_string_ostream os(sbuf);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000939 os << "'?' condition is ";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000940
941 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000942 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000943 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000944 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000945
Ted Kremenek00605e02009-03-27 20:55:39 +0000946 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000947
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000948 if (const Stmt *S = End.asStmt())
949 End = PDB.getEnclosingStmtLocation(S);
950
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000951 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
952 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000953 break;
954 }
955
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000956 // Determine control-flow for short-circuited '&&' and '||'.
957 case Stmt::BinaryOperatorClass: {
958 if (!PDB.supportsLogicalOpControlFlow())
959 break;
960
961 BinaryOperator *B = cast<BinaryOperator>(T);
962 std::string sbuf;
963 llvm::raw_string_ostream os(sbuf);
964 os << "Left side of '";
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000965
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000966 if (B->getOpcode() == BinaryOperator::LAnd) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000967 os << "&&" << "' is ";
968
969 if (*(Src->succ_begin()+1) == Dst) {
970 os << "false";
971 PathDiagnosticLocation End(B->getLHS(), SMgr);
972 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
973 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
974 os.str()));
975 }
976 else {
977 os << "true";
978 PathDiagnosticLocation Start(B->getLHS(), SMgr);
979 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
980 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
981 os.str()));
982 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000983 }
984 else {
985 assert(B->getOpcode() == BinaryOperator::LOr);
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000986 os << "||" << "' is ";
987
988 if (*(Src->succ_begin()+1) == Dst) {
989 os << "false";
990 PathDiagnosticLocation Start(B->getLHS(), SMgr);
991 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
992 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
993 os.str()));
994 }
995 else {
996 os << "true";
997 PathDiagnosticLocation End(B->getLHS(), SMgr);
998 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
999 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1000 os.str()));
1001 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +00001002 }
1003
Ted Kremenekbabdd7b2009-03-27 05:06:10 +00001004 break;
1005 }
1006
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001007 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001008 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001009 std::string sbuf;
1010 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001011
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001012 os << "Loop condition is true. ";
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001013 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
1014
1015 if (const Stmt *S = End.asStmt())
1016 End = PDB.getEnclosingStmtLocation(S);
1017
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001018 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1019 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001020 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001021 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001022 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001023
1024 if (const Stmt *S = End.asStmt())
1025 End = PDB.getEnclosingStmtLocation(S);
1026
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001027 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1028 "Loop condition is false. Exiting loop"));
1029 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001030
1031 break;
1032 }
1033
Ted Kremenek61f3e052008-04-03 04:42:52 +00001034 case Stmt::WhileStmtClass:
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001035 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001036 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001037 std::string sbuf;
1038 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001039
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001040 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +00001041 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001042 if (const Stmt *S = End.asStmt())
1043 End = PDB.getEnclosingStmtLocation(S);
1044
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001045 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1046 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001047 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001048 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001049 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001050 if (const Stmt *S = End.asStmt())
1051 End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001052
1053 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1054 "Loop condition is true. Entering loop body"));
1055 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001056
1057 break;
1058 }
1059
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001060 case Stmt::IfStmtClass: {
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001061 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1062
1063 if (const Stmt *S = End.asStmt())
1064 End = PDB.getEnclosingStmtLocation(S);
1065
Ted Kremenek61f3e052008-04-03 04:42:52 +00001066 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001067 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1068 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +00001069 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001070 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1071 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +00001072
1073 break;
1074 }
1075 }
Ted Kremenek6837faa2008-04-09 00:20:43 +00001076 }
Ted Kremenek5a429952008-04-23 23:35:07 +00001077
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001078 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
1079 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001080 PD.push_front(p);
1081
Ted Kremenek9e240492008-10-04 05:50:14 +00001082 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
1083 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001084 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +00001085 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
1086 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001087 }
Ted Kremenek61f3e052008-04-03 04:42:52 +00001088 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001089
1090 // After constructing the full PathDiagnostic, do a pass over it to compact
1091 // PathDiagnosticPieces that occur within a macro.
1092 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001093}
1094
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001095
Ted Kremenekcf118d42009-02-04 23:49:09 +00001096void BugReporter::Register(BugType *BT) {
1097 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001098}
1099
Ted Kremenekcf118d42009-02-04 23:49:09 +00001100void BugReporter::EmitReport(BugReport* R) {
1101 // Compute the bug report's hash to determine its equivalence class.
1102 llvm::FoldingSetNodeID ID;
1103 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001104
Ted Kremenekcf118d42009-02-04 23:49:09 +00001105 // Lookup the equivance class. If there isn't one, create it.
1106 BugType& BT = R->getBugType();
1107 Register(&BT);
1108 void *InsertPos;
1109 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1110
1111 if (!EQ) {
1112 EQ = new BugReportEquivClass(R);
1113 BT.EQClasses.InsertNode(EQ, InsertPos);
1114 }
1115 else
1116 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001117}
1118
Ted Kremenekcf118d42009-02-04 23:49:09 +00001119void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1120 assert(!EQ.Reports.empty());
1121 BugReport &R = **EQ.begin();
1122
1123 // FIXME: Make sure we use the 'R' for the path that was actually used.
1124 // Probably doesn't make a difference in practice.
1125 BugType& BT = R.getBugType();
1126
1127 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1128 R.getDescription(),
1129 BT.getCategory()));
1130 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001131
1132 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001133 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001134 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001135
Ted Kremenek3148eb42009-01-24 00:55:43 +00001136 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001137 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001138 const SourceRange *Beg = 0, *End = 0;
1139 R.getRanges(*this, Beg, End);
1140 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001141 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001142 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1143 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001144
Ted Kremenek3148eb42009-01-24 00:55:43 +00001145 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001146 default: assert(0 && "Don't handle this many ranges yet!");
1147 case 0: Diag.Report(L, ErrorDiag); break;
1148 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1149 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1150 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001151 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001152
1153 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1154 if (!PD)
1155 return;
1156
1157 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001158 PathDiagnosticPiece* piece =
1159 new PathDiagnosticEventPiece(L, R.getDescription());
1160
Ted Kremenek3148eb42009-01-24 00:55:43 +00001161 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1162 D->push_back(piece);
1163 }
1164
1165 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001166}
Ted Kremenek57202072008-07-14 17:40:50 +00001167
Ted Kremenek8c036c72008-09-20 04:23:38 +00001168void BugReporter::EmitBasicReport(const char* name, const char* str,
1169 SourceLocation Loc,
1170 SourceRange* RBeg, unsigned NumRanges) {
1171 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1172}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001173
Ted Kremenek8c036c72008-09-20 04:23:38 +00001174void BugReporter::EmitBasicReport(const char* name, const char* category,
1175 const char* str, SourceLocation Loc,
1176 SourceRange* RBeg, unsigned NumRanges) {
1177
Ted Kremenekcf118d42009-02-04 23:49:09 +00001178 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1179 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001180 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001181 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1182 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1183 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001184}