blob: 4ada636e8e93c1ad41ff8d0c318af8c5bae129b3 [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenekb697b102009-02-23 22:44:26 +000052 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000053}
54
Zhongxing Xuc5619d92009-08-06 01:32:16 +000055static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000056GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000057 return N->pred_empty() ? NULL : *(N->pred_begin());
58}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000059
Zhongxing Xuc5619d92009-08-06 01:32:16 +000060static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000061GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000062 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000063}
64
Ted Kremenek9c378f72011-08-12 23:37:29 +000065static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000067 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000068 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekb697b102009-02-23 22:44:26 +000070 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000071}
72
Ted Kremenek9c378f72011-08-12 23:37:29 +000073static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000074 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000075 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000076 // Check if the statement is '?' or '&&'/'||'. These are "merges",
77 // not actual statement points.
78 switch (S->getStmtClass()) {
79 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000080 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000081 case Stmt::ConditionalOperatorClass: continue;
82 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000083 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
84 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 continue;
86 break;
87 }
88 default:
89 break;
90 }
Ted Kremenekb697b102009-02-23 22:44:26 +000091 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekb697b102009-02-23 22:44:26 +000094 return 0;
95}
96
Ted Kremenek5f85e172009-07-22 22:35:28 +000097static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000098GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000099 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000100 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekb697b102009-02-23 22:44:26 +0000102 return GetPreviousStmt(N);
103}
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek5f85e172009-07-22 22:35:28 +0000105static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000106GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000107 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000108 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return GetNextStmt(N);
111}
112
113//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000114// Diagnostic cleanup.
115//===----------------------------------------------------------------------===//
116
117/// Recursively scan through a path and prune out calls and macros pieces
118/// that aren't needed. Return true if afterwards the path contains
119/// "interesting stuff" which means it should be pruned from the parent path.
120static bool RemoveUneededCalls(PathPieces &pieces) {
121 bool containsSomethingInteresting = false;
122 const unsigned N = pieces.size();
123
124 for (unsigned i = 0 ; i < N ; ++i) {
125 // Remove the front piece from the path. If it is still something we
126 // want to keep once we are done, we will push it back on the end.
127 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
128 pieces.pop_front();
129
Ted Kremenek72516742012-03-01 00:05:06 +0000130 switch (piece->getKind()) {
131 case PathDiagnosticPiece::Call: {
132 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
133 // Recursively clean out the subclass. Keep this call around if
134 // it contains any informative diagnostics.
135 if (!RemoveUneededCalls(call->path))
136 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000137 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000138 break;
139 }
140 case PathDiagnosticPiece::Macro: {
141 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
142 if (!RemoveUneededCalls(macro->subPieces))
143 continue;
144 containsSomethingInteresting = true;
145 break;
146 }
147 case PathDiagnosticPiece::Event: {
148 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
149 // We never throw away an event, but we do throw it away wholesale
150 // as part of a path if we throw the entire path away.
Ted Kremenek76aadc32012-03-09 01:13:14 +0000151 if (event->isPrunable())
152 continue;
153 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000154 break;
155 }
156 case PathDiagnosticPiece::ControlFlow:
157 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000158 }
159
160 pieces.push_back(piece);
161 }
162
163 return containsSomethingInteresting;
164}
165
166//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000167// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000168//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000169
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000170typedef llvm::DenseMap<const ExplodedNode*,
171const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000172
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000173namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000174class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000175 NodeBackMap& M;
176public:
177 NodeMapClosure(NodeBackMap *m) : M(*m) {}
178 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek9c378f72011-08-12 23:37:29 +0000180 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000181 NodeBackMap::iterator I = M.find(N);
182 return I == M.end() ? 0 : I->second;
183 }
184};
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000186class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000187 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000188 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000189 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000190 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000191public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000192 const LocationContext *LC;
193
Ted Kremenek8966bc12009-05-06 21:39:49 +0000194 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000195 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000196 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000197 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000198 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
199 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek9c378f72011-08-12 23:37:29 +0000201 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek9c378f72011-08-12 23:37:29 +0000203 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
204 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Anna Zaks8e6431a2011-08-18 22:37:56 +0000206 BugReport *getBugReport() { return R; }
207
Tom Care212f6d32010-09-16 03:50:38 +0000208 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000209
210 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000212 const Stmt *getParent(const Stmt *S) {
213 return getParentMap().getParent(S);
214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek8966bc12009-05-06 21:39:49 +0000216 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000217
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000218 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
David Blaikieef3643f2011-09-26 00:51:36 +0000220 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
221 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000222 }
223
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000224 bool supportsLogicalOpControlFlow() const {
225 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000226 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000227};
228} // end anonymous namespace
229
Ted Kremenek00605e02009-03-27 20:55:39 +0000230PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000231PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000232 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000233 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000234
Anna Zaks0cd59482011-09-16 19:18:30 +0000235 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
236 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000237}
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek00605e02009-03-27 20:55:39 +0000239PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000240PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
241 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000242
Ted Kremenek143ca222008-05-06 18:11:09 +0000243 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000244 if (os.str().empty())
245 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek00605e02009-03-27 20:55:39 +0000247 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek00605e02009-03-27 20:55:39 +0000249 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000250 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000251 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000252 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000253 else {
254 os << "Execution jumps to the end of the ";
255 const Decl *D = N->getLocationContext()->getDecl();
256 if (isa<ObjCMethodDecl>(D))
257 os << "method";
258 else if (isa<FunctionDecl>(D))
259 os << "function";
260 else {
261 assert(isa<BlockDecl>(D));
262 os << "anonymous block";
263 }
264 os << '.';
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000267 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000268}
269
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000270static bool IsNested(const Stmt *S, ParentMap &PM) {
271 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
272 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000274 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000276 if (Parent)
277 switch (Parent->getStmtClass()) {
278 case Stmt::ForStmtClass:
279 case Stmt::DoStmtClass:
280 case Stmt::WhileStmtClass:
281 return true;
282 default:
283 break;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000287}
288
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000289PathDiagnosticLocation
290PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000291 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000292 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000293 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000294
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000295 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000296 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000298 if (!Parent)
299 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000301 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000302 case Stmt::BinaryOperatorClass: {
303 const BinaryOperator *B = cast<BinaryOperator>(Parent);
304 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000305 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000306 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000307 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000308 case Stmt::CompoundStmtClass:
309 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000310 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000311 case Stmt::ChooseExprClass:
312 // Similar to '?' if we are referring to condition, just have the edge
313 // point to the entire choose expression.
314 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000315 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000316 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000317 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000318 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000319 case Stmt::ConditionalOperatorClass:
320 // For '?', if we are referring to condition, just have the edge point
321 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000322 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000323 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000324 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000325 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000326 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000327 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000328 case Stmt::ForStmtClass:
329 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000330 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000331 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000332 case Stmt::IfStmtClass:
333 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000335 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000336 case Stmt::ObjCForCollectionStmtClass:
337 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000338 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000339 break;
340 case Stmt::WhileStmtClass:
341 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000342 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000343 break;
344 default:
345 break;
346 }
347
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000348 S = Parent;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000351 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000352
353 // Special case: DeclStmts can appear in for statement declarations, in which
354 // case the ForStmt is the context.
355 if (isa<DeclStmt>(S)) {
356 if (const Stmt *Parent = P.getParent(S)) {
357 switch (Parent->getStmtClass()) {
358 case Stmt::ForStmtClass:
359 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000360 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000361 default:
362 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000363 }
364 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000365 }
366 else if (isa<BinaryOperator>(S)) {
367 // Special case: the binary operator represents the initialization
368 // code in a for statement (this can happen when the variable being
369 // initialized is an old variable.
370 if (const ForStmt *FS =
371 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
372 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000373 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000374 }
375 }
376
Anna Zaks220ac8c2011-09-15 01:08:34 +0000377 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000378}
379
Ted Kremenekcf118d42009-02-04 23:49:09 +0000380//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000381// "Minimal" path diagnostic generation algorithm.
382//===----------------------------------------------------------------------===//
383
Ted Kremenek77d09442012-03-02 01:27:31 +0000384static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000385
Ted Kremenek31061982009-03-31 23:00:32 +0000386static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
387 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000388 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000389
Ted Kremenek31061982009-03-31 23:00:32 +0000390 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000391 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000392 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000393 ? NULL : *(N->pred_begin());
394 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000395 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000396 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000397 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek31061982009-03-31 23:00:32 +0000399 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000400
401 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
402 PathDiagnosticCallPiece *C =
403 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
404 PD.getActivePath().push_front(C);
405 PD.pushActivePath(&C->path);
406 continue;
407 }
408
409 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
410 PD.popActivePath();
411 // The current active path should never be empty. Either we
412 // just added a bunch of stuff to the top-level path, or
413 // we have a previous CallExit. If the front of the active
414 // path is not a PathDiagnosticCallPiece, it means that the
415 // path terminated within a function call. We must then take the
416 // current contents of the active path and place it within
417 // a new PathDiagnosticCallPiece.
418 assert(!PD.getActivePath().empty());
419 PathDiagnosticCallPiece *C =
420 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
421 if (!C)
422 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
423 C->setCallee(*CE, SMgr);
424 continue;
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek9c378f72011-08-12 23:37:29 +0000427 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
428 const CFGBlock *Src = BE->getSrc();
429 const CFGBlock *Dst = BE->getDst();
430 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek31061982009-03-31 23:00:32 +0000432 if (!T)
433 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anna Zaks590dd8e2011-09-20 21:38:35 +0000435 PathDiagnosticLocation Start =
436 PathDiagnosticLocation::createBegin(T, SMgr,
437 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek31061982009-03-31 23:00:32 +0000439 switch (T->getStmtClass()) {
440 default:
441 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Ted Kremenek31061982009-03-31 23:00:32 +0000443 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000444 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000445 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek31061982009-03-31 23:00:32 +0000447 if (!S)
448 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Ted Kremenek31061982009-03-31 23:00:32 +0000450 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000451 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000452 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek31061982009-03-31 23:00:32 +0000454 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000455 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000456 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000457 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000458 break;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
461 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000462 // Figure out what case arm we took.
463 std::string sbuf;
464 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Ted Kremenek9c378f72011-08-12 23:37:29 +0000466 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000467 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek31061982009-03-31 23:00:32 +0000469 switch (S->getStmtClass()) {
470 default:
471 os << "No cases match in the switch statement. "
472 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000473 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000474 break;
475 case Stmt::DefaultStmtClass:
476 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000477 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000478 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek31061982009-03-31 23:00:32 +0000480 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000481 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000482 const CaseStmt *Case = cast<CaseStmt>(S);
483 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000484
485 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000486 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Ted Kremenek9c378f72011-08-12 23:37:29 +0000488 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000489 // FIXME: Maybe this should be an assertion. Are there cases
490 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000491 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000492 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 if (D) {
495 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000496 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000497 }
498 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000499
500 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000501 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000502
Ted Kremenek31061982009-03-31 23:00:32 +0000503 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000504 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000505 break;
506 }
507 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000508 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000509 os.str()));
510 }
511 else {
512 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000513 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000514 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000515 os.str()));
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Ted Kremenek31061982009-03-31 23:00:32 +0000518 break;
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek31061982009-03-31 23:00:32 +0000521 case Stmt::BreakStmtClass:
522 case Stmt::ContinueStmtClass: {
523 std::string sbuf;
524 llvm::raw_string_ostream os(sbuf);
525 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000526 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000527 os.str()));
528 break;
529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Ted Kremenek31061982009-03-31 23:00:32 +0000531 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000532 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000533 case Stmt::ConditionalOperatorClass: {
534 std::string sbuf;
535 llvm::raw_string_ostream os(sbuf);
536 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Ted Kremenek31061982009-03-31 23:00:32 +0000538 if (*(Src->succ_begin()+1) == Dst)
539 os << "false";
540 else
541 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek31061982009-03-31 23:00:32 +0000543 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 if (const Stmt *S = End.asStmt())
546 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek2042fc12012-02-24 06:00:00 +0000548 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000549 os.str()));
550 break;
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek31061982009-03-31 23:00:32 +0000553 // Determine control-flow for short-circuited '&&' and '||'.
554 case Stmt::BinaryOperatorClass: {
555 if (!PDB.supportsLogicalOpControlFlow())
556 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000558 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000559 std::string sbuf;
560 llvm::raw_string_ostream os(sbuf);
561 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000562
John McCall2de56d12010-08-25 11:45:40 +0000563 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000564 os << "&&" << "' 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";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000568 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000569 PathDiagnosticLocation Start =
570 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000571 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000572 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000573 }
Ted Kremenek31061982009-03-31 23:00:32 +0000574 else {
575 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000576 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000577 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000578 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000579 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000580 }
Ted Kremenek31061982009-03-31 23:00:32 +0000581 }
582 else {
John McCall2de56d12010-08-25 11:45:40 +0000583 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000584 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Ted Kremenek31061982009-03-31 23:00:32 +0000586 if (*(Src->succ_begin()+1) == Dst) {
587 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000588 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000589 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000590 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000591 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000592 }
593 else {
594 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000595 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000596 PathDiagnosticLocation Start =
597 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000598 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000599 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000600 }
601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Ted Kremenek31061982009-03-31 23:00:32 +0000603 break;
604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
606 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000607 if (*(Src->succ_begin()) == Dst) {
608 std::string sbuf;
609 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Ted Kremenek31061982009-03-31 23:00:32 +0000611 os << "Loop condition is true. ";
612 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremenek31061982009-03-31 23:00:32 +0000614 if (const Stmt *S = End.asStmt())
615 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek2042fc12012-02-24 06:00:00 +0000617 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000618 os.str()));
619 }
620 else {
621 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenek31061982009-03-31 23:00:32 +0000623 if (const Stmt *S = End.asStmt())
624 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek2042fc12012-02-24 06:00:00 +0000626 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000627 "Loop condition is false. Exiting loop"));
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Ted Kremenek31061982009-03-31 23:00:32 +0000630 break;
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek31061982009-03-31 23:00:32 +0000633 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000634 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000635 if (*(Src->succ_begin()+1) == 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 false. ";
640 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
641 if (const Stmt *S = End.asStmt())
642 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek2042fc12012-02-24 06:00:00 +0000644 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000645 os.str()));
646 }
647 else {
648 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
649 if (const Stmt *S = End.asStmt())
650 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek2042fc12012-02-24 06:00:00 +0000652 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000653 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek31061982009-03-31 23:00:32 +0000656 break;
657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Ted Kremenek31061982009-03-31 23:00:32 +0000659 case Stmt::IfStmtClass: {
660 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek31061982009-03-31 23:00:32 +0000662 if (const Stmt *S = End.asStmt())
663 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Ted Kremenek31061982009-03-31 23:00:32 +0000665 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000666 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000667 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000668 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000669 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000670 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Ted Kremenek31061982009-03-31 23:00:32 +0000672 break;
673 }
674 }
675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000677 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000678 // Add diagnostic pieces from custom visitors.
679 BugReport *R = PDB.getBugReport();
680 for (BugReport::visitor_iterator I = R->visitor_begin(),
681 E = R->visitor_end(); I!=E; ++I) {
682 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenek2042fc12012-02-24 06:00:00 +0000683 PD.getActivePath().push_front(p);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000684 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000685 }
Ted Kremenek31061982009-03-31 23:00:32 +0000686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenek14856d72009-04-06 23:06:54 +0000688 // After constructing the full PathDiagnostic, do a pass over it to compact
689 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000690 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000691}
692
693//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000694// "Extensive" PathDiagnostic generation.
695//===----------------------------------------------------------------------===//
696
697static bool IsControlFlowExpr(const Stmt *S) {
698 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000700 if (!E)
701 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000702
703 E = E->IgnoreParenCasts();
704
John McCall56ca35d2011-02-17 10:25:35 +0000705 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000706 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000708 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
709 if (B->isLogicalOp())
710 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000711
712 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000713}
714
Ted Kremenek14856d72009-04-06 23:06:54 +0000715namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000716class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000717 bool IsDead;
718public:
719 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
720 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000721
722 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000723 bool isDead() const { return IsDead; }
724};
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000726class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000727 std::vector<ContextLocation> CLocs;
728 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000729 PathDiagnostic &PD;
730 PathDiagnosticBuilder &PDB;
731 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000733 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek14856d72009-04-06 23:06:54 +0000735 bool containsLocation(const PathDiagnosticLocation &Container,
736 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek14856d72009-04-06 23:06:54 +0000738 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek9650cf32009-05-11 21:42:34 +0000740 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
741 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000742 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000743 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000744 while (1) {
745 // Adjust the location for some expressions that are best referenced
746 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000747 switch (S->getStmtClass()) {
748 default:
749 break;
750 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000751 case Stmt::GenericSelectionExprClass:
752 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000753 firstCharOnly = true;
754 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000755 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000756 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000757 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000758 firstCharOnly = true;
759 continue;
760 case Stmt::ChooseExprClass:
761 S = cast<ChooseExpr>(S)->getCond();
762 firstCharOnly = true;
763 continue;
764 case Stmt::BinaryOperatorClass:
765 S = cast<BinaryOperator>(S)->getLHS();
766 firstCharOnly = true;
767 continue;
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek9650cf32009-05-11 21:42:34 +0000770 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek9650cf32009-05-11 21:42:34 +0000773 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000774 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek9650cf32009-05-11 21:42:34 +0000777 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000778 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000779
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000780 return L;
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek14856d72009-04-06 23:06:54 +0000783 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000784 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000785 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000786 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000787 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000788 CLocs.pop_back();
789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek14856d72009-04-06 23:06:54 +0000791public:
792 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
793 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremeneka301a672009-04-22 18:16:20 +0000795 // If the PathDiagnostic already has pieces, add the enclosing statement
796 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000797 if (!PD.path.empty()) {
798 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek14856d72009-04-06 23:06:54 +0000800 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000801 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000802 }
803 }
804
805 ~EdgeBuilder() {
806 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000807
Ted Kremeneka301a672009-04-22 18:16:20 +0000808 // Finally, add an initial edge from the start location of the first
809 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000810 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000811 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000812 PDB.getSourceManager());
813 if (L.isValid())
814 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000815 }
816
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000817 void flushLocations() {
818 while (!CLocs.empty())
819 popLocation();
820 PrevLoc = PathDiagnosticLocation();
821 }
822
Ted Kremenek14856d72009-04-06 23:06:54 +0000823 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000825 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek14856d72009-04-06 23:06:54 +0000827 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000828 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000829};
Ted Kremenek14856d72009-04-06 23:06:54 +0000830} // end anonymous namespace
831
832
833PathDiagnosticLocation
834EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
835 if (const Stmt *S = L.asStmt()) {
836 if (IsControlFlowExpr(S))
837 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
839 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek14856d72009-04-06 23:06:54 +0000842 return L;
843}
844
845bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
846 const PathDiagnosticLocation &Containee) {
847
848 if (Container == Containee)
849 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek14856d72009-04-06 23:06:54 +0000851 if (Container.asDecl())
852 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek14856d72009-04-06 23:06:54 +0000854 if (const Stmt *S = Containee.asStmt())
855 if (const Stmt *ContainerS = Container.asStmt()) {
856 while (S) {
857 if (S == ContainerS)
858 return true;
859 S = PDB.getParent(S);
860 }
861 return false;
862 }
863
864 // Less accurate: compare using source ranges.
865 SourceRange ContainerR = Container.asRange();
866 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenek14856d72009-04-06 23:06:54 +0000868 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000869 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
870 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
871 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
872 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chandler Carruth64211622011-07-25 21:09:52 +0000874 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
875 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
876 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
877 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek14856d72009-04-06 23:06:54 +0000879 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000880 assert(ContaineeBegLine <= ContaineeEndLine);
881
Ted Kremenek14856d72009-04-06 23:06:54 +0000882 return (ContainerBegLine <= ContaineeBegLine &&
883 ContainerEndLine >= ContaineeEndLine &&
884 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000885 SM.getExpansionColumnNumber(ContainerRBeg) <=
886 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000887 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000888 SM.getExpansionColumnNumber(ContainerREnd) >=
889 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000890}
891
Ted Kremenek14856d72009-04-06 23:06:54 +0000892void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
893 if (!PrevLoc.isValid()) {
894 PrevLoc = NewLoc;
895 return;
896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000898 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
899 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000901 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000902 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Ted Kremenek14856d72009-04-06 23:06:54 +0000904 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000905 if (NewLocClean.asLocation().getExpansionLoc() ==
906 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000907 return;
908
Ted Kremenek2042fc12012-02-24 06:00:00 +0000909 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000910 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000911}
912
913void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremeneka301a672009-04-22 18:16:20 +0000915 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
916 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremenek14856d72009-04-06 23:06:54 +0000918 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
919
920 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000921 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremenek14856d72009-04-06 23:06:54 +0000923 // Is the top location context the same as the one for the new location?
924 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000925 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000926 if (IsConsumedExpr(TopContextLoc) &&
927 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000928 TopContextLoc.markDead();
929
Ted Kremenek14856d72009-04-06 23:06:54 +0000930 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000931 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000932
933 return;
934 }
935
936 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000937 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000938 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000940 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000941 CLocs.push_back(ContextLocation(CLoc, true));
942 return;
943 }
944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Ted Kremenek14856d72009-04-06 23:06:54 +0000946 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000947 return;
Ted Kremenek14856d72009-04-06 23:06:54 +0000948 }
949
950 // Context does not contain the location. Flush it.
951 popLocation();
952 }
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000954 // If we reach here, there is no enclosing context. Just add the edge.
955 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +0000956}
957
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000958bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
959 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
960 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000962 return false;
963}
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Ted Kremeneke1baed32009-05-05 23:13:38 +0000965void EdgeBuilder::addExtendedContext(const Stmt *S) {
966 if (!S)
967 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
969 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000970 while (Parent) {
971 if (isa<CompoundStmt>(Parent))
972 Parent = PDB.getParent(Parent);
973 else
974 break;
975 }
976
977 if (Parent) {
978 switch (Parent->getStmtClass()) {
979 case Stmt::DoStmtClass:
980 case Stmt::ObjCAtSynchronizedStmtClass:
981 addContext(Parent);
982 default:
983 break;
984 }
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremeneke1baed32009-05-05 23:13:38 +0000987 addContext(S);
988}
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Ted Kremenek14856d72009-04-06 23:06:54 +0000990void EdgeBuilder::addContext(const Stmt *S) {
991 if (!S)
992 return;
993
Ted Kremenek59950d32012-02-24 07:12:52 +0000994 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek14856d72009-04-06 23:06:54 +0000996 while (!CLocs.empty()) {
997 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
998
999 // Is the top location context the same as the one for the new location?
1000 if (TopContextLoc == L)
1001 return;
1002
1003 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001005 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 }
1007
1008 // Context does not contain the location. Flush it.
1009 popLocation();
1010 }
1011
1012 CLocs.push_back(L);
1013}
1014
1015static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1016 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001017 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001018 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001019 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001020
Ted Kremenek9c378f72011-08-12 23:37:29 +00001021 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001022 while (NextNode) {
1023 N = NextNode;
1024 NextNode = GetPredecessorNode(N);
1025 ProgramPoint P = N->getLocation();
1026
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001027 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001028 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1029 const StackFrameContext *LCtx =
1030 CE->getLocationContext()->getCurrentStackFrame();
1031 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1032 PDB.getSourceManager(),
1033 LCtx);
1034 EB.addEdge(Loc, true);
1035 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001036 PathDiagnosticCallPiece *C =
1037 PathDiagnosticCallPiece::construct(N, *CE, SM);
1038 PD.getActivePath().push_front(C);
1039 PD.pushActivePath(&C->path);
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001040 break;
1041 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001042
Ted Kremenek2042fc12012-02-24 06:00:00 +00001043 // Pop the call hierarchy if we are done walking the contents
1044 // of a function call.
1045 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001046 // Add an edge to the start of the function.
1047 const Decl *D = CE->getCalleeContext()->getDecl();
1048 PathDiagnosticLocation pos =
1049 PathDiagnosticLocation::createBegin(D, SM);
1050 EB.addEdge(pos);
1051
1052 // Flush all locations, and pop the active path.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001053 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001054 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001055 assert(!PD.getActivePath().empty());
1056 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001057
Ted Kremenek2042fc12012-02-24 06:00:00 +00001058 // The current active path should never be empty. Either we
1059 // just added a bunch of stuff to the top-level path, or
1060 // we have a previous CallExit. If the front of the active
1061 // path is not a PathDiagnosticCallPiece, it means that the
1062 // path terminated within a function call. We must then take the
1063 // current contents of the active path and place it within
1064 // a new PathDiagnosticCallPiece.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001065 PathDiagnosticCallPiece *C =
Ted Kremenek2042fc12012-02-24 06:00:00 +00001066 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1067 if (!C)
1068 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
1069 C->setCallee(*CE, SM);
1070 EB.addContext(CE->getCallExpr());
1071 break;
1072 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001073
1074 // Note that is important that we update the LocationContext
1075 // after looking at CallExits. CallExit basically adds an
1076 // edge in the *caller*, so we don't want to update the LocationContext
1077 // too soon.
1078 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001079
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001080 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001081 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001082 const CFGBlock &Blk = *BE->getSrc();
1083 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001085 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001086 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001087 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001088 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001090 if (!Term) {
1091 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1092 CS = dyn_cast<CompoundStmt>(FS->getBody());
1093 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001094 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001097 PathDiagnosticEventPiece *p =
1098 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001099 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001100 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001102 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001103 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001105 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001106 PathDiagnosticLocation BL =
1107 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001108 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001109 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001112 if (Term)
1113 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001115 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001116 }
1117
Mike Stump1eb44332009-09-09 15:08:12 +00001118 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001119 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1120 const Stmt *stmt = S->getStmt();
1121 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001122 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001123 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001124 }
1125 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001126 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001127 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001128
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001129 break;
1130 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001131
1132
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001133 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001135 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001136 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Anna Zaks8e6431a2011-08-18 22:37:56 +00001138 // Add pieces from custom visitors.
1139 BugReport *R = PDB.getBugReport();
1140 for (BugReport::visitor_iterator I = R->visitor_begin(),
1141 E = R->visitor_end(); I!=E; ++I) {
1142 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001143 const PathDiagnosticLocation &Loc = p->getLocation();
1144 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001145 PD.getActivePath().push_front(p);
Ted Kremenek8966bc12009-05-06 21:39:49 +00001146 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001147 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001150 }
1151}
1152
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001153//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001154// Methods for BugType and subclasses.
1155//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001156BugType::~BugType() { }
1157
Ted Kremenekcf118d42009-02-04 23:49:09 +00001158void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001159
David Blaikie99ba9e32011-12-20 02:48:34 +00001160void BuiltinBug::anchor() {}
1161
Ted Kremenekcf118d42009-02-04 23:49:09 +00001162//===----------------------------------------------------------------------===//
1163// Methods for BugReport and subclasses.
1164//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001165
David Blaikie99ba9e32011-12-20 02:48:34 +00001166void BugReport::NodeResolver::anchor() {}
1167
Anna Zaks8e6431a2011-08-18 22:37:56 +00001168void BugReport::addVisitor(BugReporterVisitor* visitor) {
1169 if (!visitor)
1170 return;
1171
1172 llvm::FoldingSetNodeID ID;
1173 visitor->Profile(ID);
1174 void *InsertPos;
1175
1176 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1177 delete visitor;
1178 return;
1179 }
1180
1181 CallbacksSet.InsertNode(visitor, InsertPos);
1182 Callbacks = F.add(visitor, Callbacks);
1183}
1184
1185BugReport::~BugReport() {
1186 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001187 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001188 }
1189}
Anna Zakse172e8b2011-08-17 23:00:25 +00001190
1191void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1192 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001193 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001194 if (UniqueingLocation.isValid()) {
1195 UniqueingLocation.Profile(hash);
1196 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001197 Location.Profile(hash);
1198 } else {
1199 assert(ErrorNode);
1200 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1201 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001202
1203 for (SmallVectorImpl<SourceRange>::const_iterator I =
1204 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1205 const SourceRange range = *I;
1206 if (!range.isValid())
1207 continue;
1208 hash.AddInteger(range.getBegin().getRawEncoding());
1209 hash.AddInteger(range.getEnd().getRawEncoding());
1210 }
1211}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001212
Ted Kremenek76aadc32012-03-09 01:13:14 +00001213void BugReport::markInteresting(SymbolRef sym) {
1214 if (!sym)
1215 return;
1216 interestingSymbols.insert(sym);
1217}
1218
1219void BugReport::markInteresting(const MemRegion *R) {
1220 if (!R)
1221 return;
1222 R = R->getBaseRegion();
1223 interestingRegions.insert(R);
1224
1225 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1226 interestingSymbols.insert(SR->getSymbol());
1227}
1228
1229void BugReport::markInteresting(SVal V) {
1230 markInteresting(V.getAsRegion());
1231 markInteresting(V.getAsSymbol());
1232}
1233
1234bool BugReport::isInteresting(SVal V) const {
1235 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1236}
1237
1238bool BugReport::isInteresting(SymbolRef sym) const {
1239 if (!sym)
1240 return false;
1241 return interestingSymbols.count(sym);
1242}
1243
1244bool BugReport::isInteresting(const MemRegion *R) const {
1245 if (!R)
1246 return false;
1247 R = R->getBaseRegion();
1248 bool b = interestingRegions.count(R);
1249 if (b)
1250 return true;
1251 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1252 return interestingSymbols.count(SR->getSymbol());
1253 return false;
1254}
1255
1256
Ted Kremenek9c378f72011-08-12 23:37:29 +00001257const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001258 if (!ErrorNode)
1259 return 0;
1260
Tom Care212f6d32010-09-16 03:50:38 +00001261 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001262 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Ted Kremenek9c378f72011-08-12 23:37:29 +00001264 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001265 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001266 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001267 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001268 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001269 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001270 S = GetStmt(ProgP);
1271
1272 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001273}
1274
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001275std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001276BugReport::getRanges() {
1277 // If no custom ranges, add the range of the statement corresponding to
1278 // the error node.
1279 if (Ranges.empty()) {
1280 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1281 addRange(E->getSourceRange());
1282 else
1283 return std::make_pair(ranges_iterator(), ranges_iterator());
1284 }
1285
Anna Zaks14924262011-08-24 20:31:06 +00001286 // User-specified absence of range info.
1287 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1288 return std::make_pair(ranges_iterator(), ranges_iterator());
1289
Anna Zakse172e8b2011-08-17 23:00:25 +00001290 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001291}
1292
Anna Zaks590dd8e2011-09-20 21:38:35 +00001293PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001294 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001295 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001296 "Either Location or ErrorNode should be specified but not both.");
1297
Ted Kremenek9c378f72011-08-12 23:37:29 +00001298 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001299 const LocationContext *LC = ErrorNode->getLocationContext();
1300
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001301 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001302 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001303 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001304 // For binary operators, return the location of the operator.
1305 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001306 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001307
Anna Zaks590dd8e2011-09-20 21:38:35 +00001308 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001309 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001310 } else {
1311 assert(Location.isValid());
1312 return Location;
1313 }
1314
Anna Zaks590dd8e2011-09-20 21:38:35 +00001315 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001316}
1317
Ted Kremenekcf118d42009-02-04 23:49:09 +00001318//===----------------------------------------------------------------------===//
1319// Methods for BugReporter and subclasses.
1320//===----------------------------------------------------------------------===//
1321
1322BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001323 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001324}
1325
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001326GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001327BugReporterData::~BugReporterData() {}
1328
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001329ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001330
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001331ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001332GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1333
Anna Zaks3b030a22011-08-19 01:57:09 +00001334BugReporter::~BugReporter() {
1335 FlushReports();
1336
1337 // Free the bug reports we are tracking.
1338 typedef std::vector<BugReportEquivClass *> ContTy;
1339 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1340 I != E; ++I) {
1341 delete *I;
1342 }
1343}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001344
1345void BugReporter::FlushReports() {
1346 if (BugTypes.isEmpty())
1347 return;
1348
1349 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001350 // warnings and new BugTypes.
1351 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1352 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001353 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001354 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001355 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001356 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001357 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001358 const_cast<BugType*>(*I)->FlushReports(*this);
1359
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001360 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1361 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1362 BugReportEquivClass& EQ = *EI;
1363 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001364 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001365
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001366 // BugReporter owns and deletes only BugTypes created implicitly through
1367 // EmitBasicReport.
1368 // FIXME: There are leaks from checkers that assume that the BugTypes they
1369 // create will be destroyed by the BugReporter.
1370 for (llvm::StringMap<BugType*>::iterator
1371 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1372 delete I->second;
1373
Ted Kremenekcf118d42009-02-04 23:49:09 +00001374 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001375 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376}
1377
1378//===----------------------------------------------------------------------===//
1379// PathDiagnostics generation.
1380//===----------------------------------------------------------------------===//
1381
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001382static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001383 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001384MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001385 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenekcf118d42009-02-04 23:49:09 +00001387 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001388 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001389 // error node unless there are two or more error nodes with the same minimum
1390 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001391 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001392 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001393
1394 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001395 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1396 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Ted Kremenekcf118d42009-02-04 23:49:09 +00001398 // Create owning pointers for GTrim and NMap just to ensure that they are
1399 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001400 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1401 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenekcf118d42009-02-04 23:49:09 +00001403 // Find the (first) error node in the trimmed graph. We just need to consult
1404 // the node map (NMap) which maps from nodes in the original graph to nodes
1405 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001406
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001407 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001408 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001409 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001410
Ted Kremenek40406fe2010-12-03 06:52:30 +00001411 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1412 const ExplodedNode *originalNode = nodes[nodeIndex];
1413 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001414 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001415 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001416 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Ted Kremenek938332c2009-05-16 01:11:58 +00001419 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001420
1421 // Create a new (third!) graph with a single path. This is the graph
1422 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001423 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek10aa5542009-03-12 23:41:59 +00001425 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001426 // to the root node, and then construct a new graph that contains only
1427 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001428 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001430 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001431 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001433 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001434 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001435 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001437 if (Visited.find(Node) != Visited.end())
1438 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001440 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001442 if (Node->pred_empty()) {
1443 Root = Node;
1444 break;
1445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001447 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001448 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001449 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001450 }
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Ted Kremenek938332c2009-05-16 01:11:58 +00001452 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Ted Kremenek10aa5542009-03-12 23:41:59 +00001454 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001455 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001456 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001457 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001458 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001460 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001461 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001462 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001463 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 // Create the equivalent node in the new graph with the same state
1466 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001467 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001469 // Store the mapping to the original node.
1470 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1471 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001472 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001474 // Link up the new node with the previous node.
1475 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001476 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001478 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001480 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001481 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001482 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001483 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001485 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001486 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001487 }
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001489 // Find the next successor node. We choose the node that is marked
1490 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001491 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1492 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001493 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001495 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001497 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001499 if (I == Visited.end())
1500 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001502 if (!N || I->second < MinVal) {
1503 N = *SI;
1504 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001505 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001506 }
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Ted Kremenek938332c2009-05-16 01:11:58 +00001508 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ted Kremenek938332c2009-05-16 01:11:58 +00001511 assert(First);
1512
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001513 return std::make_pair(std::make_pair(GNew, BM),
1514 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001515}
1516
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001517/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1518/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001519static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001520 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1521 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001523 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001524 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001526 MacroStackTy MacroStack;
1527 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Ted Kremenek77d09442012-03-02 01:27:31 +00001529 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001530 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001531
1532 PathDiagnosticPiece *piece = I->getPtr();
1533
1534 // Recursively compact calls.
1535 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1536 CompactPathDiagnostic(call->path, SM);
1537 }
1538
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001539 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001540 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001542 // Determine the instantiation location, which is the location we group
1543 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001544 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001545 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001546 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001548 if (Loc.isFileID()) {
1549 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001550 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001551 continue;
1552 }
1553
1554 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001556 // Is the PathDiagnosticPiece within the same macro group?
1557 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001558 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001559 continue;
1560 }
1561
1562 // We aren't in the same group. Are we descending into a new macro
1563 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001564 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001565
1566 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001567 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001568 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001570 // Walk the entire macro stack.
1571 while (!MacroStack.empty()) {
1572 if (InstantiationLoc == MacroStack.back().second) {
1573 MacroGroup = MacroStack.back().first;
1574 break;
1575 }
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001577 if (ParentInstantiationLoc == MacroStack.back().second) {
1578 MacroGroup = MacroStack.back().first;
1579 break;
1580 }
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001582 MacroStack.pop_back();
1583 }
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001585 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1586 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001587 PathDiagnosticMacroPiece *NewGroup =
1588 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001589 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001590
1591 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001592 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001593 else {
1594 assert(InstantiationLoc.isFileID());
1595 Pieces.push_back(NewGroup);
1596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001598 MacroGroup = NewGroup;
1599 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1600 }
1601
1602 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001603 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001606 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001607 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Ted Kremenek77d09442012-03-02 01:27:31 +00001609 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1610 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001611}
1612
Ted Kremenek7dc86642009-03-31 20:22:36 +00001613void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001614 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Ted Kremenek40406fe2010-12-03 06:52:30 +00001616 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001617 SmallVector<const ExplodedNode *, 10> errorNodes;
1618 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001619 E = bugReports.end(); I != E; ++I) {
1620 errorNodes.push_back((*I)->getErrorNode());
1621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001623 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001624 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001625 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001626 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001627 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremenekcf118d42009-02-04 23:49:09 +00001629 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001630 assert(GPair.second.second < bugReports.size());
1631 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001632 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001634 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1635 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001636 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001637
1638 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001639 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1640 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Anna Zaks8e6431a2011-08-18 22:37:56 +00001642 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001643 R->addVisitor(new NilReceiverBRVisitor());
1644 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Anna Zaks23f395e2011-08-20 01:27:22 +00001646 // Generate the very last diagnostic piece - the piece is visible before
1647 // the trace is expanded.
1648 PathDiagnosticPiece *LastPiece = 0;
1649 for (BugReport::visitor_iterator I = R->visitor_begin(),
1650 E = R->visitor_end(); I!=E; ++I) {
1651 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1652 assert (!LastPiece &&
1653 "There can only be one final piece in a diagnostic.");
1654 LastPiece = Piece;
1655 }
1656 }
1657 if (!LastPiece)
1658 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1659 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001660 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001661 else
1662 return;
1663
Ted Kremenek7dc86642009-03-31 20:22:36 +00001664 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001665 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001666 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001667 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001668 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001669 GenerateMinimalPathDiagnostic(PD, PDB, N);
1670 break;
1671 }
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001672
1673 // Finally, prune the diagnostic path of uninteresting stuff.
1674 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1675 assert(hasSomethingInteresting);
1676 (void) hasSomethingInteresting;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001677}
1678
Ted Kremenekcf118d42009-02-04 23:49:09 +00001679void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001680 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001681}
1682
Mike Stump1eb44332009-09-09 15:08:12 +00001683void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001684 // Compute the bug report's hash to determine its equivalence class.
1685 llvm::FoldingSetNodeID ID;
1686 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001687
1688 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001689 BugType& BT = R->getBugType();
1690 Register(&BT);
1691 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001692 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Ted Kremenekcf118d42009-02-04 23:49:09 +00001694 if (!EQ) {
1695 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001696 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001697 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001698 }
1699 else
1700 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001701}
1702
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001703
1704//===----------------------------------------------------------------------===//
1705// Emitting reports in equivalence classes.
1706//===----------------------------------------------------------------------===//
1707
1708namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001709struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001710 const ExplodedNode *N;
1711 ExplodedNode::const_succ_iterator I, E;
1712
1713 FRIEC_WLItem(const ExplodedNode *n)
1714 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1715};
1716}
1717
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001718static BugReport *
1719FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001720 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001721
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001722 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1723 assert(I != E);
1724 BugReport *R = *I;
1725 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001726
Ted Kremenek40406fe2010-12-03 06:52:30 +00001727 // If we don't need to suppress any of the nodes because they are
1728 // post-dominated by a sink, simply add all the nodes in the equivalence class
1729 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001730 if (!BT.isSuppressOnSink()) {
1731 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001732 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001733 if (N) {
1734 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001735 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001736 }
1737 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001738 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001739 }
1740
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001741 // For bug reports that should be suppressed when all paths are post-dominated
1742 // by a sink node, iterate through the reports in the equivalence class
1743 // until we find one that isn't post-dominated (if one exists). We use a
1744 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1745 // this as a recursive function, but we don't want to risk blowing out the
1746 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001747 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001748
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001749 for (; I != E; ++I) {
1750 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001751 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001752
Ted Kremenek40406fe2010-12-03 06:52:30 +00001753 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001754 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001755 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001756 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001757 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001758 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001759 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001760 if (errorNode->succ_empty()) {
1761 bugReports.push_back(R);
1762 if (!exampleReport)
1763 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001764 continue;
1765 }
1766
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001767 // At this point we know that 'N' is not a sink and it has at least one
1768 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1769 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001770 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001771 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1772
1773 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001774 WL.push_back(errorNode);
1775 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001776
1777 while (!WL.empty()) {
1778 WLItem &WI = WL.back();
1779 assert(!WI.N->succ_empty());
1780
1781 for (; WI.I != WI.E; ++WI.I) {
1782 const ExplodedNode *Succ = *WI.I;
1783 // End-of-path node?
1784 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001785 // If we found an end-of-path node that is not a sink.
1786 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001787 bugReports.push_back(R);
1788 if (!exampleReport)
1789 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001790 WL.clear();
1791 break;
1792 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001793 // Found a sink? Continue on to the next successor.
1794 continue;
1795 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001796 // Mark the successor as visited. If it hasn't been explored,
1797 // enqueue it to the DFS worklist.
1798 unsigned &mark = Visited[Succ];
1799 if (!mark) {
1800 mark = 1;
1801 WL.push_back(Succ);
1802 break;
1803 }
1804 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001805
1806 // The worklist may have been cleared at this point. First
1807 // check if it is empty before checking the last item.
1808 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001809 WL.pop_back();
1810 }
1811 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001812
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001813 // ExampleReport will be NULL if all the nodes in the equivalence class
1814 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001815 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001816}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001817
1818//===----------------------------------------------------------------------===//
1819// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1820// uses global state, which eventually should go elsewhere.
1821//===----------------------------------------------------------------------===//
1822namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001823class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001824 llvm::FoldingSetNodeID ID;
1825public:
1826 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001827 R->Profile(ID);
1828 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001829 }
1830
1831 void Profile(llvm::FoldingSetNodeID &id) {
1832 id = ID;
1833 }
1834
1835 llvm::FoldingSetNodeID &getID() { return ID; }
1836};
1837}
1838
1839static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1840 // FIXME: Eventually this diagnostic cache should reside in something
1841 // like AnalysisManager instead of being a static variable. This is
1842 // really unsafe in the long term.
1843 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1844 static DiagnosticCache DC;
1845
1846 void *InsertPos;
1847 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1848
1849 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1850 delete Item;
1851 return true;
1852 }
1853
1854 DC.InsertNode(Item, InsertPos);
1855 return false;
1856}
1857
Ted Kremenekcf118d42009-02-04 23:49:09 +00001858void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001859 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001860 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1861 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001862 return;
1863
David Blaikieef3643f2011-09-26 00:51:36 +00001864 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Ted Kremenekcf118d42009-02-04 23:49:09 +00001866 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001867 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001868 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001870 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001871 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001872 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001873 ? exampleReport->getDescription()
1874 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001875 BT.getCategory()));
1876
Ted Kremenek40406fe2010-12-03 06:52:30 +00001877 if (!bugReports.empty())
1878 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Ted Kremenek40406fe2010-12-03 06:52:30 +00001880 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001881 return;
1882
Ted Kremenek072192b2008-04-30 23:47:44 +00001883 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001884 const BugReport::ExtraTextList &Meta =
1885 exampleReport->getExtraText();
1886 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1887 e = Meta.end(); i != e; ++i) {
1888 D->addMeta(*i);
1889 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001890
Ted Kremenek3148eb42009-01-24 00:55:43 +00001891 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001892 BugReport::ranges_iterator Beg, End;
1893 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001894 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001895
1896 // Search the description for '%', as that will be interpretted as a
1897 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001898 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001899 unsigned ErrorDiag;
1900 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001901 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001902 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001903 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001904 if (*I == '%')
1905 Out << "%%";
1906 else
1907 Out << *I;
1908
1909 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001910 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001911 }
Ted Kremenek57202072008-07-14 17:40:50 +00001912
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001913 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001914 DiagnosticBuilder diagBuilder = Diag.Report(
1915 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001916 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001917 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001918 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001919
David Blaikieef3643f2011-09-26 00:51:36 +00001920 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001921 if (!PD)
1922 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Ted Kremenek802e0242012-02-08 04:32:34 +00001924 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001925 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1926 exampleReport->getLocation(getSourceManager()),
1927 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001928
Ted Kremenek3148eb42009-01-24 00:55:43 +00001929 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001930 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00001931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Ted Kremenek3148eb42009-01-24 00:55:43 +00001933 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001934}
Ted Kremenek57202072008-07-14 17:40:50 +00001935
Chris Lattner5f9e2722011-07-23 10:55:15 +00001936void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001937 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001938 SourceRange* RBeg, unsigned NumRanges) {
1939 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1940}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001941
Chris Lattner5f9e2722011-07-23 10:55:15 +00001942void BugReporter::EmitBasicReport(StringRef name,
1943 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001944 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001945 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001947 // 'BT' is owned by BugReporter.
1948 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001949 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001950 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1951 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001952}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001953
Chris Lattner5f9e2722011-07-23 10:55:15 +00001954BugType *BugReporter::getBugTypeForName(StringRef name,
1955 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001956 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001957 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1958 llvm::StringMapEntry<BugType *> &
1959 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1960 BugType *BT = entry.getValue();
1961 if (!BT) {
1962 BT = new BugType(name, category);
1963 entry.setValue(BT);
1964 }
1965 return BT;
1966}