blob: ffa1593fd564be91a91b4b22ed0c5e0e1a427ccf [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"
22#include "clang/Analysis/ProgramPoint.h"
23#include "clang/Analysis/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000025#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000026#include "llvm/ADT/STLExtras.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000027#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000028
29using namespace clang;
30
Ted Kremenekcf118d42009-02-04 23:49:09 +000031//===----------------------------------------------------------------------===//
32// static functions.
33//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
Ted Kremenekb697b102009-02-23 22:44:26 +000035static inline Stmt* GetStmt(ProgramPoint P) {
36 if (const PostStmt* PS = dyn_cast<PostStmt>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000037 return PS->getStmt();
Ted Kremenekb697b102009-02-23 22:44:26 +000038 else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000039 return BE->getSrc()->getTerminator();
Ted Kremenek61f3e052008-04-03 04:42:52 +000040
Ted Kremenekb697b102009-02-23 22:44:26 +000041 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000042}
43
Ted Kremenek3148eb42009-01-24 00:55:43 +000044static inline const ExplodedNode<GRState>*
Ted Kremenekb697b102009-02-23 22:44:26 +000045GetPredecessorNode(const ExplodedNode<GRState>* N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000046 return N->pred_empty() ? NULL : *(N->pred_begin());
47}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000048
Ted Kremenekb697b102009-02-23 22:44:26 +000049static inline const ExplodedNode<GRState>*
50GetSuccessorNode(const ExplodedNode<GRState>* N) {
51 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000052}
53
Ted Kremenekb697b102009-02-23 22:44:26 +000054static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) {
55 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
56 if (Stmt *S = GetStmt(N->getLocation()))
57 return S;
58
59 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000060}
61
Ted Kremenekb697b102009-02-23 22:44:26 +000062static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) {
63 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
64 if (Stmt *S = GetStmt(N->getLocation()))
65 return S;
66
67 return 0;
68}
69
70static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) {
71 if (Stmt *S = GetStmt(N->getLocation()))
72 return S;
73
74 return GetPreviousStmt(N);
75}
76
77static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) {
78 if (Stmt *S = GetStmt(N->getLocation()))
79 return S;
80
81 return GetNextStmt(N);
82}
83
84//===----------------------------------------------------------------------===//
85// Diagnostics for 'execution continues on line XXX'.
86//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +000087
Ted Kremenek082cb8d2009-03-12 18:41:53 +000088static SourceLocation ExecutionContinues(SourceManager& SMgr,
89 const ExplodedNode<GRState>* N,
90 const Decl& D,
91 bool* OutHasStmt = 0) {
92 if (Stmt *S = GetNextStmt(N)) {
93 if (OutHasStmt) *OutHasStmt = true;
94 return S->getLocStart();
95 }
96 else {
97 if (OutHasStmt) *OutHasStmt = false;
98 return D.getBody()->getRBracLoc();
99 }
100}
101
102static SourceLocation ExecutionContinues(llvm::raw_string_ostream& os,
103 SourceManager& SMgr,
104 const ExplodedNode<GRState>* N,
105 const Decl& D) {
Ted Kremenekb479dad2009-02-23 23:13:51 +0000106
Ted Kremenek143ca222008-05-06 18:11:09 +0000107 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000108 if (os.str().empty())
109 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000110
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000111 bool hasStmt;
112 SourceLocation Loc = ExecutionContinues(SMgr, N, D, &hasStmt);
113
114 if (hasStmt)
Ted Kremenekb697b102009-02-23 22:44:26 +0000115 os << "Execution continues on line "
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000116 << SMgr.getInstantiationLineNumber(Loc) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000117 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000118 os << "Execution jumps to the end of the "
119 << (isa<ObjCMethodDecl>(D) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000120
121 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000122}
123
Ted Kremenekcf118d42009-02-04 23:49:09 +0000124//===----------------------------------------------------------------------===//
125// Methods for BugType and subclasses.
126//===----------------------------------------------------------------------===//
127BugType::~BugType() {}
128void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000129
Ted Kremenekcf118d42009-02-04 23:49:09 +0000130//===----------------------------------------------------------------------===//
131// Methods for BugReport and subclasses.
132//===----------------------------------------------------------------------===//
133BugReport::~BugReport() {}
134RangedBugReport::~RangedBugReport() {}
135
136Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000137 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000138 Stmt *S = NULL;
139
Ted Kremenekcf118d42009-02-04 23:49:09 +0000140 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000141 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000142 }
143 if (!S) S = GetStmt(ProgP);
144
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000145 return S;
146}
147
148PathDiagnosticPiece*
149BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000150 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000151
152 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000153
154 if (!S)
155 return NULL;
156
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000157 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000158 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000159
Ted Kremenekde7161f2008-04-03 18:00:37 +0000160 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000161 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000162
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000163 for (; Beg != End; ++Beg)
164 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000165
166 return P;
167}
168
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000169void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
170 const SourceRange*& end) {
171
172 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
173 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000174 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000175 beg = &R;
176 end = beg+1;
177 }
178 else
179 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000180}
181
Ted Kremenekcf118d42009-02-04 23:49:09 +0000182SourceLocation BugReport::getLocation() const {
183 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000184 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
185 // For member expressions, return the location of the '.' or '->'.
186 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
187 return ME->getMemberLoc();
188
Ted Kremenekcf118d42009-02-04 23:49:09 +0000189 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000190 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000191
192 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000193}
194
Ted Kremenek3148eb42009-01-24 00:55:43 +0000195PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
196 const ExplodedNode<GRState>* PrevN,
197 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000198 BugReporter& BR,
199 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000200 return NULL;
201}
202
Ted Kremenekcf118d42009-02-04 23:49:09 +0000203//===----------------------------------------------------------------------===//
204// Methods for BugReporter and subclasses.
205//===----------------------------------------------------------------------===//
206
207BugReportEquivClass::~BugReportEquivClass() {
208 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
209}
210
211GRBugReporter::~GRBugReporter() { FlushReports(); }
212BugReporterData::~BugReporterData() {}
213
214ExplodedGraph<GRState>&
215GRBugReporter::getGraph() { return Eng.getGraph(); }
216
217GRStateManager&
218GRBugReporter::getStateManager() { return Eng.getStateManager(); }
219
220BugReporter::~BugReporter() { FlushReports(); }
221
222void BugReporter::FlushReports() {
223 if (BugTypes.isEmpty())
224 return;
225
226 // First flush the warnings for each BugType. This may end up creating new
227 // warnings and new BugTypes. Because ImmutableSet is a functional data
228 // structure, we do not need to worry about the iterators being invalidated.
229 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
230 const_cast<BugType*>(*I)->FlushReports(*this);
231
232 // Iterate through BugTypes a second time. BugTypes may have been updated
233 // with new BugType objects and new warnings.
234 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
235 BugType *BT = const_cast<BugType*>(*I);
236
237 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
238 SetTy& EQClasses = BT->EQClasses;
239
240 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
241 BugReportEquivClass& EQ = *EI;
242 FlushReport(EQ);
243 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000244
Ted Kremenekcf118d42009-02-04 23:49:09 +0000245 // Delete the BugType object. This will also delete the equivalence
246 // classes.
247 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000248 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000249
250 // Remove all references to the BugType objects.
251 BugTypes = F.GetEmptySet();
252}
253
254//===----------------------------------------------------------------------===//
255// PathDiagnostics generation.
256//===----------------------------------------------------------------------===//
257
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000258typedef llvm::DenseMap<const ExplodedNode<GRState>*,
259 const ExplodedNode<GRState>*> NodeBackMap;
260
261static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000262 std::pair<ExplodedNode<GRState>*, unsigned> >
263MakeReportGraph(const ExplodedGraph<GRState>* G,
264 const ExplodedNode<GRState>** NStart,
265 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000266
Ted Kremenekcf118d42009-02-04 23:49:09 +0000267 // Create the trimmed graph. It will contain the shortest paths from the
268 // error nodes to the root. In the new graph we should only have one
269 // error node unless there are two or more error nodes with the same minimum
270 // path length.
271 ExplodedGraph<GRState>* GTrim;
272 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000273
274 llvm::DenseMap<const void*, const void*> InverseMap;
275 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000276
277 // Create owning pointers for GTrim and NMap just to ensure that they are
278 // released when this function exists.
279 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
280 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
281
282 // Find the (first) error node in the trimmed graph. We just need to consult
283 // the node map (NMap) which maps from nodes in the original graph to nodes
284 // in the new graph.
285 const ExplodedNode<GRState>* N = 0;
286 unsigned NodeIndex = 0;
287
288 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
289 if ((N = NMap->getMappedNode(*I))) {
290 NodeIndex = (I - NStart) / sizeof(*I);
291 break;
292 }
293
294 assert(N && "No error node found in the trimmed graph.");
295
296 // Create a new (third!) graph with a single path. This is the graph
297 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000298 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000299 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
300 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000301
Ted Kremenek10aa5542009-03-12 23:41:59 +0000302 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000303 // to the root node, and then construct a new graph that contains only
304 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000305 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000306 std::queue<const ExplodedNode<GRState>*> WS;
307 WS.push(N);
308
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000309 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000310 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000311
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000312 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000313 const ExplodedNode<GRState>* Node = WS.front();
314 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000315
316 if (Visited.find(Node) != Visited.end())
317 continue;
318
319 Visited[Node] = cnt++;
320
321 if (Node->pred_empty()) {
322 Root = Node;
323 break;
324 }
325
Ted Kremenek3148eb42009-01-24 00:55:43 +0000326 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000327 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000328 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000329 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000330
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000331 assert (Root);
332
Ted Kremenek10aa5542009-03-12 23:41:59 +0000333 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000334 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000335 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000336 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000337
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000338 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000339 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000340 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000341 assert (I != Visited.end());
342
343 // Create the equivalent node in the new graph with the same state
344 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000345 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000346 GNew->getNode(N->getLocation(), N->getState());
347
348 // Store the mapping to the original node.
349 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
350 assert(IMitr != InverseMap.end() && "No mapping to original node.");
351 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000352
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000353 // Link up the new node with the previous node.
354 if (Last)
355 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000356
357 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000358
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000359 // Are we at the final node?
360 if (I->second == 0) {
361 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000362 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000363 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000364
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000365 // Find the next successor node. We choose the node that is marked
366 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000367 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
368 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000369 N = 0;
370
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000371 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000372
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000373 I = Visited.find(*SI);
374
375 if (I == Visited.end())
376 continue;
377
378 if (!N || I->second < MinVal) {
379 N = *SI;
380 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000381 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000382 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000383
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000384 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000385 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000386
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000387 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000388 return std::make_pair(std::make_pair(GNew, BM),
389 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000390}
391
Ted Kremenek3148eb42009-01-24 00:55:43 +0000392static const VarDecl*
393GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
394 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000395
396 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
397
398 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000399
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000400 if (!isa<PostStmt>(P))
401 continue;
402
403 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000404
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000405 if (!DR)
406 continue;
407
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000408 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000409
410 if (X != Y)
411 continue;
412
413 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
414
415 if (!VD)
416 continue;
417
418 return VD;
419 }
420
421 return 0;
422}
423
Ted Kremenek9e240492008-10-04 05:50:14 +0000424namespace {
425class VISIBILITY_HIDDEN NotableSymbolHandler
426 : public StoreManager::BindingsHandler {
427
Ted Kremenek2dabd432008-12-05 02:27:51 +0000428 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000429 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000430 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000431 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000432 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000433 PathDiagnostic& PD;
434 BugReporter& BR;
435
436public:
437
Ted Kremenek3148eb42009-01-24 00:55:43 +0000438 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
439 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000440 PathDiagnostic& pd, BugReporter& br)
441 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
442
Ted Kremenekbe912242009-03-05 16:31:07 +0000443 bool HandleBinding(StoreManager& SMgr, Store store,
444 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000445
Ted Kremenek2dabd432008-12-05 02:27:51 +0000446 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000447
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000448 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000449 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000450 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000451 ScanSym = SV->getSymbol();
452 else
453 return true;
454
455 if (ScanSym != Sym)
456 return true;
457
458 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000459 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000460
461 if (X == V) // Same binding?
462 return true;
463
464 // Different binding. Only handle assignments for now. We don't pull
465 // this check out of the loop because we will eventually handle other
466 // cases.
467
468 VarDecl *VD = 0;
469
Ted Kremenek3148eb42009-01-24 00:55:43 +0000470 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000471 if (!B->isAssignmentOp())
472 return true;
473
474 // What variable did we assign to?
475 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
476
477 if (!DR)
478 return true;
479
480 VD = dyn_cast<VarDecl>(DR->getDecl());
481 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000482 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000483 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
484 // assume that each DeclStmt has a single Decl. This invariant
485 // holds by contruction in the CFG.
486 VD = dyn_cast<VarDecl>(*DS->decl_begin());
487 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000488
489 if (!VD)
490 return true;
491
492 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000493 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000494
495 if (!MostRecent)
496 return true;
497
498 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000499 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
500
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000501 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000502 std::string msg = "'" + std::string(VD->getNameAsString()) +
503 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000504
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000505 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000506 }
507
508 return true;
509 }
510};
511}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000512
Ted Kremenek3148eb42009-01-24 00:55:43 +0000513static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
514 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000515 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000516 PathDiagnostic& PD) {
517
Ted Kremenek3148eb42009-01-24 00:55:43 +0000518 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000519 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000520
521 if (!PrevSt)
522 return;
523
Ted Kremenek9e240492008-10-04 05:50:14 +0000524 // Look at the region bindings of the current state that map to the
525 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000526 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000527 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
528 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
529}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000530
Ted Kremenek9e240492008-10-04 05:50:14 +0000531namespace {
532class VISIBILITY_HIDDEN ScanNotableSymbols
533 : public StoreManager::BindingsHandler {
534
Ted Kremenek2dabd432008-12-05 02:27:51 +0000535 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000536 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000537 Stmt* S;
538 GRBugReporter& BR;
539 PathDiagnostic& PD;
540
541public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000542 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000543 PathDiagnostic& pd)
544 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000545
Ted Kremenekbe912242009-03-05 16:31:07 +0000546 bool HandleBinding(StoreManager& SMgr, Store store,
547 const MemRegion* R, SVal V) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000548 SymbolRef ScanSym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000549
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000550 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000551 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000552 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000553 ScanSym = SV->getSymbol();
554 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000555 return true;
556
Ted Kremenek94c96982009-03-03 22:06:47 +0000557 assert (ScanSym.isValid());
Ted Kremenek9e240492008-10-04 05:50:14 +0000558
559 if (!BR.isNotable(ScanSym))
560 return true;
561
562 if (AlreadyProcessed.count(ScanSym))
563 return true;
564
565 AlreadyProcessed.insert(ScanSym);
566
567 HandleNotableSymbol(N, S, ScanSym, BR, PD);
568 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000569 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000570};
571} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000572
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000573namespace {
574class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
575 NodeBackMap& M;
576public:
577 NodeMapClosure(NodeBackMap *m) : M(*m) {}
578 ~NodeMapClosure() {}
579
580 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
581 NodeBackMap::iterator I = M.find(N);
582 return I == M.end() ? 0 : I->second;
583 }
584};
585}
586
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000587/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
588/// and collapses PathDiagosticPieces that are expanded by macros.
589static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
590 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
591 MacroStackTy;
592
593 typedef std::vector<PathDiagnosticPiece*>
594 PiecesTy;
595
596 MacroStackTy MacroStack;
597 PiecesTy Pieces;
598
599 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
600 // Get the location of the PathDiagnosticPiece.
601 const FullSourceLoc Loc = I->getLocation();
602
603 // Determine the instantiation location, which is the location we group
604 // related PathDiagnosticPieces.
605 SourceLocation InstantiationLoc = Loc.isMacroID() ?
606 SM.getInstantiationLoc(Loc) :
607 SourceLocation();
608
609 if (Loc.isFileID()) {
610 MacroStack.clear();
611 Pieces.push_back(&*I);
612 continue;
613 }
614
615 assert(Loc.isMacroID());
616
617 // Is the PathDiagnosticPiece within the same macro group?
618 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
619 MacroStack.back().first->push_back(&*I);
620 continue;
621 }
622
623 // We aren't in the same group. Are we descending into a new macro
624 // or are part of an old one?
625 PathDiagnosticMacroPiece *MacroGroup = 0;
626
627 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
628 SM.getInstantiationLoc(Loc) :
629 SourceLocation();
630
631 // Walk the entire macro stack.
632 while (!MacroStack.empty()) {
633 if (InstantiationLoc == MacroStack.back().second) {
634 MacroGroup = MacroStack.back().first;
635 break;
636 }
637
638 if (ParentInstantiationLoc == MacroStack.back().second) {
639 MacroGroup = MacroStack.back().first;
640 break;
641 }
642
643 MacroStack.pop_back();
644 }
645
646 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
647 // Create a new macro group and add it to the stack.
648 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
649
650 if (MacroGroup)
651 MacroGroup->push_back(NewGroup);
652 else {
653 assert(InstantiationLoc.isFileID());
654 Pieces.push_back(NewGroup);
655 }
656
657 MacroGroup = NewGroup;
658 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
659 }
660
661 // Finally, add the PathDiagnosticPiece to the group.
662 MacroGroup->push_back(&*I);
663 }
664
665 // Now take the pieces and construct a new PathDiagnostic.
666 PD.resetPath(false);
667
668 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
669 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
670 if (!MP->containsEvent()) {
671 delete MP;
672 continue;
673 }
674
675 PD.push_back(*I);
676 }
677}
678
Ted Kremenekc0959972008-07-02 21:24:01 +0000679void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000680 BugReportEquivClass& EQ) {
681
682 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000683
Ted Kremenekcf118d42009-02-04 23:49:09 +0000684 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
685 const ExplodedNode<GRState>* N = I->getEndNode();
686 if (N) Nodes.push_back(N);
687 }
688
689 if (Nodes.empty())
690 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000691
692 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000693 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000694 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000695 std::pair<ExplodedNode<GRState>*, unsigned> >&
696 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000697
Ted Kremenekcf118d42009-02-04 23:49:09 +0000698 // Find the BugReport with the original location.
699 BugReport *R = 0;
700 unsigned i = 0;
701 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
702 if (i == GPair.second.second) { R = *I; break; }
703
704 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000705
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000706 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
707 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000708 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000709
Ted Kremenekcf118d42009-02-04 23:49:09 +0000710 // Start building the path diagnostic...
711 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000712 PD.push_back(Piece);
713 else
714 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000715
Ted Kremenek3148eb42009-01-24 00:55:43 +0000716 const ExplodedNode<GRState>* NextNode = N->pred_empty()
717 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000718
Ted Kremenekc0959972008-07-02 21:24:01 +0000719 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000720 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000721 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000722
Ted Kremenek6837faa2008-04-09 00:20:43 +0000723 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000724 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000725 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000726
727 ProgramPoint P = N->getLocation();
728
729 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000730 CFGBlock* Src = BE->getSrc();
731 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000732 Stmt* T = Src->getTerminator();
733
734 if (!T)
735 continue;
736
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000737 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000738
739 switch (T->getStmtClass()) {
740 default:
741 break;
742
743 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000744 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000745 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000746
747 if (!S)
748 continue;
749
Ted Kremenek297308e2009-02-10 23:56:07 +0000750 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000751 llvm::raw_string_ostream os(sbuf);
752 SourceLocation End = S->getLocStart();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000753
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000754 os << "Control jumps to line " << SMgr.getInstantiationLineNumber(End);
755 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
756 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000757 break;
758 }
759
Ted Kremenek297308e2009-02-10 23:56:07 +0000760 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000761 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000762 std::string sbuf;
763 llvm::raw_string_ostream os(sbuf);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000764 SourceLocation End;
765
766 if (Stmt* S = Dst->getLabel()) {
767 End = S->getLocStart();
768
Ted Kremenek5a429952008-04-23 23:35:07 +0000769 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000770 default:
771 os << "No cases match in the switch statement. "
772 "Control jumps to line "
773 << SMgr.getInstantiationLineNumber(End);
774 break;
775 case Stmt::DefaultStmtClass:
776 os << "Control jumps to the 'default' case at line "
777 << SMgr.getInstantiationLineNumber(End);
778 break;
779
780 case Stmt::CaseStmtClass: {
781 os << "Control jumps to 'case ";
782 CaseStmt* Case = cast<CaseStmt>(S);
783 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
784
785 // Determine if it is an enum.
786 bool GetRawInt = true;
787
788 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
789 // FIXME: Maybe this should be an assertion. Are there cases
790 // were it is not an EnumConstantDecl?
791 EnumConstantDecl* D =
792 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000793
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000794 if (D) {
795 GetRawInt = false;
796 os << D->getNameAsString();
797 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000798 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000799
800 if (GetRawInt) {
801
802 // Not an enum.
803 Expr* CondE = cast<SwitchStmt>(T)->getCond();
804 unsigned bits = Ctx.getTypeSize(CondE->getType());
805 llvm::APSInt V(bits, false);
806
807 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
808 assert (false && "Case condition must be constant.");
809 continue;
810 }
811
812 os << V;
813 }
814
815 os << ":' at line " << SMgr.getInstantiationLineNumber(End);
816 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000817 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000818 }
819 }
Ted Kremenek56783922008-04-25 01:29:56 +0000820 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000821 os << "'Default' branch taken. ";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000822 End = ExecutionContinues(os, SMgr, N,
823 getStateManager().getCodeDecl());
Ted Kremenek56783922008-04-25 01:29:56 +0000824 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000825
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 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000830
831 case Stmt::BreakStmtClass:
832 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000833 std::string sbuf;
834 llvm::raw_string_ostream os(sbuf);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000835 SourceLocation End = ExecutionContinues(os, SMgr, N,
836 getStateManager().getCodeDecl());
837 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
838 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000839 break;
840 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000841
842 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000843 std::string sbuf;
844 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000845 os << "'?' condition evaluates to ";
846
847 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000848 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000849 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000850 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000851
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000852 SourceLocation End =
853 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
854
855 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
856 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000857 break;
858 }
859
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000860 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000861 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000862 std::string sbuf;
863 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000864
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000865 os << "Loop condition is true. ";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000866 SourceLocation End =
867 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
868 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
869 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000870 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000871 else {
872 SourceLocation End =
873 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
874 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
875 "Loop condition is false. Exiting loop"));
876 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000877
878 break;
879 }
880
Ted Kremenek61f3e052008-04-03 04:42:52 +0000881 case Stmt::WhileStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000882 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000883 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000884 std::string sbuf;
885 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000886
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000887 os << "Loop condition is false. ";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000888 SourceLocation End =
889 ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl());
Ted Kremenek297308e2009-02-10 23:56:07 +0000890
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000891 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
892 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000893 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000894 else {
895 SourceLocation End =
896 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
897
898 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
899 "Loop condition is true. Entering loop body"));
900 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000901
902 break;
903 }
904
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000905 case Stmt::IfStmtClass: {
906 SourceLocation End =
907 ExecutionContinues(SMgr, N, getStateManager().getCodeDecl());
908
Ted Kremenek61f3e052008-04-03 04:42:52 +0000909 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000910 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
911 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +0000912 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000913 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
914 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000915
916 break;
917 }
918 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000919 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000920
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000921 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
922 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000923 PD.push_front(p);
924
Ted Kremenek9e240492008-10-04 05:50:14 +0000925 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
926 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000927 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000928 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
929 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000930 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000931 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000932
933 // After constructing the full PathDiagnostic, do a pass over it to compact
934 // PathDiagnosticPieces that occur within a macro.
935 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000936}
937
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000938
Ted Kremenekcf118d42009-02-04 23:49:09 +0000939void BugReporter::Register(BugType *BT) {
940 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000941}
942
Ted Kremenekcf118d42009-02-04 23:49:09 +0000943void BugReporter::EmitReport(BugReport* R) {
944 // Compute the bug report's hash to determine its equivalence class.
945 llvm::FoldingSetNodeID ID;
946 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000947
Ted Kremenekcf118d42009-02-04 23:49:09 +0000948 // Lookup the equivance class. If there isn't one, create it.
949 BugType& BT = R->getBugType();
950 Register(&BT);
951 void *InsertPos;
952 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
953
954 if (!EQ) {
955 EQ = new BugReportEquivClass(R);
956 BT.EQClasses.InsertNode(EQ, InsertPos);
957 }
958 else
959 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000960}
961
Ted Kremenekcf118d42009-02-04 23:49:09 +0000962void BugReporter::FlushReport(BugReportEquivClass& EQ) {
963 assert(!EQ.Reports.empty());
964 BugReport &R = **EQ.begin();
965
966 // FIXME: Make sure we use the 'R' for the path that was actually used.
967 // Probably doesn't make a difference in practice.
968 BugType& BT = R.getBugType();
969
970 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
971 R.getDescription(),
972 BT.getCategory()));
973 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +0000974
975 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +0000976 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000977 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +0000978
Ted Kremenek3148eb42009-01-24 00:55:43 +0000979 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +0000980 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +0000981 const SourceRange *Beg = 0, *End = 0;
982 R.getRanges(*this, Beg, End);
983 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000984 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +0000985 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
986 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +0000987
Ted Kremenek3148eb42009-01-24 00:55:43 +0000988 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000989 default: assert(0 && "Don't handle this many ranges yet!");
990 case 0: Diag.Report(L, ErrorDiag); break;
991 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
992 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
993 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +0000994 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000995
996 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
997 if (!PD)
998 return;
999
1000 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001001 PathDiagnosticPiece* piece =
1002 new PathDiagnosticEventPiece(L, R.getDescription());
1003
Ted Kremenek3148eb42009-01-24 00:55:43 +00001004 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1005 D->push_back(piece);
1006 }
1007
1008 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001009}
Ted Kremenek57202072008-07-14 17:40:50 +00001010
Ted Kremenek8c036c72008-09-20 04:23:38 +00001011void BugReporter::EmitBasicReport(const char* name, const char* str,
1012 SourceLocation Loc,
1013 SourceRange* RBeg, unsigned NumRanges) {
1014 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1015}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001016
Ted Kremenek8c036c72008-09-20 04:23:38 +00001017void BugReporter::EmitBasicReport(const char* name, const char* category,
1018 const char* str, SourceLocation Loc,
1019 SourceRange* RBeg, unsigned NumRanges) {
1020
Ted Kremenekcf118d42009-02-04 23:49:09 +00001021 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1022 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001023 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001024 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1025 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1026 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001027}