blob: 88d4fa15edf444f424510d38f0c431207d7571d6 [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
111 bool supportsLogicalOpControlFlow() const {
112 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
113 }
114};
115} // end anonymous namespace
116
Ted Kremenek00605e02009-03-27 20:55:39 +0000117PathDiagnosticLocation
118PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) {
119 if (Stmt *S = GetNextStmt(N))
120 return PathDiagnosticLocation(S, SMgr);
121
122 return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000123}
124
Ted Kremenek00605e02009-03-27 20:55:39 +0000125PathDiagnosticLocation
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000126PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os,
127 const ExplodedNode<GRState>* N) {
128
Ted Kremenek143ca222008-05-06 18:11:09 +0000129 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000130 if (os.str().empty())
131 os << ' ';
Ted Kremenek143ca222008-05-06 18:11:09 +0000132
Ted Kremenek00605e02009-03-27 20:55:39 +0000133 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000134
Ted Kremenek00605e02009-03-27 20:55:39 +0000135 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000136 os << "Execution continues on line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000137 << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.';
Ted Kremenekb697b102009-02-23 22:44:26 +0000138 else
Ted Kremenekb479dad2009-02-23 23:13:51 +0000139 os << "Execution jumps to the end of the "
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000140 << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.';
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000141
142 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000143}
144
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145//===----------------------------------------------------------------------===//
146// Methods for BugType and subclasses.
147//===----------------------------------------------------------------------===//
148BugType::~BugType() {}
149void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000150
Ted Kremenekcf118d42009-02-04 23:49:09 +0000151//===----------------------------------------------------------------------===//
152// Methods for BugReport and subclasses.
153//===----------------------------------------------------------------------===//
154BugReport::~BugReport() {}
155RangedBugReport::~RangedBugReport() {}
156
157Stmt* BugReport::getStmt(BugReporter& BR) const {
Ted Kremenek200ed922008-05-02 23:21:21 +0000158 ProgramPoint ProgP = EndNode->getLocation();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000159 Stmt *S = NULL;
160
Ted Kremenekcf118d42009-02-04 23:49:09 +0000161 if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) {
Ted Kremenekb697b102009-02-23 22:44:26 +0000162 if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000163 }
164 if (!S) S = GetStmt(ProgP);
165
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000166 return S;
167}
168
169PathDiagnosticPiece*
170BugReport::getEndPath(BugReporter& BR,
Ted Kremenek3148eb42009-01-24 00:55:43 +0000171 const ExplodedNode<GRState>* EndPathNode) {
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000172
173 Stmt* S = getStmt(BR);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000174
175 if (!S)
176 return NULL;
177
Ted Kremenekc9fa2f72008-05-01 23:13:35 +0000178 FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000179 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000180
Ted Kremenekde7161f2008-04-03 18:00:37 +0000181 const SourceRange *Beg, *End;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000182 getRanges(BR, Beg, End);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000183
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000184 for (; Beg != End; ++Beg)
185 P->addRange(*Beg);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000186
187 return P;
188}
189
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000190void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg,
191 const SourceRange*& end) {
192
193 if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) {
194 R = E->getSourceRange();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000195 assert(R.isValid());
Ted Kremenekbb77e9b2008-05-01 22:50:36 +0000196 beg = &R;
197 end = beg+1;
198 }
199 else
200 beg = end = 0;
Ted Kremenekf1ae7052008-04-03 17:57:38 +0000201}
202
Ted Kremenekcf118d42009-02-04 23:49:09 +0000203SourceLocation BugReport::getLocation() const {
204 if (EndNode)
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000205 if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) {
206 // For member expressions, return the location of the '.' or '->'.
207 if (MemberExpr* ME = dyn_cast<MemberExpr>(S))
208 return ME->getMemberLoc();
209
Ted Kremenekcf118d42009-02-04 23:49:09 +0000210 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +0000211 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000212
213 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000214}
215
Ted Kremenek3148eb42009-01-24 00:55:43 +0000216PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N,
217 const ExplodedNode<GRState>* PrevN,
218 const ExplodedGraph<GRState>& G,
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000219 BugReporter& BR,
220 NodeResolver &NR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000221 return NULL;
222}
223
Ted Kremenekcf118d42009-02-04 23:49:09 +0000224//===----------------------------------------------------------------------===//
225// Methods for BugReporter and subclasses.
226//===----------------------------------------------------------------------===//
227
228BugReportEquivClass::~BugReportEquivClass() {
229 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
230}
231
232GRBugReporter::~GRBugReporter() { FlushReports(); }
233BugReporterData::~BugReporterData() {}
234
235ExplodedGraph<GRState>&
236GRBugReporter::getGraph() { return Eng.getGraph(); }
237
238GRStateManager&
239GRBugReporter::getStateManager() { return Eng.getStateManager(); }
240
241BugReporter::~BugReporter() { FlushReports(); }
242
243void BugReporter::FlushReports() {
244 if (BugTypes.isEmpty())
245 return;
246
247 // First flush the warnings for each BugType. This may end up creating new
248 // warnings and new BugTypes. Because ImmutableSet is a functional data
249 // structure, we do not need to worry about the iterators being invalidated.
250 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
251 const_cast<BugType*>(*I)->FlushReports(*this);
252
253 // Iterate through BugTypes a second time. BugTypes may have been updated
254 // with new BugType objects and new warnings.
255 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) {
256 BugType *BT = const_cast<BugType*>(*I);
257
258 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
259 SetTy& EQClasses = BT->EQClasses;
260
261 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
262 BugReportEquivClass& EQ = *EI;
263 FlushReport(EQ);
264 }
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000265
Ted Kremenekcf118d42009-02-04 23:49:09 +0000266 // Delete the BugType object. This will also delete the equivalence
267 // classes.
268 delete BT;
Ted Kremenek94826a72008-04-03 04:59:14 +0000269 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000270
271 // Remove all references to the BugType objects.
272 BugTypes = F.GetEmptySet();
273}
274
275//===----------------------------------------------------------------------===//
276// PathDiagnostics generation.
277//===----------------------------------------------------------------------===//
278
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000279typedef llvm::DenseMap<const ExplodedNode<GRState>*,
280 const ExplodedNode<GRState>*> NodeBackMap;
281
282static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000283 std::pair<ExplodedNode<GRState>*, unsigned> >
284MakeReportGraph(const ExplodedGraph<GRState>* G,
285 const ExplodedNode<GRState>** NStart,
286 const ExplodedNode<GRState>** NEnd) {
Ted Kremenek94826a72008-04-03 04:59:14 +0000287
Ted Kremenekcf118d42009-02-04 23:49:09 +0000288 // Create the trimmed graph. It will contain the shortest paths from the
289 // error nodes to the root. In the new graph we should only have one
290 // error node unless there are two or more error nodes with the same minimum
291 // path length.
292 ExplodedGraph<GRState>* GTrim;
293 InterExplodedGraphMap<GRState>* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000294
295 llvm::DenseMap<const void*, const void*> InverseMap;
296 llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000297
298 // Create owning pointers for GTrim and NMap just to ensure that they are
299 // released when this function exists.
300 llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim);
301 llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap);
302
303 // Find the (first) error node in the trimmed graph. We just need to consult
304 // the node map (NMap) which maps from nodes in the original graph to nodes
305 // in the new graph.
306 const ExplodedNode<GRState>* N = 0;
307 unsigned NodeIndex = 0;
308
309 for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I)
310 if ((N = NMap->getMappedNode(*I))) {
311 NodeIndex = (I - NStart) / sizeof(*I);
312 break;
313 }
314
315 assert(N && "No error node found in the trimmed graph.");
316
317 // Create a new (third!) graph with a single path. This is the graph
318 // that will be returned to the caller.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000319 ExplodedGraph<GRState> *GNew =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000320 new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(),
321 GTrim->getContext());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000322
Ted Kremenek10aa5542009-03-12 23:41:59 +0000323 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000324 // to the root node, and then construct a new graph that contains only
325 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000326 llvm::DenseMap<const void*,unsigned> Visited;
Ted Kremenek10aa5542009-03-12 23:41:59 +0000327 std::queue<const ExplodedNode<GRState>*> WS;
328 WS.push(N);
329
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000330 unsigned cnt = 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000331 const ExplodedNode<GRState>* Root = 0;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000332
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000333 while (!WS.empty()) {
Ted Kremenek10aa5542009-03-12 23:41:59 +0000334 const ExplodedNode<GRState>* Node = WS.front();
335 WS.pop();
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000336
337 if (Visited.find(Node) != Visited.end())
338 continue;
339
340 Visited[Node] = cnt++;
341
342 if (Node->pred_empty()) {
343 Root = Node;
344 break;
345 }
346
Ted Kremenek3148eb42009-01-24 00:55:43 +0000347 for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000348 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000349 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000350 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000351
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000352 assert (Root);
353
Ted Kremenek10aa5542009-03-12 23:41:59 +0000354 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000355 // with the lowest number.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000356 ExplodedNode<GRState> *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000357 NodeBackMap *BM = new NodeBackMap();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000358
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000359 for ( N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000360 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000361 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000362 assert (I != Visited.end());
363
364 // Create the equivalent node in the new graph with the same state
365 // and location.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000366 ExplodedNode<GRState>* NewN =
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000367 GNew->getNode(N->getLocation(), N->getState());
368
369 // Store the mapping to the original node.
370 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
371 assert(IMitr != InverseMap.end() && "No mapping to original node.");
372 (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000373
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000374 // Link up the new node with the previous node.
375 if (Last)
376 NewN->addPredecessor(Last);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000377
378 Last = NewN;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000379
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000380 // Are we at the final node?
381 if (I->second == 0) {
382 First = NewN;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000383 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000384 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000385
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000386 // Find the next successor node. We choose the node that is marked
387 // with the lowest DFS number.
Ted Kremenek3148eb42009-01-24 00:55:43 +0000388 ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin();
389 ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +0000390 N = 0;
391
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000392 for (unsigned MinVal = 0; SI != SE; ++SI) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000393
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000394 I = Visited.find(*SI);
395
396 if (I == Visited.end())
397 continue;
398
399 if (!N || I->second < MinVal) {
400 N = *SI;
401 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +0000402 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000403 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000404
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000405 assert (N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000406 }
Ted Kremenekcf118d42009-02-04 23:49:09 +0000407
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000408 assert (First);
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000409 return std::make_pair(std::make_pair(GNew, BM),
410 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000411}
412
Ted Kremenek3148eb42009-01-24 00:55:43 +0000413static const VarDecl*
414GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N,
415 GRStateManager& VMgr, SVal X) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000416
417 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
418
419 ProgramPoint P = N->getLocation();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000420
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000421 if (!isa<PostStmt>(P))
422 continue;
423
424 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Ted Kremenekcf118d42009-02-04 23:49:09 +0000425
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000426 if (!DR)
427 continue;
428
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000429 SVal Y = VMgr.GetSVal(N->getState(), DR);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000430
431 if (X != Y)
432 continue;
433
434 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
435
436 if (!VD)
437 continue;
438
439 return VD;
440 }
441
442 return 0;
443}
444
Ted Kremenek9e240492008-10-04 05:50:14 +0000445namespace {
446class VISIBILITY_HIDDEN NotableSymbolHandler
447 : public StoreManager::BindingsHandler {
448
Ted Kremenek2dabd432008-12-05 02:27:51 +0000449 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +0000450 const GRState* PrevSt;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000451 const Stmt* S;
Ted Kremenek9e240492008-10-04 05:50:14 +0000452 GRStateManager& VMgr;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000453 const ExplodedNode<GRState>* Pred;
Ted Kremenek9e240492008-10-04 05:50:14 +0000454 PathDiagnostic& PD;
455 BugReporter& BR;
456
457public:
458
Ted Kremenek3148eb42009-01-24 00:55:43 +0000459 NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s,
460 GRStateManager& vmgr, const ExplodedNode<GRState>* pred,
Ted Kremenek9e240492008-10-04 05:50:14 +0000461 PathDiagnostic& pd, BugReporter& br)
462 : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {}
463
Ted Kremenekbe912242009-03-05 16:31:07 +0000464 bool HandleBinding(StoreManager& SMgr, Store store,
465 const MemRegion* R, SVal V) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000466
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000467 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000468
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000469 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000470 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000471 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek9e240492008-10-04 05:50:14 +0000472 ScanSym = SV->getSymbol();
473 else
474 return true;
475
476 if (ScanSym != Sym)
477 return true;
478
479 // Check if the previous state has this binding.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000480 SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R));
Ted Kremenek9e240492008-10-04 05:50:14 +0000481
482 if (X == V) // Same binding?
483 return true;
484
485 // Different binding. Only handle assignments for now. We don't pull
486 // this check out of the loop because we will eventually handle other
487 // cases.
488
489 VarDecl *VD = 0;
490
Ted Kremenek3148eb42009-01-24 00:55:43 +0000491 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000492 if (!B->isAssignmentOp())
493 return true;
494
495 // What variable did we assign to?
496 DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
497
498 if (!DR)
499 return true;
500
501 VD = dyn_cast<VarDecl>(DR->getDecl());
502 }
Ted Kremenek3148eb42009-01-24 00:55:43 +0000503 else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenekf21a4b42008-10-06 18:37:46 +0000504 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
505 // assume that each DeclStmt has a single Decl. This invariant
506 // holds by contruction in the CFG.
507 VD = dyn_cast<VarDecl>(*DS->decl_begin());
508 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000509
510 if (!VD)
511 return true;
512
513 // What is the most recently referenced variable with this binding?
Ted Kremenek3148eb42009-01-24 00:55:43 +0000514 const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Ted Kremenek9e240492008-10-04 05:50:14 +0000515
516 if (!MostRecent)
517 return true;
518
519 // Create the diagnostic.
Ted Kremenek9e240492008-10-04 05:50:14 +0000520 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
521
Ted Kremenek3daea0a2009-02-26 20:29:19 +0000522 if (Loc::IsLocType(VD->getType())) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000523 std::string msg = "'" + std::string(VD->getNameAsString()) +
524 "' now aliases '" + MostRecent->getNameAsString() + "'";
Ted Kremenek9e240492008-10-04 05:50:14 +0000525
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000526 PD.push_front(new PathDiagnosticEventPiece(L, msg));
Ted Kremenek9e240492008-10-04 05:50:14 +0000527 }
528
529 return true;
530 }
531};
532}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000533
Ted Kremenek3148eb42009-01-24 00:55:43 +0000534static void HandleNotableSymbol(const ExplodedNode<GRState>* N,
535 const Stmt* S,
Ted Kremenek2dabd432008-12-05 02:27:51 +0000536 SymbolRef Sym, BugReporter& BR,
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000537 PathDiagnostic& PD) {
538
Ted Kremenek3148eb42009-01-24 00:55:43 +0000539 const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000540 const GRState* PrevSt = Pred ? Pred->getState() : 0;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000541
542 if (!PrevSt)
543 return;
544
Ted Kremenek9e240492008-10-04 05:50:14 +0000545 // Look at the region bindings of the current state that map to the
546 // specified symbol. Are any of them not in the previous state?
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000547 GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek9e240492008-10-04 05:50:14 +0000548 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
549 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
550}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000551
Ted Kremenek9e240492008-10-04 05:50:14 +0000552namespace {
553class VISIBILITY_HIDDEN ScanNotableSymbols
554 : public StoreManager::BindingsHandler {
555
Ted Kremenek2dabd432008-12-05 02:27:51 +0000556 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek3148eb42009-01-24 00:55:43 +0000557 const ExplodedNode<GRState>* N;
Ted Kremenek9e240492008-10-04 05:50:14 +0000558 Stmt* S;
559 GRBugReporter& BR;
560 PathDiagnostic& PD;
561
562public:
Ted Kremenek3148eb42009-01-24 00:55:43 +0000563 ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br,
Ted Kremenek9e240492008-10-04 05:50:14 +0000564 PathDiagnostic& pd)
565 : N(n), S(s), BR(br), PD(pd) {}
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000566
Ted Kremenekbe912242009-03-05 16:31:07 +0000567 bool HandleBinding(StoreManager& SMgr, Store store,
568 const MemRegion* R, SVal V) {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000569 SymbolRef ScanSym = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +0000570
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000571 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000572 ScanSym = SV->getSymbol();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000573 else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000574 ScanSym = SV->getSymbol();
575 else
Ted Kremenek9e240492008-10-04 05:50:14 +0000576 return true;
577
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000578 assert (ScanSym);
Ted Kremenek9e240492008-10-04 05:50:14 +0000579
580 if (!BR.isNotable(ScanSym))
581 return true;
582
583 if (AlreadyProcessed.count(ScanSym))
584 return true;
585
586 AlreadyProcessed.insert(ScanSym);
587
588 HandleNotableSymbol(N, S, ScanSym, BR, PD);
589 return true;
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000590 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000591};
592} // end anonymous namespace
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000593
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000594namespace {
595class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver {
596 NodeBackMap& M;
597public:
598 NodeMapClosure(NodeBackMap *m) : M(*m) {}
599 ~NodeMapClosure() {}
600
601 const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) {
602 NodeBackMap::iterator I = M.find(N);
603 return I == M.end() ? 0 : I->second;
604 }
605};
606}
607
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000608/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
609/// and collapses PathDiagosticPieces that are expanded by macros.
610static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
611 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
612 MacroStackTy;
613
614 typedef std::vector<PathDiagnosticPiece*>
615 PiecesTy;
616
617 MacroStackTy MacroStack;
618 PiecesTy Pieces;
619
620 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
621 // Get the location of the PathDiagnosticPiece.
622 const FullSourceLoc Loc = I->getLocation();
623
624 // Determine the instantiation location, which is the location we group
625 // related PathDiagnosticPieces.
626 SourceLocation InstantiationLoc = Loc.isMacroID() ?
627 SM.getInstantiationLoc(Loc) :
628 SourceLocation();
629
630 if (Loc.isFileID()) {
631 MacroStack.clear();
632 Pieces.push_back(&*I);
633 continue;
634 }
635
636 assert(Loc.isMacroID());
637
638 // Is the PathDiagnosticPiece within the same macro group?
639 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
640 MacroStack.back().first->push_back(&*I);
641 continue;
642 }
643
644 // We aren't in the same group. Are we descending into a new macro
645 // or are part of an old one?
646 PathDiagnosticMacroPiece *MacroGroup = 0;
647
648 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
649 SM.getInstantiationLoc(Loc) :
650 SourceLocation();
651
652 // Walk the entire macro stack.
653 while (!MacroStack.empty()) {
654 if (InstantiationLoc == MacroStack.back().second) {
655 MacroGroup = MacroStack.back().first;
656 break;
657 }
658
659 if (ParentInstantiationLoc == MacroStack.back().second) {
660 MacroGroup = MacroStack.back().first;
661 break;
662 }
663
664 MacroStack.pop_back();
665 }
666
667 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
668 // Create a new macro group and add it to the stack.
669 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
670
671 if (MacroGroup)
672 MacroGroup->push_back(NewGroup);
673 else {
674 assert(InstantiationLoc.isFileID());
675 Pieces.push_back(NewGroup);
676 }
677
678 MacroGroup = NewGroup;
679 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
680 }
681
682 // Finally, add the PathDiagnosticPiece to the group.
683 MacroGroup->push_back(&*I);
684 }
685
686 // Now take the pieces and construct a new PathDiagnostic.
687 PD.resetPath(false);
688
689 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
690 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
691 if (!MP->containsEvent()) {
692 delete MP;
693 continue;
694 }
695
696 PD.push_back(*I);
697 }
698}
699
Ted Kremenekc0959972008-07-02 21:24:01 +0000700void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000701 BugReportEquivClass& EQ) {
702
703 std::vector<const ExplodedNode<GRState>*> Nodes;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000704
Ted Kremenekcf118d42009-02-04 23:49:09 +0000705 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
706 const ExplodedNode<GRState>* N = I->getEndNode();
707 if (N) Nodes.push_back(N);
708 }
709
710 if (Nodes.empty())
711 return;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000712
713 // Construct a new graph that contains only a single path from the error
Ted Kremenekcf118d42009-02-04 23:49:09 +0000714 // node to a root.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000715 const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000716 std::pair<ExplodedNode<GRState>*, unsigned> >&
717 GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size());
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000718
Ted Kremenekcf118d42009-02-04 23:49:09 +0000719 // Find the BugReport with the original location.
720 BugReport *R = 0;
721 unsigned i = 0;
722 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i)
723 if (i == GPair.second.second) { R = *I; break; }
724
725 assert(R && "No original report found for sliced graph.");
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000726
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000727 llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first);
728 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000729 const ExplodedNode<GRState> *N = GPair.second.first;
Ted Kremeneka43a1eb2008-04-23 23:02:12 +0000730
Ted Kremenekcf118d42009-02-04 23:49:09 +0000731 // Start building the path diagnostic...
732 if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000733 PD.push_back(Piece);
734 else
735 return;
Ted Kremenek6837faa2008-04-09 00:20:43 +0000736
Ted Kremenek3148eb42009-01-24 00:55:43 +0000737 const ExplodedNode<GRState>* NextNode = N->pred_empty()
738 ? NULL : *(N->pred_begin());
Ted Kremenek6837faa2008-04-09 00:20:43 +0000739
Ted Kremenekc0959972008-07-02 21:24:01 +0000740 ASTContext& Ctx = getContext();
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000741 SourceManager& SMgr = Ctx.getSourceManager();
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000742 NodeMapClosure NMC(BackMap.get());
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000743 PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(),
744 getPathDiagnosticClient());
Ted Kremenekbd7efa82008-04-17 23:44:37 +0000745
Ted Kremenek6837faa2008-04-09 00:20:43 +0000746 while (NextNode) {
Ted Kremenek6837faa2008-04-09 00:20:43 +0000747 N = NextNode;
Ted Kremenekb697b102009-02-23 22:44:26 +0000748 NextNode = GetPredecessorNode(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000749
750 ProgramPoint P = N->getLocation();
751
752 if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000753 CFGBlock* Src = BE->getSrc();
754 CFGBlock* Dst = BE->getDst();
Ted Kremenek61f3e052008-04-03 04:42:52 +0000755 Stmt* T = Src->getTerminator();
756
757 if (!T)
758 continue;
759
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000760 FullSourceLoc Start(T->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000761
762 switch (T->getStmtClass()) {
763 default:
764 break;
765
766 case Stmt::GotoStmtClass:
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000767 case Stmt::IndirectGotoStmtClass: {
Ted Kremenekb697b102009-02-23 22:44:26 +0000768 Stmt* S = GetNextStmt(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000769
770 if (!S)
771 continue;
772
Ted Kremenek297308e2009-02-10 23:56:07 +0000773 std::string sbuf;
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000774 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000775 PathDiagnosticLocation End(S->getLocStart(), SMgr);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000776
Ted Kremenek00605e02009-03-27 20:55:39 +0000777 os << "Control jumps to line "
778 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000779 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
780 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000781 break;
782 }
783
Ted Kremenek297308e2009-02-10 23:56:07 +0000784 case Stmt::SwitchStmtClass: {
Ted Kremenek61f3e052008-04-03 04:42:52 +0000785 // Figure out what case arm we took.
Ted Kremenek297308e2009-02-10 23:56:07 +0000786 std::string sbuf;
787 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000788
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000789 if (Stmt* S = Dst->getLabel()) {
Ted Kremenek00605e02009-03-27 20:55:39 +0000790 PathDiagnosticLocation End(S, SMgr);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000791
Ted Kremenek5a429952008-04-23 23:35:07 +0000792 switch (S->getStmtClass()) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000793 default:
794 os << "No cases match in the switch statement. "
795 "Control jumps to line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000796 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000797 break;
798 case Stmt::DefaultStmtClass:
799 os << "Control jumps to the 'default' case at line "
Ted Kremenek00605e02009-03-27 20:55:39 +0000800 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000801 break;
802
803 case Stmt::CaseStmtClass: {
804 os << "Control jumps to 'case ";
805 CaseStmt* Case = cast<CaseStmt>(S);
806 Expr* LHS = Case->getLHS()->IgnoreParenCasts();
807
808 // Determine if it is an enum.
809 bool GetRawInt = true;
810
811 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
812 // FIXME: Maybe this should be an assertion. Are there cases
813 // were it is not an EnumConstantDecl?
814 EnumConstantDecl* D =
815 dyn_cast<EnumConstantDecl>(DR->getDecl());
Ted Kremenek5a429952008-04-23 23:35:07 +0000816
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000817 if (D) {
818 GetRawInt = false;
819 os << D->getNameAsString();
820 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000821 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000822
823 if (GetRawInt) {
824
825 // Not an enum.
826 Expr* CondE = cast<SwitchStmt>(T)->getCond();
827 unsigned bits = Ctx.getTypeSize(CondE->getType());
828 llvm::APSInt V(bits, false);
829
830 if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) {
831 assert (false && "Case condition must be constant.");
832 continue;
833 }
834
835 os << V;
836 }
837
Ted Kremenek00605e02009-03-27 20:55:39 +0000838 os << ":' at line "
839 << End.asLocation().getInstantiationLineNumber();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000840 break;
Ted Kremenek61f3e052008-04-03 04:42:52 +0000841 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000842 }
Ted Kremenek00605e02009-03-27 20:55:39 +0000843 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
844 os.str()));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000845 }
Ted Kremenek56783922008-04-25 01:29:56 +0000846 else {
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000847 os << "'Default' branch taken. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000848 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
849 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
850 os.str()));
Ted Kremenek56783922008-04-25 01:29:56 +0000851 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000852
Ted Kremenek61f3e052008-04-03 04:42:52 +0000853 break;
854 }
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000855
856 case Stmt::BreakStmtClass:
857 case Stmt::ContinueStmtClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000858 std::string sbuf;
859 llvm::raw_string_ostream os(sbuf);
Ted Kremenek00605e02009-03-27 20:55:39 +0000860 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000861 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
862 os.str()));
Ted Kremenek2673c9f2008-04-25 19:01:27 +0000863 break;
864 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000865
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000866 // Determine control-flow for ternary '?'.
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000867 case Stmt::ConditionalOperatorClass: {
Ted Kremenek297308e2009-02-10 23:56:07 +0000868 std::string sbuf;
869 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000870 os << "'?' condition evaluates to ";
871
872 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000873 os << "false";
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000874 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000875 os << "true";
Ted Kremenek61f3e052008-04-03 04:42:52 +0000876
Ted Kremenek00605e02009-03-27 20:55:39 +0000877 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000878
879 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
880 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000881 break;
882 }
883
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000884 // Determine control-flow for short-circuited '&&' and '||'.
885 case Stmt::BinaryOperatorClass: {
886 if (!PDB.supportsLogicalOpControlFlow())
887 break;
888
889 BinaryOperator *B = cast<BinaryOperator>(T);
890 std::string sbuf;
891 llvm::raw_string_ostream os(sbuf);
892 os << "Left side of '";
893
894 if (B->getOpcode() == BinaryOperator::LAnd) {
895 os << "&&";
896 }
897 else {
898 assert(B->getOpcode() == BinaryOperator::LOr);
899 os << "||";
900 }
901
902 os << "' is ";
903 if (*(Src->succ_begin()+1) == Dst)
904 os << (B->getOpcode() == BinaryOperator::LAnd
905 ? "false" : "true");
906 else
907 os << (B->getOpcode() == BinaryOperator::LAnd
908 ? "true" : "false");
909
910 PathDiagnosticLocation Start(B->getLHS(), SMgr);
911 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
912
913 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
914 os.str()));
915 break;
916 }
917
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000918 case Stmt::DoStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000919 if (*(Src->succ_begin()) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000920 std::string sbuf;
921 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000922
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000923 os << "Loop condition is true. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000924 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000925 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
926 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000927 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000928 else {
Ted Kremenek00605e02009-03-27 20:55:39 +0000929 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000930 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
931 "Loop condition is false. Exiting loop"));
932 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000933
934 break;
935 }
936
Ted Kremenek61f3e052008-04-03 04:42:52 +0000937 case Stmt::WhileStmtClass:
Ted Kremenek00605e02009-03-27 20:55:39 +0000938 case Stmt::ForStmtClass: {
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000939 if (*(Src->succ_begin()+1) == Dst) {
Ted Kremenek297308e2009-02-10 23:56:07 +0000940 std::string sbuf;
941 llvm::raw_string_ostream os(sbuf);
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000942
Ted Kremenekc3517eb2008-09-12 18:17:46 +0000943 os << "Loop condition is false. ";
Ted Kremenek00605e02009-03-27 20:55:39 +0000944 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000945 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
946 os.str()));
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000947 }
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000948 else {
Ted Kremenek00605e02009-03-27 20:55:39 +0000949 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000950
951 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
952 "Loop condition is true. Entering loop body"));
953 }
Ted Kremenek706e3cf2008-04-07 23:35:17 +0000954
955 break;
956 }
957
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000958 case Stmt::IfStmtClass: {
Ted Kremenek00605e02009-03-27 20:55:39 +0000959 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000960 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000961 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
962 "Taking false branch"));
Ted Kremenek025fedc2009-03-02 21:41:18 +0000963 else
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000964 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
965 "Taking true branch"));
Ted Kremenek61f3e052008-04-03 04:42:52 +0000966
967 break;
968 }
969 }
Ted Kremenek6837faa2008-04-09 00:20:43 +0000970 }
Ted Kremenek5a429952008-04-23 23:35:07 +0000971
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000972 if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this,
973 NMC))
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000974 PD.push_front(p);
975
Ted Kremenek9e240492008-10-04 05:50:14 +0000976 if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
977 // Scan the region bindings, and see if a "notable" symbol has a new
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000978 // lval binding.
Ted Kremenek9e240492008-10-04 05:50:14 +0000979 ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD);
980 getStateManager().iterBindings(N->getState(), SNS);
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000981 }
Ted Kremenek61f3e052008-04-03 04:42:52 +0000982 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000983
984 // After constructing the full PathDiagnostic, do a pass over it to compact
985 // PathDiagnosticPieces that occur within a macro.
986 CompactPathDiagnostic(PD, getSourceManager());
Ted Kremenek61f3e052008-04-03 04:42:52 +0000987}
988
Ted Kremenek1aa44c72008-05-22 23:45:19 +0000989
Ted Kremenekcf118d42009-02-04 23:49:09 +0000990void BugReporter::Register(BugType *BT) {
991 BugTypes = F.Add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +0000992}
993
Ted Kremenekcf118d42009-02-04 23:49:09 +0000994void BugReporter::EmitReport(BugReport* R) {
995 // Compute the bug report's hash to determine its equivalence class.
996 llvm::FoldingSetNodeID ID;
997 R->Profile(ID);
Ted Kremenek61f3e052008-04-03 04:42:52 +0000998
Ted Kremenekcf118d42009-02-04 23:49:09 +0000999 // Lookup the equivance class. If there isn't one, create it.
1000 BugType& BT = R->getBugType();
1001 Register(&BT);
1002 void *InsertPos;
1003 BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos);
1004
1005 if (!EQ) {
1006 EQ = new BugReportEquivClass(R);
1007 BT.EQClasses.InsertNode(EQ, InsertPos);
1008 }
1009 else
1010 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001011}
1012
Ted Kremenekcf118d42009-02-04 23:49:09 +00001013void BugReporter::FlushReport(BugReportEquivClass& EQ) {
1014 assert(!EQ.Reports.empty());
1015 BugReport &R = **EQ.begin();
1016
1017 // FIXME: Make sure we use the 'R' for the path that was actually used.
1018 // Probably doesn't make a difference in practice.
1019 BugType& BT = R.getBugType();
1020
1021 llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(),
1022 R.getDescription(),
1023 BT.getCategory()));
1024 GeneratePathDiagnostic(*D.get(), EQ);
Ted Kremenek072192b2008-04-30 23:47:44 +00001025
1026 // Get the meta data.
Ted Kremenek072192b2008-04-30 23:47:44 +00001027 std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001028 for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001029
Ted Kremenek3148eb42009-01-24 00:55:43 +00001030 // Emit a summary diagnostic to the regular Diagnostics engine.
Ted Kremenekc0959972008-07-02 21:24:01 +00001031 PathDiagnosticClient* PD = getPathDiagnosticClient();
Ted Kremenek3148eb42009-01-24 00:55:43 +00001032 const SourceRange *Beg = 0, *End = 0;
1033 R.getRanges(*this, Beg, End);
1034 Diagnostic& Diag = getDiagnostic();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001035 FullSourceLoc L(R.getLocation(), getSourceManager());
Ted Kremenekd90e7082009-02-07 22:36:41 +00001036 unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning,
1037 R.getDescription().c_str());
Ted Kremenek57202072008-07-14 17:40:50 +00001038
Ted Kremenek3148eb42009-01-24 00:55:43 +00001039 switch (End-Beg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +00001040 default: assert(0 && "Don't handle this many ranges yet!");
1041 case 0: Diag.Report(L, ErrorDiag); break;
1042 case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break;
1043 case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break;
1044 case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001045 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001046
1047 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1048 if (!PD)
1049 return;
1050
1051 if (D->empty()) {
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001052 PathDiagnosticPiece* piece =
1053 new PathDiagnosticEventPiece(L, R.getDescription());
1054
Ted Kremenek3148eb42009-01-24 00:55:43 +00001055 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1056 D->push_back(piece);
1057 }
1058
1059 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001060}
Ted Kremenek57202072008-07-14 17:40:50 +00001061
Ted Kremenek8c036c72008-09-20 04:23:38 +00001062void BugReporter::EmitBasicReport(const char* name, const char* str,
1063 SourceLocation Loc,
1064 SourceRange* RBeg, unsigned NumRanges) {
1065 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1066}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001067
Ted Kremenek8c036c72008-09-20 04:23:38 +00001068void BugReporter::EmitBasicReport(const char* name, const char* category,
1069 const char* str, SourceLocation Loc,
1070 SourceRange* RBeg, unsigned NumRanges) {
1071
Ted Kremenekcf118d42009-02-04 23:49:09 +00001072 // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'.
1073 BugType *BT = new BugType(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001074 FullSourceLoc L = getContext().getFullLoc(Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001075 RangedBugReport *R = new DiagBugReport(*BT, str, L);
1076 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1077 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001078}