blob: be666706414a91940f4210b4ff82e69ffeeae082 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
11// PathDiagnostics for analyses based on GRSimpleVals.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/CFG.h"
21#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000023#include "clang/Analysis/ProgramPoint.h"
24#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000026#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000027#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000028#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000029#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000030
31using namespace clang;
32
Ted Kremenekcf118d42009-02-04 23:49:09 +000033//===----------------------------------------------------------------------===//
34// static functions.
35//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000036
Ted Kremenekb697b102009-02-23 22:44:26 +000037static inline Stmt* GetStmt(ProgramPoint P) {
38 if (const PostStmt* PS = dyn_cast<PostStmt>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000039 return PS->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000040 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000041 return BE->getSrc()->getTerminator();
Ted Kremenek61f3e052008-04-03 04:42:52 +000042
Ted Kremenekb697b102009-02-23 22:44:26 +000043 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000044}
45
Ted Kremenek3148eb42009-01-24 00:55:43 +000046static inline const ExplodedNode<GRState>*
Ted Kremenekb697b102009-02-23 22:44:26 +000047GetPredecessorNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000048 return N->pred_empty() ? NULL : *(N->pred_begin());
49}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000050
Ted Kremenekb697b102009-02-23 22:44:26 +000051static inline const ExplodedNode<GRState>*
52GetSuccessorNode(const ExplodedNode<GRState>* N) {
53 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000054}
55
Ted Kremenekb697b102009-02-23 22:44:26 +000056static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) {
57 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
58 if (Stmt *S = GetStmt(N->getLocation()))
59 return S;
60
61 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000062}
63
Ted Kremenekb697b102009-02-23 22:44:26 +000064static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) {
65 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
66 if (Stmt *S = GetStmt(N->getLocation()))
67 return S;
68
69 return 0;
70}
71
72static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
73 if (Stmt *S = GetStmt(N->getLocation()))
74 return S;
75
76 return GetPreviousStmt(N);
77}
78
79static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
80 if (Stmt *S = GetStmt(N->getLocation()))
81 return S;
82
83 return GetNextStmt(N);
84}
85
86//===----------------------------------------------------------------------===//
87// Diagnostics for 'execution continues on line XXX'.
88//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +000089
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000090namespace {
91class VISIBILITY_HIDDEN PathDiagnosticBuilder {
92 SourceManager &SMgr;
93 const Decl& CodeDecl;
94 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +000095 llvm::OwningPtr<ParentMap> PM;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000096public:
97 PathDiagnosticBuilder(SourceManager &smgr, const Decl& codedecl,
98 PathDiagnosticClient *pdc)
99 : SMgr(smgr), CodeDecl(codedecl), PDC(pdc) {}
100
Ted Kremenek00605e02009-03-27 20:55:39 +0000101 PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N);
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000102
Ted Kremenek00605e02009-03-27 20:55:39 +0000103 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os,
104 const ExplodedNode<GRState>* N);
105
106 ParentMap& getParentMap() {
107 if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody()));
108 return *PM.get();
109 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000110
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000111 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
112
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000113 bool supportsLogicalOpControlFlow() const {
114 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
115 }
116};
117} // end anonymous namespace
118
Ted Kremenek00605e02009-03-27 20:55:39 +0000119PathDiagnosticLocation
120PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) {
121 if (Stmt *S = GetNextStmt(N))
122 return PathDiagnosticLocation(S, SMgr);
123
124 return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000125}
126
Ted Kremenek00605e02009-03-27 20:55:39 +0000127PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000128PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
129 const ExplodedNode<GRState>* N) {
130
Ted Kremenek143ca222008-05-06 18:11:09 +0000131 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000132 if (os.str().empty())
133 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000134
Ted Kremenek00605e02009-03-27 20:55:39 +0000135 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000136
Ted Kremenek00605e02009-03-27 20:55:39 +0000137 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000138 os << "Execution continues on line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000139 << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000140 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000141 os << "Execution jumps to the end of the "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000142 << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000143
144 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000145}
146
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000147PathDiagnosticLocation
148PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
149 assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
150 ParentMap &P = getParentMap();
151 while (isa<Expr>(S)) {
152 const Stmt *Parent = P.getParent(S);
153
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000154 if (!Parent)
155 break;
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000156
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000157 switch (Parent->getStmtClass()) {
158 case Stmt::CompoundStmtClass:
159 case Stmt::StmtExprClass:
160 return PathDiagnosticLocation(S, SMgr);
161 case Stmt::DoStmtClass:
162 if (cast<DoStmt>(Parent)->getCond() != S)
163 return PathDiagnosticLocation(S, SMgr);
164 break;
165 case Stmt::ForStmtClass:
166 if (cast<ForStmt>(Parent)->getBody() == S)
167 return PathDiagnosticLocation(S, SMgr);
168 break;
169 case Stmt::IfStmtClass:
170 if (cast<IfStmt>(Parent)->getCond() != S)
171 return PathDiagnosticLocation(S, SMgr);
172 break;
173 case Stmt::ObjCForCollectionStmtClass:
174 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
175 return PathDiagnosticLocation(S, SMgr);
176 break;
177 case Stmt::WhileStmtClass:
178 if (cast<WhileStmt>(Parent)->getCond() != S)
179 return PathDiagnosticLocation(S, SMgr);
180 break;
181 default:
182 break;
183 }
184
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000185 S = Parent;
186 }
187
188 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
189 return PathDiagnosticLocation(S, SMgr);
190}
191
Ted Kremenekcf118d42009-02-04 23:49:09 +0000192//===----------------------------------------------------------------------===//
193// Methods for BugType and subclasses.
194//===----------------------------------------------------------------------===//
195BugType::~BugType() {}
196void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000197
Ted Kremenekcf118d42009-02-04 23:49:09 +0000198//===----------------------------------------------------------------------===//
199// Methods for BugReport and subclasses.
200//===----------------------------------------------------------------------===//
201BugReport::~BugReport() {}
202RangedBugReport::~RangedBugReport() {}
203
204Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000205 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000206 Stmt *S = NULL;
207
Ted Kremenekcf118d42009-02-04 23:49:09 +0000208 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000209 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000210 }
211 if (!S) S = GetStmt(ProgP);
212
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000213 return S;
214}
215
216PathDiagnosticPiece*
217BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000218 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000219
220 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000221
222 if (!S)
223 return NULL;
224
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000225 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000226 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000227
Ted Kremenekde7161f2008-04-03 18:00:37 +0000228 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000229 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000230
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000231 for (; Beg != End; ++Beg)
232 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000233
234 return P;
235}
236
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000237void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
238 const SourceRange*& end) {
239
240 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
241 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000242 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000243 beg = &R;
244 end = beg+1;
245 }
246 else
247 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000248}
249
Ted Kremenekcf118d42009-02-04 23:49:09 +0000250SourceLocation BugReport::getLocation() const {
251 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000252 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
253 // For member expressions, return the location of the '.' or '->'.
254 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
255 return ME->getMemberLoc();
256
Ted Kremenekcf118d42009-02-04 23:49:09 +0000257 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000258 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000259
260 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000261}
262
Ted Kremenek3148eb42009-01-24 00:55:43 +0000263PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
264 const ExplodedNode<GRState>* PrevN,
265 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000266 BugReporter& BR,
267 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000268 return NULL;
269}
270
Ted Kremenekcf118d42009-02-04 23:49:09 +0000271//===----------------------------------------------------------------------===//
272// Methods for BugReporter and subclasses.
273//===----------------------------------------------------------------------===//
274
275BugReportEquivClass::~BugReportEquivClass() {
276 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
277}
278
279GRBugReporter::~GRBugReporter() { FlushReports(); }
280BugReporterData::~BugReporterData() {}
281
282ExplodedGraph<GRState>&
283GRBugReporter::getGraph() { return Eng.getGraph(); }
284
285GRStateManager&
286GRBugReporter::getStateManager() { return Eng.getStateManager(); }
287
288BugReporter::~BugReporter() { FlushReports(); }
289
290void BugReporter::FlushReports() {
291 if (BugTypes.isEmpty())
292 return;
293
294 // First flush the warnings for each BugType. This may end up creating new
295 // warnings and new BugTypes. Because ImmutableSet is a functional data
296 // structure, we do not need to worry about the iterators being invalidated.
297 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
298 const_cast<BugType*>(*I)->FlushReports(*this);
299
300 // Iterate through BugTypes a second time. BugTypes may have been updated
301 // with new BugType objects and new warnings.
302 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
303 BugType *BT = const_cast<BugType*>(*I);
304
305 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
306 SetTy& EQClasses = BT->EQClasses;
307
308 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
309 BugReportEquivClass& EQ = *EI;
310 FlushReport(EQ);
311 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000312
Ted Kremenekcf118d42009-02-04 23:49:09 +0000313 // Delete the BugType object. This will also delete the equivalence
314 // classes.
315 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000316 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000317
318 // Remove all references to the BugType objects.
319 BugTypes = F.GetEmptySet();
320}
321
322//===----------------------------------------------------------------------===//
323// PathDiagnostics generation.
324//===----------------------------------------------------------------------===//
325
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000326typedef llvm::DenseMap<const ExplodedNode<GRState>*,
327 const ExplodedNode<GRState>*> NodeBackMap;
328
329static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000330 std::pair<ExplodedNode<GRState>*, unsigned> >
331MakeReportGraph(const ExplodedGraph<GRState>* G,
332 const ExplodedNode<GRState>** NStart,
333 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000334
Ted Kremenekcf118d42009-02-04 23:49:09 +0000335 // Create the trimmed graph. It will contain the shortest paths from the
336 // error nodes to the root. In the new graph we should only have one
337 // error node unless there are two or more error nodes with the same minimum
338 // path length.
339 ExplodedGraph<GRState>* GTrim;
340 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000341
342 llvm::DenseMap<const void*, const void*> InverseMap;
343 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000344
345 // Create owning pointers for GTrim and NMap just to ensure that they are
346 // released when this function exists.
347 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
348 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
349
350 // Find the (first) error node in the trimmed graph. We just need to consult
351 // the node map (NMap) which maps from nodes in the original graph to nodes
352 // in the new graph.
353 const ExplodedNode<GRState>* N = 0;
354 unsigned NodeIndex = 0;
355
356 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
357 if ((N = NMap->getMappedNode(*I))) {
358 NodeIndex = (I - NStart) / sizeof(*I);
359 break;
360 }
361
362 assert(N && "No error node found in the trimmed graph.");
363
364 // Create a new (third!) graph with a single path. This is the graph
365 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000366 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000367 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
368 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000369
Ted Kremenek10aa5542009-03-12 23:41:59 +0000370 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000371 // to the root node, and then construct a new graph that contains only
372 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000373 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000374 std::queue<const ExplodedNode<GRState>*> WS;
375 WS.push(N);
376
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000377 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000378 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000379
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000380 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000381 const ExplodedNode<GRState>* Node = WS.front();
382 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000383
384 if (Visited.find(Node) != Visited.end())
385 continue;
386
387 Visited[Node] = cnt++;
388
389 if (Node->pred_empty()) {
390 Root = Node;
391 break;
392 }
393
Ted Kremenek3148eb42009-01-24 00:55:43 +0000394 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000395 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000396 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000397 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000398
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000399 assert (Root);
400
Ted Kremenek10aa5542009-03-12 23:41:59 +0000401 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000402 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000403 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000404 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000405
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000406 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000407 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000408 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000409 assert (I != Visited.end());
410
411 // Create the equivalent node in the new graph with the same state
412 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000413 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000414 GNew->getNode(N->getLocation(), N->getState());
415
416 // Store the mapping to the original node.
417 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
418 assert(IMitr != InverseMap.end() && "No mapping to original node.");
419 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000420
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000421 // Link up the new node with the previous node.
422 if (Last)
423 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000424
425 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000426
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000427 // Are we at the final node?
428 if (I->second == 0) {
429 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000430 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000431 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000432
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000433 // Find the next successor node. We choose the node that is marked
434 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000435 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
436 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000437 N = 0;
438
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000439 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000440
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000441 I = Visited.find(*SI);
442
443 if (I == Visited.end())
444 continue;
445
446 if (!N || I->second < MinVal) {
447 N = *SI;
448 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000449 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000450 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000451
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000452 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000453 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000454
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000455 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000456 return std::make_pair(std::make_pair(GNew, BM),
457 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000458}
459
Ted Kremenek3148eb42009-01-24 00:55:43 +0000460static const VarDecl*
461GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
462 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000463
464 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
465
466 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000467
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000468 if (!isa<PostStmt>(P))
469 continue;
470
471 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000472
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000473 if (!DR)
474 continue;
475
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000476 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000477
478 if (X != Y)
479 continue;
480
481 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
482
483 if (!VD)
484 continue;
485
486 return VD;
487 }
488
489 return 0;
490}
491
Ted Kremenek9e240492008-10-04 05:50:14 +0000492namespace {
493class VISIBILITY_HIDDEN NotableSymbolHandler
494 : public StoreManager::BindingsHandler {
495
Ted Kremenek2dabd432008-12-05 02:27:51 +0000496 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000497 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000498 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000499 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000500 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000501 PathDiagnostic& PD;
502 BugReporter& BR;
503
504public:
505
Ted Kremenek3148eb42009-01-24 00:55:43 +0000506 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
507 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000508 PathDiagnostic& pd, BugReporter& br)
509 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
510
Ted Kremenekbe912242009-03-05 16:31:07 +0000511 bool HandleBinding(StoreManager& SMgr, Store store,
512 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000513
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000514 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000515
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000516 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000517 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000518 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000519 ScanSym = SV->getSymbol();
520 else
521 return true;
522
523 if (ScanSym != Sym)
524 return true;
525
526 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000527 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000528
529 if (X == V) // Same binding?
530 return true;
531
532 // Different binding. Only handle assignments for now. We don't pull
533 // this check out of the loop because we will eventually handle other
534 // cases.
535
536 VarDecl *VD = 0;
537
Ted Kremenek3148eb42009-01-24 00:55:43 +0000538 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000539 if (!B->isAssignmentOp())
540 return true;
541
542 // What variable did we assign to?
543 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
544
545 if (!DR)
546 return true;
547
548 VD = dyn_cast<VarDecl>(DR->getDecl());
549 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000550 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000551 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
552 // assume that each DeclStmt has a single Decl. This invariant
553 // holds by contruction in the CFG.
554 VD = dyn_cast<VarDecl>(*DS->decl_begin());
555 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000556
557 if (!VD)
558 return true;
559
560 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000561 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000562
563 if (!MostRecent)
564 return true;
565
566 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000567 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
568
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000569 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000570 std::string msg = "'" + std::string(VD->getNameAsString()) +
571 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000572
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000573 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000574 }
575
576 return true;
577 }
578};
579}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000580
Ted Kremenek3148eb42009-01-24 00:55:43 +0000581static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
582 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000583 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000584 PathDiagnostic& PD) {
585
Ted Kremenek3148eb42009-01-24 00:55:43 +0000586 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000587 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000588
589 if (!PrevSt)
590 return;
591
Ted Kremenek9e240492008-10-04 05:50:14 +0000592 // Look at the region bindings of the current state that map to the
593 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000594 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000595 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
596 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
597}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000598
Ted Kremenek9e240492008-10-04 05:50:14 +0000599namespace {
600class VISIBILITY_HIDDEN ScanNotableSymbols
601 : public StoreManager::BindingsHandler {
602
Ted Kremenek2dabd432008-12-05 02:27:51 +0000603 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000604 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000605 Stmt* S;
606 GRBugReporter& BR;
607 PathDiagnostic& PD;
608
609public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000610 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000611 PathDiagnostic& pd)
612 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000613
Ted Kremenekbe912242009-03-05 16:31:07 +0000614 bool HandleBinding(StoreManager& SMgr, Store store,
615 const MemRegion* R, SVal V) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000616 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000617
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000618 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000619 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000620 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000621 ScanSym = SV->getSymbol();
622 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000623 return true;
624
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000625 assert (ScanSym);
Ted Kremenek9e240492008-10-04 05:50:14 +0000626
627 if (!BR.isNotable(ScanSym))
628 return true;
629
630 if (AlreadyProcessed.count(ScanSym))
631 return true;
632
633 AlreadyProcessed.insert(ScanSym);
634
635 HandleNotableSymbol(N, S, ScanSym, BR, PD);
636 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000637 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000638};
639} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000640
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000641namespace {
642class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
643 NodeBackMap& M;
644public:
645 NodeMapClosure(NodeBackMap *m) : M(*m) {}
646 ~NodeMapClosure() {}
647
648 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
649 NodeBackMap::iterator I = M.find(N);
650 return I == M.end() ? 0 : I->second;
651 }
652};
653}
654
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000655/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
656/// and collapses PathDiagosticPieces that are expanded by macros.
657static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
658 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
659 MacroStackTy;
660
661 typedef std::vector<PathDiagnosticPiece*>
662 PiecesTy;
663
664 MacroStackTy MacroStack;
665 PiecesTy Pieces;
666
667 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
668 // Get the location of the PathDiagnosticPiece.
669 const FullSourceLoc Loc = I->getLocation();
670
671 // Determine the instantiation location, which is the location we group
672 // related PathDiagnosticPieces.
673 SourceLocation InstantiationLoc = Loc.isMacroID() ?
674 SM.getInstantiationLoc(Loc) :
675 SourceLocation();
676
677 if (Loc.isFileID()) {
678 MacroStack.clear();
679 Pieces.push_back(&*I);
680 continue;
681 }
682
683 assert(Loc.isMacroID());
684
685 // Is the PathDiagnosticPiece within the same macro group?
686 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
687 MacroStack.back().first->push_back(&*I);
688 continue;
689 }
690
691 // We aren't in the same group. Are we descending into a new macro
692 // or are part of an old one?
693 PathDiagnosticMacroPiece *MacroGroup = 0;
694
695 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
696 SM.getInstantiationLoc(Loc) :
697 SourceLocation();
698
699 // Walk the entire macro stack.
700 while (!MacroStack.empty()) {
701 if (InstantiationLoc == MacroStack.back().second) {
702 MacroGroup = MacroStack.back().first;
703 break;
704 }
705
706 if (ParentInstantiationLoc == MacroStack.back().second) {
707 MacroGroup = MacroStack.back().first;
708 break;
709 }
710
711 MacroStack.pop_back();
712 }
713
714 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
715 // Create a new macro group and add it to the stack.
716 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
717
718 if (MacroGroup)
719 MacroGroup->push_back(NewGroup);
720 else {
721 assert(InstantiationLoc.isFileID());
722 Pieces.push_back(NewGroup);
723 }
724
725 MacroGroup = NewGroup;
726 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
727 }
728
729 // Finally, add the PathDiagnosticPiece to the group.
730 MacroGroup->push_back(&*I);
731 }
732
733 // Now take the pieces and construct a new PathDiagnostic.
734 PD.resetPath(false);
735
736 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
737 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
738 if (!MP->containsEvent()) {
739 delete MP;
740 continue;
741 }
742
743 PD.push_back(*I);
744 }
745}
746
Ted Kremenekc0959972008-07-02 21:24:01 +0000747void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000748 BugReportEquivClass& EQ) {
749
750 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000751
Ted Kremenekcf118d42009-02-04 23:49:09 +0000752 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
753 const ExplodedNode<GRState>* N = I->getEndNode();
754 if (N) Nodes.push_back(N);
755 }
756
757 if (Nodes.empty())
758 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000759
760 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000761 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000762 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000763 std::pair<ExplodedNode<GRState>*, unsigned> >&
764 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000765
Ted Kremenekcf118d42009-02-04 23:49:09 +0000766 // Find the BugReport with the original location.
767 BugReport *R = 0;
768 unsigned i = 0;
769 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
770 if (i == GPair.second.second) { R = *I; break; }
771
772 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000773
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000774 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
775 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000776 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000777
Ted Kremenekcf118d42009-02-04 23:49:09 +0000778 // Start building the path diagnostic...
779 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000780 PD.push_back(Piece);
781 else
782 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000783
Ted Kremenek3148eb42009-01-24 00:55:43 +0000784 const ExplodedNode<GRState>* NextNode = N->pred_empty()
785 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000786
Ted Kremenekc0959972008-07-02 21:24:01 +0000787 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000788 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000789 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000790 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
791 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000792
Ted Kremenek6837faa2008-04-09 00:20:43 +0000793 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000794 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000795 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000796
797 ProgramPoint P = N->getLocation();
798
799 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000800 CFGBlock* Src = BE->getSrc();
801 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000802 Stmt* T = Src->getTerminator();
803
804 if (!T)
805 continue;
806
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000807 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000808
809 switch (T->getStmtClass()) {
810 default:
811 break;
812
813 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000814 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000815 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000816
817 if (!S)
818 continue;
819
Ted Kremenek297308e2009-02-10 23:56:07 +0000820 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000821 llvm::raw_string_ostream os(sbuf);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000822 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000823
Ted Kremenek00605e02009-03-27 20:55:39 +0000824 os << "Control jumps to line "
825 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000826 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
827 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000828 break;
829 }
830
Ted Kremenek297308e2009-02-10 23:56:07 +0000831 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000832 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000833 std::string sbuf;
834 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000835
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000836 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000837 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000838
Ted Kremenek5a429952008-04-23 23:35:07 +0000839 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000840 default:
841 os << "No cases match in the switch statement. "
842 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000843 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000844 break;
845 case Stmt::DefaultStmtClass:
846 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000847 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000848 break;
849
850 case Stmt::CaseStmtClass: {
851 os << "Control jumps to 'case ";
852 CaseStmt* Case = cast<CaseStmt>(S);
853 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
854
855 // Determine if it is an enum.
856 bool GetRawInt = true;
857
858 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
859 // FIXME: Maybe this should be an assertion. Are there cases
860 // were it is not an EnumConstantDecl?
861 EnumConstantDecl* D =
862 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000863
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000864 if (D) {
865 GetRawInt = false;
866 os << D->getNameAsString();
867 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000868 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000869
870 if (GetRawInt) {
871
872 // Not an enum.
873 Expr* CondE = cast<SwitchStmt>(T)->getCond();
874 unsigned bits = Ctx.getTypeSize(CondE->getType());
875 llvm::APSInt V(bits, false);
876
877 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
878 assert (false && "Case condition must be constant.");
879 continue;
880 }
881
882 os << V;
883 }
884
Ted Kremenek00605e02009-03-27 20:55:39 +0000885 os << ":' at line "
886 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000887 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000888 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000889 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000890 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
891 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000892 }
Ted Kremenek56783922008-04-25 01:29:56 +0000893 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000894 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000895 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
896 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
897 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000898 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000899
Ted Kremenek61f3e052008-04-03 04:42:52 +0000900 break;
901 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000902
903 case Stmt::BreakStmtClass:
904 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000905 std::string sbuf;
906 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000907 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000908 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
909 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000910 break;
911 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000912
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000913 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000914 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000915 std::string sbuf;
916 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000917 os << "'?' condition evaluates to ";
918
919 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000920 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000921 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000922 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000923
Ted Kremenek00605e02009-03-27 20:55:39 +0000924 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000925
926 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
927 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000928 break;
929 }
930
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000931 // Determine control-flow for short-circuited '&&' and '||'.
932 case Stmt::BinaryOperatorClass: {
933 if (!PDB.supportsLogicalOpControlFlow())
934 break;
935
936 BinaryOperator *B = cast<BinaryOperator>(T);
937 std::string sbuf;
938 llvm::raw_string_ostream os(sbuf);
939 os << "Left side of '";
940
941 if (B->getOpcode() == BinaryOperator::LAnd) {
942 os << "&&";
943 }
944 else {
945 assert(B->getOpcode() == BinaryOperator::LOr);
946 os << "||";
947 }
948
949 os << "' is ";
950 if (*(Src->succ_begin()+1) == Dst)
951 os << (B->getOpcode() == BinaryOperator::LAnd
952 ? "false" : "true");
953 else
954 os << (B->getOpcode() == BinaryOperator::LAnd
955 ? "true" : "false");
956
957 PathDiagnosticLocation Start(B->getLHS(), SMgr);
958 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
959
960 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
961 os.str()));
962 break;
963 }
964
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000965 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000966 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000967 std::string sbuf;
968 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000969
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000970 os << "Loop condition is true. ";
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000971 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
972
973 if (const Stmt *S = End.asStmt())
974 End = PDB.getEnclosingStmtLocation(S);
975
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000976 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
977 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000978 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000979 else {
Ted Kremenek00605e02009-03-27 20:55:39 +0000980 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000981
982 if (const Stmt *S = End.asStmt())
983 End = PDB.getEnclosingStmtLocation(S);
984
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000985 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
986 "Loop condition is false. Exiting loop"));
987 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000988
989 break;
990 }
991
Ted Kremenek61f3e052008-04-03 04:42:52 +0000992 case Stmt::WhileStmtClass:
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000993 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000994 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000995 std::string sbuf;
996 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000997
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000998 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000999 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001000 if (const Stmt *S = End.asStmt())
1001 End = PDB.getEnclosingStmtLocation(S);
1002
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001003 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1004 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001005 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001006 else {
Ted Kremenek00605e02009-03-27 20:55:39 +00001007 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001008 if (const Stmt *S = End.asStmt())
1009 End = PDB.getEnclosingStmtLocation(S);
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001010
1011 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1012 "Loop condition is true. Entering loop body"));
1013 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +00001014
1015 break;
1016 }
1017
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001018 case Stmt::IfStmtClass: {
Ted Kremenekd8c938b2009-03-27 21:16:25 +00001019 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
1020
1021 if (const Stmt *S = End.asStmt())
1022 End = PDB.getEnclosingStmtLocation(S);
1023
Ted Kremenek61f3e052008-04-03 04:42:52 +00001024 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001025 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1026 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +00001027 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +00001028 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
1029 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +00001030
1031 break;
1032 }
1033 }
Ted Kremenek6837faa2008-04-09 00:20:43 +00001034 }
Ted Kremenek5a429952008-04-23 23:35:07 +00001035
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001036 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
1037 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001038 PD.push_front(p);
1039
Ted Kremenek9e240492008-10-04 05:50:14 +00001040 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
1041 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001042 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +00001043 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
1044 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001045 }
Ted Kremenek61f3e052008-04-03 04:42:52 +00001046 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001047
1048 // After constructing the full PathDiagnostic, do a pass over it to compact
1049 // PathDiagnosticPieces that occur within a macro.
1050 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001051}
1052
Ted Kremenek1aa44c72008-05-22 23:45:19 +00001053
Ted Kremenekcf118d42009-02-04 23:49:09 +00001054void BugReporter::Register(BugType *BT) {
1055 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001056}
1057
Ted Kremenekcf118d42009-02-04 23:49:09 +00001058void BugReporter::EmitReport(BugReport* R) {
1059 // Compute the bug report's hash to determine its equivalence class.
1060 llvm::FoldingSetNodeID ID;
1061 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001062
Ted Kremenekcf118d42009-02-04 23:49:09 +00001063 // Lookup the equivance class. If there isn't one, create it.
1064 BugType& BT = R->getBugType();
1065 Register(&BT);
1066 void *InsertPos;
1067 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1068
1069 if (!EQ) {
1070 EQ = new BugReportEquivClass(R);
1071 BT.EQClasses.InsertNode(EQ, InsertPos);
1072 }
1073 else
1074 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001075}
1076
Ted Kremenekcf118d42009-02-04 23:49:09 +00001077void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1078 assert(!EQ.Reports.empty());
1079 BugReport &R = **EQ.begin();
1080
1081 // FIXME: Make sure we use the 'R' for the path that was actually used.
1082 // Probably doesn't make a difference in practice.
1083 BugType& BT = R.getBugType();
1084
1085 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1086 R.getDescription(),
1087 BT.getCategory()));
1088 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001089
1090 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001091 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001092 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001093
Ted Kremenek3148eb42009-01-24 00:55:43 +00001094 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001095 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001096 const SourceRange *Beg = 0, *End = 0;
1097 R.getRanges(*this, Beg, End);
1098 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001099 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001100 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1101 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001102
Ted Kremenek3148eb42009-01-24 00:55:43 +00001103 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001104 default: assert(0 && "Don't handle this many ranges yet!");
1105 case 0: Diag.Report(L, ErrorDiag); break;
1106 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1107 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1108 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001109 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001110
1111 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1112 if (!PD)
1113 return;
1114
1115 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001116 PathDiagnosticPiece* piece =
1117 new PathDiagnosticEventPiece(L, R.getDescription());
1118
Ted Kremenek3148eb42009-01-24 00:55:43 +00001119 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1120 D->push_back(piece);
1121 }
1122
1123 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001124}
Ted Kremenek57202072008-07-14 17:40:50 +00001125
Ted Kremenek8c036c72008-09-20 04:23:38 +00001126void BugReporter::EmitBasicReport(const char* name, const char* str,
1127 SourceLocation Loc,
1128 SourceRange* RBeg, unsigned NumRanges) {
1129 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1130}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001131
Ted Kremenek8c036c72008-09-20 04:23:38 +00001132void BugReporter::EmitBasicReport(const char* name, const char* category,
1133 const char* str, SourceLocation Loc,
1134 SourceRange* RBeg, unsigned NumRanges) {
1135
Ted Kremenekcf118d42009-02-04 23:49:09 +00001136 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1137 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001138 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001139 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1140 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1141 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001142}