blob: 995ef264eb4b31980473cead0ddfce8059f19672 [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();
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenekb697b102009-02-23 22:44:26 +000052 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000053}
54
Zhongxing Xuc5619d92009-08-06 01:32:16 +000055static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000056GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000057 return N->pred_empty() ? NULL : *(N->pred_begin());
58}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000059
Zhongxing Xuc5619d92009-08-06 01:32:16 +000060static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000061GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000062 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000063}
64
Ted Kremenek9c378f72011-08-12 23:37:29 +000065static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000067 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000068 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekb697b102009-02-23 22:44:26 +000070 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000071}
72
Ted Kremenek9c378f72011-08-12 23:37:29 +000073static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000074 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000075 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000076 // Check if the statement is '?' or '&&'/'||'. These are "merges",
77 // not actual statement points.
78 switch (S->getStmtClass()) {
79 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000080 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000081 case Stmt::ConditionalOperatorClass: continue;
82 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000083 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
84 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 continue;
86 break;
87 }
88 default:
89 break;
90 }
Ted Kremenekb697b102009-02-23 22:44:26 +000091 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekb697b102009-02-23 22:44:26 +000094 return 0;
95}
96
Ted Kremenek5f85e172009-07-22 22:35:28 +000097static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000098GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000099 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000100 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekb697b102009-02-23 22:44:26 +0000102 return GetPreviousStmt(N);
103}
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek5f85e172009-07-22 22:35:28 +0000105static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000106GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000107 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000108 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return GetNextStmt(N);
111}
112
113//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000114// Diagnostic cleanup.
115//===----------------------------------------------------------------------===//
116
117/// Recursively scan through a path and prune out calls and macros pieces
118/// that aren't needed. Return true if afterwards the path contains
119/// "interesting stuff" which means it should be pruned from the parent path.
120static bool RemoveUneededCalls(PathPieces &pieces) {
121 bool containsSomethingInteresting = false;
122 const unsigned N = pieces.size();
123
124 for (unsigned i = 0 ; i < N ; ++i) {
125 // Remove the front piece from the path. If it is still something we
126 // want to keep once we are done, we will push it back on the end.
127 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
128 pieces.pop_front();
129
Ted Kremenek72516742012-03-01 00:05:06 +0000130 switch (piece->getKind()) {
131 case PathDiagnosticPiece::Call: {
132 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
133 // Recursively clean out the subclass. Keep this call around if
134 // it contains any informative diagnostics.
135 if (!RemoveUneededCalls(call->path))
136 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000137 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000138 break;
139 }
140 case PathDiagnosticPiece::Macro: {
141 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
142 if (!RemoveUneededCalls(macro->subPieces))
143 continue;
144 containsSomethingInteresting = true;
145 break;
146 }
147 case PathDiagnosticPiece::Event: {
148 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
149 // We never throw away an event, but we do throw it away wholesale
150 // as part of a path if we throw the entire path away.
Ted Kremenek76aadc32012-03-09 01:13:14 +0000151 if (event->isPrunable())
152 continue;
153 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000154 break;
155 }
156 case PathDiagnosticPiece::ControlFlow:
157 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000158 }
159
160 pieces.push_back(piece);
161 }
162
163 return containsSomethingInteresting;
164}
165
166//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000167// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000168//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000169
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000170typedef llvm::DenseMap<const ExplodedNode*,
171const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000172
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000173namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000174class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000175 NodeBackMap& M;
176public:
177 NodeMapClosure(NodeBackMap *m) : M(*m) {}
178 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek9c378f72011-08-12 23:37:29 +0000180 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000181 NodeBackMap::iterator I = M.find(N);
182 return I == M.end() ? 0 : I->second;
183 }
184};
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000186class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000187 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000188 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000189 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000190 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000191public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000192 const LocationContext *LC;
193
Ted Kremenek8966bc12009-05-06 21:39:49 +0000194 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000195 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000196 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000197 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000198 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
199 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek9c378f72011-08-12 23:37:29 +0000201 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek9c378f72011-08-12 23:37:29 +0000203 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
204 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Anna Zaks8e6431a2011-08-18 22:37:56 +0000206 BugReport *getBugReport() { return R; }
207
Tom Care212f6d32010-09-16 03:50:38 +0000208 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000209
210 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000212 const Stmt *getParent(const Stmt *S) {
213 return getParentMap().getParent(S);
214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek8966bc12009-05-06 21:39:49 +0000216 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000217
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000218 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
David Blaikieef3643f2011-09-26 00:51:36 +0000220 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
221 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000222 }
223
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000224 bool supportsLogicalOpControlFlow() const {
225 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000226 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000227};
228} // end anonymous namespace
229
Ted Kremenek00605e02009-03-27 20:55:39 +0000230PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000231PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000232 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000233 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000234
Anna Zaks0cd59482011-09-16 19:18:30 +0000235 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
236 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000237}
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek00605e02009-03-27 20:55:39 +0000239PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000240PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
241 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000242
Ted Kremenek143ca222008-05-06 18:11:09 +0000243 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000244 if (os.str().empty())
245 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek00605e02009-03-27 20:55:39 +0000247 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek00605e02009-03-27 20:55:39 +0000249 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000250 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000251 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000252 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000253 else {
254 os << "Execution jumps to the end of the ";
255 const Decl *D = N->getLocationContext()->getDecl();
256 if (isa<ObjCMethodDecl>(D))
257 os << "method";
258 else if (isa<FunctionDecl>(D))
259 os << "function";
260 else {
261 assert(isa<BlockDecl>(D));
262 os << "anonymous block";
263 }
264 os << '.';
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000267 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000268}
269
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000270static bool IsNested(const Stmt *S, ParentMap &PM) {
271 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
272 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000274 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000276 if (Parent)
277 switch (Parent->getStmtClass()) {
278 case Stmt::ForStmtClass:
279 case Stmt::DoStmtClass:
280 case Stmt::WhileStmtClass:
281 return true;
282 default:
283 break;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000287}
288
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000289PathDiagnosticLocation
290PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000291 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000292 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000293 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000294
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000295 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000296 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000298 if (!Parent)
299 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000301 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000302 case Stmt::BinaryOperatorClass: {
303 const BinaryOperator *B = cast<BinaryOperator>(Parent);
304 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000305 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000306 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000307 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000308 case Stmt::CompoundStmtClass:
309 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000310 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000311 case Stmt::ChooseExprClass:
312 // Similar to '?' if we are referring to condition, just have the edge
313 // point to the entire choose expression.
314 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000315 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000316 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000317 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000318 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000319 case Stmt::ConditionalOperatorClass:
320 // For '?', if we are referring to condition, just have the edge point
321 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000322 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000323 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000324 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000325 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000326 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000327 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000328 case Stmt::ForStmtClass:
329 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000330 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000331 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000332 case Stmt::IfStmtClass:
333 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000335 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000336 case Stmt::ObjCForCollectionStmtClass:
337 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000338 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000339 break;
340 case Stmt::WhileStmtClass:
341 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000342 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000343 break;
344 default:
345 break;
346 }
347
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000348 S = Parent;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000351 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000352
353 // Special case: DeclStmts can appear in for statement declarations, in which
354 // case the ForStmt is the context.
355 if (isa<DeclStmt>(S)) {
356 if (const Stmt *Parent = P.getParent(S)) {
357 switch (Parent->getStmtClass()) {
358 case Stmt::ForStmtClass:
359 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000360 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000361 default:
362 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000363 }
364 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000365 }
366 else if (isa<BinaryOperator>(S)) {
367 // Special case: the binary operator represents the initialization
368 // code in a for statement (this can happen when the variable being
369 // initialized is an old variable.
370 if (const ForStmt *FS =
371 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
372 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000373 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000374 }
375 }
376
Anna Zaks220ac8c2011-09-15 01:08:34 +0000377 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000378}
379
Ted Kremenekcf118d42009-02-04 23:49:09 +0000380//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000381// "Minimal" path diagnostic generation algorithm.
382//===----------------------------------------------------------------------===//
383
Ted Kremenek77d09442012-03-02 01:27:31 +0000384static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000385
Ted Kremenek31061982009-03-31 23:00:32 +0000386static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
387 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000388 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000389
Ted Kremenek31061982009-03-31 23:00:32 +0000390 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000391 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000392 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000393 ? NULL : *(N->pred_begin());
394 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000395 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000396 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000397 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek31061982009-03-31 23:00:32 +0000399 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000400
401 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
402 PathDiagnosticCallPiece *C =
403 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
404 PD.getActivePath().push_front(C);
405 PD.pushActivePath(&C->path);
406 continue;
407 }
408
409 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
410 PD.popActivePath();
411 // The current active path should never be empty. Either we
412 // just added a bunch of stuff to the top-level path, or
413 // we have a previous CallExit. If the front of the active
414 // path is not a PathDiagnosticCallPiece, it means that the
415 // path terminated within a function call. We must then take the
416 // current contents of the active path and place it within
417 // a new PathDiagnosticCallPiece.
418 assert(!PD.getActivePath().empty());
419 PathDiagnosticCallPiece *C =
420 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +0000421 if (!C) {
422 const Decl *Caller = CE->getLocationContext()->getDecl();
423 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
424 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000425 C->setCallee(*CE, SMgr);
426 continue;
427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek9c378f72011-08-12 23:37:29 +0000429 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
430 const CFGBlock *Src = BE->getSrc();
431 const CFGBlock *Dst = BE->getDst();
432 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 if (!T)
435 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anna Zaks590dd8e2011-09-20 21:38:35 +0000437 PathDiagnosticLocation Start =
438 PathDiagnosticLocation::createBegin(T, SMgr,
439 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek31061982009-03-31 23:00:32 +0000441 switch (T->getStmtClass()) {
442 default:
443 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek31061982009-03-31 23:00:32 +0000445 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000446 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000447 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Ted Kremenek31061982009-03-31 23:00:32 +0000449 if (!S)
450 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek31061982009-03-31 23:00:32 +0000452 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000453 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000454 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek31061982009-03-31 23:00:32 +0000456 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000457 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000458 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000459 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000460 break;
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
463 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000464 // Figure out what case arm we took.
465 std::string sbuf;
466 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek9c378f72011-08-12 23:37:29 +0000468 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000469 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek31061982009-03-31 23:00:32 +0000471 switch (S->getStmtClass()) {
472 default:
473 os << "No cases match in the switch statement. "
474 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000475 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000476 break;
477 case Stmt::DefaultStmtClass:
478 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000479 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000480 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek31061982009-03-31 23:00:32 +0000482 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000483 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000484 const CaseStmt *Case = cast<CaseStmt>(S);
485 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000486
487 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000488 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek9c378f72011-08-12 23:37:29 +0000490 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000491 // FIXME: Maybe this should be an assertion. Are there cases
492 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000493 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000494 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 if (D) {
497 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000498 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000499 }
500 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000501
502 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000503 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000506 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000507 break;
508 }
509 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000510 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000511 os.str()));
512 }
513 else {
514 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000515 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000516 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000517 os.str()));
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek31061982009-03-31 23:00:32 +0000520 break;
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Ted Kremenek31061982009-03-31 23:00:32 +0000523 case Stmt::BreakStmtClass:
524 case Stmt::ContinueStmtClass: {
525 std::string sbuf;
526 llvm::raw_string_ostream os(sbuf);
527 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000528 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000529 os.str()));
530 break;
531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek31061982009-03-31 23:00:32 +0000533 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000534 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000535 case Stmt::ConditionalOperatorClass: {
536 std::string sbuf;
537 llvm::raw_string_ostream os(sbuf);
538 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 if (*(Src->succ_begin()+1) == Dst)
541 os << "false";
542 else
543 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 if (const Stmt *S = End.asStmt())
548 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek2042fc12012-02-24 06:00:00 +0000550 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000551 os.str()));
552 break;
553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek31061982009-03-31 23:00:32 +0000555 // Determine control-flow for short-circuited '&&' and '||'.
556 case Stmt::BinaryOperatorClass: {
557 if (!PDB.supportsLogicalOpControlFlow())
558 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000560 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000561 std::string sbuf;
562 llvm::raw_string_ostream os(sbuf);
563 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000564
John McCall2de56d12010-08-25 11:45:40 +0000565 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000566 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek31061982009-03-31 23:00:32 +0000568 if (*(Src->succ_begin()+1) == Dst) {
569 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000570 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000571 PathDiagnosticLocation Start =
572 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000573 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000574 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000575 }
Ted Kremenek31061982009-03-31 23:00:32 +0000576 else {
577 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000578 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000579 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000580 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000581 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000582 }
Ted Kremenek31061982009-03-31 23:00:32 +0000583 }
584 else {
John McCall2de56d12010-08-25 11:45:40 +0000585 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000586 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek31061982009-03-31 23:00:32 +0000588 if (*(Src->succ_begin()+1) == Dst) {
589 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000590 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000591 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000592 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000593 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000594 }
595 else {
596 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000597 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000598 PathDiagnosticLocation Start =
599 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000600 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000601 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000602 }
603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Ted Kremenek31061982009-03-31 23:00:32 +0000605 break;
606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
608 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000609 if (*(Src->succ_begin()) == Dst) {
610 std::string sbuf;
611 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Ted Kremenek31061982009-03-31 23:00:32 +0000613 os << "Loop condition is true. ";
614 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Ted Kremenek31061982009-03-31 23:00:32 +0000616 if (const Stmt *S = End.asStmt())
617 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek2042fc12012-02-24 06:00:00 +0000619 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000620 os.str()));
621 }
622 else {
623 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Ted Kremenek31061982009-03-31 23:00:32 +0000625 if (const Stmt *S = End.asStmt())
626 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Ted Kremenek2042fc12012-02-24 06:00:00 +0000628 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000629 "Loop condition is false. Exiting loop"));
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Ted Kremenek31061982009-03-31 23:00:32 +0000632 break;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek31061982009-03-31 23:00:32 +0000635 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000636 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000637 if (*(Src->succ_begin()+1) == Dst) {
638 std::string sbuf;
639 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek31061982009-03-31 23:00:32 +0000641 os << "Loop condition is false. ";
642 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
643 if (const Stmt *S = End.asStmt())
644 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek2042fc12012-02-24 06:00:00 +0000646 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000647 os.str()));
648 }
649 else {
650 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
651 if (const Stmt *S = End.asStmt())
652 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek2042fc12012-02-24 06:00:00 +0000654 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000655 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 break;
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek31061982009-03-31 23:00:32 +0000661 case Stmt::IfStmtClass: {
662 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenek31061982009-03-31 23:00:32 +0000664 if (const Stmt *S = End.asStmt())
665 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremenek31061982009-03-31 23:00:32 +0000667 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000668 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000669 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000670 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000671 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000672 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenek31061982009-03-31 23:00:32 +0000674 break;
675 }
676 }
677 }
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000679 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000680 // Add diagnostic pieces from custom visitors.
681 BugReport *R = PDB.getBugReport();
682 for (BugReport::visitor_iterator I = R->visitor_begin(),
683 E = R->visitor_end(); I!=E; ++I) {
684 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenek2042fc12012-02-24 06:00:00 +0000685 PD.getActivePath().push_front(p);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000686 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000687 }
Ted Kremenek31061982009-03-31 23:00:32 +0000688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek14856d72009-04-06 23:06:54 +0000690 // After constructing the full PathDiagnostic, do a pass over it to compact
691 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000692 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000693}
694
695//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000696// "Extensive" PathDiagnostic generation.
697//===----------------------------------------------------------------------===//
698
699static bool IsControlFlowExpr(const Stmt *S) {
700 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000702 if (!E)
703 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
705 E = E->IgnoreParenCasts();
706
John McCall56ca35d2011-02-17 10:25:35 +0000707 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000708 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000710 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
711 if (B->isLogicalOp())
712 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000713
714 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000715}
716
Ted Kremenek14856d72009-04-06 23:06:54 +0000717namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000718class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000719 bool IsDead;
720public:
721 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
722 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000723
724 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000725 bool isDead() const { return IsDead; }
726};
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000728class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000729 std::vector<ContextLocation> CLocs;
730 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000731 PathDiagnostic &PD;
732 PathDiagnosticBuilder &PDB;
733 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000735 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek14856d72009-04-06 23:06:54 +0000737 bool containsLocation(const PathDiagnosticLocation &Container,
738 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek14856d72009-04-06 23:06:54 +0000740 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek9650cf32009-05-11 21:42:34 +0000742 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
743 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000744 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000745 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000746 while (1) {
747 // Adjust the location for some expressions that are best referenced
748 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000749 switch (S->getStmtClass()) {
750 default:
751 break;
752 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000753 case Stmt::GenericSelectionExprClass:
754 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000755 firstCharOnly = true;
756 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000757 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000758 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000759 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000760 firstCharOnly = true;
761 continue;
762 case Stmt::ChooseExprClass:
763 S = cast<ChooseExpr>(S)->getCond();
764 firstCharOnly = true;
765 continue;
766 case Stmt::BinaryOperatorClass:
767 S = cast<BinaryOperator>(S)->getLHS();
768 firstCharOnly = true;
769 continue;
770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek9650cf32009-05-11 21:42:34 +0000772 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek9650cf32009-05-11 21:42:34 +0000775 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000776 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek9650cf32009-05-11 21:42:34 +0000779 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000780 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000781
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000782 return L;
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek14856d72009-04-06 23:06:54 +0000785 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000786 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000787 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000788 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000789 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000790 CLocs.pop_back();
791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenek14856d72009-04-06 23:06:54 +0000793public:
794 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
795 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremeneka301a672009-04-22 18:16:20 +0000797 // If the PathDiagnostic already has pieces, add the enclosing statement
798 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000799 if (!PD.path.empty()) {
800 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek14856d72009-04-06 23:06:54 +0000802 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000803 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000804 }
805 }
806
807 ~EdgeBuilder() {
808 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000809
Ted Kremeneka301a672009-04-22 18:16:20 +0000810 // Finally, add an initial edge from the start location of the first
811 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000812 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000813 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000814 PDB.getSourceManager());
815 if (L.isValid())
816 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000817 }
818
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000819 void flushLocations() {
820 while (!CLocs.empty())
821 popLocation();
822 PrevLoc = PathDiagnosticLocation();
823 }
824
Ted Kremenek14856d72009-04-06 23:06:54 +0000825 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000827 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek14856d72009-04-06 23:06:54 +0000829 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000830 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000831};
Ted Kremenek14856d72009-04-06 23:06:54 +0000832} // end anonymous namespace
833
834
835PathDiagnosticLocation
836EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
837 if (const Stmt *S = L.asStmt()) {
838 if (IsControlFlowExpr(S))
839 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
841 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek14856d72009-04-06 23:06:54 +0000844 return L;
845}
846
847bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
848 const PathDiagnosticLocation &Containee) {
849
850 if (Container == Containee)
851 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek14856d72009-04-06 23:06:54 +0000853 if (Container.asDecl())
854 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Ted Kremenek14856d72009-04-06 23:06:54 +0000856 if (const Stmt *S = Containee.asStmt())
857 if (const Stmt *ContainerS = Container.asStmt()) {
858 while (S) {
859 if (S == ContainerS)
860 return true;
861 S = PDB.getParent(S);
862 }
863 return false;
864 }
865
866 // Less accurate: compare using source ranges.
867 SourceRange ContainerR = Container.asRange();
868 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek14856d72009-04-06 23:06:54 +0000870 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000871 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
872 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
873 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
874 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Chandler Carruth64211622011-07-25 21:09:52 +0000876 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
877 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
878 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
879 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek14856d72009-04-06 23:06:54 +0000881 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000882 assert(ContaineeBegLine <= ContaineeEndLine);
883
Ted Kremenek14856d72009-04-06 23:06:54 +0000884 return (ContainerBegLine <= ContaineeBegLine &&
885 ContainerEndLine >= ContaineeEndLine &&
886 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000887 SM.getExpansionColumnNumber(ContainerRBeg) <=
888 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000889 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000890 SM.getExpansionColumnNumber(ContainerREnd) >=
891 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000892}
893
Ted Kremenek14856d72009-04-06 23:06:54 +0000894void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
895 if (!PrevLoc.isValid()) {
896 PrevLoc = NewLoc;
897 return;
898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000900 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
901 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000903 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000904 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremenek14856d72009-04-06 23:06:54 +0000906 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000907 if (NewLocClean.asLocation().getExpansionLoc() ==
908 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000909 return;
910
Ted Kremenek2042fc12012-02-24 06:00:00 +0000911 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000912 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000913}
914
915void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremeneka301a672009-04-22 18:16:20 +0000917 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
918 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Ted Kremenek14856d72009-04-06 23:06:54 +0000920 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
921
922 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000923 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek14856d72009-04-06 23:06:54 +0000925 // Is the top location context the same as the one for the new location?
926 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000927 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000928 if (IsConsumedExpr(TopContextLoc) &&
929 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000930 TopContextLoc.markDead();
931
Ted Kremenek14856d72009-04-06 23:06:54 +0000932 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000933 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000934
935 return;
936 }
937
938 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000939 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000940 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000942 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000943 CLocs.push_back(ContextLocation(CLoc, true));
944 return;
945 }
946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Ted Kremenek14856d72009-04-06 23:06:54 +0000948 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000949 return;
Ted Kremenek14856d72009-04-06 23:06:54 +0000950 }
951
952 // Context does not contain the location. Flush it.
953 popLocation();
954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000956 // If we reach here, there is no enclosing context. Just add the edge.
957 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +0000958}
959
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000960bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
961 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
962 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000964 return false;
965}
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Ted Kremeneke1baed32009-05-05 23:13:38 +0000967void EdgeBuilder::addExtendedContext(const Stmt *S) {
968 if (!S)
969 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000970
971 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000972 while (Parent) {
973 if (isa<CompoundStmt>(Parent))
974 Parent = PDB.getParent(Parent);
975 else
976 break;
977 }
978
979 if (Parent) {
980 switch (Parent->getStmtClass()) {
981 case Stmt::DoStmtClass:
982 case Stmt::ObjCAtSynchronizedStmtClass:
983 addContext(Parent);
984 default:
985 break;
986 }
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Ted Kremeneke1baed32009-05-05 23:13:38 +0000989 addContext(S);
990}
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek14856d72009-04-06 23:06:54 +0000992void EdgeBuilder::addContext(const Stmt *S) {
993 if (!S)
994 return;
995
Ted Kremenek59950d32012-02-24 07:12:52 +0000996 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Ted Kremenek14856d72009-04-06 23:06:54 +0000998 while (!CLocs.empty()) {
999 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1000
1001 // Is the top location context the same as the one for the new location?
1002 if (TopContextLoc == L)
1003 return;
1004
1005 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001007 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001008 }
1009
1010 // Context does not contain the location. Flush it.
1011 popLocation();
1012 }
1013
1014 CLocs.push_back(L);
1015}
1016
1017static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1018 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001019 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001020 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001021 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001022
Ted Kremenek9c378f72011-08-12 23:37:29 +00001023 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001024 while (NextNode) {
1025 N = NextNode;
1026 NextNode = GetPredecessorNode(N);
1027 ProgramPoint P = N->getLocation();
1028
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001029 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001030 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1031 const StackFrameContext *LCtx =
1032 CE->getLocationContext()->getCurrentStackFrame();
1033 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1034 PDB.getSourceManager(),
1035 LCtx);
1036 EB.addEdge(Loc, true);
1037 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001038 PathDiagnosticCallPiece *C =
1039 PathDiagnosticCallPiece::construct(N, *CE, SM);
1040 PD.getActivePath().push_front(C);
1041 PD.pushActivePath(&C->path);
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001042 break;
1043 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001044
Ted Kremenek2042fc12012-02-24 06:00:00 +00001045 // Pop the call hierarchy if we are done walking the contents
1046 // of a function call.
1047 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001048 // Add an edge to the start of the function.
1049 const Decl *D = CE->getCalleeContext()->getDecl();
1050 PathDiagnosticLocation pos =
1051 PathDiagnosticLocation::createBegin(D, SM);
1052 EB.addEdge(pos);
1053
1054 // Flush all locations, and pop the active path.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001055 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001056 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001057 assert(!PD.getActivePath().empty());
1058 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001059
Ted Kremenek2042fc12012-02-24 06:00:00 +00001060 // The current active path should never be empty. Either we
1061 // just added a bunch of stuff to the top-level path, or
1062 // we have a previous CallExit. If the front of the active
1063 // path is not a PathDiagnosticCallPiece, it means that the
1064 // path terminated within a function call. We must then take the
1065 // current contents of the active path and place it within
1066 // a new PathDiagnosticCallPiece.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001067 PathDiagnosticCallPiece *C =
Ted Kremenek2042fc12012-02-24 06:00:00 +00001068 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +00001069 if (!C) {
1070 const Decl * Caller = CE->getLocationContext()->getDecl();
1071 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1072 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001073 C->setCallee(*CE, SM);
1074 EB.addContext(CE->getCallExpr());
1075 break;
1076 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001077
1078 // Note that is important that we update the LocationContext
1079 // after looking at CallExits. CallExit basically adds an
1080 // edge in the *caller*, so we don't want to update the LocationContext
1081 // too soon.
1082 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001083
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001084 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001085 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001086 const CFGBlock &Blk = *BE->getSrc();
1087 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001089 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001090 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001091 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001092 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001094 if (!Term) {
1095 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1096 CS = dyn_cast<CompoundStmt>(FS->getBody());
1097 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001098 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001101 PathDiagnosticEventPiece *p =
1102 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001103 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001104 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001106 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001107 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001109 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001110 PathDiagnosticLocation BL =
1111 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001112 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001113 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001116 if (Term)
1117 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001119 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001120 }
1121
Mike Stump1eb44332009-09-09 15:08:12 +00001122 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001123 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1124 const Stmt *stmt = S->getStmt();
1125 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001126 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001127 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001128 }
1129 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001130 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001131 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001132
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001133 break;
1134 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001135
1136
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001137 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001139 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001140 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Anna Zaks8e6431a2011-08-18 22:37:56 +00001142 // Add pieces from custom visitors.
1143 BugReport *R = PDB.getBugReport();
1144 for (BugReport::visitor_iterator I = R->visitor_begin(),
1145 E = R->visitor_end(); I!=E; ++I) {
1146 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001147 const PathDiagnosticLocation &Loc = p->getLocation();
1148 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001149 PD.getActivePath().push_front(p);
Ted Kremenek8966bc12009-05-06 21:39:49 +00001150 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001151 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001154 }
1155}
1156
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001157//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001158// Methods for BugType and subclasses.
1159//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001160BugType::~BugType() { }
1161
Ted Kremenekcf118d42009-02-04 23:49:09 +00001162void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001163
David Blaikie99ba9e32011-12-20 02:48:34 +00001164void BuiltinBug::anchor() {}
1165
Ted Kremenekcf118d42009-02-04 23:49:09 +00001166//===----------------------------------------------------------------------===//
1167// Methods for BugReport and subclasses.
1168//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001169
David Blaikie99ba9e32011-12-20 02:48:34 +00001170void BugReport::NodeResolver::anchor() {}
1171
Anna Zaks8e6431a2011-08-18 22:37:56 +00001172void BugReport::addVisitor(BugReporterVisitor* visitor) {
1173 if (!visitor)
1174 return;
1175
1176 llvm::FoldingSetNodeID ID;
1177 visitor->Profile(ID);
1178 void *InsertPos;
1179
1180 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1181 delete visitor;
1182 return;
1183 }
1184
1185 CallbacksSet.InsertNode(visitor, InsertPos);
1186 Callbacks = F.add(visitor, Callbacks);
1187}
1188
1189BugReport::~BugReport() {
1190 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001191 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001192 }
1193}
Anna Zakse172e8b2011-08-17 23:00:25 +00001194
1195void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1196 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001197 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001198 if (UniqueingLocation.isValid()) {
1199 UniqueingLocation.Profile(hash);
1200 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001201 Location.Profile(hash);
1202 } else {
1203 assert(ErrorNode);
1204 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1205 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001206
1207 for (SmallVectorImpl<SourceRange>::const_iterator I =
1208 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1209 const SourceRange range = *I;
1210 if (!range.isValid())
1211 continue;
1212 hash.AddInteger(range.getBegin().getRawEncoding());
1213 hash.AddInteger(range.getEnd().getRawEncoding());
1214 }
1215}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001216
Ted Kremenek76aadc32012-03-09 01:13:14 +00001217void BugReport::markInteresting(SymbolRef sym) {
1218 if (!sym)
1219 return;
1220 interestingSymbols.insert(sym);
1221}
1222
1223void BugReport::markInteresting(const MemRegion *R) {
1224 if (!R)
1225 return;
1226 R = R->getBaseRegion();
1227 interestingRegions.insert(R);
1228
1229 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1230 interestingSymbols.insert(SR->getSymbol());
1231}
1232
1233void BugReport::markInteresting(SVal V) {
1234 markInteresting(V.getAsRegion());
1235 markInteresting(V.getAsSymbol());
1236}
1237
1238bool BugReport::isInteresting(SVal V) const {
1239 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1240}
1241
1242bool BugReport::isInteresting(SymbolRef sym) const {
1243 if (!sym)
1244 return false;
1245 return interestingSymbols.count(sym);
1246}
1247
1248bool BugReport::isInteresting(const MemRegion *R) const {
1249 if (!R)
1250 return false;
1251 R = R->getBaseRegion();
1252 bool b = interestingRegions.count(R);
1253 if (b)
1254 return true;
1255 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1256 return interestingSymbols.count(SR->getSymbol());
1257 return false;
1258}
1259
1260
Ted Kremenek9c378f72011-08-12 23:37:29 +00001261const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001262 if (!ErrorNode)
1263 return 0;
1264
Tom Care212f6d32010-09-16 03:50:38 +00001265 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001266 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek9c378f72011-08-12 23:37:29 +00001268 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001269 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001270 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001271 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001272 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001273 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001274 S = GetStmt(ProgP);
1275
1276 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001277}
1278
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001279std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001280BugReport::getRanges() {
1281 // If no custom ranges, add the range of the statement corresponding to
1282 // the error node.
1283 if (Ranges.empty()) {
1284 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1285 addRange(E->getSourceRange());
1286 else
1287 return std::make_pair(ranges_iterator(), ranges_iterator());
1288 }
1289
Anna Zaks14924262011-08-24 20:31:06 +00001290 // User-specified absence of range info.
1291 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1292 return std::make_pair(ranges_iterator(), ranges_iterator());
1293
Anna Zakse172e8b2011-08-17 23:00:25 +00001294 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001295}
1296
Anna Zaks590dd8e2011-09-20 21:38:35 +00001297PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001298 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001299 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001300 "Either Location or ErrorNode should be specified but not both.");
1301
Ted Kremenek9c378f72011-08-12 23:37:29 +00001302 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001303 const LocationContext *LC = ErrorNode->getLocationContext();
1304
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001305 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001306 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001307 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001308 // For binary operators, return the location of the operator.
1309 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001310 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001311
Anna Zaks590dd8e2011-09-20 21:38:35 +00001312 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001313 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001314 } else {
1315 assert(Location.isValid());
1316 return Location;
1317 }
1318
Anna Zaks590dd8e2011-09-20 21:38:35 +00001319 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001320}
1321
Ted Kremenekcf118d42009-02-04 23:49:09 +00001322//===----------------------------------------------------------------------===//
1323// Methods for BugReporter and subclasses.
1324//===----------------------------------------------------------------------===//
1325
1326BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001327 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001328}
1329
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001330GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001331BugReporterData::~BugReporterData() {}
1332
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001333ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001334
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001335ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001336GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1337
Anna Zaks3b030a22011-08-19 01:57:09 +00001338BugReporter::~BugReporter() {
1339 FlushReports();
1340
1341 // Free the bug reports we are tracking.
1342 typedef std::vector<BugReportEquivClass *> ContTy;
1343 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1344 I != E; ++I) {
1345 delete *I;
1346 }
1347}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001348
1349void BugReporter::FlushReports() {
1350 if (BugTypes.isEmpty())
1351 return;
1352
1353 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001354 // warnings and new BugTypes.
1355 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1356 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001357 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001358 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001359 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001360 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001361 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001362 const_cast<BugType*>(*I)->FlushReports(*this);
1363
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001364 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1365 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1366 BugReportEquivClass& EQ = *EI;
1367 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001368 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001369
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001370 // BugReporter owns and deletes only BugTypes created implicitly through
1371 // EmitBasicReport.
1372 // FIXME: There are leaks from checkers that assume that the BugTypes they
1373 // create will be destroyed by the BugReporter.
1374 for (llvm::StringMap<BugType*>::iterator
1375 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1376 delete I->second;
1377
Ted Kremenekcf118d42009-02-04 23:49:09 +00001378 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001379 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001380}
1381
1382//===----------------------------------------------------------------------===//
1383// PathDiagnostics generation.
1384//===----------------------------------------------------------------------===//
1385
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001386static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001387 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001388MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001389 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Ted Kremenekcf118d42009-02-04 23:49:09 +00001391 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001392 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001393 // error node unless there are two or more error nodes with the same minimum
1394 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001395 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001396 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001397
1398 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001399 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1400 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Ted Kremenekcf118d42009-02-04 23:49:09 +00001402 // Create owning pointers for GTrim and NMap just to ensure that they are
1403 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001404 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1405 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Ted Kremenekcf118d42009-02-04 23:49:09 +00001407 // Find the (first) error node in the trimmed graph. We just need to consult
1408 // the node map (NMap) which maps from nodes in the original graph to nodes
1409 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001410
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001411 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001412 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001413 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001414
Ted Kremenek40406fe2010-12-03 06:52:30 +00001415 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1416 const ExplodedNode *originalNode = nodes[nodeIndex];
1417 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001418 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001419 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001420 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001421 }
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Ted Kremenek938332c2009-05-16 01:11:58 +00001423 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001424
1425 // Create a new (third!) graph with a single path. This is the graph
1426 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001427 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenek10aa5542009-03-12 23:41:59 +00001429 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001430 // to the root node, and then construct a new graph that contains only
1431 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001432 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001434 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001435 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001437 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001438 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001439 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001441 if (Visited.find(Node) != Visited.end())
1442 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001444 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 if (Node->pred_empty()) {
1447 Root = Node;
1448 break;
1449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001451 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001452 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001453 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001454 }
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenek938332c2009-05-16 01:11:58 +00001456 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Ted Kremenek10aa5542009-03-12 23:41:59 +00001458 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001459 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001460 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001461 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001462 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001464 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001466 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001467 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001469 // Create the equivalent node in the new graph with the same state
1470 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001471 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001473 // Store the mapping to the original node.
1474 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1475 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001476 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001478 // Link up the new node with the previous node.
1479 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001480 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001482 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001485 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001486 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001487 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001488 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001489 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001490 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001493 // Find the next successor node. We choose the node that is marked
1494 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001495 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1496 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001497 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001499 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001501 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001503 if (I == Visited.end())
1504 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001506 if (!N || I->second < MinVal) {
1507 N = *SI;
1508 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001509 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek938332c2009-05-16 01:11:58 +00001512 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001513 }
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek938332c2009-05-16 01:11:58 +00001515 assert(First);
1516
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001517 return std::make_pair(std::make_pair(GNew, BM),
1518 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001519}
1520
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001521/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1522/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001523static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001524 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1525 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001527 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001528 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001530 MacroStackTy MacroStack;
1531 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Ted Kremenek77d09442012-03-02 01:27:31 +00001533 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001534 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001535
1536 PathDiagnosticPiece *piece = I->getPtr();
1537
1538 // Recursively compact calls.
1539 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1540 CompactPathDiagnostic(call->path, SM);
1541 }
1542
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001543 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001544 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001546 // Determine the instantiation location, which is the location we group
1547 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001548 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001549 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001550 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001552 if (Loc.isFileID()) {
1553 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001554 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001555 continue;
1556 }
1557
1558 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001560 // Is the PathDiagnosticPiece within the same macro group?
1561 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001562 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001563 continue;
1564 }
1565
1566 // We aren't in the same group. Are we descending into a new macro
1567 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001568 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001569
1570 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001571 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001572 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001574 // Walk the entire macro stack.
1575 while (!MacroStack.empty()) {
1576 if (InstantiationLoc == MacroStack.back().second) {
1577 MacroGroup = MacroStack.back().first;
1578 break;
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001581 if (ParentInstantiationLoc == MacroStack.back().second) {
1582 MacroGroup = MacroStack.back().first;
1583 break;
1584 }
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001586 MacroStack.pop_back();
1587 }
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001589 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1590 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001591 PathDiagnosticMacroPiece *NewGroup =
1592 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001593 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001594
1595 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001596 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001597 else {
1598 assert(InstantiationLoc.isFileID());
1599 Pieces.push_back(NewGroup);
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001602 MacroGroup = NewGroup;
1603 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1604 }
1605
1606 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001607 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001608 }
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001610 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001611 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Ted Kremenek77d09442012-03-02 01:27:31 +00001613 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1614 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001615}
1616
Ted Kremenek7dc86642009-03-31 20:22:36 +00001617void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001618 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Ted Kremenek40406fe2010-12-03 06:52:30 +00001620 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001621 SmallVector<const ExplodedNode *, 10> errorNodes;
1622 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001623 E = bugReports.end(); I != E; ++I) {
1624 errorNodes.push_back((*I)->getErrorNode());
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001627 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001628 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001629 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001630 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001631 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Ted Kremenekcf118d42009-02-04 23:49:09 +00001633 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001634 assert(GPair.second.second < bugReports.size());
1635 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001636 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001638 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1639 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001640 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001641
1642 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001643 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1644 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Anna Zaks8e6431a2011-08-18 22:37:56 +00001646 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001647 R->addVisitor(new NilReceiverBRVisitor());
1648 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Anna Zaks23f395e2011-08-20 01:27:22 +00001650 // Generate the very last diagnostic piece - the piece is visible before
1651 // the trace is expanded.
1652 PathDiagnosticPiece *LastPiece = 0;
1653 for (BugReport::visitor_iterator I = R->visitor_begin(),
1654 E = R->visitor_end(); I!=E; ++I) {
1655 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1656 assert (!LastPiece &&
1657 "There can only be one final piece in a diagnostic.");
1658 LastPiece = Piece;
1659 }
1660 }
1661 if (!LastPiece)
1662 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1663 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001664 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001665 else
1666 return;
1667
Ted Kremenek7dc86642009-03-31 20:22:36 +00001668 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001669 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001670 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001671 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001672 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001673 GenerateMinimalPathDiagnostic(PD, PDB, N);
1674 break;
1675 }
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001676
1677 // Finally, prune the diagnostic path of uninteresting stuff.
1678 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1679 assert(hasSomethingInteresting);
1680 (void) hasSomethingInteresting;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001681}
1682
Ted Kremenekcf118d42009-02-04 23:49:09 +00001683void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001684 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001685}
1686
Mike Stump1eb44332009-09-09 15:08:12 +00001687void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001688 // Compute the bug report's hash to determine its equivalence class.
1689 llvm::FoldingSetNodeID ID;
1690 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001691
1692 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001693 BugType& BT = R->getBugType();
1694 Register(&BT);
1695 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001696 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Ted Kremenekcf118d42009-02-04 23:49:09 +00001698 if (!EQ) {
1699 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001700 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001701 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001702 }
1703 else
1704 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001705}
1706
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001707
1708//===----------------------------------------------------------------------===//
1709// Emitting reports in equivalence classes.
1710//===----------------------------------------------------------------------===//
1711
1712namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001713struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001714 const ExplodedNode *N;
1715 ExplodedNode::const_succ_iterator I, E;
1716
1717 FRIEC_WLItem(const ExplodedNode *n)
1718 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1719};
1720}
1721
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001722static BugReport *
1723FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001724 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001725
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001726 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1727 assert(I != E);
1728 BugReport *R = *I;
1729 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001730
Ted Kremenek40406fe2010-12-03 06:52:30 +00001731 // If we don't need to suppress any of the nodes because they are
1732 // post-dominated by a sink, simply add all the nodes in the equivalence class
1733 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001734 if (!BT.isSuppressOnSink()) {
1735 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001736 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001737 if (N) {
1738 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001739 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001740 }
1741 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001742 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001743 }
1744
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001745 // For bug reports that should be suppressed when all paths are post-dominated
1746 // by a sink node, iterate through the reports in the equivalence class
1747 // until we find one that isn't post-dominated (if one exists). We use a
1748 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1749 // this as a recursive function, but we don't want to risk blowing out the
1750 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001751 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001752
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001753 for (; I != E; ++I) {
1754 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001755 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001756
Ted Kremenek40406fe2010-12-03 06:52:30 +00001757 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001758 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001759 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001760 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001761 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001762 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001763 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001764 if (errorNode->succ_empty()) {
1765 bugReports.push_back(R);
1766 if (!exampleReport)
1767 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001768 continue;
1769 }
1770
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001771 // At this point we know that 'N' is not a sink and it has at least one
1772 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1773 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001774 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001775 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1776
1777 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001778 WL.push_back(errorNode);
1779 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001780
1781 while (!WL.empty()) {
1782 WLItem &WI = WL.back();
1783 assert(!WI.N->succ_empty());
1784
1785 for (; WI.I != WI.E; ++WI.I) {
1786 const ExplodedNode *Succ = *WI.I;
1787 // End-of-path node?
1788 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001789 // If we found an end-of-path node that is not a sink.
1790 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001791 bugReports.push_back(R);
1792 if (!exampleReport)
1793 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001794 WL.clear();
1795 break;
1796 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001797 // Found a sink? Continue on to the next successor.
1798 continue;
1799 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001800 // Mark the successor as visited. If it hasn't been explored,
1801 // enqueue it to the DFS worklist.
1802 unsigned &mark = Visited[Succ];
1803 if (!mark) {
1804 mark = 1;
1805 WL.push_back(Succ);
1806 break;
1807 }
1808 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001809
1810 // The worklist may have been cleared at this point. First
1811 // check if it is empty before checking the last item.
1812 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001813 WL.pop_back();
1814 }
1815 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001816
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001817 // ExampleReport will be NULL if all the nodes in the equivalence class
1818 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001819 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001820}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001821
1822//===----------------------------------------------------------------------===//
1823// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1824// uses global state, which eventually should go elsewhere.
1825//===----------------------------------------------------------------------===//
1826namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001827class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001828 llvm::FoldingSetNodeID ID;
1829public:
1830 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001831 R->Profile(ID);
1832 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001833 }
1834
1835 void Profile(llvm::FoldingSetNodeID &id) {
1836 id = ID;
1837 }
1838
1839 llvm::FoldingSetNodeID &getID() { return ID; }
1840};
1841}
1842
1843static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1844 // FIXME: Eventually this diagnostic cache should reside in something
1845 // like AnalysisManager instead of being a static variable. This is
1846 // really unsafe in the long term.
1847 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1848 static DiagnosticCache DC;
1849
1850 void *InsertPos;
1851 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1852
1853 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1854 delete Item;
1855 return true;
1856 }
1857
1858 DC.InsertNode(Item, InsertPos);
1859 return false;
1860}
1861
Ted Kremenekcf118d42009-02-04 23:49:09 +00001862void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001863 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001864 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1865 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001866 return;
1867
David Blaikieef3643f2011-09-26 00:51:36 +00001868 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Ted Kremenekcf118d42009-02-04 23:49:09 +00001870 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001871 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001872 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001874 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001875 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001876 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001877 ? exampleReport->getDescription()
1878 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001879 BT.getCategory()));
1880
Ted Kremenek40406fe2010-12-03 06:52:30 +00001881 if (!bugReports.empty())
1882 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Ted Kremenek40406fe2010-12-03 06:52:30 +00001884 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001885 return;
1886
Ted Kremenek072192b2008-04-30 23:47:44 +00001887 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001888 const BugReport::ExtraTextList &Meta =
1889 exampleReport->getExtraText();
1890 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1891 e = Meta.end(); i != e; ++i) {
1892 D->addMeta(*i);
1893 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001894
Ted Kremenek3148eb42009-01-24 00:55:43 +00001895 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001896 BugReport::ranges_iterator Beg, End;
1897 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001898 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001899
1900 // Search the description for '%', as that will be interpretted as a
1901 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001902 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001903 unsigned ErrorDiag;
1904 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001905 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001906 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001907 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001908 if (*I == '%')
1909 Out << "%%";
1910 else
1911 Out << *I;
1912
1913 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001914 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001915 }
Ted Kremenek57202072008-07-14 17:40:50 +00001916
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001917 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001918 DiagnosticBuilder diagBuilder = Diag.Report(
1919 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001920 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001921 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001922 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001923
David Blaikieef3643f2011-09-26 00:51:36 +00001924 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001925 if (!PD)
1926 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Ted Kremenek802e0242012-02-08 04:32:34 +00001928 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001929 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1930 exampleReport->getLocation(getSourceManager()),
1931 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001932
Ted Kremenek3148eb42009-01-24 00:55:43 +00001933 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001934 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001935 }
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Ted Kremenek3148eb42009-01-24 00:55:43 +00001937 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001938}
Ted Kremenek57202072008-07-14 17:40:50 +00001939
Chris Lattner5f9e2722011-07-23 10:55:15 +00001940void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001941 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001942 SourceRange* RBeg, unsigned NumRanges) {
1943 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1944}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001945
Chris Lattner5f9e2722011-07-23 10:55:15 +00001946void BugReporter::EmitBasicReport(StringRef name,
1947 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001948 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001949 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001951 // 'BT' is owned by BugReporter.
1952 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001953 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001954 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1955 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001956}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001957
Chris Lattner5f9e2722011-07-23 10:55:15 +00001958BugType *BugReporter::getBugTypeForName(StringRef name,
1959 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001960 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001961 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1962 llvm::StringMapEntry<BugType *> &
1963 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1964 BugType *BT = entry.getValue();
1965 if (!BT) {
1966 BT = new BugType(name, category);
1967 entry.setValue(BT);
1968 }
1969 return BT;
1970}