blob: 363a6dffee2b1c4391cabffe340b27d74f8b570e [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//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000383typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
384typedef SmallVector<StackDiagPair, 6> StackDiagVector;
385
Anna Zaks368a0d52012-03-15 21:13:02 +0000386static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000387 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000388 // If the piece contains a special message, add it to all the call
389 // pieces on the active stack.
390 if (PathDiagnosticEventPiece *ep =
391 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000392
Anna Zaks56a938f2012-03-16 23:24:20 +0000393 if (ep->hasCallStackHint())
394 for (StackDiagVector::iterator I = CallStack.begin(),
395 E = CallStack.end(); I != E; ++I) {
396 PathDiagnosticCallPiece *CP = I->first;
397 const ExplodedNode *N = I->second;
398 StringRef stackMsg = ep->getCallStackMessage(N);
399
Anna Zaks368a0d52012-03-15 21:13:02 +0000400 // The last message on the path to final bug is the most important
401 // one. Since we traverse the path backwards, do not add the message
402 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000403 if (!CP->hasCallStackMessage())
404 CP->setCallStackMessage(stackMsg);
405 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000406 }
407}
Ted Kremenek31061982009-03-31 23:00:32 +0000408
Ted Kremenek77d09442012-03-02 01:27:31 +0000409static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000410
Ted Kremenek31061982009-03-31 23:00:32 +0000411static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
412 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000413 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000414
Ted Kremenek31061982009-03-31 23:00:32 +0000415 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000416 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000417 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000418 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000419
Anna Zaks56a938f2012-03-16 23:24:20 +0000420 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000421
Ted Kremenek31061982009-03-31 23:00:32 +0000422 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000423 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000424 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000425 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek31061982009-03-31 23:00:32 +0000427 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000428
429 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
430 PathDiagnosticCallPiece *C =
431 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
432 PD.getActivePath().push_front(C);
433 PD.pushActivePath(&C->path);
Anna Zaks56a938f2012-03-16 23:24:20 +0000434 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek2042fc12012-02-24 06:00:00 +0000435 continue;
436 }
437
438 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
439 PD.popActivePath();
440 // The current active path should never be empty. Either we
441 // just added a bunch of stuff to the top-level path, or
442 // we have a previous CallExit. If the front of the active
443 // path is not a PathDiagnosticCallPiece, it means that the
444 // path terminated within a function call. We must then take the
445 // current contents of the active path and place it within
446 // a new PathDiagnosticCallPiece.
447 assert(!PD.getActivePath().empty());
448 PathDiagnosticCallPiece *C =
449 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +0000450 if (!C) {
451 const Decl *Caller = CE->getLocationContext()->getDecl();
452 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
453 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000454 C->setCallee(*CE, SMgr);
Anna Zaks368a0d52012-03-15 21:13:02 +0000455 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +0000456 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +0000457 CallStack.pop_back();
458 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000459 continue;
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Ted Kremenek9c378f72011-08-12 23:37:29 +0000462 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
463 const CFGBlock *Src = BE->getSrc();
464 const CFGBlock *Dst = BE->getDst();
465 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek31061982009-03-31 23:00:32 +0000467 if (!T)
468 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Anna Zaks590dd8e2011-09-20 21:38:35 +0000470 PathDiagnosticLocation Start =
471 PathDiagnosticLocation::createBegin(T, SMgr,
472 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek31061982009-03-31 23:00:32 +0000474 switch (T->getStmtClass()) {
475 default:
476 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek31061982009-03-31 23:00:32 +0000478 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000479 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000480 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek31061982009-03-31 23:00:32 +0000482 if (!S)
483 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek31061982009-03-31 23:00:32 +0000485 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000486 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000487 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000490 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000491 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000492 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000493 break;
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
496 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000497 // Figure out what case arm we took.
498 std::string sbuf;
499 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek9c378f72011-08-12 23:37:29 +0000501 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000502 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek31061982009-03-31 23:00:32 +0000504 switch (S->getStmtClass()) {
505 default:
506 os << "No cases match in the switch statement. "
507 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000508 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000509 break;
510 case Stmt::DefaultStmtClass:
511 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000512 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000513 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenek31061982009-03-31 23:00:32 +0000515 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000516 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000517 const CaseStmt *Case = cast<CaseStmt>(S);
518 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000519
520 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000521 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Ted Kremenek9c378f72011-08-12 23:37:29 +0000523 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000524 // FIXME: Maybe this should be an assertion. Are there cases
525 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000526 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000527 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Ted Kremenek31061982009-03-31 23:00:32 +0000529 if (D) {
530 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000531 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000532 }
533 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000534
535 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000536 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000537
Ted Kremenek31061982009-03-31 23:00:32 +0000538 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000539 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000540 break;
541 }
542 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000543 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000544 os.str()));
545 }
546 else {
547 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000548 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000549 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000550 os.str()));
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek31061982009-03-31 23:00:32 +0000553 break;
554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Ted Kremenek31061982009-03-31 23:00:32 +0000556 case Stmt::BreakStmtClass:
557 case Stmt::ContinueStmtClass: {
558 std::string sbuf;
559 llvm::raw_string_ostream os(sbuf);
560 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000561 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000562 os.str()));
563 break;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Ted Kremenek31061982009-03-31 23:00:32 +0000566 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000567 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000568 case Stmt::ConditionalOperatorClass: {
569 std::string sbuf;
570 llvm::raw_string_ostream os(sbuf);
571 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Ted Kremenek31061982009-03-31 23:00:32 +0000573 if (*(Src->succ_begin()+1) == Dst)
574 os << "false";
575 else
576 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremenek31061982009-03-31 23:00:32 +0000578 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Ted Kremenek31061982009-03-31 23:00:32 +0000580 if (const Stmt *S = End.asStmt())
581 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Ted Kremenek2042fc12012-02-24 06:00:00 +0000583 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000584 os.str()));
585 break;
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek31061982009-03-31 23:00:32 +0000588 // Determine control-flow for short-circuited '&&' and '||'.
589 case Stmt::BinaryOperatorClass: {
590 if (!PDB.supportsLogicalOpControlFlow())
591 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000593 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000594 std::string sbuf;
595 llvm::raw_string_ostream os(sbuf);
596 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000597
John McCall2de56d12010-08-25 11:45:40 +0000598 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000599 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Ted Kremenek31061982009-03-31 23:00:32 +0000601 if (*(Src->succ_begin()+1) == Dst) {
602 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000603 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000604 PathDiagnosticLocation Start =
605 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000606 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000607 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000608 }
Ted Kremenek31061982009-03-31 23:00:32 +0000609 else {
610 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000611 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000612 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000613 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000614 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000615 }
Ted Kremenek31061982009-03-31 23:00:32 +0000616 }
617 else {
John McCall2de56d12010-08-25 11:45:40 +0000618 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000619 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Ted Kremenek31061982009-03-31 23:00:32 +0000621 if (*(Src->succ_begin()+1) == Dst) {
622 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000623 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000624 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000625 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000626 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000627 }
628 else {
629 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000630 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000631 PathDiagnosticLocation Start =
632 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000633 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000634 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000635 }
636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Ted Kremenek31061982009-03-31 23:00:32 +0000638 break;
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
641 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000642 if (*(Src->succ_begin()) == Dst) {
643 std::string sbuf;
644 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek31061982009-03-31 23:00:32 +0000646 os << "Loop condition is true. ";
647 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 if (const Stmt *S = End.asStmt())
650 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek2042fc12012-02-24 06:00:00 +0000652 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000653 os.str()));
654 }
655 else {
656 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 if (const Stmt *S = End.asStmt())
659 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek2042fc12012-02-24 06:00:00 +0000661 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000662 "Loop condition is false. Exiting loop"));
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Ted Kremenek31061982009-03-31 23:00:32 +0000665 break;
666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Ted Kremenek31061982009-03-31 23:00:32 +0000668 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000669 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000670 if (*(Src->succ_begin()+1) == Dst) {
671 std::string sbuf;
672 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenek31061982009-03-31 23:00:32 +0000674 os << "Loop condition is false. ";
675 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
676 if (const Stmt *S = End.asStmt())
677 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek2042fc12012-02-24 06:00:00 +0000679 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000680 os.str()));
681 }
682 else {
683 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
684 if (const Stmt *S = End.asStmt())
685 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremenek2042fc12012-02-24 06:00:00 +0000687 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000688 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Ted Kremenek31061982009-03-31 23:00:32 +0000691 break;
692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek31061982009-03-31 23:00:32 +0000694 case Stmt::IfStmtClass: {
695 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek31061982009-03-31 23:00:32 +0000697 if (const Stmt *S = End.asStmt())
698 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Ted Kremenek31061982009-03-31 23:00:32 +0000700 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000701 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000702 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000703 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000704 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000705 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 break;
708 }
709 }
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000712 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000713 // Add diagnostic pieces from custom visitors.
714 BugReport *R = PDB.getBugReport();
715 for (BugReport::visitor_iterator I = R->visitor_begin(),
716 E = R->visitor_end(); I!=E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000717 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000718 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000719 updateStackPiecesWithMessage(p, CallStack);
720 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000721 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000722 }
Ted Kremenek31061982009-03-31 23:00:32 +0000723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek14856d72009-04-06 23:06:54 +0000725 // After constructing the full PathDiagnostic, do a pass over it to compact
726 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000727 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000728}
729
730//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000731// "Extensive" PathDiagnostic generation.
732//===----------------------------------------------------------------------===//
733
734static bool IsControlFlowExpr(const Stmt *S) {
735 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000737 if (!E)
738 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000739
740 E = E->IgnoreParenCasts();
741
John McCall56ca35d2011-02-17 10:25:35 +0000742 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000743 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000745 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
746 if (B->isLogicalOp())
747 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000748
749 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000750}
751
Ted Kremenek14856d72009-04-06 23:06:54 +0000752namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000753class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000754 bool IsDead;
755public:
756 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
757 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000758
759 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000760 bool isDead() const { return IsDead; }
761};
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000763class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000764 std::vector<ContextLocation> CLocs;
765 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000766 PathDiagnostic &PD;
767 PathDiagnosticBuilder &PDB;
768 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000770 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek14856d72009-04-06 23:06:54 +0000772 bool containsLocation(const PathDiagnosticLocation &Container,
773 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek14856d72009-04-06 23:06:54 +0000775 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek9650cf32009-05-11 21:42:34 +0000777 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
778 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000779 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000780 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000781 while (1) {
782 // Adjust the location for some expressions that are best referenced
783 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000784 switch (S->getStmtClass()) {
785 default:
786 break;
787 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000788 case Stmt::GenericSelectionExprClass:
789 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000790 firstCharOnly = true;
791 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000792 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000793 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000794 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000795 firstCharOnly = true;
796 continue;
797 case Stmt::ChooseExprClass:
798 S = cast<ChooseExpr>(S)->getCond();
799 firstCharOnly = true;
800 continue;
801 case Stmt::BinaryOperatorClass:
802 S = cast<BinaryOperator>(S)->getLHS();
803 firstCharOnly = true;
804 continue;
805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek9650cf32009-05-11 21:42:34 +0000807 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenek9650cf32009-05-11 21:42:34 +0000810 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000811 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenek9650cf32009-05-11 21:42:34 +0000814 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000815 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000816
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000817 return L;
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek14856d72009-04-06 23:06:54 +0000820 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000821 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000822 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000823 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000824 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000825 CLocs.pop_back();
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenek14856d72009-04-06 23:06:54 +0000828public:
829 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
830 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremeneka301a672009-04-22 18:16:20 +0000832 // If the PathDiagnostic already has pieces, add the enclosing statement
833 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000834 if (!PD.path.empty()) {
835 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenek14856d72009-04-06 23:06:54 +0000837 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000838 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000839 }
840 }
841
842 ~EdgeBuilder() {
843 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000844
Ted Kremeneka301a672009-04-22 18:16:20 +0000845 // Finally, add an initial edge from the start location of the first
846 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000847 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000848 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000849 PDB.getSourceManager());
850 if (L.isValid())
851 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000852 }
853
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000854 void flushLocations() {
855 while (!CLocs.empty())
856 popLocation();
857 PrevLoc = PathDiagnosticLocation();
858 }
859
Ted Kremenek14856d72009-04-06 23:06:54 +0000860 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000862 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Ted Kremenek14856d72009-04-06 23:06:54 +0000864 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000865 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000866};
Ted Kremenek14856d72009-04-06 23:06:54 +0000867} // end anonymous namespace
868
869
870PathDiagnosticLocation
871EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
872 if (const Stmt *S = L.asStmt()) {
873 if (IsControlFlowExpr(S))
874 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
876 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek14856d72009-04-06 23:06:54 +0000879 return L;
880}
881
882bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
883 const PathDiagnosticLocation &Containee) {
884
885 if (Container == Containee)
886 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Ted Kremenek14856d72009-04-06 23:06:54 +0000888 if (Container.asDecl())
889 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek14856d72009-04-06 23:06:54 +0000891 if (const Stmt *S = Containee.asStmt())
892 if (const Stmt *ContainerS = Container.asStmt()) {
893 while (S) {
894 if (S == ContainerS)
895 return true;
896 S = PDB.getParent(S);
897 }
898 return false;
899 }
900
901 // Less accurate: compare using source ranges.
902 SourceRange ContainerR = Container.asRange();
903 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Ted Kremenek14856d72009-04-06 23:06:54 +0000905 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000906 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
907 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
908 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
909 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Chandler Carruth64211622011-07-25 21:09:52 +0000911 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
912 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
913 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
914 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Ted Kremenek14856d72009-04-06 23:06:54 +0000916 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000917 assert(ContaineeBegLine <= ContaineeEndLine);
918
Ted Kremenek14856d72009-04-06 23:06:54 +0000919 return (ContainerBegLine <= ContaineeBegLine &&
920 ContainerEndLine >= ContaineeEndLine &&
921 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000922 SM.getExpansionColumnNumber(ContainerRBeg) <=
923 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000924 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000925 SM.getExpansionColumnNumber(ContainerREnd) >=
926 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000927}
928
Ted Kremenek14856d72009-04-06 23:06:54 +0000929void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
930 if (!PrevLoc.isValid()) {
931 PrevLoc = NewLoc;
932 return;
933 }
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000935 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
936 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000938 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000939 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek14856d72009-04-06 23:06:54 +0000941 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000942 if (NewLocClean.asLocation().getExpansionLoc() ==
943 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000944 return;
945
Ted Kremenek2042fc12012-02-24 06:00:00 +0000946 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000947 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000948}
949
950void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Ted Kremeneka301a672009-04-22 18:16:20 +0000952 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
953 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Ted Kremenek14856d72009-04-06 23:06:54 +0000955 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
956
957 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000958 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek14856d72009-04-06 23:06:54 +0000960 // Is the top location context the same as the one for the new location?
961 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000962 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000963 if (IsConsumedExpr(TopContextLoc) &&
964 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000965 TopContextLoc.markDead();
966
Ted Kremenek14856d72009-04-06 23:06:54 +0000967 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000968 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000969
970 return;
971 }
972
973 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000974 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000975 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000977 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000978 CLocs.push_back(ContextLocation(CLoc, true));
979 return;
980 }
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek14856d72009-04-06 23:06:54 +0000983 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000984 return;
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 }
986
987 // Context does not contain the location. Flush it.
988 popLocation();
989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000991 // If we reach here, there is no enclosing context. Just add the edge.
992 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +0000993}
994
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000995bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
996 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
997 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000999 return false;
1000}
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremeneke1baed32009-05-05 23:13:38 +00001002void EdgeBuilder::addExtendedContext(const Stmt *S) {
1003 if (!S)
1004 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
1006 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001007 while (Parent) {
1008 if (isa<CompoundStmt>(Parent))
1009 Parent = PDB.getParent(Parent);
1010 else
1011 break;
1012 }
1013
1014 if (Parent) {
1015 switch (Parent->getStmtClass()) {
1016 case Stmt::DoStmtClass:
1017 case Stmt::ObjCAtSynchronizedStmtClass:
1018 addContext(Parent);
1019 default:
1020 break;
1021 }
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Ted Kremeneke1baed32009-05-05 23:13:38 +00001024 addContext(S);
1025}
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Ted Kremenek14856d72009-04-06 23:06:54 +00001027void EdgeBuilder::addContext(const Stmt *S) {
1028 if (!S)
1029 return;
1030
Ted Kremenek59950d32012-02-24 07:12:52 +00001031 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Ted Kremenek14856d72009-04-06 23:06:54 +00001033 while (!CLocs.empty()) {
1034 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1035
1036 // Is the top location context the same as the one for the new location?
1037 if (TopContextLoc == L)
1038 return;
1039
1040 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001041 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001042 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001043 }
1044
1045 // Context does not contain the location. Flush it.
1046 popLocation();
1047 }
1048
1049 CLocs.push_back(L);
1050}
1051
1052static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1053 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001054 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001055 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001056 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001057 StackDiagVector CallStack;
Ted Kremenek14856d72009-04-06 23:06:54 +00001058
Ted Kremenek9c378f72011-08-12 23:37:29 +00001059 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001060 while (NextNode) {
1061 N = NextNode;
1062 NextNode = GetPredecessorNode(N);
1063 ProgramPoint P = N->getLocation();
1064
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001065 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001066 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1067 const StackFrameContext *LCtx =
1068 CE->getLocationContext()->getCurrentStackFrame();
1069 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1070 PDB.getSourceManager(),
1071 LCtx);
1072 EB.addEdge(Loc, true);
1073 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001074 PathDiagnosticCallPiece *C =
1075 PathDiagnosticCallPiece::construct(N, *CE, SM);
1076 PD.getActivePath().push_front(C);
1077 PD.pushActivePath(&C->path);
Anna Zaks56a938f2012-03-16 23:24:20 +00001078 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001079 break;
1080 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001081
Ted Kremenek2042fc12012-02-24 06:00:00 +00001082 // Pop the call hierarchy if we are done walking the contents
1083 // of a function call.
1084 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001085 // Add an edge to the start of the function.
1086 const Decl *D = CE->getCalleeContext()->getDecl();
1087 PathDiagnosticLocation pos =
1088 PathDiagnosticLocation::createBegin(D, SM);
1089 EB.addEdge(pos);
1090
1091 // Flush all locations, and pop the active path.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001092 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001093 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001094 assert(!PD.getActivePath().empty());
1095 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001096
Ted Kremenek2042fc12012-02-24 06:00:00 +00001097 // The current active path should never be empty. Either we
1098 // just added a bunch of stuff to the top-level path, or
1099 // we have a previous CallExit. If the front of the active
1100 // path is not a PathDiagnosticCallPiece, it means that the
1101 // path terminated within a function call. We must then take the
1102 // current contents of the active path and place it within
1103 // a new PathDiagnosticCallPiece.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001104 PathDiagnosticCallPiece *C =
Ted Kremenek2042fc12012-02-24 06:00:00 +00001105 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +00001106 if (!C) {
1107 const Decl * Caller = CE->getLocationContext()->getDecl();
1108 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1109 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001110 C->setCallee(*CE, SM);
1111 EB.addContext(CE->getCallExpr());
Anna Zaks368a0d52012-03-15 21:13:02 +00001112
1113 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001114 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001115 CallStack.pop_back();
1116 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001117 break;
1118 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001119
1120 // Note that is important that we update the LocationContext
1121 // after looking at CallExits. CallExit basically adds an
1122 // edge in the *caller*, so we don't want to update the LocationContext
1123 // too soon.
1124 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001125
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001126 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001127 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001128 const CFGBlock &Blk = *BE->getSrc();
1129 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001131 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001132 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001133 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001134 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001136 if (!Term) {
1137 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1138 CS = dyn_cast<CompoundStmt>(FS->getBody());
1139 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001140 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001143 PathDiagnosticEventPiece *p =
1144 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001145 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001146 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001148 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001149 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001151 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001152 PathDiagnosticLocation BL =
1153 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001154 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001155 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001158 if (Term)
1159 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001161 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001162 }
1163
Mike Stump1eb44332009-09-09 15:08:12 +00001164 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001165 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1166 const Stmt *stmt = S->getStmt();
1167 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001168 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001169 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001170 }
1171 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001172 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001173 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001174
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001175 break;
1176 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001177
1178
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001179 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001181 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001182 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Anna Zaks8e6431a2011-08-18 22:37:56 +00001184 // Add pieces from custom visitors.
1185 BugReport *R = PDB.getBugReport();
1186 for (BugReport::visitor_iterator I = R->visitor_begin(),
1187 E = R->visitor_end(); I!=E; ++I) {
1188 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001189 const PathDiagnosticLocation &Loc = p->getLocation();
1190 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001191 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001192 updateStackPiecesWithMessage(p, CallStack);
1193
Ted Kremenek8966bc12009-05-06 21:39:49 +00001194 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001195 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001198 }
1199}
1200
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001201//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001202// Methods for BugType and subclasses.
1203//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001204BugType::~BugType() { }
1205
Ted Kremenekcf118d42009-02-04 23:49:09 +00001206void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001207
David Blaikie99ba9e32011-12-20 02:48:34 +00001208void BuiltinBug::anchor() {}
1209
Ted Kremenekcf118d42009-02-04 23:49:09 +00001210//===----------------------------------------------------------------------===//
1211// Methods for BugReport and subclasses.
1212//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001213
David Blaikie99ba9e32011-12-20 02:48:34 +00001214void BugReport::NodeResolver::anchor() {}
1215
Anna Zaks8e6431a2011-08-18 22:37:56 +00001216void BugReport::addVisitor(BugReporterVisitor* visitor) {
1217 if (!visitor)
1218 return;
1219
1220 llvm::FoldingSetNodeID ID;
1221 visitor->Profile(ID);
1222 void *InsertPos;
1223
1224 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1225 delete visitor;
1226 return;
1227 }
1228
1229 CallbacksSet.InsertNode(visitor, InsertPos);
1230 Callbacks = F.add(visitor, Callbacks);
1231}
1232
1233BugReport::~BugReport() {
1234 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001235 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001236 }
1237}
Anna Zakse172e8b2011-08-17 23:00:25 +00001238
1239void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1240 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001241 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001242 if (UniqueingLocation.isValid()) {
1243 UniqueingLocation.Profile(hash);
1244 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001245 Location.Profile(hash);
1246 } else {
1247 assert(ErrorNode);
1248 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1249 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001250
1251 for (SmallVectorImpl<SourceRange>::const_iterator I =
1252 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1253 const SourceRange range = *I;
1254 if (!range.isValid())
1255 continue;
1256 hash.AddInteger(range.getBegin().getRawEncoding());
1257 hash.AddInteger(range.getEnd().getRawEncoding());
1258 }
1259}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001260
Ted Kremenek76aadc32012-03-09 01:13:14 +00001261void BugReport::markInteresting(SymbolRef sym) {
1262 if (!sym)
1263 return;
1264 interestingSymbols.insert(sym);
Jordy Rose8ec588e2012-03-15 22:45:29 +00001265
1266 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
1267 interestingRegions.insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001268}
1269
1270void BugReport::markInteresting(const MemRegion *R) {
1271 if (!R)
1272 return;
1273 R = R->getBaseRegion();
1274 interestingRegions.insert(R);
Jordy Rose8ec588e2012-03-15 22:45:29 +00001275
Ted Kremenek76aadc32012-03-09 01:13:14 +00001276 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1277 interestingSymbols.insert(SR->getSymbol());
1278}
1279
1280void BugReport::markInteresting(SVal V) {
1281 markInteresting(V.getAsRegion());
1282 markInteresting(V.getAsSymbol());
1283}
1284
1285bool BugReport::isInteresting(SVal V) const {
1286 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1287}
1288
1289bool BugReport::isInteresting(SymbolRef sym) const {
1290 if (!sym)
1291 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001292 // We don't currently consider metadata symbols to be interesting
1293 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenek76aadc32012-03-09 01:13:14 +00001294 return interestingSymbols.count(sym);
1295}
1296
1297bool BugReport::isInteresting(const MemRegion *R) const {
1298 if (!R)
1299 return false;
1300 R = R->getBaseRegion();
1301 bool b = interestingRegions.count(R);
1302 if (b)
1303 return true;
1304 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1305 return interestingSymbols.count(SR->getSymbol());
1306 return false;
1307}
1308
1309
Ted Kremenek9c378f72011-08-12 23:37:29 +00001310const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001311 if (!ErrorNode)
1312 return 0;
1313
Tom Care212f6d32010-09-16 03:50:38 +00001314 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001315 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Ted Kremenek9c378f72011-08-12 23:37:29 +00001317 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001318 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001319 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001320 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001321 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001322 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001323 S = GetStmt(ProgP);
1324
1325 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001326}
1327
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001328std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001329BugReport::getRanges() {
1330 // If no custom ranges, add the range of the statement corresponding to
1331 // the error node.
1332 if (Ranges.empty()) {
1333 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1334 addRange(E->getSourceRange());
1335 else
1336 return std::make_pair(ranges_iterator(), ranges_iterator());
1337 }
1338
Anna Zaks14924262011-08-24 20:31:06 +00001339 // User-specified absence of range info.
1340 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1341 return std::make_pair(ranges_iterator(), ranges_iterator());
1342
Anna Zakse172e8b2011-08-17 23:00:25 +00001343 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001344}
1345
Anna Zaks590dd8e2011-09-20 21:38:35 +00001346PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001347 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001348 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001349 "Either Location or ErrorNode should be specified but not both.");
1350
Ted Kremenek9c378f72011-08-12 23:37:29 +00001351 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001352 const LocationContext *LC = ErrorNode->getLocationContext();
1353
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001354 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001355 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001356 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001357 // For binary operators, return the location of the operator.
1358 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001359 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001360
Anna Zaks590dd8e2011-09-20 21:38:35 +00001361 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001362 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001363 } else {
1364 assert(Location.isValid());
1365 return Location;
1366 }
1367
Anna Zaks590dd8e2011-09-20 21:38:35 +00001368 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001369}
1370
Ted Kremenekcf118d42009-02-04 23:49:09 +00001371//===----------------------------------------------------------------------===//
1372// Methods for BugReporter and subclasses.
1373//===----------------------------------------------------------------------===//
1374
1375BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001376 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001377}
1378
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001379GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001380BugReporterData::~BugReporterData() {}
1381
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001382ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001383
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001384ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001385GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1386
Anna Zaks3b030a22011-08-19 01:57:09 +00001387BugReporter::~BugReporter() {
1388 FlushReports();
1389
1390 // Free the bug reports we are tracking.
1391 typedef std::vector<BugReportEquivClass *> ContTy;
1392 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1393 I != E; ++I) {
1394 delete *I;
1395 }
1396}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001397
1398void BugReporter::FlushReports() {
1399 if (BugTypes.isEmpty())
1400 return;
1401
1402 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001403 // warnings and new BugTypes.
1404 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1405 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001406 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001407 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001408 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001409 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001410 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001411 const_cast<BugType*>(*I)->FlushReports(*this);
1412
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001413 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1414 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1415 BugReportEquivClass& EQ = *EI;
1416 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001417 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001418
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001419 // BugReporter owns and deletes only BugTypes created implicitly through
1420 // EmitBasicReport.
1421 // FIXME: There are leaks from checkers that assume that the BugTypes they
1422 // create will be destroyed by the BugReporter.
1423 for (llvm::StringMap<BugType*>::iterator
1424 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1425 delete I->second;
1426
Ted Kremenekcf118d42009-02-04 23:49:09 +00001427 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001428 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001429}
1430
1431//===----------------------------------------------------------------------===//
1432// PathDiagnostics generation.
1433//===----------------------------------------------------------------------===//
1434
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001435static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001436 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001437MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001438 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremenekcf118d42009-02-04 23:49:09 +00001440 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001441 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001442 // error node unless there are two or more error nodes with the same minimum
1443 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001444 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001445 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001446
1447 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001448 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1449 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Ted Kremenekcf118d42009-02-04 23:49:09 +00001451 // Create owning pointers for GTrim and NMap just to ensure that they are
1452 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001453 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1454 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenekcf118d42009-02-04 23:49:09 +00001456 // Find the (first) error node in the trimmed graph. We just need to consult
1457 // the node map (NMap) which maps from nodes in the original graph to nodes
1458 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001459
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001460 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001461 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001462 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001463
Ted Kremenek40406fe2010-12-03 06:52:30 +00001464 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1465 const ExplodedNode *originalNode = nodes[nodeIndex];
1466 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001467 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001468 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001469 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Ted Kremenek938332c2009-05-16 01:11:58 +00001472 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001473
1474 // Create a new (third!) graph with a single path. This is the graph
1475 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001476 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremenek10aa5542009-03-12 23:41:59 +00001478 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001479 // to the root node, and then construct a new graph that contains only
1480 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001481 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001483 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001484 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001486 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001487 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001488 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001490 if (Visited.find(Node) != Visited.end())
1491 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001493 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001495 if (Node->pred_empty()) {
1496 Root = Node;
1497 break;
1498 }
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001500 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001501 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001502 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Ted Kremenek938332c2009-05-16 01:11:58 +00001505 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Ted Kremenek10aa5542009-03-12 23:41:59 +00001507 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001508 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001509 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001510 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001511 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001513 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001514 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001515 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001516 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001518 // Create the equivalent node in the new graph with the same state
1519 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001520 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001522 // Store the mapping to the original node.
1523 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1524 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001525 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001527 // Link up the new node with the previous node.
1528 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001529 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001531 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001533 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001534 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001535 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001536 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001537 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001538 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001539 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001540 }
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001542 // Find the next successor node. We choose the node that is marked
1543 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001544 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1545 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001546 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001548 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001550 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001552 if (I == Visited.end())
1553 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001555 if (!N || I->second < MinVal) {
1556 N = *SI;
1557 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001558 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Ted Kremenek938332c2009-05-16 01:11:58 +00001561 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001562 }
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek938332c2009-05-16 01:11:58 +00001564 assert(First);
1565
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001566 return std::make_pair(std::make_pair(GNew, BM),
1567 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001568}
1569
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001570/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1571/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001572static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001573 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1574 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001576 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001577 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001579 MacroStackTy MacroStack;
1580 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremenek77d09442012-03-02 01:27:31 +00001582 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001583 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001584
1585 PathDiagnosticPiece *piece = I->getPtr();
1586
1587 // Recursively compact calls.
1588 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1589 CompactPathDiagnostic(call->path, SM);
1590 }
1591
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001592 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001593 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001595 // Determine the instantiation location, which is the location we group
1596 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001597 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001598 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001599 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001601 if (Loc.isFileID()) {
1602 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001603 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001604 continue;
1605 }
1606
1607 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001609 // Is the PathDiagnosticPiece within the same macro group?
1610 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001611 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001612 continue;
1613 }
1614
1615 // We aren't in the same group. Are we descending into a new macro
1616 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001617 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001618
1619 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001620 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001621 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001623 // Walk the entire macro stack.
1624 while (!MacroStack.empty()) {
1625 if (InstantiationLoc == MacroStack.back().second) {
1626 MacroGroup = MacroStack.back().first;
1627 break;
1628 }
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001630 if (ParentInstantiationLoc == MacroStack.back().second) {
1631 MacroGroup = MacroStack.back().first;
1632 break;
1633 }
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001635 MacroStack.pop_back();
1636 }
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001638 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1639 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001640 PathDiagnosticMacroPiece *NewGroup =
1641 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001642 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001643
1644 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001645 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001646 else {
1647 assert(InstantiationLoc.isFileID());
1648 Pieces.push_back(NewGroup);
1649 }
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001651 MacroGroup = NewGroup;
1652 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1653 }
1654
1655 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001656 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001657 }
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001659 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001660 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Ted Kremenek77d09442012-03-02 01:27:31 +00001662 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1663 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001664}
1665
Ted Kremenek7dc86642009-03-31 20:22:36 +00001666void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001667 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenek40406fe2010-12-03 06:52:30 +00001669 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001670 SmallVector<const ExplodedNode *, 10> errorNodes;
1671 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001672 E = bugReports.end(); I != E; ++I) {
1673 errorNodes.push_back((*I)->getErrorNode());
1674 }
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001676 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001677 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001678 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001679 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001680 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Ted Kremenekcf118d42009-02-04 23:49:09 +00001682 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001683 assert(GPair.second.second < bugReports.size());
1684 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001685 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001687 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1688 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001689 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001690
1691 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001692 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1693 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Anna Zaks8e6431a2011-08-18 22:37:56 +00001695 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001696 R->addVisitor(new NilReceiverBRVisitor());
1697 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Anna Zaks23f395e2011-08-20 01:27:22 +00001699 // Generate the very last diagnostic piece - the piece is visible before
1700 // the trace is expanded.
1701 PathDiagnosticPiece *LastPiece = 0;
1702 for (BugReport::visitor_iterator I = R->visitor_begin(),
1703 E = R->visitor_end(); I!=E; ++I) {
1704 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1705 assert (!LastPiece &&
1706 "There can only be one final piece in a diagnostic.");
1707 LastPiece = Piece;
1708 }
1709 }
1710 if (!LastPiece)
1711 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1712 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001713 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001714 else
1715 return;
1716
Ted Kremenek7dc86642009-03-31 20:22:36 +00001717 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001718 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001719 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001720 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001721 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001722 GenerateMinimalPathDiagnostic(PD, PDB, N);
1723 break;
1724 }
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001725
1726 // Finally, prune the diagnostic path of uninteresting stuff.
1727 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1728 assert(hasSomethingInteresting);
1729 (void) hasSomethingInteresting;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001730}
1731
Ted Kremenekcf118d42009-02-04 23:49:09 +00001732void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001733 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001734}
1735
Mike Stump1eb44332009-09-09 15:08:12 +00001736void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001737 // Compute the bug report's hash to determine its equivalence class.
1738 llvm::FoldingSetNodeID ID;
1739 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001740
1741 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001742 BugType& BT = R->getBugType();
1743 Register(&BT);
1744 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001745 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001746
Ted Kremenekcf118d42009-02-04 23:49:09 +00001747 if (!EQ) {
1748 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001749 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001750 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001751 }
1752 else
1753 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001754}
1755
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001756
1757//===----------------------------------------------------------------------===//
1758// Emitting reports in equivalence classes.
1759//===----------------------------------------------------------------------===//
1760
1761namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001762struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001763 const ExplodedNode *N;
1764 ExplodedNode::const_succ_iterator I, E;
1765
1766 FRIEC_WLItem(const ExplodedNode *n)
1767 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1768};
1769}
1770
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001771static BugReport *
1772FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001773 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001774
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001775 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1776 assert(I != E);
1777 BugReport *R = *I;
1778 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001779
Ted Kremenek40406fe2010-12-03 06:52:30 +00001780 // If we don't need to suppress any of the nodes because they are
1781 // post-dominated by a sink, simply add all the nodes in the equivalence class
1782 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001783 if (!BT.isSuppressOnSink()) {
1784 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001785 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001786 if (N) {
1787 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001788 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001789 }
1790 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001791 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001792 }
1793
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001794 // For bug reports that should be suppressed when all paths are post-dominated
1795 // by a sink node, iterate through the reports in the equivalence class
1796 // until we find one that isn't post-dominated (if one exists). We use a
1797 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1798 // this as a recursive function, but we don't want to risk blowing out the
1799 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001800 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001801
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001802 for (; I != E; ++I) {
1803 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001804 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001805
Ted Kremenek40406fe2010-12-03 06:52:30 +00001806 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001807 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001808 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001809 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001810 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001811 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001812 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001813 if (errorNode->succ_empty()) {
1814 bugReports.push_back(R);
1815 if (!exampleReport)
1816 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001817 continue;
1818 }
1819
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001820 // At this point we know that 'N' is not a sink and it has at least one
1821 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1822 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001823 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001824 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1825
1826 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001827 WL.push_back(errorNode);
1828 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001829
1830 while (!WL.empty()) {
1831 WLItem &WI = WL.back();
1832 assert(!WI.N->succ_empty());
1833
1834 for (; WI.I != WI.E; ++WI.I) {
1835 const ExplodedNode *Succ = *WI.I;
1836 // End-of-path node?
1837 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001838 // If we found an end-of-path node that is not a sink.
1839 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001840 bugReports.push_back(R);
1841 if (!exampleReport)
1842 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001843 WL.clear();
1844 break;
1845 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001846 // Found a sink? Continue on to the next successor.
1847 continue;
1848 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001849 // Mark the successor as visited. If it hasn't been explored,
1850 // enqueue it to the DFS worklist.
1851 unsigned &mark = Visited[Succ];
1852 if (!mark) {
1853 mark = 1;
1854 WL.push_back(Succ);
1855 break;
1856 }
1857 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001858
1859 // The worklist may have been cleared at this point. First
1860 // check if it is empty before checking the last item.
1861 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001862 WL.pop_back();
1863 }
1864 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001865
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001866 // ExampleReport will be NULL if all the nodes in the equivalence class
1867 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001868 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001869}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001870
1871//===----------------------------------------------------------------------===//
1872// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1873// uses global state, which eventually should go elsewhere.
1874//===----------------------------------------------------------------------===//
1875namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001876class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001877 llvm::FoldingSetNodeID ID;
1878public:
1879 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001880 R->Profile(ID);
1881 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001882 }
1883
1884 void Profile(llvm::FoldingSetNodeID &id) {
1885 id = ID;
1886 }
1887
1888 llvm::FoldingSetNodeID &getID() { return ID; }
1889};
1890}
1891
1892static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1893 // FIXME: Eventually this diagnostic cache should reside in something
1894 // like AnalysisManager instead of being a static variable. This is
1895 // really unsafe in the long term.
1896 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1897 static DiagnosticCache DC;
1898
1899 void *InsertPos;
1900 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1901
1902 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1903 delete Item;
1904 return true;
1905 }
1906
1907 DC.InsertNode(Item, InsertPos);
1908 return false;
1909}
1910
Ted Kremenekcf118d42009-02-04 23:49:09 +00001911void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001912 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001913 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1914 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001915 return;
1916
David Blaikieef3643f2011-09-26 00:51:36 +00001917 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Ted Kremenekcf118d42009-02-04 23:49:09 +00001919 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001920 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001921 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001923 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001924 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001925 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001926 ? exampleReport->getDescription()
1927 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001928 BT.getCategory()));
1929
Ted Kremenek40406fe2010-12-03 06:52:30 +00001930 if (!bugReports.empty())
1931 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Ted Kremenek40406fe2010-12-03 06:52:30 +00001933 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001934 return;
1935
Ted Kremenek072192b2008-04-30 23:47:44 +00001936 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001937 const BugReport::ExtraTextList &Meta =
1938 exampleReport->getExtraText();
1939 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1940 e = Meta.end(); i != e; ++i) {
1941 D->addMeta(*i);
1942 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001943
Ted Kremenek3148eb42009-01-24 00:55:43 +00001944 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001945 BugReport::ranges_iterator Beg, End;
1946 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001947 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001948
1949 // Search the description for '%', as that will be interpretted as a
1950 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001951 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001952 unsigned ErrorDiag;
1953 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001954 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001955 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001956 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001957 if (*I == '%')
1958 Out << "%%";
1959 else
1960 Out << *I;
1961
1962 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001963 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001964 }
Ted Kremenek57202072008-07-14 17:40:50 +00001965
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001966 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001967 DiagnosticBuilder diagBuilder = Diag.Report(
1968 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001969 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001970 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001971 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001972
David Blaikieef3643f2011-09-26 00:51:36 +00001973 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001974 if (!PD)
1975 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Ted Kremenek802e0242012-02-08 04:32:34 +00001977 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001978 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1979 exampleReport->getLocation(getSourceManager()),
1980 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001981
Ted Kremenek3148eb42009-01-24 00:55:43 +00001982 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001983 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Ted Kremenek3148eb42009-01-24 00:55:43 +00001986 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001987}
Ted Kremenek57202072008-07-14 17:40:50 +00001988
Chris Lattner5f9e2722011-07-23 10:55:15 +00001989void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001990 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001991 SourceRange* RBeg, unsigned NumRanges) {
1992 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1993}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001994
Chris Lattner5f9e2722011-07-23 10:55:15 +00001995void BugReporter::EmitBasicReport(StringRef name,
1996 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001997 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001998 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002000 // 'BT' is owned by BugReporter.
2001 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002002 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002003 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2004 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002005}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002006
Chris Lattner5f9e2722011-07-23 10:55:15 +00002007BugType *BugReporter::getBugTypeForName(StringRef name,
2008 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002009 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002010 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2011 llvm::StringMapEntry<BugType *> &
2012 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2013 BugType *BT = entry.getValue();
2014 if (!BT) {
2015 BT = new BugType(name, category);
2016 entry.setValue(BT);
2017 }
2018 return BT;
2019}