blob: 1866a27f728b5346631dfc0d6d32866b1f5441af [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.
Anna Zaks80de4872012-08-29 21:22:37 +0000124bool BugReporter::RemoveUneededCalls(PathPieces &pieces, BugReport *R) {
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000125 bool containsSomethingInteresting = false;
126 const unsigned N = pieces.size();
127
128 for (unsigned i = 0 ; i < N ; ++i) {
129 // Remove the front piece from the path. If it is still something we
130 // want to keep once we are done, we will push it back on the end.
131 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
132 pieces.pop_front();
133
Ted Kremenek72516742012-03-01 00:05:06 +0000134 switch (piece->getKind()) {
135 case PathDiagnosticPiece::Call: {
136 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000137 // Check if the location context is interesting.
138 assert(LocationContextMap.count(call));
139 if (R->isInteresting(LocationContextMap[call])) {
140 containsSomethingInteresting = true;
141 break;
142 }
Ted Kremenek72516742012-03-01 00:05:06 +0000143 // Recursively clean out the subclass. Keep this call around if
144 // it contains any informative diagnostics.
Anna Zaks80de4872012-08-29 21:22:37 +0000145 if (!RemoveUneededCalls(call->path, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000146 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000147 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000148 break;
149 }
150 case PathDiagnosticPiece::Macro: {
151 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
Anna Zaks80de4872012-08-29 21:22:37 +0000152 if (!RemoveUneededCalls(macro->subPieces, R))
Ted Kremenek72516742012-03-01 00:05:06 +0000153 continue;
154 containsSomethingInteresting = true;
155 break;
156 }
157 case PathDiagnosticPiece::Event: {
158 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
159 // We never throw away an event, but we do throw it away wholesale
160 // as part of a path if we throw the entire path away.
Ted Kremenek76aadc32012-03-09 01:13:14 +0000161 if (event->isPrunable())
162 continue;
163 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000164 break;
165 }
166 case PathDiagnosticPiece::ControlFlow:
167 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000168 }
169
170 pieces.push_back(piece);
171 }
172
173 return containsSomethingInteresting;
174}
175
176//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000177// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000178//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000179
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000180typedef llvm::DenseMap<const ExplodedNode*,
181const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000182
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000183namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000184class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000185 NodeBackMap& M;
186public:
187 NodeMapClosure(NodeBackMap *m) : M(*m) {}
188 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenek9c378f72011-08-12 23:37:29 +0000190 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000191 NodeBackMap::iterator I = M.find(N);
192 return I == M.end() ? 0 : I->second;
193 }
194};
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000196class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000197 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000198 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000199 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000200 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000201public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000202 const LocationContext *LC;
203
Ted Kremenek8966bc12009-05-06 21:39:49 +0000204 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000205 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000206 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000207 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000208 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
209 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek9c378f72011-08-12 23:37:29 +0000211 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek9c378f72011-08-12 23:37:29 +0000213 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
214 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Anna Zaks8e6431a2011-08-18 22:37:56 +0000216 BugReport *getBugReport() { return R; }
217
Tom Care212f6d32010-09-16 03:50:38 +0000218 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000219
220 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000222 const Stmt *getParent(const Stmt *S) {
223 return getParentMap().getParent(S);
224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremenek8966bc12009-05-06 21:39:49 +0000226 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000227
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000228 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
David Blaikieef3643f2011-09-26 00:51:36 +0000230 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
231 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000232 }
233
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000234 bool supportsLogicalOpControlFlow() const {
235 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000236 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000237};
238} // end anonymous namespace
239
Ted Kremenek00605e02009-03-27 20:55:39 +0000240PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000241PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000242 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000243 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000244
Anna Zaks0cd59482011-09-16 19:18:30 +0000245 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
246 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000247}
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek00605e02009-03-27 20:55:39 +0000249PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000250PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
251 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000252
Ted Kremenek143ca222008-05-06 18:11:09 +0000253 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000254 if (os.str().empty())
255 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Ted Kremenek00605e02009-03-27 20:55:39 +0000257 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenek00605e02009-03-27 20:55:39 +0000259 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000260 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000261 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000262 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000263 else {
264 os << "Execution jumps to the end of the ";
265 const Decl *D = N->getLocationContext()->getDecl();
266 if (isa<ObjCMethodDecl>(D))
267 os << "method";
268 else if (isa<FunctionDecl>(D))
269 os << "function";
270 else {
271 assert(isa<BlockDecl>(D));
272 os << "anonymous block";
273 }
274 os << '.';
275 }
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000277 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000278}
279
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000280static bool IsNested(const Stmt *S, ParentMap &PM) {
281 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
282 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000284 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000286 if (Parent)
287 switch (Parent->getStmtClass()) {
288 case Stmt::ForStmtClass:
289 case Stmt::DoStmtClass:
290 case Stmt::WhileStmtClass:
291 return true;
292 default:
293 break;
294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
296 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000297}
298
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000299PathDiagnosticLocation
300PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000301 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000302 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000303 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000304
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000305 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000306 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000308 if (!Parent)
309 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000311 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000312 case Stmt::BinaryOperatorClass: {
313 const BinaryOperator *B = cast<BinaryOperator>(Parent);
314 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000315 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000316 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000317 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000318 case Stmt::CompoundStmtClass:
319 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000320 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000321 case Stmt::ChooseExprClass:
322 // Similar to '?' if we are referring to condition, just have the edge
323 // point to the entire choose expression.
324 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000325 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000326 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000327 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000328 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000329 case Stmt::ConditionalOperatorClass:
330 // For '?', if we are referring to condition, just have the edge point
331 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000332 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000333 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000334 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000335 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000336 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000337 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000338 case Stmt::ForStmtClass:
339 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000340 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000341 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000342 case Stmt::IfStmtClass:
343 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000344 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000345 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000346 case Stmt::ObjCForCollectionStmtClass:
347 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000348 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000349 break;
350 case Stmt::WhileStmtClass:
351 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000352 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000353 break;
354 default:
355 break;
356 }
357
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000358 S = Parent;
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000361 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000362
363 // Special case: DeclStmts can appear in for statement declarations, in which
364 // case the ForStmt is the context.
365 if (isa<DeclStmt>(S)) {
366 if (const Stmt *Parent = P.getParent(S)) {
367 switch (Parent->getStmtClass()) {
368 case Stmt::ForStmtClass:
369 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000370 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000371 default:
372 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000373 }
374 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000375 }
376 else if (isa<BinaryOperator>(S)) {
377 // Special case: the binary operator represents the initialization
378 // code in a for statement (this can happen when the variable being
379 // initialized is an old variable.
380 if (const ForStmt *FS =
381 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
382 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000383 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000384 }
385 }
386
Anna Zaks220ac8c2011-09-15 01:08:34 +0000387 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000388}
389
Ted Kremenekcf118d42009-02-04 23:49:09 +0000390//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000391// "Minimal" path diagnostic generation algorithm.
392//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000393typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
394typedef SmallVector<StackDiagPair, 6> StackDiagVector;
395
Anna Zaks368a0d52012-03-15 21:13:02 +0000396static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000397 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000398 // If the piece contains a special message, add it to all the call
399 // pieces on the active stack.
400 if (PathDiagnosticEventPiece *ep =
401 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000402
Anna Zaks56a938f2012-03-16 23:24:20 +0000403 if (ep->hasCallStackHint())
404 for (StackDiagVector::iterator I = CallStack.begin(),
405 E = CallStack.end(); I != E; ++I) {
406 PathDiagnosticCallPiece *CP = I->first;
407 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000408 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000409
Anna Zaks368a0d52012-03-15 21:13:02 +0000410 // The last message on the path to final bug is the most important
411 // one. Since we traverse the path backwards, do not add the message
412 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000413 if (!CP->hasCallStackMessage())
414 CP->setCallStackMessage(stackMsg);
415 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000416 }
417}
Ted Kremenek31061982009-03-31 23:00:32 +0000418
Ted Kremenek77d09442012-03-02 01:27:31 +0000419static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000420
Ted Kremenek31061982009-03-31 23:00:32 +0000421static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
422 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000423 const ExplodedNode *N,
424 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000425
Ted Kremenek31061982009-03-31 23:00:32 +0000426 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000427 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000428 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000429 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000430
Anna Zaks56a938f2012-03-16 23:24:20 +0000431 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000432
Ted Kremenek31061982009-03-31 23:00:32 +0000433 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000434 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000435 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000436 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek31061982009-03-31 23:00:32 +0000438 ProgramPoint P = N->getLocation();
Jordan Rose183ba8e2012-07-26 20:04:05 +0000439
Anna Zaks80de4872012-08-29 21:22:37 +0000440 do {
441 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
442 PathDiagnosticCallPiece *C =
443 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
444 GRBugReporter& BR = PDB.getBugReporter();
445 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
446 PD.getActivePath().push_front(C);
447 PD.pushActivePath(&C->path);
448 CallStack.push_back(StackDiagPair(C, N));
449 break;
Anna Zaks93739372012-03-14 18:58:28 +0000450 }
Jordan Rose183ba8e2012-07-26 20:04:05 +0000451
Anna Zaks80de4872012-08-29 21:22:37 +0000452 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
453 // Flush all locations, and pop the active path.
454 bool VisitedEntireCall = PD.isWithinCall();
455 PD.popActivePath();
456
457 // Either we just added a bunch of stuff to the top-level path, or
458 // we have a previous CallExitEnd. If the former, it means that the
459 // path terminated within a function call. We must then take the
460 // current contents of the active path and place it within
461 // a new PathDiagnosticCallPiece.
462 PathDiagnosticCallPiece *C;
463 if (VisitedEntireCall) {
464 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
465 } else {
466 const Decl *Caller = CE->getLocationContext()->getDecl();
467 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
468 GRBugReporter& BR = PDB.getBugReporter();
469 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
470 }
471
472 C->setCallee(*CE, SMgr);
473 if (!CallStack.empty()) {
474 assert(CallStack.back().first == C);
475 CallStack.pop_back();
476 }
477 break;
Anna Zaks368a0d52012-03-15 21:13:02 +0000478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Anna Zaks80de4872012-08-29 21:22:37 +0000480 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
481 const CFGBlock *Src = BE->getSrc();
482 const CFGBlock *Dst = BE->getDst();
483 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anna Zaks80de4872012-08-29 21:22:37 +0000485 if (!T)
486 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anna Zaks80de4872012-08-29 21:22:37 +0000488 PathDiagnosticLocation Start =
489 PathDiagnosticLocation::createBegin(T, SMgr,
490 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anna Zaks80de4872012-08-29 21:22:37 +0000492 switch (T->getStmtClass()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000493 default:
494 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000497 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000498 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek31061982009-03-31 23:00:32 +0000500 if (!S)
Anna Zaks80de4872012-08-29 21:22:37 +0000501 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Ted Kremenek31061982009-03-31 23:00:32 +0000503 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000504 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000505 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek31061982009-03-31 23:00:32 +0000507 os << "Control jumps to line "
Anna Zaks80de4872012-08-29 21:22:37 +0000508 << End.asLocation().getExpansionLineNumber();
509 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
510 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000511 break;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
514 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000515 // Figure out what case arm we took.
516 std::string sbuf;
517 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Ted Kremenek9c378f72011-08-12 23:37:29 +0000519 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000520 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Ted Kremenek31061982009-03-31 23:00:32 +0000522 switch (S->getStmtClass()) {
Anna Zaks80de4872012-08-29 21:22:37 +0000523 default:
524 os << "No cases match in the switch statement. "
525 "Control jumps to line "
526 << End.asLocation().getExpansionLineNumber();
527 break;
528 case Stmt::DefaultStmtClass:
529 os << "Control jumps to the 'default' case at line "
530 << End.asLocation().getExpansionLineNumber();
531 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Anna Zaks80de4872012-08-29 21:22:37 +0000533 case Stmt::CaseStmtClass: {
534 os << "Control jumps to 'case ";
535 const CaseStmt *Case = cast<CaseStmt>(S);
536 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Anna Zaks80de4872012-08-29 21:22:37 +0000538 // Determine if it is an enum.
539 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Anna Zaks80de4872012-08-29 21:22:37 +0000541 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
542 // FIXME: Maybe this should be an assertion. Are there cases
543 // were it is not an EnumConstantDecl?
544 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000545 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Anna Zaks80de4872012-08-29 21:22:37 +0000547 if (D) {
548 GetRawInt = false;
549 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000550 }
Ted Kremenek31061982009-03-31 23:00:32 +0000551 }
Anna Zaks80de4872012-08-29 21:22:37 +0000552
553 if (GetRawInt)
554 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
555
556 os << ":' at line "
557 << End.asLocation().getExpansionLineNumber();
558 break;
Ted Kremenek31061982009-03-31 23:00:32 +0000559 }
Anna Zaks80de4872012-08-29 21:22:37 +0000560 }
561 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
562 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000563 }
564 else {
565 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000566 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000567 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
568 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Ted Kremenek31061982009-03-31 23:00:32 +0000571 break;
572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek31061982009-03-31 23:00:32 +0000574 case Stmt::BreakStmtClass:
575 case Stmt::ContinueStmtClass: {
576 std::string sbuf;
577 llvm::raw_string_ostream os(sbuf);
578 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Anna Zaks80de4872012-08-29 21:22:37 +0000579 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
580 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000581 break;
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Anna Zaks80de4872012-08-29 21:22:37 +0000584 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000585 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000586 case Stmt::ConditionalOperatorClass: {
587 std::string sbuf;
588 llvm::raw_string_ostream os(sbuf);
589 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Ted Kremenek31061982009-03-31 23:00:32 +0000591 if (*(Src->succ_begin()+1) == Dst)
592 os << "false";
593 else
594 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Ted Kremenek31061982009-03-31 23:00:32 +0000596 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremenek31061982009-03-31 23:00:32 +0000598 if (const Stmt *S = End.asStmt())
599 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Anna Zaks80de4872012-08-29 21:22:37 +0000601 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
602 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000603 break;
604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Anna Zaks80de4872012-08-29 21:22:37 +0000606 // Determine control-flow for short-circuited '&&' and '||'.
Ted Kremenek31061982009-03-31 23:00:32 +0000607 case Stmt::BinaryOperatorClass: {
608 if (!PDB.supportsLogicalOpControlFlow())
609 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000611 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000612 std::string sbuf;
613 llvm::raw_string_ostream os(sbuf);
614 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000615
John McCall2de56d12010-08-25 11:45:40 +0000616 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000617 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek31061982009-03-31 23:00:32 +0000619 if (*(Src->succ_begin()+1) == Dst) {
620 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000621 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000622 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000623 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
624 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
625 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000626 }
Ted Kremenek31061982009-03-31 23:00:32 +0000627 else {
628 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000629 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000630 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000631 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
632 Start, End, os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000633 }
Ted Kremenek31061982009-03-31 23:00:32 +0000634 }
635 else {
John McCall2de56d12010-08-25 11:45:40 +0000636 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000637 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 if (*(Src->succ_begin()+1) == Dst) {
640 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000641 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000642 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Anna Zaks80de4872012-08-29 21:22:37 +0000643 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
644 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000645 }
646 else {
647 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000648 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000649 PathDiagnosticLocation Start =
Anna Zaks80de4872012-08-29 21:22:37 +0000650 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
651 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
652 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000653 }
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek31061982009-03-31 23:00:32 +0000656 break;
657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
659 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000660 if (*(Src->succ_begin()) == Dst) {
661 std::string sbuf;
662 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenek31061982009-03-31 23:00:32 +0000664 os << "Loop condition is true. ";
665 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremenek31061982009-03-31 23:00:32 +0000667 if (const Stmt *S = End.asStmt())
668 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Anna Zaks80de4872012-08-29 21:22:37 +0000670 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
671 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000672 }
673 else {
674 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek31061982009-03-31 23:00:32 +0000676 if (const Stmt *S = End.asStmt())
677 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Anna Zaks80de4872012-08-29 21:22:37 +0000679 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
680 Start, End, "Loop condition is false. Exiting loop"));
Ted Kremenek31061982009-03-31 23:00:32 +0000681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremenek31061982009-03-31 23:00:32 +0000683 break;
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Ted Kremenek31061982009-03-31 23:00:32 +0000686 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000687 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000688 if (*(Src->succ_begin()+1) == Dst) {
689 std::string sbuf;
690 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenek31061982009-03-31 23:00:32 +0000692 os << "Loop condition is false. ";
693 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
694 if (const Stmt *S = End.asStmt())
695 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Anna Zaks80de4872012-08-29 21:22:37 +0000697 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
698 Start, End, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000699 }
700 else {
701 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
702 if (const Stmt *S = End.asStmt())
703 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Anna Zaks80de4872012-08-29 21:22:37 +0000705 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
706 Start, End, "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenek31061982009-03-31 23:00:32 +0000709 break;
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek31061982009-03-31 23:00:32 +0000712 case Stmt::IfStmtClass: {
713 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremenek31061982009-03-31 23:00:32 +0000715 if (const Stmt *S = End.asStmt())
716 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek31061982009-03-31 23:00:32 +0000718 if (*(Src->succ_begin()+1) == Dst)
Anna Zaks80de4872012-08-29 21:22:37 +0000719 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
720 Start, End, "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000721 else
Anna Zaks80de4872012-08-29 21:22:37 +0000722 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
723 Start, End, "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek31061982009-03-31 23:00:32 +0000725 break;
726 }
Anna Zaks80de4872012-08-29 21:22:37 +0000727 }
Ted Kremenek31061982009-03-31 23:00:32 +0000728 }
Anna Zaks80de4872012-08-29 21:22:37 +0000729 } while(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000731 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000732 // Add diagnostic pieces from custom visitors.
733 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000734 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
735 E = visitors.end();
736 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000737 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000738 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000739 updateStackPiecesWithMessage(p, CallStack);
740 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000741 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000742 }
Ted Kremenek31061982009-03-31 23:00:32 +0000743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek14856d72009-04-06 23:06:54 +0000745 // After constructing the full PathDiagnostic, do a pass over it to compact
746 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000747 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000748}
749
750//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000751// "Extensive" PathDiagnostic generation.
752//===----------------------------------------------------------------------===//
753
754static bool IsControlFlowExpr(const Stmt *S) {
755 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000757 if (!E)
758 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
760 E = E->IgnoreParenCasts();
761
John McCall56ca35d2011-02-17 10:25:35 +0000762 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000763 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000765 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
766 if (B->isLogicalOp())
767 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000768
769 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000770}
771
Ted Kremenek14856d72009-04-06 23:06:54 +0000772namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000773class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000774 bool IsDead;
775public:
776 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
777 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000778
779 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000780 bool isDead() const { return IsDead; }
781};
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000783class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000784 std::vector<ContextLocation> CLocs;
785 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000786 PathDiagnostic &PD;
787 PathDiagnosticBuilder &PDB;
788 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000790 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenek14856d72009-04-06 23:06:54 +0000792 bool containsLocation(const PathDiagnosticLocation &Container,
793 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek14856d72009-04-06 23:06:54 +0000795 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek9650cf32009-05-11 21:42:34 +0000797 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
798 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000799 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000800 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000801 while (1) {
802 // Adjust the location for some expressions that are best referenced
803 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000804 switch (S->getStmtClass()) {
805 default:
806 break;
807 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000808 case Stmt::GenericSelectionExprClass:
809 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000810 firstCharOnly = true;
811 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000812 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000813 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000814 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000815 firstCharOnly = true;
816 continue;
817 case Stmt::ChooseExprClass:
818 S = cast<ChooseExpr>(S)->getCond();
819 firstCharOnly = true;
820 continue;
821 case Stmt::BinaryOperatorClass:
822 S = cast<BinaryOperator>(S)->getLHS();
823 firstCharOnly = true;
824 continue;
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek9650cf32009-05-11 21:42:34 +0000827 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek9650cf32009-05-11 21:42:34 +0000830 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000831 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremenek9650cf32009-05-11 21:42:34 +0000834 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000835 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000836
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000837 return L;
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Ted Kremenek14856d72009-04-06 23:06:54 +0000840 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000841 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000842 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000843 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000844 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000845 CLocs.pop_back();
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek14856d72009-04-06 23:06:54 +0000848public:
849 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
850 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Ted Kremeneka301a672009-04-22 18:16:20 +0000852 // If the PathDiagnostic already has pieces, add the enclosing statement
853 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000854 if (!PD.path.empty()) {
855 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek14856d72009-04-06 23:06:54 +0000857 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000858 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000859 }
860 }
861
862 ~EdgeBuilder() {
863 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000864
Ted Kremeneka301a672009-04-22 18:16:20 +0000865 // Finally, add an initial edge from the start location of the first
866 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000867 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000868 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000869 PDB.getSourceManager());
870 if (L.isValid())
871 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000872 }
873
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000874 void flushLocations() {
875 while (!CLocs.empty())
876 popLocation();
877 PrevLoc = PathDiagnosticLocation();
878 }
879
Ted Kremenek14856d72009-04-06 23:06:54 +0000880 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000882 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek14856d72009-04-06 23:06:54 +0000884 void addContext(const Stmt *S);
Jordan Rose183ba8e2012-07-26 20:04:05 +0000885 void addContext(const PathDiagnosticLocation &L);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000886 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000887};
Ted Kremenek14856d72009-04-06 23:06:54 +0000888} // end anonymous namespace
889
890
891PathDiagnosticLocation
892EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
893 if (const Stmt *S = L.asStmt()) {
894 if (IsControlFlowExpr(S))
895 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000896
897 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek14856d72009-04-06 23:06:54 +0000900 return L;
901}
902
903bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
904 const PathDiagnosticLocation &Containee) {
905
906 if (Container == Containee)
907 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Ted Kremenek14856d72009-04-06 23:06:54 +0000909 if (Container.asDecl())
910 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Ted Kremenek14856d72009-04-06 23:06:54 +0000912 if (const Stmt *S = Containee.asStmt())
913 if (const Stmt *ContainerS = Container.asStmt()) {
914 while (S) {
915 if (S == ContainerS)
916 return true;
917 S = PDB.getParent(S);
918 }
919 return false;
920 }
921
922 // Less accurate: compare using source ranges.
923 SourceRange ContainerR = Container.asRange();
924 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Ted Kremenek14856d72009-04-06 23:06:54 +0000926 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000927 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
928 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
929 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
930 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chandler Carruth64211622011-07-25 21:09:52 +0000932 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
933 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
934 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
935 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Ted Kremenek14856d72009-04-06 23:06:54 +0000937 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000938 assert(ContaineeBegLine <= ContaineeEndLine);
939
Ted Kremenek14856d72009-04-06 23:06:54 +0000940 return (ContainerBegLine <= ContaineeBegLine &&
941 ContainerEndLine >= ContaineeEndLine &&
942 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000943 SM.getExpansionColumnNumber(ContainerRBeg) <=
944 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000945 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000946 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000947 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000948}
949
Ted Kremenek14856d72009-04-06 23:06:54 +0000950void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
951 if (!PrevLoc.isValid()) {
952 PrevLoc = NewLoc;
953 return;
954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000956 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
957 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000959 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000960 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Ted Kremenek14856d72009-04-06 23:06:54 +0000962 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000963 if (NewLocClean.asLocation().getExpansionLoc() ==
964 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000965 return;
966
Ted Kremenek2042fc12012-02-24 06:00:00 +0000967 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000968 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000969}
970
971void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremeneka301a672009-04-22 18:16:20 +0000973 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
974 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Ted Kremenek14856d72009-04-06 23:06:54 +0000976 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
977
978 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000979 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 // Is the top location context the same as the one for the new location?
982 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000983 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000984 if (IsConsumedExpr(TopContextLoc) &&
985 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000986 TopContextLoc.markDead();
987
Ted Kremenek14856d72009-04-06 23:06:54 +0000988 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000989 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000990
991 return;
992 }
993
994 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000995 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000996 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000998 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000999 CLocs.push_back(ContextLocation(CLoc, true));
1000 return;
1001 }
1002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001005 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 }
1007
1008 // Context does not contain the location. Flush it.
1009 popLocation();
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001012 // If we reach here, there is no enclosing context. Just add the edge.
1013 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001014}
1015
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001016bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1017 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1018 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001020 return false;
1021}
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremeneke1baed32009-05-05 23:13:38 +00001023void EdgeBuilder::addExtendedContext(const Stmt *S) {
1024 if (!S)
1025 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001026
1027 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001028 while (Parent) {
1029 if (isa<CompoundStmt>(Parent))
1030 Parent = PDB.getParent(Parent);
1031 else
1032 break;
1033 }
1034
1035 if (Parent) {
1036 switch (Parent->getStmtClass()) {
1037 case Stmt::DoStmtClass:
1038 case Stmt::ObjCAtSynchronizedStmtClass:
1039 addContext(Parent);
1040 default:
1041 break;
1042 }
1043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremeneke1baed32009-05-05 23:13:38 +00001045 addContext(S);
1046}
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek14856d72009-04-06 23:06:54 +00001048void EdgeBuilder::addContext(const Stmt *S) {
1049 if (!S)
1050 return;
1051
Ted Kremenek59950d32012-02-24 07:12:52 +00001052 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Jordan Rose183ba8e2012-07-26 20:04:05 +00001053 addContext(L);
1054}
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Jordan Rose183ba8e2012-07-26 20:04:05 +00001056void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001057 while (!CLocs.empty()) {
1058 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1059
1060 // Is the top location context the same as the one for the new location?
1061 if (TopContextLoc == L)
1062 return;
1063
1064 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001065 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001066 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001067 }
1068
1069 // Context does not contain the location. Flush it.
1070 popLocation();
1071 }
1072
1073 CLocs.push_back(L);
1074}
1075
Ted Kremenek11abcec2012-05-02 00:31:29 +00001076// Cone-of-influence: support the reverse propagation of "interesting" symbols
1077// and values by tracing interesting calculations backwards through evaluated
1078// expressions along a path. This is probably overly complicated, but the idea
1079// is that if an expression computed an "interesting" value, the child
1080// expressions are are also likely to be "interesting" as well (which then
1081// propagates to the values they in turn compute). This reverse propagation
1082// is needed to track interesting correlations across function call boundaries,
1083// where formal arguments bind to actual arguments, etc. This is also needed
1084// because the constraint solver sometimes simplifies certain symbolic values
1085// into constants when appropriate, and this complicates reasoning about
1086// interesting values.
1087typedef llvm::DenseSet<const Expr *> InterestingExprs;
1088
1089static void reversePropagateIntererstingSymbols(BugReport &R,
1090 InterestingExprs &IE,
1091 const ProgramState *State,
1092 const Expr *Ex,
1093 const LocationContext *LCtx) {
1094 SVal V = State->getSVal(Ex, LCtx);
1095 if (!(R.isInteresting(V) || IE.count(Ex)))
1096 return;
1097
1098 switch (Ex->getStmtClass()) {
1099 default:
1100 if (!isa<CastExpr>(Ex))
1101 break;
1102 // Fall through.
1103 case Stmt::BinaryOperatorClass:
1104 case Stmt::UnaryOperatorClass: {
1105 for (Stmt::const_child_iterator CI = Ex->child_begin(),
1106 CE = Ex->child_end();
1107 CI != CE; ++CI) {
1108 if (const Expr *child = dyn_cast_or_null<Expr>(*CI)) {
1109 IE.insert(child);
1110 SVal ChildV = State->getSVal(child, LCtx);
1111 R.markInteresting(ChildV);
1112 }
1113 break;
1114 }
1115 }
1116 }
1117
1118 R.markInteresting(V);
1119}
1120
1121static void reversePropagateInterestingSymbols(BugReport &R,
1122 InterestingExprs &IE,
1123 const ProgramState *State,
1124 const LocationContext *CalleeCtx,
1125 const LocationContext *CallerCtx)
1126{
Jordan Rose852aa0d2012-07-10 22:07:52 +00001127 // FIXME: Handle non-CallExpr-based CallEvents.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001128 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1129 const Stmt *CallSite = Callee->getCallSite();
Jordan Rose852aa0d2012-07-10 22:07:52 +00001130 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001131 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1132 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1133 PE = FD->param_end();
1134 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1135 for (; AI != AE && PI != PE; ++AI, ++PI) {
1136 if (const Expr *ArgE = *AI) {
1137 if (const ParmVarDecl *PD = *PI) {
1138 Loc LV = State->getLValue(PD, CalleeCtx);
1139 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1140 IE.insert(ArgE);
1141 }
1142 }
1143 }
1144 }
1145 }
1146}
1147
Ted Kremenek14856d72009-04-06 23:06:54 +00001148static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1149 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001150 const ExplodedNode *N,
1151 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001152 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001153 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001154 StackDiagVector CallStack;
Ted Kremenek11abcec2012-05-02 00:31:29 +00001155 InterestingExprs IE;
Ted Kremenek14856d72009-04-06 23:06:54 +00001156
Ted Kremenek9c378f72011-08-12 23:37:29 +00001157 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001158 while (NextNode) {
1159 N = NextNode;
1160 NextNode = GetPredecessorNode(N);
1161 ProgramPoint P = N->getLocation();
1162
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001163 do {
Ted Kremenek11abcec2012-05-02 00:31:29 +00001164 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
1165 if (const Expr *Ex = PS->getStmtAs<Expr>())
1166 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1167 N->getState().getPtr(), Ex,
1168 N->getLocationContext());
1169 }
1170
Anna Zaks0b3ade82012-04-20 21:59:08 +00001171 if (const CallExitEnd *CE = dyn_cast<CallExitEnd>(&P)) {
Jordan Rose183ba8e2012-07-26 20:04:05 +00001172 const Stmt *S = CE->getCalleeContext()->getCallSite();
1173 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
Jordan Rose852aa0d2012-07-10 22:07:52 +00001174 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1175 N->getState().getPtr(), Ex,
1176 N->getLocationContext());
Jordan Rose852aa0d2012-07-10 22:07:52 +00001177 }
Jordan Rose183ba8e2012-07-26 20:04:05 +00001178
1179 PathDiagnosticCallPiece *C =
1180 PathDiagnosticCallPiece::construct(N, *CE, SM);
Anna Zaks80de4872012-08-29 21:22:37 +00001181 GRBugReporter& BR = PDB.getBugReporter();
1182 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Jordan Rose183ba8e2012-07-26 20:04:05 +00001183
1184 EB.addEdge(C->callReturn, true);
1185 EB.flushLocations();
1186
1187 PD.getActivePath().push_front(C);
1188 PD.pushActivePath(&C->path);
1189 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001190 break;
1191 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001192
Ted Kremenek2042fc12012-02-24 06:00:00 +00001193 // Pop the call hierarchy if we are done walking the contents
1194 // of a function call.
1195 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001196 // Add an edge to the start of the function.
1197 const Decl *D = CE->getCalleeContext()->getDecl();
1198 PathDiagnosticLocation pos =
1199 PathDiagnosticLocation::createBegin(D, SM);
1200 EB.addEdge(pos);
1201
1202 // Flush all locations, and pop the active path.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001203 bool VisitedEntireCall = PD.isWithinCall();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001204 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001205 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001206 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001207
Jordan Rose183ba8e2012-07-26 20:04:05 +00001208 // Either we just added a bunch of stuff to the top-level path, or
1209 // we have a previous CallExitEnd. If the former, it means that the
Ted Kremenek2042fc12012-02-24 06:00:00 +00001210 // path terminated within a function call. We must then take the
1211 // current contents of the active path and place it within
1212 // a new PathDiagnosticCallPiece.
Jordan Rose183ba8e2012-07-26 20:04:05 +00001213 PathDiagnosticCallPiece *C;
1214 if (VisitedEntireCall) {
1215 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1216 } else {
1217 const Decl *Caller = CE->getLocationContext()->getDecl();
Anna Zaks93739372012-03-14 18:58:28 +00001218 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
Anna Zaks80de4872012-08-29 21:22:37 +00001219 GRBugReporter& BR = PDB.getBugReporter();
1220 BR.addCallPieceLocationContextPair(C, CE->getCalleeContext());
Anna Zaks93739372012-03-14 18:58:28 +00001221 }
Jordan Rose852aa0d2012-07-10 22:07:52 +00001222
Jordan Rose183ba8e2012-07-26 20:04:05 +00001223 C->setCallee(*CE, SM);
1224 EB.addContext(C->getLocation());
Anna Zaks368a0d52012-03-15 21:13:02 +00001225
1226 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001227 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001228 CallStack.pop_back();
1229 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001230 break;
1231 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001232
1233 // Note that is important that we update the LocationContext
1234 // after looking at CallExits. CallExit basically adds an
1235 // edge in the *caller*, so we don't want to update the LocationContext
1236 // too soon.
1237 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001238
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001239 // Block edges.
Ted Kremenek11abcec2012-05-02 00:31:29 +00001240 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1241 // Does this represent entering a call? If so, look at propagating
1242 // interesting symbols across call boundaries.
1243 if (NextNode) {
1244 const LocationContext *CallerCtx = NextNode->getLocationContext();
1245 const LocationContext *CalleeCtx = PDB.LC;
1246 if (CallerCtx != CalleeCtx) {
1247 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1248 N->getState().getPtr(),
1249 CalleeCtx, CallerCtx);
1250 }
1251 }
1252
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001253 const CFGBlock &Blk = *BE->getSrc();
1254 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001256 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001257 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001258 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001259 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001261 if (!Term) {
1262 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1263 CS = dyn_cast<CompoundStmt>(FS->getBody());
1264 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001265 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001268 PathDiagnosticEventPiece *p =
1269 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001270 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001271 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001273 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001274 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001276 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001277 PathDiagnosticLocation BL =
1278 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001279 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001280 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001283 if (Term)
1284 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001286 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001287 }
1288
Mike Stump1eb44332009-09-09 15:08:12 +00001289 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001290 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1291 const Stmt *stmt = S->getStmt();
1292 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001293 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001294 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001295 }
1296 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001297 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001298 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001299
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001300 break;
1301 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001302
1303
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001304 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001306 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001307 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Anna Zaks8e6431a2011-08-18 22:37:56 +00001309 // Add pieces from custom visitors.
1310 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001311 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1312 E = visitors.end();
1313 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001314 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001315 const PathDiagnosticLocation &Loc = p->getLocation();
1316 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001317 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001318 updateStackPiecesWithMessage(p, CallStack);
1319
Ted Kremenek8966bc12009-05-06 21:39:49 +00001320 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001321 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001324 }
1325}
1326
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001327//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001328// Methods for BugType and subclasses.
1329//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001330BugType::~BugType() { }
1331
Ted Kremenekcf118d42009-02-04 23:49:09 +00001332void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001333
David Blaikie99ba9e32011-12-20 02:48:34 +00001334void BuiltinBug::anchor() {}
1335
Ted Kremenekcf118d42009-02-04 23:49:09 +00001336//===----------------------------------------------------------------------===//
1337// Methods for BugReport and subclasses.
1338//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001339
David Blaikie99ba9e32011-12-20 02:48:34 +00001340void BugReport::NodeResolver::anchor() {}
1341
Anna Zaks8e6431a2011-08-18 22:37:56 +00001342void BugReport::addVisitor(BugReporterVisitor* visitor) {
1343 if (!visitor)
1344 return;
1345
1346 llvm::FoldingSetNodeID ID;
1347 visitor->Profile(ID);
1348 void *InsertPos;
1349
1350 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1351 delete visitor;
1352 return;
1353 }
1354
1355 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001356 Callbacks.push_back(visitor);
1357 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001358}
1359
1360BugReport::~BugReport() {
1361 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001362 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001363 }
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001364 while (!interestingSymbols.empty()) {
1365 popInterestingSymbolsAndRegions();
1366 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001367}
Anna Zakse172e8b2011-08-17 23:00:25 +00001368
Ted Kremenek07189522012-04-04 18:11:35 +00001369const Decl *BugReport::getDeclWithIssue() const {
1370 if (DeclWithIssue)
1371 return DeclWithIssue;
1372
1373 const ExplodedNode *N = getErrorNode();
1374 if (!N)
1375 return 0;
1376
1377 const LocationContext *LC = N->getLocationContext();
1378 return LC->getCurrentStackFrame()->getDecl();
1379}
1380
Anna Zakse172e8b2011-08-17 23:00:25 +00001381void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1382 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001383 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001384 if (UniqueingLocation.isValid()) {
1385 UniqueingLocation.Profile(hash);
1386 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001387 Location.Profile(hash);
1388 } else {
1389 assert(ErrorNode);
1390 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1391 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001392
1393 for (SmallVectorImpl<SourceRange>::const_iterator I =
1394 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1395 const SourceRange range = *I;
1396 if (!range.isValid())
1397 continue;
1398 hash.AddInteger(range.getBegin().getRawEncoding());
1399 hash.AddInteger(range.getEnd().getRawEncoding());
1400 }
1401}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001402
Ted Kremenek76aadc32012-03-09 01:13:14 +00001403void BugReport::markInteresting(SymbolRef sym) {
1404 if (!sym)
1405 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001406
1407 // If the symbol wasn't already in our set, note a configuration change.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001408 if (getInterestingSymbols().insert(sym).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001409 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001410
1411 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001412 getInterestingRegions().insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001413}
1414
1415void BugReport::markInteresting(const MemRegion *R) {
1416 if (!R)
1417 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001418
1419 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001420 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001421 if (getInterestingRegions().insert(R).second)
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001422 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001423
Ted Kremenek76aadc32012-03-09 01:13:14 +00001424 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001425 getInterestingSymbols().insert(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001426}
1427
1428void BugReport::markInteresting(SVal V) {
1429 markInteresting(V.getAsRegion());
1430 markInteresting(V.getAsSymbol());
1431}
1432
Anna Zaks80de4872012-08-29 21:22:37 +00001433void BugReport::markInteresting(const LocationContext *LC) {
1434 if (!LC)
1435 return;
1436 InterestingLocationContexts.insert(LC);
1437}
1438
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001439bool BugReport::isInteresting(SVal V) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001440 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1441}
1442
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001443bool BugReport::isInteresting(SymbolRef sym) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001444 if (!sym)
1445 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001446 // We don't currently consider metadata symbols to be interesting
1447 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001448 return getInterestingSymbols().count(sym);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001449}
1450
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001451bool BugReport::isInteresting(const MemRegion *R) {
Ted Kremenek76aadc32012-03-09 01:13:14 +00001452 if (!R)
1453 return false;
1454 R = R->getBaseRegion();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001455 bool b = getInterestingRegions().count(R);
Ted Kremenek76aadc32012-03-09 01:13:14 +00001456 if (b)
1457 return true;
1458 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001459 return getInterestingSymbols().count(SR->getSymbol());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001460 return false;
1461}
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001462
Anna Zaks80de4872012-08-29 21:22:37 +00001463bool BugReport::isInteresting(const LocationContext *LC) {
1464 if (!LC)
1465 return false;
1466 return InterestingLocationContexts.count(LC);
1467}
1468
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001469void BugReport::lazyInitializeInterestingSets() {
1470 if (interestingSymbols.empty()) {
1471 interestingSymbols.push_back(new Symbols());
1472 interestingRegions.push_back(new Regions());
1473 }
1474}
1475
1476BugReport::Symbols &BugReport::getInterestingSymbols() {
1477 lazyInitializeInterestingSets();
1478 return *interestingSymbols.back();
1479}
1480
1481BugReport::Regions &BugReport::getInterestingRegions() {
1482 lazyInitializeInterestingSets();
1483 return *interestingRegions.back();
1484}
1485
1486void BugReport::pushInterestingSymbolsAndRegions() {
1487 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
1488 interestingRegions.push_back(new Regions(getInterestingRegions()));
1489}
1490
1491void BugReport::popInterestingSymbolsAndRegions() {
1492 delete interestingSymbols.back();
1493 interestingSymbols.pop_back();
1494 delete interestingRegions.back();
1495 interestingRegions.pop_back();
1496}
Ted Kremenek76aadc32012-03-09 01:13:14 +00001497
Ted Kremenek9c378f72011-08-12 23:37:29 +00001498const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001499 if (!ErrorNode)
1500 return 0;
1501
Tom Care212f6d32010-09-16 03:50:38 +00001502 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001503 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Ted Kremenek9c378f72011-08-12 23:37:29 +00001505 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001506 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001507 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001508 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001509 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001510 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001511 S = GetStmt(ProgP);
1512
1513 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001514}
1515
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001516std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001517BugReport::getRanges() {
1518 // If no custom ranges, add the range of the statement corresponding to
1519 // the error node.
1520 if (Ranges.empty()) {
1521 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1522 addRange(E->getSourceRange());
1523 else
1524 return std::make_pair(ranges_iterator(), ranges_iterator());
1525 }
1526
Anna Zaks14924262011-08-24 20:31:06 +00001527 // User-specified absence of range info.
1528 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1529 return std::make_pair(ranges_iterator(), ranges_iterator());
1530
Anna Zakse172e8b2011-08-17 23:00:25 +00001531 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001532}
1533
Anna Zaks590dd8e2011-09-20 21:38:35 +00001534PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001535 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001536 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001537 "Either Location or ErrorNode should be specified but not both.");
1538
Ted Kremenek9c378f72011-08-12 23:37:29 +00001539 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001540 const LocationContext *LC = ErrorNode->getLocationContext();
1541
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001542 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001543 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001544 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001545 // For binary operators, return the location of the operator.
1546 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001547 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001548
Anna Zaks590dd8e2011-09-20 21:38:35 +00001549 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001550 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001551 } else {
1552 assert(Location.isValid());
1553 return Location;
1554 }
1555
Anna Zaks590dd8e2011-09-20 21:38:35 +00001556 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001557}
1558
Ted Kremenekcf118d42009-02-04 23:49:09 +00001559//===----------------------------------------------------------------------===//
1560// Methods for BugReporter and subclasses.
1561//===----------------------------------------------------------------------===//
1562
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001563BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001564GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001565BugReporterData::~BugReporterData() {}
1566
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001567ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001568
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001569ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001570GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1571
Anna Zaks3b030a22011-08-19 01:57:09 +00001572BugReporter::~BugReporter() {
1573 FlushReports();
1574
1575 // Free the bug reports we are tracking.
1576 typedef std::vector<BugReportEquivClass *> ContTy;
1577 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1578 I != E; ++I) {
1579 delete *I;
1580 }
1581}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001582
1583void BugReporter::FlushReports() {
1584 if (BugTypes.isEmpty())
1585 return;
1586
1587 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001588 // warnings and new BugTypes.
1589 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1590 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001591 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001592 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001593 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001594 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001595 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001596 const_cast<BugType*>(*I)->FlushReports(*this);
1597
Anna Zaksd015f4f2012-08-02 23:41:05 +00001598 // We need to flush reports in deterministic order to ensure the order
1599 // of the reports is consistent between runs.
Anna Zaks0eb6c372012-08-02 00:41:43 +00001600 typedef std::vector<BugReportEquivClass *> ContVecTy;
1601 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
1602 EI != EE; ++EI){
1603 BugReportEquivClass& EQ = **EI;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001604 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001605 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001606
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001607 // BugReporter owns and deletes only BugTypes created implicitly through
1608 // EmitBasicReport.
1609 // FIXME: There are leaks from checkers that assume that the BugTypes they
1610 // create will be destroyed by the BugReporter.
1611 for (llvm::StringMap<BugType*>::iterator
1612 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1613 delete I->second;
1614
Ted Kremenekcf118d42009-02-04 23:49:09 +00001615 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001616 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001617}
1618
1619//===----------------------------------------------------------------------===//
1620// PathDiagnostics generation.
1621//===----------------------------------------------------------------------===//
1622
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001623static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001624 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001625MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001626 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Ted Kremenekcf118d42009-02-04 23:49:09 +00001628 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001629 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001630 // error node unless there are two or more error nodes with the same minimum
1631 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001632 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001633 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001634
1635 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001636 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1637 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Ted Kremenekcf118d42009-02-04 23:49:09 +00001639 // Create owning pointers for GTrim and NMap just to ensure that they are
1640 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001641 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1642 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Ted Kremenekcf118d42009-02-04 23:49:09 +00001644 // Find the (first) error node in the trimmed graph. We just need to consult
1645 // the node map (NMap) which maps from nodes in the original graph to nodes
1646 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001647
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001648 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001649 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001650 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001651
Ted Kremenek40406fe2010-12-03 06:52:30 +00001652 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1653 const ExplodedNode *originalNode = nodes[nodeIndex];
1654 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001655 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001656 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001657 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001658 }
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Ted Kremenek938332c2009-05-16 01:11:58 +00001660 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001661
1662 // Create a new (third!) graph with a single path. This is the graph
1663 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001664 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Ted Kremenek10aa5542009-03-12 23:41:59 +00001666 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001667 // to the root node, and then construct a new graph that contains only
1668 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001669 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001671 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001672 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001674 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001675 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001676 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001678 if (Visited.find(Node) != Visited.end())
1679 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001681 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001683 if (Node->pred_empty()) {
1684 Root = Node;
1685 break;
1686 }
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001688 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001689 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001690 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001691 }
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Ted Kremenek938332c2009-05-16 01:11:58 +00001693 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Ted Kremenek10aa5542009-03-12 23:41:59 +00001695 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001696 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001697 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001698 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001699 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001701 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001702 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001703 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001704 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001706 // Create the equivalent node in the new graph with the same state
1707 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001708 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001710 // Store the mapping to the original node.
1711 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1712 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001713 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001715 // Link up the new node with the previous node.
1716 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001717 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001719 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001721 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001722 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001723 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001724 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001725 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001726 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001727 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001728 }
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001730 // Find the next successor node. We choose the node that is marked
1731 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001732 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1733 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001734 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001736 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001738 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001740 if (I == Visited.end())
1741 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001743 if (!N || I->second < MinVal) {
1744 N = *SI;
1745 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001746 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001747 }
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Ted Kremenek938332c2009-05-16 01:11:58 +00001749 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001750 }
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Ted Kremenek938332c2009-05-16 01:11:58 +00001752 assert(First);
1753
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001754 return std::make_pair(std::make_pair(GNew, BM),
1755 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001756}
1757
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001758/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1759/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001760static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001761 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1762 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001764 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001765 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001767 MacroStackTy MacroStack;
1768 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Ted Kremenek77d09442012-03-02 01:27:31 +00001770 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001771 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001772
1773 PathDiagnosticPiece *piece = I->getPtr();
1774
1775 // Recursively compact calls.
1776 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1777 CompactPathDiagnostic(call->path, SM);
1778 }
1779
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001780 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001781 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001783 // Determine the instantiation location, which is the location we group
1784 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001785 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001786 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001787 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001789 if (Loc.isFileID()) {
1790 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001791 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001792 continue;
1793 }
1794
1795 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001797 // Is the PathDiagnosticPiece within the same macro group?
1798 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001799 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001800 continue;
1801 }
1802
1803 // We aren't in the same group. Are we descending into a new macro
1804 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001805 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001806
1807 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001808 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001809 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001811 // Walk the entire macro stack.
1812 while (!MacroStack.empty()) {
1813 if (InstantiationLoc == MacroStack.back().second) {
1814 MacroGroup = MacroStack.back().first;
1815 break;
1816 }
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001818 if (ParentInstantiationLoc == MacroStack.back().second) {
1819 MacroGroup = MacroStack.back().first;
1820 break;
1821 }
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001823 MacroStack.pop_back();
1824 }
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001826 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1827 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001828 PathDiagnosticMacroPiece *NewGroup =
1829 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001830 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001831
1832 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001833 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001834 else {
1835 assert(InstantiationLoc.isFileID());
1836 Pieces.push_back(NewGroup);
1837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001839 MacroGroup = NewGroup;
1840 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1841 }
1842
1843 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001844 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001847 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001848 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Ted Kremenek77d09442012-03-02 01:27:31 +00001850 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1851 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001852}
1853
Ted Kremenek7dc86642009-03-31 20:22:36 +00001854void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001855 PathDiagnosticConsumer &PC,
1856 ArrayRef<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Ted Kremenek40406fe2010-12-03 06:52:30 +00001858 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001859 SmallVector<const ExplodedNode *, 10> errorNodes;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001860 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
1861 E = bugReports.end(); I != E; ++I) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001862 errorNodes.push_back((*I)->getErrorNode());
1863 }
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001865 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001866 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001867 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001868 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001869 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Ted Kremenekcf118d42009-02-04 23:49:09 +00001871 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001872 assert(GPair.second.second < bugReports.size());
1873 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001874 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001876 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1877 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001878 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001879
1880 // Start building the path diagnostic...
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001881 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), &PC);
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Anna Zaks8e6431a2011-08-18 22:37:56 +00001883 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001884 R->addVisitor(new NilReceiverBRVisitor());
1885 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001887 BugReport::VisitorList visitors;
1888 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001889
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001890 // While generating diagnostics, it's possible the visitors will decide
1891 // new symbols and regions are interesting, or add other visitors based on
1892 // the information they find. If they do, we need to regenerate the path
1893 // based on our new report configuration.
1894 do {
1895 // Get a clean copy of all the visitors.
1896 for (BugReport::visitor_iterator I = R->visitor_begin(),
1897 E = R->visitor_end(); I != E; ++I)
1898 visitors.push_back((*I)->clone());
1899
1900 // Clear out the active path from any previous work.
1901 PD.getActivePath().clear();
1902 originalReportConfigToken = R->getConfigurationChangeToken();
1903
1904 // Generate the very last diagnostic piece - the piece is visible before
1905 // the trace is expanded.
1906 PathDiagnosticPiece *LastPiece = 0;
1907 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1908 I != E; ++I) {
1909 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1910 assert (!LastPiece &&
1911 "There can only be one final piece in a diagnostic.");
1912 LastPiece = Piece;
1913 }
1914 }
1915 if (!LastPiece)
1916 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1917 if (LastPiece)
1918 PD.getActivePath().push_back(LastPiece);
1919 else
1920 return;
1921
1922 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001923 case PathDiagnosticConsumer::Extensive:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001924 GenerateExtensivePathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001925 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001926 case PathDiagnosticConsumer::Minimal:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001927 GenerateMinimalPathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek7dc86642009-03-31 20:22:36 +00001928 break;
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00001929 case PathDiagnosticConsumer::None:
1930 llvm_unreachable("PathDiagnosticConsumer::None should never appear here");
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001931 }
1932
1933 // Clean up the visitors we used.
1934 llvm::DeleteContainerPointers(visitors);
1935
1936 // Did anything change while generating this path?
1937 finalReportConfigToken = R->getConfigurationChangeToken();
1938 } while(finalReportConfigToken != originalReportConfigToken);
1939
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001940 // Finally, prune the diagnostic path of uninteresting stuff.
Ted Kremeneked7948b2012-05-31 06:03:17 +00001941 if (R->shouldPrunePath()) {
Anna Zaks80de4872012-08-29 21:22:37 +00001942 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces(), R);
Ted Kremeneked7948b2012-05-31 06:03:17 +00001943 assert(hasSomethingInteresting);
1944 (void) hasSomethingInteresting;
1945 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001946}
1947
Ted Kremenekcf118d42009-02-04 23:49:09 +00001948void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001949 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001950}
1951
Mike Stump1eb44332009-09-09 15:08:12 +00001952void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001953 // Compute the bug report's hash to determine its equivalence class.
1954 llvm::FoldingSetNodeID ID;
1955 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001956
1957 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001958 BugType& BT = R->getBugType();
1959 Register(&BT);
1960 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001961 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Ted Kremenekcf118d42009-02-04 23:49:09 +00001963 if (!EQ) {
1964 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001965 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001966 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001967 }
1968 else
1969 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001970}
1971
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001972
1973//===----------------------------------------------------------------------===//
1974// Emitting reports in equivalence classes.
1975//===----------------------------------------------------------------------===//
1976
1977namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001978struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001979 const ExplodedNode *N;
1980 ExplodedNode::const_succ_iterator I, E;
1981
1982 FRIEC_WLItem(const ExplodedNode *n)
1983 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1984};
1985}
1986
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001987static BugReport *
1988FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001989 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001990
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001991 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1992 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001993 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001994
Ted Kremenek40406fe2010-12-03 06:52:30 +00001995 // If we don't need to suppress any of the nodes because they are
1996 // post-dominated by a sink, simply add all the nodes in the equivalence class
1997 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001998 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001999 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002000 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002001 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002002 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002003 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002004 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002005 }
2006 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002007 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002008 }
2009
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002010 // For bug reports that should be suppressed when all paths are post-dominated
2011 // by a sink node, iterate through the reports in the equivalence class
2012 // until we find one that isn't post-dominated (if one exists). We use a
2013 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2014 // this as a recursive function, but we don't want to risk blowing out the
2015 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002016 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002017
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002018 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002019 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002020
Ted Kremenek40406fe2010-12-03 06:52:30 +00002021 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002022 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002023 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00002024 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002025 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002026 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002027 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002028 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002029 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002030 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002031 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002032 continue;
2033 }
2034
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002035 // At this point we know that 'N' is not a sink and it has at least one
2036 // successor. Use a DFS worklist to find a non-sink end-of-path node.
2037 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002038 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002039 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
2040
2041 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002042 WL.push_back(errorNode);
2043 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002044
2045 while (!WL.empty()) {
2046 WLItem &WI = WL.back();
2047 assert(!WI.N->succ_empty());
2048
2049 for (; WI.I != WI.E; ++WI.I) {
2050 const ExplodedNode *Succ = *WI.I;
2051 // End-of-path node?
2052 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002053 // If we found an end-of-path node that is not a sink.
2054 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002055 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00002056 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00002057 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002058 WL.clear();
2059 break;
2060 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002061 // Found a sink? Continue on to the next successor.
2062 continue;
2063 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002064 // Mark the successor as visited. If it hasn't been explored,
2065 // enqueue it to the DFS worklist.
2066 unsigned &mark = Visited[Succ];
2067 if (!mark) {
2068 mark = 1;
2069 WL.push_back(Succ);
2070 break;
2071 }
2072 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002073
2074 // The worklist may have been cleared at this point. First
2075 // check if it is empty before checking the last item.
2076 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002077 WL.pop_back();
2078 }
2079 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00002080
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002081 // ExampleReport will be NULL if all the nodes in the equivalence class
2082 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002083 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00002084}
Ted Kremeneke0a58072009-09-18 22:37:37 +00002085
Ted Kremenekcf118d42009-02-04 23:49:09 +00002086void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002087 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00002088 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002089 if (exampleReport) {
2090 const PathDiagnosticConsumers &C = getPathDiagnosticConsumers();
2091 for (PathDiagnosticConsumers::const_iterator I=C.begin(),
2092 E=C.end(); I != E; ++I) {
2093 FlushReport(exampleReport, **I, bugReports);
2094 }
2095 }
2096}
2097
2098void BugReporter::FlushReport(BugReport *exampleReport,
2099 PathDiagnosticConsumer &PD,
2100 ArrayRef<BugReport*> bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Ted Kremenekcf118d42009-02-04 23:49:09 +00002102 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00002103 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00002104 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002106 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00002107 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
2108 exampleReport->getBugType().getName(),
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002109 PD.useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00002110 ? exampleReport->getDescription()
2111 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002112 BT.getCategory()));
2113
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002114 // Generate the full path diagnostic, using the generation scheme
2115 // specified by the PathDiagnosticConsumer.
2116 if (PD.getGenerationScheme() != PathDiagnosticConsumer::None) {
2117 if (!bugReports.empty())
2118 GeneratePathDiagnostic(*D.get(), PD, bugReports);
2119 }
2120
2121 // If the path is empty, generate a single step path with the location
2122 // of the issue.
2123 if (D->path.empty()) {
2124 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
2125 PathDiagnosticPiece *piece =
2126 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
2127 BugReport::ranges_iterator Beg, End;
2128 llvm::tie(Beg, End) = exampleReport->getRanges();
2129 for ( ; Beg != End; ++Beg)
2130 piece->addRange(*Beg);
2131 D->getActivePath().push_back(piece);
2132 }
2133
Ted Kremenek072192b2008-04-30 23:47:44 +00002134 // Get the meta data.
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002135 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
Anna Zaks7f2531c2011-08-22 20:31:28 +00002136 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2137 e = Meta.end(); i != e; ++i) {
2138 D->addMeta(*i);
2139 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002140
Ted Kremenekc4bac8e2012-08-16 17:45:23 +00002141 PD.HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002142}
Ted Kremenek57202072008-07-14 17:40:50 +00002143
Ted Kremenek07189522012-04-04 18:11:35 +00002144void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
Ted Kremenek07189522012-04-04 18:11:35 +00002145 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002146 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002147 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002148 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002150 // 'BT' is owned by BugReporter.
2151 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002152 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002153 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002154 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2155 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002156}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002157
Chris Lattner5f9e2722011-07-23 10:55:15 +00002158BugType *BugReporter::getBugTypeForName(StringRef name,
2159 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002160 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002161 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2162 llvm::StringMapEntry<BugType *> &
2163 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2164 BugType *BT = entry.getValue();
2165 if (!BT) {
2166 BT = new BugType(name, category);
2167 entry.setValue(BT);
2168 }
2169 return BT;
2170}