blob: 36515870b8eb028229928010297f159ed2523b60 [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 Kremenek93e71452009-03-30 19:53:37 +0000638
639 SymbolRef ScanSym = V.getAsSymbol();
640
641 if (!ScanSym)
Ted Kremenek9e240492008-10-04 05:50:14 +0000642 return true;
643
Ted Kremenek9e240492008-10-04 05:50:14 +0000644 if (!BR.isNotable(ScanSym))
645 return true;
646
647 if (AlreadyProcessed.count(ScanSym))
648 return true;
649
650 AlreadyProcessed.insert(ScanSym);
651
652 HandleNotableSymbol(N, S, ScanSym, BR, PD);
653 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000654 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000655};
656} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000657
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000658namespace {
659class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
660 NodeBackMap& M;
661public:
662 NodeMapClosure(NodeBackMap *m) : M(*m) {}
663 ~NodeMapClosure() {}
664
665 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
666 NodeBackMap::iterator I = M.find(N);
667 return I == M.end() ? 0 : I->second;
668 }
669};
670}
671
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000672/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
673/// and collapses PathDiagosticPieces that are expanded by macros.
674static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
675 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
676 MacroStackTy;
677
678 typedef std::vector<PathDiagnosticPiece*>
679 PiecesTy;
680
681 MacroStackTy MacroStack;
682 PiecesTy Pieces;
683
684 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
685 // Get the location of the PathDiagnosticPiece.
686 const FullSourceLoc Loc = I->getLocation();
687
688 // Determine the instantiation location, which is the location we group
689 // related PathDiagnosticPieces.
690 SourceLocation InstantiationLoc = Loc.isMacroID() ?
691 SM.getInstantiationLoc(Loc) :
692 SourceLocation();
693
694 if (Loc.isFileID()) {
695 MacroStack.clear();
696 Pieces.push_back(&*I);
697 continue;
698 }
699
700 assert(Loc.isMacroID());
701
702 // Is the PathDiagnosticPiece within the same macro group?
703 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
704 MacroStack.back().first->push_back(&*I);
705 continue;
706 }
707
708 // We aren't in the same group. Are we descending into a new macro
709 // or are part of an old one?
710 PathDiagnosticMacroPiece *MacroGroup = 0;
711
712 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
713 SM.getInstantiationLoc(Loc) :
714 SourceLocation();
715
716 // Walk the entire macro stack.
717 while (!MacroStack.empty()) {
718 if (InstantiationLoc == MacroStack.back().second) {
719 MacroGroup = MacroStack.back().first;
720 break;
721 }
722
723 if (ParentInstantiationLoc == MacroStack.back().second) {
724 MacroGroup = MacroStack.back().first;
725 break;
726 }
727
728 MacroStack.pop_back();
729 }
730
731 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
732 // Create a new macro group and add it to the stack.
733 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
734
735 if (MacroGroup)
736 MacroGroup->push_back(NewGroup);
737 else {
738 assert(InstantiationLoc.isFileID());
739 Pieces.push_back(NewGroup);
740 }
741
742 MacroGroup = NewGroup;
743 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
744 }
745
746 // Finally, add the PathDiagnosticPiece to the group.
747 MacroGroup->push_back(&*I);
748 }
749
750 // Now take the pieces and construct a new PathDiagnostic.
751 PD.resetPath(false);
752
753 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
754 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
755 if (!MP->containsEvent()) {
756 delete MP;
757 continue;
758 }
759
760 PD.push_back(*I);
761 }
762}
763
Ted Kremenekc0959972008-07-02 21:24:01 +0000764void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000765 BugReportEquivClass& EQ) {
766
767 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000768
Ted Kremenekcf118d42009-02-04 23:49:09 +0000769 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
770 const ExplodedNode<GRState>* N = I->getEndNode();
771 if (N) Nodes.push_back(N);
772 }
773
774 if (Nodes.empty())
775 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000776
777 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000778 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000779 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000780 std::pair<ExplodedNode<GRState>*, unsigned> >&
781 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000782
Ted Kremenekcf118d42009-02-04 23:49:09 +0000783 // Find the BugReport with the original location.
784 BugReport *R = 0;
785 unsigned i = 0;
786 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
787 if (i == GPair.second.second) { R = *I; break; }
788
789 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000790
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000791 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
792 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000793 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000794
Ted Kremenekcf118d42009-02-04 23:49:09 +0000795 // Start building the path diagnostic...
796 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000797 PD.push_back(Piece);
798 else
799 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000800
Ted Kremenek3148eb42009-01-24 00:55:43 +0000801 const ExplodedNode<GRState>* NextNode = N->pred_empty()
802 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000803
Ted Kremenekc0959972008-07-02 21:24:01 +0000804 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000805 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000806 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000807 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
808 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000809
Ted Kremenek6837faa2008-04-09 00:20:43 +0000810 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000811 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000812 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000813
814 ProgramPoint P = N->getLocation();
815
816 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000817 CFGBlock* Src = BE->getSrc();
818 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000819 Stmt* T = Src->getTerminator();
820
821 if (!T)
822 continue;
823
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000824 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000825
826 switch (T->getStmtClass()) {
827 default:
828 break;
829
830 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000831 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000832 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000833
834 if (!S)
835 continue;
836
Ted Kremenek297308e2009-02-10 23:56:07 +0000837 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000838 llvm::raw_string_ostream os(sbuf);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000839 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000840
Ted Kremenek00605e02009-03-27 20:55:39 +0000841 os << "Control jumps to line "
842 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000843 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
844 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000845 break;
846 }
847
Ted Kremenek297308e2009-02-10 23:56:07 +0000848 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000849 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000850 std::string sbuf;
851 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000852
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000853 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000854 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000855
Ted Kremenek5a429952008-04-23 23:35:07 +0000856 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000857 default:
858 os << "No cases match in the switch statement. "
859 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000860 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000861 break;
862 case Stmt::DefaultStmtClass:
863 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000864 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000865 break;
866
867 case Stmt::CaseStmtClass: {
868 os << "Control jumps to 'case ";
869 CaseStmt* Case = cast<CaseStmt>(S);
870 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
871
872 // Determine if it is an enum.
873 bool GetRawInt = true;
874
875 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
876 // FIXME: Maybe this should be an assertion. Are there cases
877 // were it is not an EnumConstantDecl?
878 EnumConstantDecl* D =
879 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000880
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000881 if (D) {
882 GetRawInt = false;
883 os << D->getNameAsString();
884 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000885 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000886
887 if (GetRawInt) {
888
889 // Not an enum.
890 Expr* CondE = cast<SwitchStmt>(T)->getCond();
891 unsigned bits = Ctx.getTypeSize(CondE->getType());
892 llvm::APSInt V(bits, false);
893
894 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
895 assert (false && "Case condition must be constant.");
896 continue;
897 }
898
899 os << V;
900 }
901
Ted Kremenek00605e02009-03-27 20:55:39 +0000902 os << ":' at line "
903 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000904 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000905 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000906 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000907 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
908 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000909 }
Ted Kremenek56783922008-04-25 01:29:56 +0000910 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000911 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000912 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
913 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
914 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000915 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000916
Ted Kremenek61f3e052008-04-03 04:42:52 +0000917 break;
918 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000919
920 case Stmt::BreakStmtClass:
921 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000922 std::string sbuf;
923 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000924 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000925 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
926 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000927 break;
928 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000929
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000930 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000931 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000932 std::string sbuf;
933 llvm::raw_string_ostream os(sbuf);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000934 os << "'?' condition is ";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000935
936 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000937 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000938 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000939 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000940
Ted Kremenek00605e02009-03-27 20:55:39 +0000941 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000942
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000943 if (const Stmt *S = End.asStmt())
944 End = PDB.getEnclosingStmtLocation(S);
945
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000946 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
947 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000948 break;
949 }
950
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000951 // Determine control-flow for short-circuited '&&' and '||'.
952 case Stmt::BinaryOperatorClass: {
953 if (!PDB.supportsLogicalOpControlFlow())
954 break;
955
956 BinaryOperator *B = cast<BinaryOperator>(T);
957 std::string sbuf;
958 llvm::raw_string_ostream os(sbuf);
959 os << "Left side of '";
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000960
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000961 if (B->getOpcode() == BinaryOperator::LAnd) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000962 os << "&&" << "' is ";
963
964 if (*(Src->succ_begin()+1) == Dst) {
965 os << "false";
966 PathDiagnosticLocation End(B->getLHS(), SMgr);
967 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
968 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
969 os.str()));
970 }
971 else {
972 os << "true";
973 PathDiagnosticLocation Start(B->getLHS(), SMgr);
974 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
975 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
976 os.str()));
977 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000978 }
979 else {
980 assert(B->getOpcode() == BinaryOperator::LOr);
Ted Kremenekf5ab8e62009-03-28 17:33:57 +0000981 os << "||" << "' is ";
982
983 if (*(Src->succ_begin()+1) == Dst) {
984 os << "false";
985 PathDiagnosticLocation Start(B->getLHS(), SMgr);
986 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
987 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
988 os.str()));
989 }
990 else {
991 os << "true";
992 PathDiagnosticLocation End(B->getLHS(), SMgr);
993 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
994 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
995 os.str()));
996 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000997 }
998
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000999 break;
1000 }
1001
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001002 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001003 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001004 std::string sbuf;
1005 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001006
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001007 os << "Loop condition is true. ";
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001008 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
1009
1010 if (const Stmt *S = End.asStmt())
1011 End = PDB.getEnclosingStmtLocation(S);
1012
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001013 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1014 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001015 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001016 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001017 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001018
1019 if (const Stmt *S = End.asStmt())
1020 End = PDB.getEnclosingStmtLocation(S);
1021
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001022 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1023 "Loop condition is false. Exiting loop"));
1024 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001025
1026 break;
1027 }
1028
Ted Kremenek61f3e052008-04-03 04:42:52 +00001029 case Stmt::WhileStmtClass:
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001030 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001031 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +00001032 std::string sbuf;
1033 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001034
Ted Kremenekc3517eb2008-09-12 18:17:46 +00001035 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +00001036 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001037 if (const Stmt *S = End.asStmt())
1038 End = PDB.getEnclosingStmtLocation(S);
1039
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001040 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1041 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001042 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001043 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001044 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001045 if (const Stmt *S = End.asStmt())
1046 End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001047
1048 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1049 "Loop condition is true. Entering loop body"));
1050 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001051
1052 break;
1053 }
1054
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001055 case Stmt::IfStmtClass: {
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001056 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1057
1058 if (const Stmt *S = End.asStmt())
1059 End = PDB.getEnclosingStmtLocation(S);
1060
Ted Kremenek61f3e052008-04-03 04:42:52 +00001061 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001062 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1063 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +00001064 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001065 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1066 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +00001067
1068 break;
1069 }
1070 }
Ted Kremenek6837faa2008-04-09 00:20:43 +00001071 }
Ted Kremenek5a429952008-04-23 23:35:07 +00001072
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001073 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
1074 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001075 PD.push_front(p);
1076
Ted Kremenek9e240492008-10-04 05:50:14 +00001077 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
1078 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001079 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +00001080 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
1081 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001082 }
Ted Kremenek61f3e052008-04-03 04:42:52 +00001083 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001084
1085 // After constructing the full PathDiagnostic, do a pass over it to compact
1086 // PathDiagnosticPieces that occur within a macro.
1087 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001088}
1089
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001090
Ted Kremenekcf118d42009-02-04 23:49:09 +00001091void BugReporter::Register(BugType *BT) {
1092 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001093}
1094
Ted Kremenekcf118d42009-02-04 23:49:09 +00001095void BugReporter::EmitReport(BugReport* R) {
1096 // Compute the bug report's hash to determine its equivalence class.
1097 llvm::FoldingSetNodeID ID;
1098 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001099
Ted Kremenekcf118d42009-02-04 23:49:09 +00001100 // Lookup the equivance class. If there isn't one, create it.
1101 BugType& BT = R->getBugType();
1102 Register(&BT);
1103 void *InsertPos;
1104 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1105
1106 if (!EQ) {
1107 EQ = new BugReportEquivClass(R);
1108 BT.EQClasses.InsertNode(EQ, InsertPos);
1109 }
1110 else
1111 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001112}
1113
Ted Kremenekcf118d42009-02-04 23:49:09 +00001114void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1115 assert(!EQ.Reports.empty());
1116 BugReport &R = **EQ.begin();
1117
1118 // FIXME: Make sure we use the 'R' for the path that was actually used.
1119 // Probably doesn't make a difference in practice.
1120 BugType& BT = R.getBugType();
1121
1122 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1123 R.getDescription(),
1124 BT.getCategory()));
1125 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001126
1127 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001128 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001129 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001130
Ted Kremenek3148eb42009-01-24 00:55:43 +00001131 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001132 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001133 const SourceRange *Beg = 0, *End = 0;
1134 R.getRanges(*this, Beg, End);
1135 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001136 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001137 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1138 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001139
Ted Kremenek3148eb42009-01-24 00:55:43 +00001140 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001141 default: assert(0 && "Don't handle this many ranges yet!");
1142 case 0: Diag.Report(L, ErrorDiag); break;
1143 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1144 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1145 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001146 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001147
1148 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1149 if (!PD)
1150 return;
1151
1152 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001153 PathDiagnosticPiece* piece =
1154 new PathDiagnosticEventPiece(L, R.getDescription());
1155
Ted Kremenek3148eb42009-01-24 00:55:43 +00001156 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1157 D->push_back(piece);
1158 }
1159
1160 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001161}
Ted Kremenek57202072008-07-14 17:40:50 +00001162
Ted Kremenek8c036c72008-09-20 04:23:38 +00001163void BugReporter::EmitBasicReport(const char* name, const char* str,
1164 SourceLocation Loc,
1165 SourceRange* RBeg, unsigned NumRanges) {
1166 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1167}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001168
Ted Kremenek8c036c72008-09-20 04:23:38 +00001169void BugReporter::EmitBasicReport(const char* name, const char* category,
1170 const char* str, SourceLocation Loc,
1171 SourceRange* RBeg, unsigned NumRanges) {
1172
Ted Kremenekcf118d42009-02-04 23:49:09 +00001173 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1174 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001175 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001176 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1177 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1178 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001179}