blob: b59405bcd2ec63d8e658fac11896ff5b381a1f2b [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 Zaks368a0d52012-03-15 21:13:02 +0000383static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
384 llvm::SmallVector<PathDiagnosticCallPiece*, 6> &CallStack) {
385 // If the piece contains a special message, add it to all the call
386 // pieces on the active stack.
387 if (PathDiagnosticEventPiece *ep =
388 dyn_cast<PathDiagnosticEventPiece>(P)) {
389 StringRef stackMsg = ep->getCallStackMessage();
390
391 if (!stackMsg.empty())
392 for (llvm::SmallVector<PathDiagnosticCallPiece*, 6>::iterator
393 I = CallStack.begin(), E = CallStack.end(); I != E; ++I)
394 // The last message on the path to final bug is the most important
395 // one. Since we traverse the path backwards, do not add the message
396 // if one has been previously added.
397 if (!(*I)->hasCallStackMessage())
398 (*I)->setCallStackMessage(stackMsg);
399 }
400}
Ted Kremenek31061982009-03-31 23:00:32 +0000401
Ted Kremenek77d09442012-03-02 01:27:31 +0000402static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000403
Ted Kremenek31061982009-03-31 23:00:32 +0000404static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
405 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000406 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000407
Ted Kremenek31061982009-03-31 23:00:32 +0000408 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000409 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000410 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000411 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000412
413 llvm::SmallVector<PathDiagnosticCallPiece*, 6> CallStack;
414
Ted Kremenek31061982009-03-31 23:00:32 +0000415 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000416 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000417 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000418 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek31061982009-03-31 23:00:32 +0000420 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000421
422 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
423 PathDiagnosticCallPiece *C =
424 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
425 PD.getActivePath().push_front(C);
426 PD.pushActivePath(&C->path);
Anna Zaks368a0d52012-03-15 21:13:02 +0000427 CallStack.push_back(C);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000428 continue;
429 }
430
431 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
432 PD.popActivePath();
433 // The current active path should never be empty. Either we
434 // just added a bunch of stuff to the top-level path, or
435 // we have a previous CallExit. If the front of the active
436 // path is not a PathDiagnosticCallPiece, it means that the
437 // path terminated within a function call. We must then take the
438 // current contents of the active path and place it within
439 // a new PathDiagnosticCallPiece.
440 assert(!PD.getActivePath().empty());
441 PathDiagnosticCallPiece *C =
442 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +0000443 if (!C) {
444 const Decl *Caller = CE->getLocationContext()->getDecl();
445 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
446 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000447 C->setCallee(*CE, SMgr);
Anna Zaks368a0d52012-03-15 21:13:02 +0000448 if (!CallStack.empty()) {
449 assert(CallStack.back() == C);
450 CallStack.pop_back();
451 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000452 continue;
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek9c378f72011-08-12 23:37:29 +0000455 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
456 const CFGBlock *Src = BE->getSrc();
457 const CFGBlock *Dst = BE->getDst();
458 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek31061982009-03-31 23:00:32 +0000460 if (!T)
461 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anna Zaks590dd8e2011-09-20 21:38:35 +0000463 PathDiagnosticLocation Start =
464 PathDiagnosticLocation::createBegin(T, SMgr,
465 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek31061982009-03-31 23:00:32 +0000467 switch (T->getStmtClass()) {
468 default:
469 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek31061982009-03-31 23:00:32 +0000471 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000472 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000473 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremenek31061982009-03-31 23:00:32 +0000475 if (!S)
476 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek31061982009-03-31 23:00:32 +0000478 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000479 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000480 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek31061982009-03-31 23:00:32 +0000482 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000483 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000484 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000485 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000486 break;
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
489 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000490 // Figure out what case arm we took.
491 std::string sbuf;
492 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek9c378f72011-08-12 23:37:29 +0000494 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000495 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek31061982009-03-31 23:00:32 +0000497 switch (S->getStmtClass()) {
498 default:
499 os << "No cases match in the switch statement. "
500 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000501 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000502 break;
503 case Stmt::DefaultStmtClass:
504 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000505 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000506 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Ted Kremenek31061982009-03-31 23:00:32 +0000508 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000510 const CaseStmt *Case = cast<CaseStmt>(S);
511 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000512
513 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000514 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenek9c378f72011-08-12 23:37:29 +0000516 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000517 // FIXME: Maybe this should be an assertion. Are there cases
518 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000519 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000520 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Ted Kremenek31061982009-03-31 23:00:32 +0000522 if (D) {
523 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000524 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000525 }
526 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000527
528 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000529 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000530
Ted Kremenek31061982009-03-31 23:00:32 +0000531 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000532 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000533 break;
534 }
535 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000536 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000537 os.str()));
538 }
539 else {
540 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000541 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000542 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000543 os.str()));
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Ted Kremenek31061982009-03-31 23:00:32 +0000546 break;
547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Ted Kremenek31061982009-03-31 23:00:32 +0000549 case Stmt::BreakStmtClass:
550 case Stmt::ContinueStmtClass: {
551 std::string sbuf;
552 llvm::raw_string_ostream os(sbuf);
553 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000554 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000555 os.str()));
556 break;
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek31061982009-03-31 23:00:32 +0000559 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000560 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000561 case Stmt::ConditionalOperatorClass: {
562 std::string sbuf;
563 llvm::raw_string_ostream os(sbuf);
564 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Ted Kremenek31061982009-03-31 23:00:32 +0000566 if (*(Src->succ_begin()+1) == Dst)
567 os << "false";
568 else
569 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Ted Kremenek31061982009-03-31 23:00:32 +0000571 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Ted Kremenek31061982009-03-31 23:00:32 +0000573 if (const Stmt *S = End.asStmt())
574 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Ted Kremenek2042fc12012-02-24 06:00:00 +0000576 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000577 os.str()));
578 break;
579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremenek31061982009-03-31 23:00:32 +0000581 // Determine control-flow for short-circuited '&&' and '||'.
582 case Stmt::BinaryOperatorClass: {
583 if (!PDB.supportsLogicalOpControlFlow())
584 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000586 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000587 std::string sbuf;
588 llvm::raw_string_ostream os(sbuf);
589 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000590
John McCall2de56d12010-08-25 11:45:40 +0000591 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000592 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Ted Kremenek31061982009-03-31 23:00:32 +0000594 if (*(Src->succ_begin()+1) == Dst) {
595 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000596 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000597 PathDiagnosticLocation Start =
598 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000599 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000600 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000601 }
Ted Kremenek31061982009-03-31 23:00:32 +0000602 else {
603 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000604 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000605 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
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 }
610 else {
John McCall2de56d12010-08-25 11:45:40 +0000611 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000612 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremenek31061982009-03-31 23:00:32 +0000614 if (*(Src->succ_begin()+1) == Dst) {
615 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000616 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000617 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000618 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000619 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000620 }
621 else {
622 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000623 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000624 PathDiagnosticLocation Start =
625 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000626 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000627 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000628 }
629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Ted Kremenek31061982009-03-31 23:00:32 +0000631 break;
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
634 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000635 if (*(Src->succ_begin()) == Dst) {
636 std::string sbuf;
637 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 os << "Loop condition is true. ";
640 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek31061982009-03-31 23:00:32 +0000642 if (const Stmt *S = End.asStmt())
643 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Ted Kremenek2042fc12012-02-24 06:00:00 +0000645 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000646 os.str()));
647 }
648 else {
649 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek31061982009-03-31 23:00:32 +0000651 if (const Stmt *S = End.asStmt())
652 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek2042fc12012-02-24 06:00:00 +0000654 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000655 "Loop condition is false. Exiting loop"));
656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 break;
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek31061982009-03-31 23:00:32 +0000661 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000662 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000663 if (*(Src->succ_begin()+1) == Dst) {
664 std::string sbuf;
665 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremenek31061982009-03-31 23:00:32 +0000667 os << "Loop condition is false. ";
668 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
669 if (const Stmt *S = End.asStmt())
670 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Ted Kremenek2042fc12012-02-24 06:00:00 +0000672 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000673 os.str()));
674 }
675 else {
676 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
677 if (const Stmt *S = End.asStmt())
678 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremenek2042fc12012-02-24 06:00:00 +0000680 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000681 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000682 }
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremenek31061982009-03-31 23:00:32 +0000684 break;
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremenek31061982009-03-31 23:00:32 +0000687 case Stmt::IfStmtClass: {
688 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek31061982009-03-31 23:00:32 +0000690 if (const Stmt *S = End.asStmt())
691 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Ted Kremenek31061982009-03-31 23:00:32 +0000693 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000694 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000695 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000696 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000697 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000698 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Ted Kremenek31061982009-03-31 23:00:32 +0000700 break;
701 }
702 }
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000705 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000706 // Add diagnostic pieces from custom visitors.
707 BugReport *R = PDB.getBugReport();
708 for (BugReport::visitor_iterator I = R->visitor_begin(),
709 E = R->visitor_end(); I!=E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000710 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000711 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000712 updateStackPiecesWithMessage(p, CallStack);
713 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000714 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000715 }
Ted Kremenek31061982009-03-31 23:00:32 +0000716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek14856d72009-04-06 23:06:54 +0000718 // After constructing the full PathDiagnostic, do a pass over it to compact
719 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000720 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000721}
722
723//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000724// "Extensive" PathDiagnostic generation.
725//===----------------------------------------------------------------------===//
726
727static bool IsControlFlowExpr(const Stmt *S) {
728 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000730 if (!E)
731 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000732
733 E = E->IgnoreParenCasts();
734
John McCall56ca35d2011-02-17 10:25:35 +0000735 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000736 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000738 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
739 if (B->isLogicalOp())
740 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
742 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000743}
744
Ted Kremenek14856d72009-04-06 23:06:54 +0000745namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000746class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000747 bool IsDead;
748public:
749 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
750 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000751
752 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000753 bool isDead() const { return IsDead; }
754};
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000756class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000757 std::vector<ContextLocation> CLocs;
758 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000759 PathDiagnostic &PD;
760 PathDiagnosticBuilder &PDB;
761 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000763 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek14856d72009-04-06 23:06:54 +0000765 bool containsLocation(const PathDiagnosticLocation &Container,
766 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek14856d72009-04-06 23:06:54 +0000768 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek9650cf32009-05-11 21:42:34 +0000770 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
771 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000772 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000773 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000774 while (1) {
775 // Adjust the location for some expressions that are best referenced
776 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000777 switch (S->getStmtClass()) {
778 default:
779 break;
780 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000781 case Stmt::GenericSelectionExprClass:
782 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000783 firstCharOnly = true;
784 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000785 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000786 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000787 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000788 firstCharOnly = true;
789 continue;
790 case Stmt::ChooseExprClass:
791 S = cast<ChooseExpr>(S)->getCond();
792 firstCharOnly = true;
793 continue;
794 case Stmt::BinaryOperatorClass:
795 S = cast<BinaryOperator>(S)->getLHS();
796 firstCharOnly = true;
797 continue;
798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek9650cf32009-05-11 21:42:34 +0000800 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek9650cf32009-05-11 21:42:34 +0000803 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000804 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek9650cf32009-05-11 21:42:34 +0000807 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000808 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000809
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000810 return L;
811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremenek14856d72009-04-06 23:06:54 +0000813 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000814 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000815 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000816 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000817 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000818 CLocs.pop_back();
819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Ted Kremenek14856d72009-04-06 23:06:54 +0000821public:
822 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
823 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremeneka301a672009-04-22 18:16:20 +0000825 // If the PathDiagnostic already has pieces, add the enclosing statement
826 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000827 if (!PD.path.empty()) {
828 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek14856d72009-04-06 23:06:54 +0000830 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000831 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000832 }
833 }
834
835 ~EdgeBuilder() {
836 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000837
Ted Kremeneka301a672009-04-22 18:16:20 +0000838 // Finally, add an initial edge from the start location of the first
839 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000840 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000841 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000842 PDB.getSourceManager());
843 if (L.isValid())
844 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000845 }
846
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000847 void flushLocations() {
848 while (!CLocs.empty())
849 popLocation();
850 PrevLoc = PathDiagnosticLocation();
851 }
852
Ted Kremenek14856d72009-04-06 23:06:54 +0000853 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000855 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek14856d72009-04-06 23:06:54 +0000857 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000858 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000859};
Ted Kremenek14856d72009-04-06 23:06:54 +0000860} // end anonymous namespace
861
862
863PathDiagnosticLocation
864EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
865 if (const Stmt *S = L.asStmt()) {
866 if (IsControlFlowExpr(S))
867 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000868
869 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek14856d72009-04-06 23:06:54 +0000872 return L;
873}
874
875bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
876 const PathDiagnosticLocation &Containee) {
877
878 if (Container == Containee)
879 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek14856d72009-04-06 23:06:54 +0000881 if (Container.asDecl())
882 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek14856d72009-04-06 23:06:54 +0000884 if (const Stmt *S = Containee.asStmt())
885 if (const Stmt *ContainerS = Container.asStmt()) {
886 while (S) {
887 if (S == ContainerS)
888 return true;
889 S = PDB.getParent(S);
890 }
891 return false;
892 }
893
894 // Less accurate: compare using source ranges.
895 SourceRange ContainerR = Container.asRange();
896 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek14856d72009-04-06 23:06:54 +0000898 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000899 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
900 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
901 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
902 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Chandler Carruth64211622011-07-25 21:09:52 +0000904 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
905 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
906 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
907 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Ted Kremenek14856d72009-04-06 23:06:54 +0000909 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000910 assert(ContaineeBegLine <= ContaineeEndLine);
911
Ted Kremenek14856d72009-04-06 23:06:54 +0000912 return (ContainerBegLine <= ContaineeBegLine &&
913 ContainerEndLine >= ContaineeEndLine &&
914 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000915 SM.getExpansionColumnNumber(ContainerRBeg) <=
916 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000917 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000918 SM.getExpansionColumnNumber(ContainerREnd) >=
919 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000920}
921
Ted Kremenek14856d72009-04-06 23:06:54 +0000922void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
923 if (!PrevLoc.isValid()) {
924 PrevLoc = NewLoc;
925 return;
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000928 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
929 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000931 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000932 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek14856d72009-04-06 23:06:54 +0000934 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000935 if (NewLocClean.asLocation().getExpansionLoc() ==
936 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000937 return;
938
Ted Kremenek2042fc12012-02-24 06:00:00 +0000939 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000940 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000941}
942
943void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Ted Kremeneka301a672009-04-22 18:16:20 +0000945 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
946 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Ted Kremenek14856d72009-04-06 23:06:54 +0000948 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
949
950 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000951 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Ted Kremenek14856d72009-04-06 23:06:54 +0000953 // Is the top location context the same as the one for the new location?
954 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000955 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000956 if (IsConsumedExpr(TopContextLoc) &&
957 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000958 TopContextLoc.markDead();
959
Ted Kremenek14856d72009-04-06 23:06:54 +0000960 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000961 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000962
963 return;
964 }
965
966 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000967 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000968 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000970 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000971 CLocs.push_back(ContextLocation(CLoc, true));
972 return;
973 }
974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Ted Kremenek14856d72009-04-06 23:06:54 +0000976 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000977 return;
Ted Kremenek14856d72009-04-06 23:06:54 +0000978 }
979
980 // Context does not contain the location. Flush it.
981 popLocation();
982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000984 // If we reach here, there is no enclosing context. Just add the edge.
985 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +0000986}
987
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000988bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
989 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
990 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000992 return false;
993}
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Ted Kremeneke1baed32009-05-05 23:13:38 +0000995void EdgeBuilder::addExtendedContext(const Stmt *S) {
996 if (!S)
997 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
999 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001000 while (Parent) {
1001 if (isa<CompoundStmt>(Parent))
1002 Parent = PDB.getParent(Parent);
1003 else
1004 break;
1005 }
1006
1007 if (Parent) {
1008 switch (Parent->getStmtClass()) {
1009 case Stmt::DoStmtClass:
1010 case Stmt::ObjCAtSynchronizedStmtClass:
1011 addContext(Parent);
1012 default:
1013 break;
1014 }
1015 }
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremeneke1baed32009-05-05 23:13:38 +00001017 addContext(S);
1018}
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek14856d72009-04-06 23:06:54 +00001020void EdgeBuilder::addContext(const Stmt *S) {
1021 if (!S)
1022 return;
1023
Ted Kremenek59950d32012-02-24 07:12:52 +00001024 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek14856d72009-04-06 23:06:54 +00001026 while (!CLocs.empty()) {
1027 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1028
1029 // Is the top location context the same as the one for the new location?
1030 if (TopContextLoc == L)
1031 return;
1032
1033 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001034 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001035 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001036 }
1037
1038 // Context does not contain the location. Flush it.
1039 popLocation();
1040 }
1041
1042 CLocs.push_back(L);
1043}
1044
1045static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1046 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001047 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001048 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001049 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks368a0d52012-03-15 21:13:02 +00001050 llvm::SmallVector<PathDiagnosticCallPiece*, 6> CallStack;
Ted Kremenek14856d72009-04-06 23:06:54 +00001051
Ted Kremenek9c378f72011-08-12 23:37:29 +00001052 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001053 while (NextNode) {
1054 N = NextNode;
1055 NextNode = GetPredecessorNode(N);
1056 ProgramPoint P = N->getLocation();
1057
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001058 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001059 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1060 const StackFrameContext *LCtx =
1061 CE->getLocationContext()->getCurrentStackFrame();
1062 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1063 PDB.getSourceManager(),
1064 LCtx);
1065 EB.addEdge(Loc, true);
1066 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001067 PathDiagnosticCallPiece *C =
1068 PathDiagnosticCallPiece::construct(N, *CE, SM);
1069 PD.getActivePath().push_front(C);
1070 PD.pushActivePath(&C->path);
Anna Zaks368a0d52012-03-15 21:13:02 +00001071 CallStack.push_back(C);
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001072 break;
1073 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001074
Ted Kremenek2042fc12012-02-24 06:00:00 +00001075 // Pop the call hierarchy if we are done walking the contents
1076 // of a function call.
1077 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001078 // Add an edge to the start of the function.
1079 const Decl *D = CE->getCalleeContext()->getDecl();
1080 PathDiagnosticLocation pos =
1081 PathDiagnosticLocation::createBegin(D, SM);
1082 EB.addEdge(pos);
1083
1084 // Flush all locations, and pop the active path.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001085 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001086 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001087 assert(!PD.getActivePath().empty());
1088 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001089
Ted Kremenek2042fc12012-02-24 06:00:00 +00001090 // The current active path should never be empty. Either we
1091 // just added a bunch of stuff to the top-level path, or
1092 // we have a previous CallExit. If the front of the active
1093 // path is not a PathDiagnosticCallPiece, it means that the
1094 // path terminated within a function call. We must then take the
1095 // current contents of the active path and place it within
1096 // a new PathDiagnosticCallPiece.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001097 PathDiagnosticCallPiece *C =
Ted Kremenek2042fc12012-02-24 06:00:00 +00001098 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +00001099 if (!C) {
1100 const Decl * Caller = CE->getLocationContext()->getDecl();
1101 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1102 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001103 C->setCallee(*CE, SM);
1104 EB.addContext(CE->getCallExpr());
Anna Zaks368a0d52012-03-15 21:13:02 +00001105
1106 if (!CallStack.empty()) {
1107 assert(CallStack.back() == C);
1108 CallStack.pop_back();
1109 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001110 break;
1111 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001112
1113 // Note that is important that we update the LocationContext
1114 // after looking at CallExits. CallExit basically adds an
1115 // edge in the *caller*, so we don't want to update the LocationContext
1116 // too soon.
1117 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001118
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001119 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001120 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001121 const CFGBlock &Blk = *BE->getSrc();
1122 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001124 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001125 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001126 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001127 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001129 if (!Term) {
1130 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1131 CS = dyn_cast<CompoundStmt>(FS->getBody());
1132 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001133 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001136 PathDiagnosticEventPiece *p =
1137 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001138 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001139 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001141 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001142 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001144 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001145 PathDiagnosticLocation BL =
1146 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001147 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001148 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001151 if (Term)
1152 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001154 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001155 }
1156
Mike Stump1eb44332009-09-09 15:08:12 +00001157 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001158 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1159 const Stmt *stmt = S->getStmt();
1160 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001161 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001162 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001163 }
1164 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001165 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001166 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001167
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001168 break;
1169 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001170
1171
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001172 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001174 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001175 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Anna Zaks8e6431a2011-08-18 22:37:56 +00001177 // Add pieces from custom visitors.
1178 BugReport *R = PDB.getBugReport();
1179 for (BugReport::visitor_iterator I = R->visitor_begin(),
1180 E = R->visitor_end(); I!=E; ++I) {
1181 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001182 const PathDiagnosticLocation &Loc = p->getLocation();
1183 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001184 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001185 updateStackPiecesWithMessage(p, CallStack);
1186
Ted Kremenek8966bc12009-05-06 21:39:49 +00001187 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001188 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001189 }
Mike Stump1eb44332009-09-09 15:08:12 +00001190 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001191 }
1192}
1193
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001194//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001195// Methods for BugType and subclasses.
1196//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001197BugType::~BugType() { }
1198
Ted Kremenekcf118d42009-02-04 23:49:09 +00001199void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001200
David Blaikie99ba9e32011-12-20 02:48:34 +00001201void BuiltinBug::anchor() {}
1202
Ted Kremenekcf118d42009-02-04 23:49:09 +00001203//===----------------------------------------------------------------------===//
1204// Methods for BugReport and subclasses.
1205//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001206
David Blaikie99ba9e32011-12-20 02:48:34 +00001207void BugReport::NodeResolver::anchor() {}
1208
Anna Zaks8e6431a2011-08-18 22:37:56 +00001209void BugReport::addVisitor(BugReporterVisitor* visitor) {
1210 if (!visitor)
1211 return;
1212
1213 llvm::FoldingSetNodeID ID;
1214 visitor->Profile(ID);
1215 void *InsertPos;
1216
1217 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1218 delete visitor;
1219 return;
1220 }
1221
1222 CallbacksSet.InsertNode(visitor, InsertPos);
1223 Callbacks = F.add(visitor, Callbacks);
1224}
1225
1226BugReport::~BugReport() {
1227 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001228 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001229 }
1230}
Anna Zakse172e8b2011-08-17 23:00:25 +00001231
1232void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1233 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001234 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001235 if (UniqueingLocation.isValid()) {
1236 UniqueingLocation.Profile(hash);
1237 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001238 Location.Profile(hash);
1239 } else {
1240 assert(ErrorNode);
1241 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1242 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001243
1244 for (SmallVectorImpl<SourceRange>::const_iterator I =
1245 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1246 const SourceRange range = *I;
1247 if (!range.isValid())
1248 continue;
1249 hash.AddInteger(range.getBegin().getRawEncoding());
1250 hash.AddInteger(range.getEnd().getRawEncoding());
1251 }
1252}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001253
Ted Kremenek76aadc32012-03-09 01:13:14 +00001254void BugReport::markInteresting(SymbolRef sym) {
1255 if (!sym)
1256 return;
1257 interestingSymbols.insert(sym);
Jordy Rose8ec588e2012-03-15 22:45:29 +00001258
1259 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
1260 interestingRegions.insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001261}
1262
1263void BugReport::markInteresting(const MemRegion *R) {
1264 if (!R)
1265 return;
1266 R = R->getBaseRegion();
1267 interestingRegions.insert(R);
Jordy Rose8ec588e2012-03-15 22:45:29 +00001268
Ted Kremenek76aadc32012-03-09 01:13:14 +00001269 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1270 interestingSymbols.insert(SR->getSymbol());
1271}
1272
1273void BugReport::markInteresting(SVal V) {
1274 markInteresting(V.getAsRegion());
1275 markInteresting(V.getAsSymbol());
1276}
1277
1278bool BugReport::isInteresting(SVal V) const {
1279 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1280}
1281
1282bool BugReport::isInteresting(SymbolRef sym) const {
1283 if (!sym)
1284 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001285 // We don't currently consider metadata symbols to be interesting
1286 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenek76aadc32012-03-09 01:13:14 +00001287 return interestingSymbols.count(sym);
1288}
1289
1290bool BugReport::isInteresting(const MemRegion *R) const {
1291 if (!R)
1292 return false;
1293 R = R->getBaseRegion();
1294 bool b = interestingRegions.count(R);
1295 if (b)
1296 return true;
1297 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1298 return interestingSymbols.count(SR->getSymbol());
1299 return false;
1300}
1301
1302
Ted Kremenek9c378f72011-08-12 23:37:29 +00001303const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001304 if (!ErrorNode)
1305 return 0;
1306
Tom Care212f6d32010-09-16 03:50:38 +00001307 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001308 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Ted Kremenek9c378f72011-08-12 23:37:29 +00001310 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001311 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001312 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001313 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001314 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001315 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001316 S = GetStmt(ProgP);
1317
1318 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001319}
1320
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001321std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001322BugReport::getRanges() {
1323 // If no custom ranges, add the range of the statement corresponding to
1324 // the error node.
1325 if (Ranges.empty()) {
1326 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1327 addRange(E->getSourceRange());
1328 else
1329 return std::make_pair(ranges_iterator(), ranges_iterator());
1330 }
1331
Anna Zaks14924262011-08-24 20:31:06 +00001332 // User-specified absence of range info.
1333 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1334 return std::make_pair(ranges_iterator(), ranges_iterator());
1335
Anna Zakse172e8b2011-08-17 23:00:25 +00001336 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001337}
1338
Anna Zaks590dd8e2011-09-20 21:38:35 +00001339PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001340 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001341 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001342 "Either Location or ErrorNode should be specified but not both.");
1343
Ted Kremenek9c378f72011-08-12 23:37:29 +00001344 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001345 const LocationContext *LC = ErrorNode->getLocationContext();
1346
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001347 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001348 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001349 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001350 // For binary operators, return the location of the operator.
1351 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001352 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001353
Anna Zaks590dd8e2011-09-20 21:38:35 +00001354 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001355 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001356 } else {
1357 assert(Location.isValid());
1358 return Location;
1359 }
1360
Anna Zaks590dd8e2011-09-20 21:38:35 +00001361 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001362}
1363
Ted Kremenekcf118d42009-02-04 23:49:09 +00001364//===----------------------------------------------------------------------===//
1365// Methods for BugReporter and subclasses.
1366//===----------------------------------------------------------------------===//
1367
1368BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001369 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001370}
1371
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001372GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001373BugReporterData::~BugReporterData() {}
1374
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001375ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001377ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001378GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1379
Anna Zaks3b030a22011-08-19 01:57:09 +00001380BugReporter::~BugReporter() {
1381 FlushReports();
1382
1383 // Free the bug reports we are tracking.
1384 typedef std::vector<BugReportEquivClass *> ContTy;
1385 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1386 I != E; ++I) {
1387 delete *I;
1388 }
1389}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001390
1391void BugReporter::FlushReports() {
1392 if (BugTypes.isEmpty())
1393 return;
1394
1395 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001396 // warnings and new BugTypes.
1397 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1398 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001399 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001400 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001401 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001402 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001403 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001404 const_cast<BugType*>(*I)->FlushReports(*this);
1405
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001406 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1407 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1408 BugReportEquivClass& EQ = *EI;
1409 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001410 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001411
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001412 // BugReporter owns and deletes only BugTypes created implicitly through
1413 // EmitBasicReport.
1414 // FIXME: There are leaks from checkers that assume that the BugTypes they
1415 // create will be destroyed by the BugReporter.
1416 for (llvm::StringMap<BugType*>::iterator
1417 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1418 delete I->second;
1419
Ted Kremenekcf118d42009-02-04 23:49:09 +00001420 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001421 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001422}
1423
1424//===----------------------------------------------------------------------===//
1425// PathDiagnostics generation.
1426//===----------------------------------------------------------------------===//
1427
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001428static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001429 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001430MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001431 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Ted Kremenekcf118d42009-02-04 23:49:09 +00001433 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001434 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001435 // error node unless there are two or more error nodes with the same minimum
1436 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001437 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001438 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001439
1440 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001441 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1442 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Ted Kremenekcf118d42009-02-04 23:49:09 +00001444 // Create owning pointers for GTrim and NMap just to ensure that they are
1445 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001446 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1447 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Ted Kremenekcf118d42009-02-04 23:49:09 +00001449 // Find the (first) error node in the trimmed graph. We just need to consult
1450 // the node map (NMap) which maps from nodes in the original graph to nodes
1451 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001452
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001453 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001454 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001455 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001456
Ted Kremenek40406fe2010-12-03 06:52:30 +00001457 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1458 const ExplodedNode *originalNode = nodes[nodeIndex];
1459 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001460 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001461 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001462 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001463 }
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek938332c2009-05-16 01:11:58 +00001465 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001466
1467 // Create a new (third!) graph with a single path. This is the graph
1468 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001469 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Ted Kremenek10aa5542009-03-12 23:41:59 +00001471 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 // to the root node, and then construct a new graph that contains only
1473 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001474 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001476 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001477 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001479 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001480 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001481 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001483 if (Visited.find(Node) != Visited.end())
1484 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001486 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001488 if (Node->pred_empty()) {
1489 Root = Node;
1490 break;
1491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001493 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001494 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001495 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001496 }
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek938332c2009-05-16 01:11:58 +00001498 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Ted Kremenek10aa5542009-03-12 23:41:59 +00001500 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001501 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001502 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001503 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001504 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001506 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001507 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001508 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001509 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001511 // Create the equivalent node in the new graph with the same state
1512 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001513 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001515 // Store the mapping to the original node.
1516 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1517 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001518 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001520 // Link up the new node with the previous node.
1521 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001522 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001524 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001526 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001527 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001528 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001529 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001530 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001531 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001532 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001535 // Find the next successor node. We choose the node that is marked
1536 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001537 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1538 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001539 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001541 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001543 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001545 if (I == Visited.end())
1546 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001548 if (!N || I->second < MinVal) {
1549 N = *SI;
1550 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001551 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Ted Kremenek938332c2009-05-16 01:11:58 +00001554 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Ted Kremenek938332c2009-05-16 01:11:58 +00001557 assert(First);
1558
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001559 return std::make_pair(std::make_pair(GNew, BM),
1560 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001561}
1562
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001563/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1564/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001565static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001566 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1567 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001569 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001570 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001572 MacroStackTy MacroStack;
1573 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Ted Kremenek77d09442012-03-02 01:27:31 +00001575 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001576 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001577
1578 PathDiagnosticPiece *piece = I->getPtr();
1579
1580 // Recursively compact calls.
1581 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1582 CompactPathDiagnostic(call->path, SM);
1583 }
1584
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001585 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001586 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001588 // Determine the instantiation location, which is the location we group
1589 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001590 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001591 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001592 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001594 if (Loc.isFileID()) {
1595 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001596 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001597 continue;
1598 }
1599
1600 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001602 // Is the PathDiagnosticPiece within the same macro group?
1603 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001604 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001605 continue;
1606 }
1607
1608 // We aren't in the same group. Are we descending into a new macro
1609 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001610 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001611
1612 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001613 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001614 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001616 // Walk the entire macro stack.
1617 while (!MacroStack.empty()) {
1618 if (InstantiationLoc == MacroStack.back().second) {
1619 MacroGroup = MacroStack.back().first;
1620 break;
1621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001623 if (ParentInstantiationLoc == MacroStack.back().second) {
1624 MacroGroup = MacroStack.back().first;
1625 break;
1626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001628 MacroStack.pop_back();
1629 }
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001631 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1632 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001633 PathDiagnosticMacroPiece *NewGroup =
1634 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001635 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001636
1637 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001638 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001639 else {
1640 assert(InstantiationLoc.isFileID());
1641 Pieces.push_back(NewGroup);
1642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001644 MacroGroup = NewGroup;
1645 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1646 }
1647
1648 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001649 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001652 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001653 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Ted Kremenek77d09442012-03-02 01:27:31 +00001655 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1656 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001657}
1658
Ted Kremenek7dc86642009-03-31 20:22:36 +00001659void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001660 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Ted Kremenek40406fe2010-12-03 06:52:30 +00001662 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001663 SmallVector<const ExplodedNode *, 10> errorNodes;
1664 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001665 E = bugReports.end(); I != E; ++I) {
1666 errorNodes.push_back((*I)->getErrorNode());
1667 }
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001669 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001670 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001671 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001672 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001673 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Ted Kremenekcf118d42009-02-04 23:49:09 +00001675 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001676 assert(GPair.second.second < bugReports.size());
1677 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001678 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001680 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1681 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001682 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001683
1684 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001685 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1686 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Anna Zaks8e6431a2011-08-18 22:37:56 +00001688 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001689 R->addVisitor(new NilReceiverBRVisitor());
1690 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Anna Zaks23f395e2011-08-20 01:27:22 +00001692 // Generate the very last diagnostic piece - the piece is visible before
1693 // the trace is expanded.
1694 PathDiagnosticPiece *LastPiece = 0;
1695 for (BugReport::visitor_iterator I = R->visitor_begin(),
1696 E = R->visitor_end(); I!=E; ++I) {
1697 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1698 assert (!LastPiece &&
1699 "There can only be one final piece in a diagnostic.");
1700 LastPiece = Piece;
1701 }
1702 }
1703 if (!LastPiece)
1704 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1705 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001706 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001707 else
1708 return;
1709
Ted Kremenek7dc86642009-03-31 20:22:36 +00001710 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001711 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001712 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001713 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001714 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001715 GenerateMinimalPathDiagnostic(PD, PDB, N);
1716 break;
1717 }
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001718
1719 // Finally, prune the diagnostic path of uninteresting stuff.
1720 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1721 assert(hasSomethingInteresting);
1722 (void) hasSomethingInteresting;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001723}
1724
Ted Kremenekcf118d42009-02-04 23:49:09 +00001725void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001726 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001727}
1728
Mike Stump1eb44332009-09-09 15:08:12 +00001729void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001730 // Compute the bug report's hash to determine its equivalence class.
1731 llvm::FoldingSetNodeID ID;
1732 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001733
1734 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001735 BugType& BT = R->getBugType();
1736 Register(&BT);
1737 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001738 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Ted Kremenekcf118d42009-02-04 23:49:09 +00001740 if (!EQ) {
1741 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001742 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001743 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001744 }
1745 else
1746 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001747}
1748
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001749
1750//===----------------------------------------------------------------------===//
1751// Emitting reports in equivalence classes.
1752//===----------------------------------------------------------------------===//
1753
1754namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001755struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001756 const ExplodedNode *N;
1757 ExplodedNode::const_succ_iterator I, E;
1758
1759 FRIEC_WLItem(const ExplodedNode *n)
1760 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1761};
1762}
1763
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001764static BugReport *
1765FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001766 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001767
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001768 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1769 assert(I != E);
1770 BugReport *R = *I;
1771 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001772
Ted Kremenek40406fe2010-12-03 06:52:30 +00001773 // If we don't need to suppress any of the nodes because they are
1774 // post-dominated by a sink, simply add all the nodes in the equivalence class
1775 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001776 if (!BT.isSuppressOnSink()) {
1777 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001778 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001779 if (N) {
1780 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001781 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001782 }
1783 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001784 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001785 }
1786
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001787 // For bug reports that should be suppressed when all paths are post-dominated
1788 // by a sink node, iterate through the reports in the equivalence class
1789 // until we find one that isn't post-dominated (if one exists). We use a
1790 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1791 // this as a recursive function, but we don't want to risk blowing out the
1792 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001793 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001794
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001795 for (; I != E; ++I) {
1796 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001797 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001798
Ted Kremenek40406fe2010-12-03 06:52:30 +00001799 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001800 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001801 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001802 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001803 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001804 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001805 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001806 if (errorNode->succ_empty()) {
1807 bugReports.push_back(R);
1808 if (!exampleReport)
1809 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001810 continue;
1811 }
1812
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001813 // At this point we know that 'N' is not a sink and it has at least one
1814 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1815 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001816 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001817 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1818
1819 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001820 WL.push_back(errorNode);
1821 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001822
1823 while (!WL.empty()) {
1824 WLItem &WI = WL.back();
1825 assert(!WI.N->succ_empty());
1826
1827 for (; WI.I != WI.E; ++WI.I) {
1828 const ExplodedNode *Succ = *WI.I;
1829 // End-of-path node?
1830 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001831 // If we found an end-of-path node that is not a sink.
1832 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001833 bugReports.push_back(R);
1834 if (!exampleReport)
1835 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001836 WL.clear();
1837 break;
1838 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001839 // Found a sink? Continue on to the next successor.
1840 continue;
1841 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001842 // Mark the successor as visited. If it hasn't been explored,
1843 // enqueue it to the DFS worklist.
1844 unsigned &mark = Visited[Succ];
1845 if (!mark) {
1846 mark = 1;
1847 WL.push_back(Succ);
1848 break;
1849 }
1850 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001851
1852 // The worklist may have been cleared at this point. First
1853 // check if it is empty before checking the last item.
1854 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001855 WL.pop_back();
1856 }
1857 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001858
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001859 // ExampleReport will be NULL if all the nodes in the equivalence class
1860 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001861 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001862}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001863
1864//===----------------------------------------------------------------------===//
1865// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1866// uses global state, which eventually should go elsewhere.
1867//===----------------------------------------------------------------------===//
1868namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001869class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001870 llvm::FoldingSetNodeID ID;
1871public:
1872 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001873 R->Profile(ID);
1874 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001875 }
1876
1877 void Profile(llvm::FoldingSetNodeID &id) {
1878 id = ID;
1879 }
1880
1881 llvm::FoldingSetNodeID &getID() { return ID; }
1882};
1883}
1884
1885static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1886 // FIXME: Eventually this diagnostic cache should reside in something
1887 // like AnalysisManager instead of being a static variable. This is
1888 // really unsafe in the long term.
1889 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1890 static DiagnosticCache DC;
1891
1892 void *InsertPos;
1893 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1894
1895 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1896 delete Item;
1897 return true;
1898 }
1899
1900 DC.InsertNode(Item, InsertPos);
1901 return false;
1902}
1903
Ted Kremenekcf118d42009-02-04 23:49:09 +00001904void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001905 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001906 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1907 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001908 return;
1909
David Blaikieef3643f2011-09-26 00:51:36 +00001910 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Ted Kremenekcf118d42009-02-04 23:49:09 +00001912 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001913 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001914 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001916 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001917 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001918 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001919 ? exampleReport->getDescription()
1920 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001921 BT.getCategory()));
1922
Ted Kremenek40406fe2010-12-03 06:52:30 +00001923 if (!bugReports.empty())
1924 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Ted Kremenek40406fe2010-12-03 06:52:30 +00001926 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001927 return;
1928
Ted Kremenek072192b2008-04-30 23:47:44 +00001929 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001930 const BugReport::ExtraTextList &Meta =
1931 exampleReport->getExtraText();
1932 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1933 e = Meta.end(); i != e; ++i) {
1934 D->addMeta(*i);
1935 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001936
Ted Kremenek3148eb42009-01-24 00:55:43 +00001937 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001938 BugReport::ranges_iterator Beg, End;
1939 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001940 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001941
1942 // Search the description for '%', as that will be interpretted as a
1943 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001944 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001945 unsigned ErrorDiag;
1946 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001947 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001948 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001949 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001950 if (*I == '%')
1951 Out << "%%";
1952 else
1953 Out << *I;
1954
1955 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001956 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001957 }
Ted Kremenek57202072008-07-14 17:40:50 +00001958
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001959 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001960 DiagnosticBuilder diagBuilder = Diag.Report(
1961 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001962 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001963 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001964 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001965
David Blaikieef3643f2011-09-26 00:51:36 +00001966 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001967 if (!PD)
1968 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Ted Kremenek802e0242012-02-08 04:32:34 +00001970 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001971 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1972 exampleReport->getLocation(getSourceManager()),
1973 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001974
Ted Kremenek3148eb42009-01-24 00:55:43 +00001975 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001976 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001977 }
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Ted Kremenek3148eb42009-01-24 00:55:43 +00001979 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001980}
Ted Kremenek57202072008-07-14 17:40:50 +00001981
Chris Lattner5f9e2722011-07-23 10:55:15 +00001982void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001983 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001984 SourceRange* RBeg, unsigned NumRanges) {
1985 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1986}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001987
Chris Lattner5f9e2722011-07-23 10:55:15 +00001988void BugReporter::EmitBasicReport(StringRef name,
1989 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001990 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001991 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001993 // 'BT' is owned by BugReporter.
1994 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001995 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001996 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1997 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001998}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001999
Chris Lattner5f9e2722011-07-23 10:55:15 +00002000BugType *BugReporter::getBugTypeForName(StringRef name,
2001 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002002 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002003 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2004 llvm::StringMapEntry<BugType *> &
2005 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2006 BugType *BT = entry.getValue();
2007 if (!BT) {
2008 BT = new BugType(name, category);
2009 entry.setValue(BT);
2010 }
2011 return BT;
2012}