blob: e95f31c0d6bd87bcd0f59418df96cb575092ffa2 [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
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Jordan Rose852aa0d2012-07-10 22:07:52 +000051 else if (const CallEnter *CE = dyn_cast<CallEnter>(&P))
52 return CE->getCallExpr();
53 else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&P))
54 return CEE->getCalleeContext()->getCallSite();
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekb697b102009-02-23 22:44:26 +000056 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000057}
58
Zhongxing Xuc5619d92009-08-06 01:32:16 +000059static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000060GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000061 return N->pred_empty() ? NULL : *(N->pred_begin());
62}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000063
Zhongxing Xuc5619d92009-08-06 01:32:16 +000064static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000065GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000067}
68
Ted Kremenek9c378f72011-08-12 23:37:29 +000069static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000070 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000071 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000072 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekb697b102009-02-23 22:44:26 +000074 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000075}
76
Ted Kremenek9c378f72011-08-12 23:37:29 +000077static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000078 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000079 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 // Check if the statement is '?' or '&&'/'||'. These are "merges",
81 // not actual statement points.
82 switch (S->getStmtClass()) {
83 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000084 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 case Stmt::ConditionalOperatorClass: continue;
86 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000087 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
88 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000089 continue;
90 break;
91 }
92 default:
93 break;
94 }
Ted Kremenekb697b102009-02-23 22:44:26 +000095 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000096 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekb697b102009-02-23 22:44:26 +000098 return 0;
99}
100
Ted Kremenek5f85e172009-07-22 22:35:28 +0000101static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000102GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000103 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000104 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenekb697b102009-02-23 22:44:26 +0000106 return GetPreviousStmt(N);
107}
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek5f85e172009-07-22 22:35:28 +0000109static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000110GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000111 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000112 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekb697b102009-02-23 22:44:26 +0000114 return GetNextStmt(N);
115}
116
117//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000118// Diagnostic cleanup.
119//===----------------------------------------------------------------------===//
120
121/// Recursively scan through a path and prune out calls and macros pieces
122/// that aren't needed. Return true if afterwards the path contains
123/// "interesting stuff" which means it should be pruned from the parent path.
Ted Kremeneka43df952012-09-21 00:09:11 +0000124bool BugReporter::RemoveUneededCalls(PathPieces &pieces, BugReport *R,
125 PathDiagnosticCallPiece *CallWithLoc) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000126 bool containsSomethingInteresting = false;
127 const unsigned N = pieces.size();
128
129 for (unsigned i = 0 ; i < N ; ++i) {
130 // Remove the front piece from the path. If it is still something we
131 // want to keep once we are done, we will push it back on the end.
132 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
133 pieces.pop_front();
134
Ted Kremeneka43df952012-09-21 00:09:11 +0000135 // Throw away pieces with invalid locations.
136 if (piece->getKind() != PathDiagnosticPiece::Call &&
137 piece->getLocation().asLocation().isInvalid())
138 continue;
139
Ted Kremenek72516742012-03-01 00:05:06 +0000140 switch (piece->getKind()) {
141 case PathDiagnosticPiece::Call: {
142 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000143 // Check if the location context is interesting.
144 assert(LocationContextMap.count(call));
145 if (R->isInteresting(LocationContextMap[call])) {
146 containsSomethingInteresting = true;
147 break;
148 }
Ted Kremenek72516742012-03-01 00:05:06 +0000149 // Recursively clean out the subclass. Keep this call around if
150 // it contains any informative diagnostics.
Ted Kremeneka43df952012-09-21 00:09:11 +0000151 PathDiagnosticCallPiece *NewCallWithLoc =
152 call->getLocation().asLocation().isValid()
153 ? call : CallWithLoc;
154
155 if (!RemoveUneededCalls(call->path, R, NewCallWithLoc))
Ted Kremenek72516742012-03-01 00:05:06 +0000156 continue;
Ted Kremeneka43df952012-09-21 00:09:11 +0000157
158 if (NewCallWithLoc == CallWithLoc && CallWithLoc) {
159 call->callEnter = CallWithLoc->callEnter;
160 }
161
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000162 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000163 break;
164 }
165 case PathDiagnosticPiece::Macro: {
166 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000167 if (!RemoveUneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000168 continue;
169 containsSomethingInteresting = true;
170 break;
171 }
172 case PathDiagnosticPiece::Event: {
173 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
Ted Kremeneka43df952012-09-21 00:09:11 +0000174
Ted Kremenek72516742012-03-01 00:05:06 +0000175 // We never throw away an event, but we do throw it away wholesale
176 // as part of a path if we throw the entire path away.
Ted Kremenek22505ef2012-09-08 07:18:18 +0000177 containsSomethingInteresting |= !event->isPrunable();
Ted Kremenek72516742012-03-01 00:05:06 +0000178 break;
179 }
180 case PathDiagnosticPiece::ControlFlow:
181 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000182 }
183
184 pieces.push_back(piece);
185 }
186
187 return containsSomethingInteresting;
188}
189
190//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000191// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000192//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000193
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000194typedef llvm::DenseMap<const ExplodedNode*,
195const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000196
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000197namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000198class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000199 NodeBackMap& M;
200public:
201 NodeMapClosure(NodeBackMap *m) : M(*m) {}
202 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek9c378f72011-08-12 23:37:29 +0000204 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000205 NodeBackMap::iterator I = M.find(N);
206 return I == M.end() ? 0 : I->second;
207 }
208};
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000210class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000211 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000212 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000213 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000214 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000215public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000216 const LocationContext *LC;
217
Ted Kremenek8966bc12009-05-06 21:39:49 +0000218 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000219 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000220 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000221 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000222 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
223 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek9c378f72011-08-12 23:37:29 +0000225 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek9c378f72011-08-12 23:37:29 +0000227 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
228 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Anna Zaks8e6431a2011-08-18 22:37:56 +0000230 BugReport *getBugReport() { return R; }
231
Tom Care212f6d32010-09-16 03:50:38 +0000232 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000233
234 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000236 const Stmt *getParent(const Stmt *S) {
237 return getParentMap().getParent(S);
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenek8966bc12009-05-06 21:39:49 +0000240 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000241
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000242 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
David Blaikieef3643f2011-09-26 00:51:36 +0000244 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
245 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000246 }
247
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000248 bool supportsLogicalOpControlFlow() const {
249 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000250 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000251};
252} // end anonymous namespace
253
Ted Kremenek00605e02009-03-27 20:55:39 +0000254PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000255PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000256 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000257 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000258
Anna Zaks0cd59482011-09-16 19:18:30 +0000259 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
260 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000261}
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenek00605e02009-03-27 20:55:39 +0000263PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000264PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
265 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000266
Ted Kremenek143ca222008-05-06 18:11:09 +0000267 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000268 if (os.str().empty())
269 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek00605e02009-03-27 20:55:39 +0000271 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Ted Kremenek00605e02009-03-27 20:55:39 +0000273 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000274 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000275 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000276 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000277 else {
278 os << "Execution jumps to the end of the ";
279 const Decl *D = N->getLocationContext()->getDecl();
280 if (isa<ObjCMethodDecl>(D))
281 os << "method";
282 else if (isa<FunctionDecl>(D))
283 os << "function";
284 else {
285 assert(isa<BlockDecl>(D));
286 os << "anonymous block";
287 }
288 os << '.';
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000291 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000292}
293
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000294static bool IsNested(const Stmt *S, ParentMap &PM) {
295 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
296 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000298 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000300 if (Parent)
301 switch (Parent->getStmtClass()) {
302 case Stmt::ForStmtClass:
303 case Stmt::DoStmtClass:
304 case Stmt::WhileStmtClass:
305 return true;
306 default:
307 break;
308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
310 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000311}
312
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000313PathDiagnosticLocation
314PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000315 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000316 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000317 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000318
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000319 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000320 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000322 if (!Parent)
323 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000325 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000326 case Stmt::BinaryOperatorClass: {
327 const BinaryOperator *B = cast<BinaryOperator>(Parent);
328 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000329 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000330 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000331 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000332 case Stmt::CompoundStmtClass:
333 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000335 case Stmt::ChooseExprClass:
336 // Similar to '?' if we are referring to condition, just have the edge
337 // point to the entire choose expression.
338 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000339 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000340 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000341 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000342 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000343 case Stmt::ConditionalOperatorClass:
344 // For '?', if we are referring to condition, just have the edge point
345 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000346 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000347 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000348 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000349 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000350 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000351 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000352 case Stmt::ForStmtClass:
353 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000354 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000355 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000356 case Stmt::IfStmtClass:
357 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000358 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000359 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000360 case Stmt::ObjCForCollectionStmtClass:
361 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000362 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000363 break;
364 case Stmt::WhileStmtClass:
365 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000366 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000367 break;
368 default:
369 break;
370 }
371
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000372 S = Parent;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000375 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000376
377 // Special case: DeclStmts can appear in for statement declarations, in which
378 // case the ForStmt is the context.
379 if (isa<DeclStmt>(S)) {
380 if (const Stmt *Parent = P.getParent(S)) {
381 switch (Parent->getStmtClass()) {
382 case Stmt::ForStmtClass:
383 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000384 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000385 default:
386 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000387 }
388 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000389 }
390 else if (isa<BinaryOperator>(S)) {
391 // Special case: the binary operator represents the initialization
392 // code in a for statement (this can happen when the variable being
393 // initialized is an old variable.
394 if (const ForStmt *FS =
395 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
396 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000397 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000398 }
399 }
400
Anna Zaks220ac8c2011-09-15 01:08:34 +0000401 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000402}
403
Ted Kremenekcf118d42009-02-04 23:49:09 +0000404//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000405// "Minimal" path diagnostic generation algorithm.
406//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000407typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
408typedef SmallVector<StackDiagPair, 6> StackDiagVector;
409
Anna Zaks368a0d52012-03-15 21:13:02 +0000410static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000411 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000412 // If the piece contains a special message, add it to all the call
413 // pieces on the active stack.
414 if (PathDiagnosticEventPiece *ep =
415 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000416
Anna Zaks56a938f2012-03-16 23:24:20 +0000417 if (ep->hasCallStackHint())
418 for (StackDiagVector::iterator I = CallStack.begin(),
419 E = CallStack.end(); I != E; ++I) {
420 PathDiagnosticCallPiece *CP = I->first;
421 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000422 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000423
Anna Zaks368a0d52012-03-15 21:13:02 +0000424 // The last message on the path to final bug is the most important
425 // one. Since we traverse the path backwards, do not add the message
426 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000427 if (!CP->hasCallStackMessage())
428 CP->setCallStackMessage(stackMsg);
429 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000430 }
431}
Ted Kremenek31061982009-03-31 23:00:32 +0000432
Ted Kremenek77d09442012-03-02 01:27:31 +0000433static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000434
Ted Kremenek31061982009-03-31 23:00:32 +0000435static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
436 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000437 const ExplodedNode *N,
438 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000441 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000442 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000443 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000444
Anna Zaks56a938f2012-03-16 23:24:20 +0000445 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000446
Ted Kremenek31061982009-03-31 23:00:32 +0000447 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000448 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000449 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000450 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek31061982009-03-31 23:00:32 +0000452 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000453
Anna Zaks80de4872012-08-29 21:22:37 +0000454 do {
455 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
456 PathDiagnosticCallPiece *C =
457 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
458 GRBugReporter& BR = PDB.getBugReporter();
459 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
460 PD.getActivePath().push_front(C);
461 PD.pushActivePath(&C->path);
462 CallStack.push_back(StackDiagPair(C, N));
463 break;
Anna Zaks93739372012-03-14 18:58:28 +0000464 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000465
Anna Zaks80de4872012-08-29 21:22:37 +0000466 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
467 // Flush all locations, and pop the active path.
468 bool VisitedEntireCall = PD.isWithinCall();
469 PD.popActivePath();
470
471 // Either we just added a bunch of stuff to the top-level path, or
472 // we have a previous CallExitEnd. If the former, it means that the
473 // path terminated within a function call. We must then take the
474 // current contents of the active path and place it within
475 // a new PathDiagnosticCallPiece.
476 PathDiagnosticCallPiece *C;
477 if (VisitedEntireCall) {
478 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
479 } else {
480 const Decl *Caller = CE->getLocationContext()->getDecl();
481 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
482 GRBugReporter& BR = PDB.getBugReporter();
483 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
484 }
485
486 C->setCallee(*CE, SMgr);
487 if (!CallStack.empty()) {
488 assert(CallStack.back().first == C);
489 CallStack.pop_back();
490 }
491 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anna Zaks80de4872012-08-29 21:22:37 +0000494 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
495 const CFGBlock *Src = BE->getSrc();
496 const CFGBlock *Dst = BE->getDst();
497 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Anna Zaks80de4872012-08-29 21:22:37 +0000499 if (!T)
500 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Anna Zaks80de4872012-08-29 21:22:37 +0000502 PathDiagnosticLocation Start =
503 PathDiagnosticLocation::createBegin(T, SMgr,
504 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Anna Zaks80de4872012-08-29 21:22:37 +0000506 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000507 default:
508 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Ted Kremenek31061982009-03-31 23:00:32 +0000510 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000511 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000512 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek31061982009-03-31 23:00:32 +0000514 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000515 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek31061982009-03-31 23:00:32 +0000517 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000518 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000519 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek31061982009-03-31 23:00:32 +0000521 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000522 << End.asLocation().getExpansionLineNumber();
523 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
524 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000525 break;
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
528 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000529 // Figure out what case arm we took.
530 std::string sbuf;
531 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek9c378f72011-08-12 23:37:29 +0000533 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000534 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek31061982009-03-31 23:00:32 +0000536 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000537 default:
538 os << "No cases match in the switch statement. "
539 "Control jumps to line "
540 << End.asLocation().getExpansionLineNumber();
541 break;
542 case Stmt::DefaultStmtClass:
543 os << "Control jumps to the 'default' case at line "
544 << End.asLocation().getExpansionLineNumber();
545 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Anna Zaks80de4872012-08-29 21:22:37 +0000547 case Stmt::CaseStmtClass: {
548 os << "Control jumps to 'case ";
549 const CaseStmt *Case = cast<CaseStmt>(S);
550 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Anna Zaks80de4872012-08-29 21:22:37 +0000552 // Determine if it is an enum.
553 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Anna Zaks80de4872012-08-29 21:22:37 +0000555 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
556 // FIXME: Maybe this should be an assertion. Are there cases
557 // were it is not an EnumConstantDecl?
558 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000559 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Anna Zaks80de4872012-08-29 21:22:37 +0000561 if (D) {
562 GetRawInt = false;
563 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000564 }
Ted Kremenek31061982009-03-31 23:00:32 +0000565 }
Anna Zaks80de4872012-08-29 21:22:37 +0000566
567 if (GetRawInt)
568 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
569
570 os << ":' at line "
571 << End.asLocation().getExpansionLineNumber();
572 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000573 }
Anna Zaks80de4872012-08-29 21:22:37 +0000574 }
575 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
576 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000577 }
578 else {
579 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000580 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000581 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
582 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 break;
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek31061982009-03-31 23:00:32 +0000588 case Stmt::BreakStmtClass:
589 case Stmt::ContinueStmtClass: {
590 std::string sbuf;
591 llvm::raw_string_ostream os(sbuf);
592 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000593 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
594 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000595 break;
596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Anna Zaks80de4872012-08-29 21:22:37 +0000598 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000599 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000600 case Stmt::ConditionalOperatorClass: {
601 std::string sbuf;
602 llvm::raw_string_ostream os(sbuf);
603 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenek31061982009-03-31 23:00:32 +0000605 if (*(Src->succ_begin()+1) == Dst)
606 os << "false";
607 else
608 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Ted Kremenek31061982009-03-31 23:00:32 +0000610 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenek31061982009-03-31 23:00:32 +0000612 if (const Stmt *S = End.asStmt())
613 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anna Zaks80de4872012-08-29 21:22:37 +0000615 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
616 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000617 break;
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anna Zaks80de4872012-08-29 21:22:37 +0000620 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000621 case Stmt::BinaryOperatorClass: {
622 if (!PDB.supportsLogicalOpControlFlow())
623 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000625 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000626 std::string sbuf;
627 llvm::raw_string_ostream os(sbuf);
628 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000629
John McCall2de56d12010-08-25 11:45:40 +0000630 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000631 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek31061982009-03-31 23:00:32 +0000633 if (*(Src->succ_begin()+1) == Dst) {
634 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000635 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000636 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000637 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
638 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
639 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000640 }
Ted Kremenek31061982009-03-31 23:00:32 +0000641 else {
642 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000643 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000644 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000645 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
646 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000647 }
Ted Kremenek31061982009-03-31 23:00:32 +0000648 }
649 else {
John McCall2de56d12010-08-25 11:45:40 +0000650 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000651 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 if (*(Src->succ_begin()+1) == Dst) {
654 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000655 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000656 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000657 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
658 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000659 }
660 else {
661 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000662 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000663 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000664 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
665 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
666 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000667 }
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 break;
671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
673 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000674 if (*(Src->succ_begin()) == Dst) {
675 std::string sbuf;
676 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Ted Kremenek31061982009-03-31 23:00:32 +0000678 os << "Loop condition is true. ";
679 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Ted Kremenek31061982009-03-31 23:00:32 +0000681 if (const Stmt *S = End.asStmt())
682 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Anna Zaks80de4872012-08-29 21:22:37 +0000684 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
685 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000686 }
687 else {
688 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek31061982009-03-31 23:00:32 +0000690 if (const Stmt *S = End.asStmt())
691 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Anna Zaks80de4872012-08-29 21:22:37 +0000693 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
694 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek31061982009-03-31 23:00:32 +0000697 break;
698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Ted Kremenek31061982009-03-31 23:00:32 +0000700 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000701 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000702 if (*(Src->succ_begin()+1) == Dst) {
703 std::string sbuf;
704 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Ted Kremenek31061982009-03-31 23:00:32 +0000706 os << "Loop condition is false. ";
707 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
708 if (const Stmt *S = End.asStmt())
709 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Anna Zaks80de4872012-08-29 21:22:37 +0000711 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
712 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000713 }
714 else {
715 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
716 if (const Stmt *S = End.asStmt())
717 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Anna Zaks80de4872012-08-29 21:22:37 +0000719 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
720 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek31061982009-03-31 23:00:32 +0000723 break;
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek31061982009-03-31 23:00:32 +0000726 case Stmt::IfStmtClass: {
727 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek31061982009-03-31 23:00:32 +0000729 if (const Stmt *S = End.asStmt())
730 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek31061982009-03-31 23:00:32 +0000732 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000733 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
734 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000735 else
Anna Zaks80de4872012-08-29 21:22:37 +0000736 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
737 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek31061982009-03-31 23:00:32 +0000739 break;
740 }
Anna Zaks80de4872012-08-29 21:22:37 +0000741 }
Ted Kremenek31061982009-03-31 23:00:32 +0000742 }
Anna Zaks80de4872012-08-29 21:22:37 +0000743 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000745 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000746 // Add diagnostic pieces from custom visitors.
747 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000748 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
749 E = visitors.end();
750 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000751 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000752 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000753 updateStackPiecesWithMessage(p, CallStack);
754 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000755 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000756 }
Ted Kremenek31061982009-03-31 23:00:32 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Ted Kremenek14856d72009-04-06 23:06:54 +0000759 // After constructing the full PathDiagnostic, do a pass over it to compact
760 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000761 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000762}
763
764//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000765// "Extensive" PathDiagnostic generation.
766//===----------------------------------------------------------------------===//
767
768static bool IsControlFlowExpr(const Stmt *S) {
769 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000771 if (!E)
772 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000773
774 E = E->IgnoreParenCasts();
775
John McCall56ca35d2011-02-17 10:25:35 +0000776 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000777 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000779 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
780 if (B->isLogicalOp())
781 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000782
783 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000784}
785
Ted Kremenek14856d72009-04-06 23:06:54 +0000786namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000787class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000788 bool IsDead;
789public:
790 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
791 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000792
793 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000794 bool isDead() const { return IsDead; }
795};
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000797class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000798 std::vector<ContextLocation> CLocs;
799 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000800 PathDiagnostic &PD;
801 PathDiagnosticBuilder &PDB;
802 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000804 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek14856d72009-04-06 23:06:54 +0000806 bool containsLocation(const PathDiagnosticLocation &Container,
807 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek14856d72009-04-06 23:06:54 +0000809 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek9650cf32009-05-11 21:42:34 +0000811 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
812 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000813 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000814 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000815 while (1) {
816 // Adjust the location for some expressions that are best referenced
817 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000818 switch (S->getStmtClass()) {
819 default:
820 break;
821 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000822 case Stmt::GenericSelectionExprClass:
823 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000824 firstCharOnly = true;
825 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000826 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000827 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000828 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000829 firstCharOnly = true;
830 continue;
831 case Stmt::ChooseExprClass:
832 S = cast<ChooseExpr>(S)->getCond();
833 firstCharOnly = true;
834 continue;
835 case Stmt::BinaryOperatorClass:
836 S = cast<BinaryOperator>(S)->getLHS();
837 firstCharOnly = true;
838 continue;
839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek9650cf32009-05-11 21:42:34 +0000841 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek9650cf32009-05-11 21:42:34 +0000844 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000845 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek9650cf32009-05-11 21:42:34 +0000848 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000849 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000850
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000851 return L;
852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek14856d72009-04-06 23:06:54 +0000854 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000855 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000856 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000857 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000858 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000859 CLocs.pop_back();
860 }
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenek14856d72009-04-06 23:06:54 +0000862public:
863 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
864 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Ted Kremeneka301a672009-04-22 18:16:20 +0000866 // If the PathDiagnostic already has pieces, add the enclosing statement
867 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000868 if (!PD.path.empty()) {
869 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek14856d72009-04-06 23:06:54 +0000871 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000872 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000873 }
874 }
875
876 ~EdgeBuilder() {
877 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000878
Ted Kremeneka301a672009-04-22 18:16:20 +0000879 // Finally, add an initial edge from the start location of the first
880 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000881 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000882 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000883 PDB.getSourceManager());
884 if (L.isValid())
885 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000886 }
887
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000888 void flushLocations() {
889 while (!CLocs.empty())
890 popLocation();
891 PrevLoc = PathDiagnosticLocation();
892 }
893
Ted Kremenek14856d72009-04-06 23:06:54 +0000894 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000896 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek14856d72009-04-06 23:06:54 +0000898 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +0000899 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000900 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000901};
Ted Kremenek14856d72009-04-06 23:06:54 +0000902} // end anonymous namespace
903
904
905PathDiagnosticLocation
906EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
907 if (const Stmt *S = L.asStmt()) {
908 if (IsControlFlowExpr(S))
909 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000910
911 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenek14856d72009-04-06 23:06:54 +0000914 return L;
915}
916
917bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
918 const PathDiagnosticLocation &Containee) {
919
920 if (Container == Containee)
921 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremenek14856d72009-04-06 23:06:54 +0000923 if (Container.asDecl())
924 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Ted Kremenek14856d72009-04-06 23:06:54 +0000926 if (const Stmt *S = Containee.asStmt())
927 if (const Stmt *ContainerS = Container.asStmt()) {
928 while (S) {
929 if (S == ContainerS)
930 return true;
931 S = PDB.getParent(S);
932 }
933 return false;
934 }
935
936 // Less accurate: compare using source ranges.
937 SourceRange ContainerR = Container.asRange();
938 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Ted Kremenek14856d72009-04-06 23:06:54 +0000940 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000941 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
942 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
943 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
944 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Chandler Carruth64211622011-07-25 21:09:52 +0000946 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
947 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
948 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
949 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenek14856d72009-04-06 23:06:54 +0000951 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000952 assert(ContaineeBegLine <= ContaineeEndLine);
953
Ted Kremenek14856d72009-04-06 23:06:54 +0000954 return (ContainerBegLine <= ContaineeBegLine &&
955 ContainerEndLine >= ContaineeEndLine &&
956 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000957 SM.getExpansionColumnNumber(ContainerRBeg) <=
958 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000959 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000960 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000961 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000962}
963
Ted Kremenek14856d72009-04-06 23:06:54 +0000964void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
965 if (!PrevLoc.isValid()) {
966 PrevLoc = NewLoc;
967 return;
968 }
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000970 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
971 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremeneka43df952012-09-21 00:09:11 +0000973 if (PrevLocClean.asLocation().isInvalid()) {
974 PrevLoc = NewLoc;
975 return;
976 }
977
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000978 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000979 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000982 if (NewLocClean.asLocation().getExpansionLoc() ==
983 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000984 return;
985
Ted Kremenek2042fc12012-02-24 06:00:00 +0000986 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000987 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000988}
989
990void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremeneka301a672009-04-22 18:16:20 +0000992 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
993 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Ted Kremenek14856d72009-04-06 23:06:54 +0000995 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
996
997 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000998 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek14856d72009-04-06 23:06:54 +00001000 // Is the top location context the same as the one for the new location?
1001 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001002 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001003 if (IsConsumedExpr(TopContextLoc) &&
1004 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001005 TopContextLoc.markDead();
1006
Ted Kremenek14856d72009-04-06 23:06:54 +00001007 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001008 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001009
1010 return;
1011 }
1012
1013 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001014 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001015 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001017 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001018 CLocs.push_back(ContextLocation(CLoc, true));
1019 return;
1020 }
1021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremenek14856d72009-04-06 23:06:54 +00001023 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001024 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 }
1026
1027 // Context does not contain the location. Flush it.
1028 popLocation();
1029 }
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001031 // If we reach here, there is no enclosing context. Just add the edge.
1032 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001033}
1034
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001035bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1036 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1037 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001039 return false;
1040}
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Ted Kremeneke1baed32009-05-05 23:13:38 +00001042void EdgeBuilder::addExtendedContext(const Stmt *S) {
1043 if (!S)
1044 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001045
1046 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001047 while (Parent) {
1048 if (isa<CompoundStmt>(Parent))
1049 Parent = PDB.getParent(Parent);
1050 else
1051 break;
1052 }
1053
1054 if (Parent) {
1055 switch (Parent->getStmtClass()) {
1056 case Stmt::DoStmtClass:
1057 case Stmt::ObjCAtSynchronizedStmtClass:
1058 addContext(Parent);
1059 default:
1060 break;
1061 }
1062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremeneke1baed32009-05-05 23:13:38 +00001064 addContext(S);
1065}
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremenek14856d72009-04-06 23:06:54 +00001067void EdgeBuilder::addContext(const Stmt *S) {
1068 if (!S)
1069 return;
1070
Ted Kremenek59950d32012-02-24 07:12:52 +00001071 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001072 addContext(L);
1073}
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Jordan Rose183ba8e2012-07-26 20:04:05 +00001075void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001076 while (!CLocs.empty()) {
1077 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1078
1079 // Is the top location context the same as the one for the new location?
1080 if (TopContextLoc == L)
1081 return;
1082
1083 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001084 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001085 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001086 }
1087
1088 // Context does not contain the location. Flush it.
1089 popLocation();
1090 }
1091
1092 CLocs.push_back(L);
1093}
1094
Ted Kremenek11abcec2012-05-02 00:31:29 +00001095// Cone-of-influence: support the reverse propagation of "interesting" symbols
1096// and values by tracing interesting calculations backwards through evaluated
1097// expressions along a path. This is probably overly complicated, but the idea
1098// is that if an expression computed an "interesting" value, the child
1099// expressions are are also likely to be "interesting" as well (which then
1100// propagates to the values they in turn compute). This reverse propagation
1101// is needed to track interesting correlations across function call boundaries,
1102// where formal arguments bind to actual arguments, etc. This is also needed
1103// because the constraint solver sometimes simplifies certain symbolic values
1104// into constants when appropriate, and this complicates reasoning about
1105// interesting values.
1106typedef llvm::DenseSet<const Expr *> InterestingExprs;
1107
1108static void reversePropagateIntererstingSymbols(BugReport &R,
1109 InterestingExprs &IE,
1110 const ProgramState *State,
1111 const Expr *Ex,
1112 const LocationContext *LCtx) {
1113 SVal V = State->getSVal(Ex, LCtx);
1114 if (!(R.isInteresting(V) || IE.count(Ex)))
1115 return;
1116
1117 switch (Ex->getStmtClass()) {
1118 default:
1119 if (!isa<CastExpr>(Ex))
1120 break;
1121 // Fall through.
1122 case Stmt::BinaryOperatorClass:
1123 case Stmt::UnaryOperatorClass: {
1124 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1125 CE = Ex->child_end();
1126 CI != CE; ++CI) {
1127 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1128 IE.insert(child);
1129 SVal ChildV = State->getSVal(child, LCtx);
1130 R.markInteresting(ChildV);
1131 }
1132 break;
1133 }
1134 }
1135 }
1136
1137 R.markInteresting(V);
1138}
1139
1140static void reversePropagateInterestingSymbols(BugReport &R,
1141 InterestingExprs &IE,
1142 const ProgramState *State,
1143 const LocationContext *CalleeCtx,
1144 const LocationContext *CallerCtx)
1145{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001146 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001147 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1148 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001149 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001150 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1151 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1152 PE = FD->param_end();
1153 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1154 for (; AI != AE && PI != PE; ++AI, ++PI) {
1155 if (const Expr *ArgE = *AI) {
1156 if (const ParmVarDecl *PD = *PI) {
1157 Loc LV = State->getLValue(PD, CalleeCtx);
1158 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1159 IE.insert(ArgE);
1160 }
1161 }
1162 }
1163 }
1164 }
1165}
1166
Ted Kremenek14856d72009-04-06 23:06:54 +00001167static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1168 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001169 const ExplodedNode *N,
1170 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001171 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001172 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001173 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001174 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001175
Ted Kremenek9c378f72011-08-12 23:37:29 +00001176 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001177 while (NextNode) {
1178 N = NextNode;
1179 NextNode = GetPredecessorNode(N);
1180 ProgramPoint P = N->getLocation();
1181
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001182 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001183 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1184 if (const Expr *Ex = PS->getStmtAs<Expr>())
1185 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1186 N->getState().getPtr(), Ex,
1187 N->getLocationContext());
1188 }
1189
Anna Zaks0b3ade82012-04-20 21:59:08 +00001190 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001191 const Stmt *S = CE->getCalleeContext()->getCallSite();
1192 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001193 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1194 N->getState().getPtr(), Ex,
1195 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001196 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001197
1198 PathDiagnosticCallPiece *C =
1199 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001200 GRBugReporter& BR = PDB.getBugReporter();
1201 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001202
1203 EB.addEdge(C->callReturn, true);
1204 EB.flushLocations();
1205
1206 PD.getActivePath().push_front(C);
1207 PD.pushActivePath(&C->path);
1208 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001209 break;
1210 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001211
Ted Kremenek2042fc12012-02-24 06:00:00 +00001212 // Pop the call hierarchy if we are done walking the contents
1213 // of a function call.
1214 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001215 // Add an edge to the start of the function.
1216 const Decl *D = CE->getCalleeContext()->getDecl();
1217 PathDiagnosticLocation pos =
1218 PathDiagnosticLocation::createBegin(D, SM);
1219 EB.addEdge(pos);
1220
1221 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001222 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001223 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001224 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001225 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001226
Jordan Rose183ba8e2012-07-26 20:04:05 +00001227 // Either we just added a bunch of stuff to the top-level path, or
1228 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001229 // path terminated within a function call. We must then take the
1230 // current contents of the active path and place it within
1231 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001232 PathDiagnosticCallPiece *C;
1233 if (VisitedEntireCall) {
1234 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1235 } else {
1236 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001237 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001238 GRBugReporter& BR = PDB.getBugReporter();
1239 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001240 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001241
Jordan Rose183ba8e2012-07-26 20:04:05 +00001242 C->setCallee(*CE, SM);
1243 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001244
1245 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001246 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001247 CallStack.pop_back();
1248 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001249 break;
1250 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001251
1252 // Note that is important that we update the LocationContext
1253 // after looking at CallExits. CallExit basically adds an
1254 // edge in the *caller*, so we don't want to update the LocationContext
1255 // too soon.
1256 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001257
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001258 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001259 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1260 // Does this represent entering a call? If so, look at propagating
1261 // interesting symbols across call boundaries.
1262 if (NextNode) {
1263 const LocationContext *CallerCtx = NextNode->getLocationContext();
1264 const LocationContext *CalleeCtx = PDB.LC;
1265 if (CallerCtx != CalleeCtx) {
1266 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1267 N->getState().getPtr(),
1268 CalleeCtx, CallerCtx);
1269 }
1270 }
1271
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001272 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001273 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001274 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001275 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001277 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1278 CS = dyn_cast<CompoundStmt>(FS->getBody());
1279 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1280 CS = dyn_cast<CompoundStmt>(WS->getBody());
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001282 PathDiagnosticEventPiece *p =
1283 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001284 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001285 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001287 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001288 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001290 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001291 PathDiagnosticLocation BL =
1292 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001293 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001294 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001295 }
Ted Kremenekf57a2aa2012-09-12 06:22:18 +00001296
1297 if (const Stmt *Term = BE->getSrc()->getTerminator())
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001298 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001300 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001301 }
1302
Mike Stump1eb44332009-09-09 15:08:12 +00001303 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Jordan Rose1a7bcc42012-09-12 22:48:08 +00001304 CFGElement First = BE->getFirstElement();
1305 if (const CFGStmt *S = First.getAs<CFGStmt>()) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001306 const Stmt *stmt = S->getStmt();
1307 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001308 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001309 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001310 }
1311 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001312 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001313 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001314
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001315 break;
1316 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001317
1318
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001319 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001321 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001322 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Anna Zaks8e6431a2011-08-18 22:37:56 +00001324 // Add pieces from custom visitors.
1325 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001326 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1327 E = visitors.end();
1328 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001329 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001330 const PathDiagnosticLocation &Loc = p->getLocation();
1331 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001332 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001333 updateStackPiecesWithMessage(p, CallStack);
1334
Ted Kremenek8966bc12009-05-06 21:39:49 +00001335 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001336 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001337 }
Mike Stump1eb44332009-09-09 15:08:12 +00001338 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001339 }
1340}
1341
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001342//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001343// Methods for BugType and subclasses.
1344//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001345BugType::~BugType() { }
1346
Ted Kremenekcf118d42009-02-04 23:49:09 +00001347void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001348
David Blaikie99ba9e32011-12-20 02:48:34 +00001349void BuiltinBug::anchor() {}
1350
Ted Kremenekcf118d42009-02-04 23:49:09 +00001351//===----------------------------------------------------------------------===//
1352// Methods for BugReport and subclasses.
1353//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001354
David Blaikie99ba9e32011-12-20 02:48:34 +00001355void BugReport::NodeResolver::anchor() {}
1356
Anna Zaks8e6431a2011-08-18 22:37:56 +00001357void BugReport::addVisitor(BugReporterVisitor* visitor) {
1358 if (!visitor)
1359 return;
1360
1361 llvm::FoldingSetNodeID ID;
1362 visitor->Profile(ID);
1363 void *InsertPos;
1364
1365 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1366 delete visitor;
1367 return;
1368 }
1369
1370 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001371 Callbacks.push_back(visitor);
1372 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001373}
1374
1375BugReport::~BugReport() {
1376 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001377 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001378 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001379 while (!interestingSymbols.empty()) {
1380 popInterestingSymbolsAndRegions();
1381 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001382}
Anna Zakse172e8b2011-08-17 23:00:25 +00001383
Ted Kremenek07189522012-04-04 18:11:35 +00001384const Decl *BugReport::getDeclWithIssue() const {
1385 if (DeclWithIssue)
1386 return DeclWithIssue;
1387
1388 const ExplodedNode *N = getErrorNode();
1389 if (!N)
1390 return 0;
1391
1392 const LocationContext *LC = N->getLocationContext();
1393 return LC->getCurrentStackFrame()->getDecl();
1394}
1395
Anna Zakse172e8b2011-08-17 23:00:25 +00001396void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1397 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001398 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001399 if (UniqueingLocation.isValid()) {
1400 UniqueingLocation.Profile(hash);
1401 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001402 Location.Profile(hash);
1403 } else {
1404 assert(ErrorNode);
1405 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1406 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001407
1408 for (SmallVectorImpl<SourceRange>::const_iterator I =
1409 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1410 const SourceRange range = *I;
1411 if (!range.isValid())
1412 continue;
1413 hash.AddInteger(range.getBegin().getRawEncoding());
1414 hash.AddInteger(range.getEnd().getRawEncoding());
1415 }
1416}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001417
Ted Kremenek76aadc32012-03-09 01:13:14 +00001418void BugReport::markInteresting(SymbolRef sym) {
1419 if (!sym)
1420 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001421
1422 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001423 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001424 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001425
1426 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001427 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001428}
1429
1430void BugReport::markInteresting(const MemRegion *R) {
1431 if (!R)
1432 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001433
1434 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001435 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001436 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001437 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001438
Ted Kremenek76aadc32012-03-09 01:13:14 +00001439 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001440 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001441}
1442
1443void BugReport::markInteresting(SVal V) {
1444 markInteresting(V.getAsRegion());
1445 markInteresting(V.getAsSymbol());
1446}
1447
Anna Zaks80de4872012-08-29 21:22:37 +00001448void BugReport::markInteresting(const LocationContext *LC) {
1449 if (!LC)
1450 return;
1451 InterestingLocationContexts.insert(LC);
1452}
1453
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001454bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001455 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1456}
1457
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001458bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001459 if (!sym)
1460 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001461 // We don't currently consider metadata symbols to be interesting
1462 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001463 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001464}
1465
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001466bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001467 if (!R)
1468 return false;
1469 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001470 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001471 if (b)
1472 return true;
1473 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001474 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001475 return false;
1476}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001477
Anna Zaks80de4872012-08-29 21:22:37 +00001478bool BugReport::isInteresting(const LocationContext *LC) {
1479 if (!LC)
1480 return false;
1481 return InterestingLocationContexts.count(LC);
1482}
1483
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001484void BugReport::lazyInitializeInterestingSets() {
1485 if (interestingSymbols.empty()) {
1486 interestingSymbols.push_back(new Symbols());
1487 interestingRegions.push_back(new Regions());
1488 }
1489}
1490
1491BugReport::Symbols &BugReport::getInterestingSymbols() {
1492 lazyInitializeInterestingSets();
1493 return *interestingSymbols.back();
1494}
1495
1496BugReport::Regions &BugReport::getInterestingRegions() {
1497 lazyInitializeInterestingSets();
1498 return *interestingRegions.back();
1499}
1500
1501void BugReport::pushInterestingSymbolsAndRegions() {
1502 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1503 interestingRegions.push_back(new Regions(getInterestingRegions()));
1504}
1505
1506void BugReport::popInterestingSymbolsAndRegions() {
1507 delete interestingSymbols.back();
1508 interestingSymbols.pop_back();
1509 delete interestingRegions.back();
1510 interestingRegions.pop_back();
1511}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001512
Ted Kremenek9c378f72011-08-12 23:37:29 +00001513const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001514 if (!ErrorNode)
1515 return 0;
1516
Tom Care212f6d32010-09-16 03:50:38 +00001517 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001518 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Ted Kremenek9c378f72011-08-12 23:37:29 +00001520 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001521 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001522 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001523 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001524 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001525 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001526 S = GetStmt(ProgP);
1527
1528 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001529}
1530
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001531std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001532BugReport::getRanges() {
1533 // If no custom ranges, add the range of the statement corresponding to
1534 // the error node.
1535 if (Ranges.empty()) {
1536 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1537 addRange(E->getSourceRange());
1538 else
1539 return std::make_pair(ranges_iterator(), ranges_iterator());
1540 }
1541
Anna Zaks14924262011-08-24 20:31:06 +00001542 // User-specified absence of range info.
1543 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1544 return std::make_pair(ranges_iterator(), ranges_iterator());
1545
Anna Zakse172e8b2011-08-17 23:00:25 +00001546 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001547}
1548
Anna Zaks590dd8e2011-09-20 21:38:35 +00001549PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001550 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001551 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001552 "Either Location or ErrorNode should be specified but not both.");
1553
Ted Kremenek9c378f72011-08-12 23:37:29 +00001554 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001555 const LocationContext *LC = ErrorNode->getLocationContext();
1556
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001557 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001558 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001559 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001560 // For binary operators, return the location of the operator.
1561 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001562 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001563
Anna Zaks590dd8e2011-09-20 21:38:35 +00001564 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001565 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001566 } else {
1567 assert(Location.isValid());
1568 return Location;
1569 }
1570
Anna Zaks590dd8e2011-09-20 21:38:35 +00001571 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001572}
1573
Ted Kremenekcf118d42009-02-04 23:49:09 +00001574//===----------------------------------------------------------------------===//
1575// Methods for BugReporter and subclasses.
1576//===----------------------------------------------------------------------===//
1577
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001578BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001579GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001580BugReporterData::~BugReporterData() {}
1581
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001582ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001583
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001584ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001585GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1586
Anna Zaks3b030a22011-08-19 01:57:09 +00001587BugReporter::~BugReporter() {
1588 FlushReports();
1589
1590 // Free the bug reports we are tracking.
1591 typedef std::vector<BugReportEquivClass *> ContTy;
1592 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1593 I != E; ++I) {
1594 delete *I;
1595 }
1596}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001597
1598void BugReporter::FlushReports() {
1599 if (BugTypes.isEmpty())
1600 return;
1601
1602 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001603 // warnings and new BugTypes.
1604 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1605 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001606 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001607 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001608 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001609 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001610 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001611 const_cast<BugType*>(*I)->FlushReports(*this);
1612
Anna Zaksd015f4f2012-08-02 23:41:05 +00001613 // We need to flush reports in deterministic order to ensure the order
1614 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001615 typedef std::vector<BugReportEquivClass *> ContVecTy;
1616 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1617 EI != EE; ++EI){
1618 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001619 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001620 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001621
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001622 // BugReporter owns and deletes only BugTypes created implicitly through
1623 // EmitBasicReport.
1624 // FIXME: There are leaks from checkers that assume that the BugTypes they
1625 // create will be destroyed by the BugReporter.
1626 for (llvm::StringMap<BugType*>::iterator
1627 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1628 delete I->second;
1629
Ted Kremenekcf118d42009-02-04 23:49:09 +00001630 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001631 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001632}
1633
1634//===----------------------------------------------------------------------===//
1635// PathDiagnostics generation.
1636//===----------------------------------------------------------------------===//
1637
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001638static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001639 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001640MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001641 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Ted Kremenekcf118d42009-02-04 23:49:09 +00001643 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001644 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001645 // error node unless there are two or more error nodes with the same minimum
1646 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001647 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001648 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001649
1650 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001651 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1652 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Ted Kremenekcf118d42009-02-04 23:49:09 +00001654 // Create owning pointers for GTrim and NMap just to ensure that they are
1655 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001656 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1657 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenekcf118d42009-02-04 23:49:09 +00001659 // Find the (first) error node in the trimmed graph. We just need to consult
1660 // the node map (NMap) which maps from nodes in the original graph to nodes
1661 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001662
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001663 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001664 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001665 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001666
Ted Kremenek40406fe2010-12-03 06:52:30 +00001667 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1668 const ExplodedNode *originalNode = nodes[nodeIndex];
1669 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001670 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001671 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001672 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001673 }
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Ted Kremenek938332c2009-05-16 01:11:58 +00001675 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001676
1677 // Create a new (third!) graph with a single path. This is the graph
1678 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001679 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Ted Kremenek10aa5542009-03-12 23:41:59 +00001681 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001682 // to the root node, and then construct a new graph that contains only
1683 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001684 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001686 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001687 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001689 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001690 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001691 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001693 if (Visited.find(Node) != Visited.end())
1694 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001696 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001698 if (Node->pred_empty()) {
1699 Root = Node;
1700 break;
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001703 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001704 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001705 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001706 }
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Ted Kremenek938332c2009-05-16 01:11:58 +00001708 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Ted Kremenek10aa5542009-03-12 23:41:59 +00001710 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001711 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001712 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001713 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001714 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001716 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001717 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001718 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001719 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001721 // Create the equivalent node in the new graph with the same state
1722 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001723 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001725 // Store the mapping to the original node.
1726 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1727 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001728 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001730 // Link up the new node with the previous node.
1731 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001732 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001734 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001736 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001737 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001738 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001739 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001740 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001741 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001742 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001743 }
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001745 // Find the next successor node. We choose the node that is marked
1746 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001747 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1748 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001749 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001751 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001753 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001755 if (I == Visited.end())
1756 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001758 if (!N || I->second < MinVal) {
1759 N = *SI;
1760 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001761 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001762 }
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Ted Kremenek938332c2009-05-16 01:11:58 +00001764 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001765 }
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Ted Kremenek938332c2009-05-16 01:11:58 +00001767 assert(First);
1768
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001769 return std::make_pair(std::make_pair(GNew, BM),
1770 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001771}
1772
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001773/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1774/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001775static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001776 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1777 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001779 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001780 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001782 MacroStackTy MacroStack;
1783 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Ted Kremenek77d09442012-03-02 01:27:31 +00001785 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001786 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001787
1788 PathDiagnosticPiece *piece = I->getPtr();
1789
1790 // Recursively compact calls.
1791 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1792 CompactPathDiagnostic(call->path, SM);
1793 }
1794
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001795 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001796 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001798 // Determine the instantiation location, which is the location we group
1799 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001800 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001801 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001802 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001804 if (Loc.isFileID()) {
1805 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001806 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001807 continue;
1808 }
1809
1810 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001812 // Is the PathDiagnosticPiece within the same macro group?
1813 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001814 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001815 continue;
1816 }
1817
1818 // We aren't in the same group. Are we descending into a new macro
1819 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001820 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001821
1822 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001823 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001824 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001826 // Walk the entire macro stack.
1827 while (!MacroStack.empty()) {
1828 if (InstantiationLoc == MacroStack.back().second) {
1829 MacroGroup = MacroStack.back().first;
1830 break;
1831 }
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001833 if (ParentInstantiationLoc == MacroStack.back().second) {
1834 MacroGroup = MacroStack.back().first;
1835 break;
1836 }
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001838 MacroStack.pop_back();
1839 }
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001841 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1842 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001843 PathDiagnosticMacroPiece *NewGroup =
1844 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001845 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001846
1847 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001848 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001849 else {
1850 assert(InstantiationLoc.isFileID());
1851 Pieces.push_back(NewGroup);
1852 }
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001854 MacroGroup = NewGroup;
1855 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1856 }
1857
1858 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001859 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001860 }
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001862 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001863 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Ted Kremenek77d09442012-03-02 01:27:31 +00001865 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1866 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001867}
1868
Ted Kremenek7dc86642009-03-31 20:22:36 +00001869void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001870 PathDiagnosticConsumer &PC,
1871 ArrayRef<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Ted Kremenek40406fe2010-12-03 06:52:30 +00001873 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001874 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001875 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1876 E = bugReports.end(); I != E; ++I) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001877 errorNodes.push_back((*I)->getErrorNode());
1878 }
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001880 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001881 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001882 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001883 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001884 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Ted Kremenekcf118d42009-02-04 23:49:09 +00001886 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001887 assert(GPair.second.second < bugReports.size());
1888 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001889 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001891 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1892 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001893 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001894
1895 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001896 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Anna Zaks8e6431a2011-08-18 22:37:56 +00001898 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001899 R->addVisitor(new NilReceiverBRVisitor());
1900 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001902 BugReport::VisitorList visitors;
1903 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001904
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001905 // While generating diagnostics, it's possible the visitors will decide
1906 // new symbols and regions are interesting, or add other visitors based on
1907 // the information they find. If they do, we need to regenerate the path
1908 // based on our new report configuration.
1909 do {
1910 // Get a clean copy of all the visitors.
1911 for (BugReport::visitor_iterator I = R->visitor_begin(),
1912 E = R->visitor_end(); I != E; ++I)
1913 visitors.push_back((*I)->clone());
1914
1915 // Clear out the active path from any previous work.
Jordan Rose3a46f5f2012-08-31 00:36:26 +00001916 PD.resetPath();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001917 originalReportConfigToken = R->getConfigurationChangeToken();
1918
1919 // Generate the very last diagnostic piece - the piece is visible before
1920 // the trace is expanded.
1921 PathDiagnosticPiece *LastPiece = 0;
1922 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1923 I != E; ++I) {
1924 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1925 assert (!LastPiece &&
1926 "There can only be one final piece in a diagnostic.");
1927 LastPiece = Piece;
1928 }
1929 }
1930 if (!LastPiece)
1931 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1932 if (LastPiece)
Jordan Rose3a46f5f2012-08-31 00:36:26 +00001933 PD.setEndOfPath(LastPiece);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001934 else
1935 return;
1936
1937 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001938 case PathDiagnosticConsumer::Extensive:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001939 GenerateExtensivePathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001940 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001941 case PathDiagnosticConsumer::Minimal:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001942 GenerateMinimalPathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek7dc86642009-03-31 20:22:36 +00001943 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001944 case PathDiagnosticConsumer::None:
1945 llvm_unreachable("PathDiagnosticConsumer::None should never appear here");
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001946 }
1947
1948 // Clean up the visitors we used.
1949 llvm::DeleteContainerPointers(visitors);
1950
1951 // Did anything change while generating this path?
1952 finalReportConfigToken = R->getConfigurationChangeToken();
1953 } while(finalReportConfigToken != originalReportConfigToken);
1954
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001955 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremeneked7948b2012-05-31 06:03:17 +00001956 if (R->shouldPrunePath()) {
Anna Zaks80de4872012-08-29 21:22:37 +00001957 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(), R);
Ted Kremeneked7948b2012-05-31 06:03:17 +00001958 assert(hasSomethingInteresting);
1959 (void) hasSomethingInteresting;
1960 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001961}
1962
Ted Kremenekcf118d42009-02-04 23:49:09 +00001963void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001964 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001965}
1966
Mike Stump1eb44332009-09-09 15:08:12 +00001967void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001968 // Compute the bug report's hash to determine its equivalence class.
1969 llvm::FoldingSetNodeID ID;
1970 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001971
1972 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001973 BugType& BT = R->getBugType();
1974 Register(&BT);
1975 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001976 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Ted Kremenekcf118d42009-02-04 23:49:09 +00001978 if (!EQ) {
1979 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001980 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001981 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001982 }
1983 else
1984 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001985}
1986
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001987
1988//===----------------------------------------------------------------------===//
1989// Emitting reports in equivalence classes.
1990//===----------------------------------------------------------------------===//
1991
1992namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001993struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001994 const ExplodedNode *N;
1995 ExplodedNode::const_succ_iterator I, E;
1996
1997 FRIEC_WLItem(const ExplodedNode *n)
1998 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1999};
2000}
2001
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002002static BugReport *
2003FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002004 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002005
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002006 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
2007 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002008 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002009
Ted Kremenek40406fe2010-12-03 06:52:30 +00002010 // If we don't need to suppress any of the nodes because they are
2011 // post-dominated by a sink, simply add all the nodes in the equivalence class
2012 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002013 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002014 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002015 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002016 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002017 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002018 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002019 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002020 }
2021 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002022 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002023 }
2024
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002025 // For bug reports that should be suppressed when all paths are post-dominated
2026 // by a sink node, iterate through the reports in the equivalence class
2027 // until we find one that isn't post-dominated (if one exists). We use a
2028 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2029 // this as a recursive function, but we don't want to risk blowing out the
2030 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002031 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002032
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002033 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002034 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002035
Ted Kremenek40406fe2010-12-03 06:52:30 +00002036 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002037 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002038 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002039 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002040 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002041 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002042 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002043 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002044 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002045 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002046 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002047 continue;
2048 }
2049
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002050 // At this point we know that 'N' is not a sink and it has at least one
2051 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2052 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002053 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002054 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2055
2056 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002057 WL.push_back(errorNode);
2058 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002059
2060 while (!WL.empty()) {
2061 WLItem &WI = WL.back();
2062 assert(!WI.N->succ_empty());
2063
2064 for (; WI.I != WI.E; ++WI.I) {
2065 const ExplodedNode *Succ = *WI.I;
2066 // End-of-path node?
2067 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002068 // If we found an end-of-path node that is not a sink.
2069 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002070 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002071 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002072 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002073 WL.clear();
2074 break;
2075 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002076 // Found a sink? Continue on to the next successor.
2077 continue;
2078 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002079 // Mark the successor as visited. If it hasn't been explored,
2080 // enqueue it to the DFS worklist.
2081 unsigned &mark = Visited[Succ];
2082 if (!mark) {
2083 mark = 1;
2084 WL.push_back(Succ);
2085 break;
2086 }
2087 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002088
2089 // The worklist may have been cleared at this point. First
2090 // check if it is empty before checking the last item.
2091 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002092 WL.pop_back();
2093 }
2094 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002095
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002096 // ExampleReport will be NULL if all the nodes in the equivalence class
2097 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002098 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002099}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002100
Ted Kremenekcf118d42009-02-04 23:49:09 +00002101void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002102 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002103 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002104 if (exampleReport) {
2105 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2106 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2107 E=C.end(); I != E; ++I) {
2108 FlushReport(exampleReport, **I, bugReports);
2109 }
2110 }
2111}
2112
2113void BugReporter::FlushReport(BugReport *exampleReport,
2114 PathDiagnosticConsumer &PD,
2115 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Ted Kremenekcf118d42009-02-04 23:49:09 +00002117 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002118 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002119 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002121 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002122 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2123 exampleReport->getBugType().getName(),
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002124 exampleReport->getDescription(),
2125 exampleReport->getShortDescription(/*Fallback=*/false),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002126 BT.getCategory()));
2127
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002128 // Generate the full path diagnostic, using the generation scheme
2129 // specified by the PathDiagnosticConsumer.
2130 if (PD.getGenerationScheme() != PathDiagnosticConsumer::None) {
2131 if (!bugReports.empty())
2132 GeneratePathDiagnostic(*D.get(), PD, bugReports);
2133 }
2134
2135 // If the path is empty, generate a single step path with the location
2136 // of the issue.
2137 if (D->path.empty()) {
2138 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2139 PathDiagnosticPiece *piece =
2140 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2141 BugReport::ranges_iterator Beg, End;
2142 llvm::tie(Beg, End) = exampleReport->getRanges();
2143 for ( ; Beg != End; ++Beg)
2144 piece->addRange(*Beg);
Jordan Rose3a46f5f2012-08-31 00:36:26 +00002145 D->setEndOfPath(piece);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002146 }
2147
Ted Kremenek072192b2008-04-30 23:47:44 +00002148 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002149 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002150 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2151 e = Meta.end(); i != e; ++i) {
2152 D->addMeta(*i);
2153 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002154
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002155 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002156}
Ted Kremenek57202072008-07-14 17:40:50 +00002157
Ted Kremenek07189522012-04-04 18:11:35 +00002158void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002159 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002160 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002161 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002162 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002164 // 'BT' is owned by BugReporter.
2165 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002166 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002167 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002168 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2169 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002170}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002171
Chris Lattner5f9e2722011-07-23 10:55:15 +00002172BugType *BugReporter::getBugTypeForName(StringRef name,
2173 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002174 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002175 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2176 llvm::StringMapEntry<BugType *> &
2177 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2178 BugType *BT = entry.getValue();
2179 if (!BT) {
2180 BT = new BugType(name, category);
2181 entry.setValue(BT);
2182 }
2183 return BT;
2184}