blob: 8d9b61484168b380b8fbd5e161bf086c0b39efed [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
130 if (PathDiagnosticCallPiece *call =
131 dyn_cast<PathDiagnosticCallPiece>(piece)) {
132 // Recursively clean out the subclass. Keep this call around if
133 // it contains any informative diagnostics.
134 if (!RemoveUneededCalls(call->path))
135 continue;
136 containsSomethingInteresting = true;
137 }
138 else if (PathDiagnosticMacroPiece *macro =
139 dyn_cast<PathDiagnosticMacroPiece>(piece)) {
140 if (!RemoveUneededCalls(macro->subPieces))
141 continue;
142 containsSomethingInteresting = true;
143 }
144 else if (PathDiagnosticEventPiece *event =
145 dyn_cast<PathDiagnosticEventPiece>(piece)) {
146 // We never throw away an event, but we do throw it away wholesale
147 // as part of a path if we throw the entire path away.
148 if (!event->isPrunable())
149 containsSomethingInteresting = true;
150 }
151
152 pieces.push_back(piece);
153 }
154
155 return containsSomethingInteresting;
156}
157
158//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000159// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000160//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000161
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000162typedef llvm::DenseMap<const ExplodedNode*,
163const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000164
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000165namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000166class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000167 NodeBackMap& M;
168public:
169 NodeMapClosure(NodeBackMap *m) : M(*m) {}
170 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek9c378f72011-08-12 23:37:29 +0000172 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000173 NodeBackMap::iterator I = M.find(N);
174 return I == M.end() ? 0 : I->second;
175 }
176};
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000178class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000179 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000180 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000181 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000182 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000183public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000184 const LocationContext *LC;
185
Ted Kremenek8966bc12009-05-06 21:39:49 +0000186 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000187 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000188 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000189 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000190 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
191 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenek9c378f72011-08-12 23:37:29 +0000193 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek9c378f72011-08-12 23:37:29 +0000195 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
196 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Anna Zaks8e6431a2011-08-18 22:37:56 +0000198 BugReport *getBugReport() { return R; }
199
Tom Care212f6d32010-09-16 03:50:38 +0000200 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000201
202 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000204 const Stmt *getParent(const Stmt *S) {
205 return getParentMap().getParent(S);
206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Ted Kremenek8966bc12009-05-06 21:39:49 +0000208 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000209
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000210 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
David Blaikieef3643f2011-09-26 00:51:36 +0000212 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
213 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000214 }
215
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000216 bool supportsLogicalOpControlFlow() const {
217 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000218 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000219};
220} // end anonymous namespace
221
Ted Kremenek00605e02009-03-27 20:55:39 +0000222PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000223PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000224 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000225 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000226
Anna Zaks0cd59482011-09-16 19:18:30 +0000227 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
228 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000229}
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenek00605e02009-03-27 20:55:39 +0000231PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000232PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
233 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000234
Ted Kremenek143ca222008-05-06 18:11:09 +0000235 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000236 if (os.str().empty())
237 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek00605e02009-03-27 20:55:39 +0000239 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenek00605e02009-03-27 20:55:39 +0000241 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000242 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000243 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000244 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000245 else {
246 os << "Execution jumps to the end of the ";
247 const Decl *D = N->getLocationContext()->getDecl();
248 if (isa<ObjCMethodDecl>(D))
249 os << "method";
250 else if (isa<FunctionDecl>(D))
251 os << "function";
252 else {
253 assert(isa<BlockDecl>(D));
254 os << "anonymous block";
255 }
256 os << '.';
257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000259 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000260}
261
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000262static bool IsNested(const Stmt *S, ParentMap &PM) {
263 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
264 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000266 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000268 if (Parent)
269 switch (Parent->getStmtClass()) {
270 case Stmt::ForStmtClass:
271 case Stmt::DoStmtClass:
272 case Stmt::WhileStmtClass:
273 return true;
274 default:
275 break;
276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277
278 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000279}
280
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000281PathDiagnosticLocation
282PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000283 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000284 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000285 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000286
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000287 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000288 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000290 if (!Parent)
291 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000293 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000294 case Stmt::BinaryOperatorClass: {
295 const BinaryOperator *B = cast<BinaryOperator>(Parent);
296 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000297 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000298 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000299 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000300 case Stmt::CompoundStmtClass:
301 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000302 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000303 case Stmt::ChooseExprClass:
304 // Similar to '?' if we are referring to condition, just have the edge
305 // point to the entire choose expression.
306 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000307 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000308 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000309 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000310 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000311 case Stmt::ConditionalOperatorClass:
312 // For '?', if we are referring to condition, just have the edge point
313 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000314 if (cast<AbstractConditionalOperator>(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);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000318 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000319 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000320 case Stmt::ForStmtClass:
321 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000322 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000323 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000324 case Stmt::IfStmtClass:
325 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000326 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000327 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000328 case Stmt::ObjCForCollectionStmtClass:
329 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000330 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000331 break;
332 case Stmt::WhileStmtClass:
333 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000335 break;
336 default:
337 break;
338 }
339
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000340 S = Parent;
341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000343 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000344
345 // Special case: DeclStmts can appear in for statement declarations, in which
346 // case the ForStmt is the context.
347 if (isa<DeclStmt>(S)) {
348 if (const Stmt *Parent = P.getParent(S)) {
349 switch (Parent->getStmtClass()) {
350 case Stmt::ForStmtClass:
351 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000352 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000353 default:
354 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000355 }
356 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000357 }
358 else if (isa<BinaryOperator>(S)) {
359 // Special case: the binary operator represents the initialization
360 // code in a for statement (this can happen when the variable being
361 // initialized is an old variable.
362 if (const ForStmt *FS =
363 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
364 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000365 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000366 }
367 }
368
Anna Zaks220ac8c2011-09-15 01:08:34 +0000369 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000370}
371
Ted Kremenekcf118d42009-02-04 23:49:09 +0000372//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000373// ScanNotableSymbols: closure-like callback for scanning Store bindings.
374//===----------------------------------------------------------------------===//
375
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000376static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
377 ProgramStateManager& VMgr,
378 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek31061982009-03-31 23:00:32 +0000380 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek31061982009-03-31 23:00:32 +0000382 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek31061982009-03-31 23:00:32 +0000384 if (!isa<PostStmt>(P))
385 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek9c378f72011-08-12 23:37:29 +0000387 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek31061982009-03-31 23:00:32 +0000389 if (!DR)
390 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek5eca4822012-01-06 22:09:28 +0000392 SVal Y = N->getState()->getSVal(DR, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek31061982009-03-31 23:00:32 +0000394 if (X != Y)
395 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek9c378f72011-08-12 23:37:29 +0000397 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek31061982009-03-31 23:00:32 +0000399 if (!VD)
400 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek31061982009-03-31 23:00:32 +0000402 return VD;
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek31061982009-03-31 23:00:32 +0000405 return 0;
406}
407
408namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000409class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000410: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek31061982009-03-31 23:00:32 +0000412 SymbolRef Sym;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000413 ProgramStateRef PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000414 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000415 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000416 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000417 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000418 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek31061982009-03-31 23:00:32 +0000420public:
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000422 NotableSymbolHandler(SymbolRef sym,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000423 ProgramStateRef prevst,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000424 const Stmt *s,
425 ProgramStateManager& vmgr,
426 const ExplodedNode *pred,
427 PathDiagnostic& pd,
428 BugReporter& br)
429 : Sym(sym),
430 PrevSt(prevst),
431 S(s),
432 VMgr(vmgr),
433 Pred(pred),
434 PD(pd),
435 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
438 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek31061982009-03-31 23:00:32 +0000442 if (ScanSym != Sym)
443 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
445 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000446 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek31061982009-03-31 23:00:32 +0000448 if (X == V) // Same binding?
449 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek31061982009-03-31 23:00:32 +0000451 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000452 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000453 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek31061982009-03-31 23:00:32 +0000455 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek31061982009-03-31 23:00:32 +0000457 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
458 if (!B->isAssignmentOp())
459 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek31061982009-03-31 23:00:32 +0000461 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000462 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 if (!DR)
465 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek31061982009-03-31 23:00:32 +0000467 VD = dyn_cast<VarDecl>(DR->getDecl());
468 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000469 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000470 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
471 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000472 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000473 VD = dyn_cast<VarDecl>(*DS->decl_begin());
474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenek31061982009-03-31 23:00:32 +0000476 if (!VD)
477 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek31061982009-03-31 23:00:32 +0000479 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000480 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek31061982009-03-31 23:00:32 +0000482 if (!MostRecent)
483 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek31061982009-03-31 23:00:32 +0000485 // Create the diagnostic.
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000486 if (Loc::isLocType(VD->getType())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000487 SmallString<64> buf;
Jordy Rose7df12342011-08-21 05:25:15 +0000488 llvm::raw_svector_ostream os(buf);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000489 os << '\'' << *VD << "' now aliases '" << *MostRecent << '\'';
Anna Zaks590dd8e2011-09-20 21:38:35 +0000490 PathDiagnosticLocation L =
491 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(),
492 Pred->getLocationContext());
Ted Kremenek2042fc12012-02-24 06:00:00 +0000493 PD.getActivePath().push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000497 }
Ted Kremenek31061982009-03-31 23:00:32 +0000498};
499}
500
Ted Kremenek9c378f72011-08-12 23:37:29 +0000501static void HandleNotableSymbol(const ExplodedNode *N,
502 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000503 SymbolRef Sym, BugReporter& BR,
504 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Ted Kremenek9c378f72011-08-12 23:37:29 +0000506 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000507 ProgramStateRef PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenek31061982009-03-31 23:00:32 +0000509 if (!PrevSt)
510 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek31061982009-03-31 23:00:32 +0000512 // Look at the region bindings of the current state that map to the
513 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000514 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000515 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
516 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
517}
518
519namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000520class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000521: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Ted Kremenek31061982009-03-31 23:00:32 +0000523 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000524 const ExplodedNode *N;
525 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000526 GRBugReporter& BR;
527 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Ted Kremenek31061982009-03-31 23:00:32 +0000529public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000530 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000531 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000532 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Ted Kremenek31061982009-03-31 23:00:32 +0000534 bool HandleBinding(StoreManager& SMgr, Store store,
535 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Ted Kremenek31061982009-03-31 23:00:32 +0000537 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek31061982009-03-31 23:00:32 +0000539 if (!ScanSym)
540 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenek31061982009-03-31 23:00:32 +0000542 if (!BR.isNotable(ScanSym))
543 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 if (AlreadyProcessed.count(ScanSym))
546 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek31061982009-03-31 23:00:32 +0000548 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek31061982009-03-31 23:00:32 +0000550 HandleNotableSymbol(N, S, ScanSym, BR, PD);
551 return true;
552 }
553};
554} // end anonymous namespace
555
556//===----------------------------------------------------------------------===//
557// "Minimal" path diagnostic generation algorithm.
558//===----------------------------------------------------------------------===//
559
Ted Kremenek14856d72009-04-06 23:06:54 +0000560static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
561
Ted Kremenek31061982009-03-31 23:00:32 +0000562static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
563 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000564 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000565
Ted Kremenek31061982009-03-31 23:00:32 +0000566 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000567 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000568 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000569 ? NULL : *(N->pred_begin());
570 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000571 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000572 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000573 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Ted Kremenek31061982009-03-31 23:00:32 +0000575 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000576
577 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
578 PathDiagnosticCallPiece *C =
579 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
580 PD.getActivePath().push_front(C);
581 PD.pushActivePath(&C->path);
582 continue;
583 }
584
585 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
586 PD.popActivePath();
587 // The current active path should never be empty. Either we
588 // just added a bunch of stuff to the top-level path, or
589 // we have a previous CallExit. If the front of the active
590 // path is not a PathDiagnosticCallPiece, it means that the
591 // path terminated within a function call. We must then take the
592 // current contents of the active path and place it within
593 // a new PathDiagnosticCallPiece.
594 assert(!PD.getActivePath().empty());
595 PathDiagnosticCallPiece *C =
596 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
597 if (!C)
598 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
599 C->setCallee(*CE, SMgr);
600 continue;
601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Ted Kremenek9c378f72011-08-12 23:37:29 +0000603 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
604 const CFGBlock *Src = BE->getSrc();
605 const CFGBlock *Dst = BE->getDst();
606 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Ted Kremenek31061982009-03-31 23:00:32 +0000608 if (!T)
609 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Anna Zaks590dd8e2011-09-20 21:38:35 +0000611 PathDiagnosticLocation Start =
612 PathDiagnosticLocation::createBegin(T, SMgr,
613 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Ted Kremenek31061982009-03-31 23:00:32 +0000615 switch (T->getStmtClass()) {
616 default:
617 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek31061982009-03-31 23:00:32 +0000619 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000620 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000621 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenek31061982009-03-31 23:00:32 +0000623 if (!S)
624 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek31061982009-03-31 23:00:32 +0000626 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000627 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000628 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Ted Kremenek31061982009-03-31 23:00:32 +0000630 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000631 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000632 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000633 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000634 break;
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
637 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000638 // Figure out what case arm we took.
639 std::string sbuf;
640 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek9c378f72011-08-12 23:37:29 +0000642 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000643 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Ted Kremenek31061982009-03-31 23:00:32 +0000645 switch (S->getStmtClass()) {
646 default:
647 os << "No cases match in the switch statement. "
648 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000649 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000650 break;
651 case Stmt::DefaultStmtClass:
652 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000653 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000654 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek31061982009-03-31 23:00:32 +0000656 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000657 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000658 const CaseStmt *Case = cast<CaseStmt>(S);
659 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000660
661 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000662 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenek9c378f72011-08-12 23:37:29 +0000664 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000665 // FIXME: Maybe this should be an assertion. Are there cases
666 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000667 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000668 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 if (D) {
671 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000672 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000673 }
674 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000675
676 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000677 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000678
Ted Kremenek31061982009-03-31 23:00:32 +0000679 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000680 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000681 break;
682 }
683 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000684 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000685 os.str()));
686 }
687 else {
688 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000689 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000690 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000691 os.str()));
692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek31061982009-03-31 23:00:32 +0000694 break;
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek31061982009-03-31 23:00:32 +0000697 case Stmt::BreakStmtClass:
698 case Stmt::ContinueStmtClass: {
699 std::string sbuf;
700 llvm::raw_string_ostream os(sbuf);
701 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000702 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000703 os.str()));
704 break;
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000708 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000709 case Stmt::ConditionalOperatorClass: {
710 std::string sbuf;
711 llvm::raw_string_ostream os(sbuf);
712 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek31061982009-03-31 23:00:32 +0000714 if (*(Src->succ_begin()+1) == Dst)
715 os << "false";
716 else
717 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenek31061982009-03-31 23:00:32 +0000721 if (const Stmt *S = End.asStmt())
722 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Ted Kremenek2042fc12012-02-24 06:00:00 +0000724 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000725 os.str()));
726 break;
727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek31061982009-03-31 23:00:32 +0000729 // Determine control-flow for short-circuited '&&' and '||'.
730 case Stmt::BinaryOperatorClass: {
731 if (!PDB.supportsLogicalOpControlFlow())
732 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000734 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000735 std::string sbuf;
736 llvm::raw_string_ostream os(sbuf);
737 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000738
John McCall2de56d12010-08-25 11:45:40 +0000739 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000740 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek31061982009-03-31 23:00:32 +0000742 if (*(Src->succ_begin()+1) == Dst) {
743 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000744 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000745 PathDiagnosticLocation Start =
746 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000747 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000748 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000749 }
Ted Kremenek31061982009-03-31 23:00:32 +0000750 else {
751 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000752 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000753 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000754 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000755 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000756 }
Ted Kremenek31061982009-03-31 23:00:32 +0000757 }
758 else {
John McCall2de56d12010-08-25 11:45:40 +0000759 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000760 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek31061982009-03-31 23:00:32 +0000762 if (*(Src->succ_begin()+1) == Dst) {
763 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000764 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000765 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000766 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000767 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000768 }
769 else {
770 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000771 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000772 PathDiagnosticLocation Start =
773 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000774 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000775 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000776 }
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek31061982009-03-31 23:00:32 +0000779 break;
780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
782 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000783 if (*(Src->succ_begin()) == Dst) {
784 std::string sbuf;
785 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek31061982009-03-31 23:00:32 +0000787 os << "Loop condition is true. ";
788 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek31061982009-03-31 23:00:32 +0000790 if (const Stmt *S = End.asStmt())
791 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenek2042fc12012-02-24 06:00:00 +0000793 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000794 os.str()));
795 }
796 else {
797 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek31061982009-03-31 23:00:32 +0000799 if (const Stmt *S = End.asStmt())
800 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek2042fc12012-02-24 06:00:00 +0000802 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000803 "Loop condition is false. Exiting loop"));
804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek31061982009-03-31 23:00:32 +0000806 break;
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek31061982009-03-31 23:00:32 +0000809 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000810 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000811 if (*(Src->succ_begin()+1) == Dst) {
812 std::string sbuf;
813 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek31061982009-03-31 23:00:32 +0000815 os << "Loop condition is false. ";
816 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
817 if (const Stmt *S = End.asStmt())
818 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek2042fc12012-02-24 06:00:00 +0000820 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000821 os.str()));
822 }
823 else {
824 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
825 if (const Stmt *S = End.asStmt())
826 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenek2042fc12012-02-24 06:00:00 +0000828 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000829 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek31061982009-03-31 23:00:32 +0000832 break;
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Ted Kremenek31061982009-03-31 23:00:32 +0000835 case Stmt::IfStmtClass: {
836 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek31061982009-03-31 23:00:32 +0000838 if (const Stmt *S = End.asStmt())
839 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek31061982009-03-31 23:00:32 +0000841 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000842 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000843 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000844 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000845 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000846 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek31061982009-03-31 23:00:32 +0000848 break;
849 }
850 }
851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000853 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000854 // Add diagnostic pieces from custom visitors.
855 BugReport *R = PDB.getBugReport();
856 for (BugReport::visitor_iterator I = R->visitor_begin(),
857 E = R->visitor_end(); I!=E; ++I) {
858 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenek2042fc12012-02-24 06:00:00 +0000859 PD.getActivePath().push_front(p);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000860 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Ted Kremenek9c378f72011-08-12 23:37:29 +0000863 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000864 // Scan the region bindings, and see if a "notable" symbol has a new
865 // lval binding.
866 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
867 PDB.getStateManager().iterBindings(N->getState(), SNS);
868 }
869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek14856d72009-04-06 23:06:54 +0000871 // After constructing the full PathDiagnostic, do a pass over it to compact
872 // PathDiagnosticPieces that occur within a macro.
873 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000874}
875
876//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000877// "Extensive" PathDiagnostic generation.
878//===----------------------------------------------------------------------===//
879
880static bool IsControlFlowExpr(const Stmt *S) {
881 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000883 if (!E)
884 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
886 E = E->IgnoreParenCasts();
887
John McCall56ca35d2011-02-17 10:25:35 +0000888 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000889 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000891 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
892 if (B->isLogicalOp())
893 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000894
895 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000896}
897
Ted Kremenek14856d72009-04-06 23:06:54 +0000898namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000899class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000900 bool IsDead;
901public:
902 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
903 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000904
905 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000906 bool isDead() const { return IsDead; }
907};
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000909class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000910 std::vector<ContextLocation> CLocs;
911 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000912 PathDiagnostic &PD;
913 PathDiagnosticBuilder &PDB;
914 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000916 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremenek14856d72009-04-06 23:06:54 +0000918 bool containsLocation(const PathDiagnosticLocation &Container,
919 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenek14856d72009-04-06 23:06:54 +0000921 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremenek9650cf32009-05-11 21:42:34 +0000923 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
924 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000925 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000926 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000927 while (1) {
928 // Adjust the location for some expressions that are best referenced
929 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000930 switch (S->getStmtClass()) {
931 default:
932 break;
933 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000934 case Stmt::GenericSelectionExprClass:
935 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000936 firstCharOnly = true;
937 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000938 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000939 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000940 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000941 firstCharOnly = true;
942 continue;
943 case Stmt::ChooseExprClass:
944 S = cast<ChooseExpr>(S)->getCond();
945 firstCharOnly = true;
946 continue;
947 case Stmt::BinaryOperatorClass:
948 S = cast<BinaryOperator>(S)->getLHS();
949 firstCharOnly = true;
950 continue;
951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Ted Kremenek9650cf32009-05-11 21:42:34 +0000953 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek9650cf32009-05-11 21:42:34 +0000956 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000957 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek9650cf32009-05-11 21:42:34 +0000960 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000961 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000962
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000963 return L;
964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Ted Kremenek14856d72009-04-06 23:06:54 +0000966 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000967 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000968 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000969 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000970 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000971 CLocs.pop_back();
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Ted Kremenek14856d72009-04-06 23:06:54 +0000974public:
975 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
976 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Ted Kremeneka301a672009-04-22 18:16:20 +0000978 // If the PathDiagnostic already has pieces, add the enclosing statement
979 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000980 if (!PD.path.empty()) {
981 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek14856d72009-04-06 23:06:54 +0000983 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000984 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 }
986 }
987
988 ~EdgeBuilder() {
989 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000990
Ted Kremeneka301a672009-04-22 18:16:20 +0000991 // Finally, add an initial edge from the start location of the first
992 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000993 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000994 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000995 PDB.getSourceManager());
996 if (L.isValid())
997 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000998 }
999
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001000 void flushLocations() {
1001 while (!CLocs.empty())
1002 popLocation();
1003 PrevLoc = PathDiagnosticLocation();
1004 }
1005
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001008 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Ted Kremenek14856d72009-04-06 23:06:54 +00001010 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001011 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +00001012};
Ted Kremenek14856d72009-04-06 23:06:54 +00001013} // end anonymous namespace
1014
1015
1016PathDiagnosticLocation
1017EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1018 if (const Stmt *S = L.asStmt()) {
1019 if (IsControlFlowExpr(S))
1020 return L;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
1022 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +00001023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 return L;
1026}
1027
1028bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1029 const PathDiagnosticLocation &Containee) {
1030
1031 if (Container == Containee)
1032 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Ted Kremenek14856d72009-04-06 23:06:54 +00001034 if (Container.asDecl())
1035 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Ted Kremenek14856d72009-04-06 23:06:54 +00001037 if (const Stmt *S = Containee.asStmt())
1038 if (const Stmt *ContainerS = Container.asStmt()) {
1039 while (S) {
1040 if (S == ContainerS)
1041 return true;
1042 S = PDB.getParent(S);
1043 }
1044 return false;
1045 }
1046
1047 // Less accurate: compare using source ranges.
1048 SourceRange ContainerR = Container.asRange();
1049 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Ted Kremenek14856d72009-04-06 23:06:54 +00001051 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001052 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1053 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1054 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1055 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chandler Carruth64211622011-07-25 21:09:52 +00001057 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1058 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1059 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1060 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek14856d72009-04-06 23:06:54 +00001062 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001063 assert(ContaineeBegLine <= ContaineeEndLine);
1064
Ted Kremenek14856d72009-04-06 23:06:54 +00001065 return (ContainerBegLine <= ContaineeBegLine &&
1066 ContainerEndLine >= ContaineeEndLine &&
1067 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001068 SM.getExpansionColumnNumber(ContainerRBeg) <=
1069 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001070 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001071 SM.getExpansionColumnNumber(ContainerREnd) >=
1072 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001073}
1074
Ted Kremenek14856d72009-04-06 23:06:54 +00001075void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1076 if (!PrevLoc.isValid()) {
1077 PrevLoc = NewLoc;
1078 return;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001081 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1082 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001084 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001085 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremenek14856d72009-04-06 23:06:54 +00001087 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001088 if (NewLocClean.asLocation().getExpansionLoc() ==
1089 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001090 return;
1091
Ted Kremenek2042fc12012-02-24 06:00:00 +00001092 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001093 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001094}
1095
1096void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Ted Kremeneka301a672009-04-22 18:16:20 +00001098 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1099 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Ted Kremenek14856d72009-04-06 23:06:54 +00001101 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1102
1103 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001104 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremenek14856d72009-04-06 23:06:54 +00001106 // Is the top location context the same as the one for the new location?
1107 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001108 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001109 if (IsConsumedExpr(TopContextLoc) &&
1110 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001111 TopContextLoc.markDead();
1112
Ted Kremenek14856d72009-04-06 23:06:54 +00001113 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001114 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001115
1116 return;
1117 }
1118
1119 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001120 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001121 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001123 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001124 CLocs.push_back(ContextLocation(CLoc, true));
1125 return;
1126 }
1127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Ted Kremenek14856d72009-04-06 23:06:54 +00001129 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001130 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001131 }
1132
1133 // Context does not contain the location. Flush it.
1134 popLocation();
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001137 // If we reach here, there is no enclosing context. Just add the edge.
1138 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001139}
1140
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001141bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1142 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1143 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001145 return false;
1146}
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremeneke1baed32009-05-05 23:13:38 +00001148void EdgeBuilder::addExtendedContext(const Stmt *S) {
1149 if (!S)
1150 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001151
1152 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001153 while (Parent) {
1154 if (isa<CompoundStmt>(Parent))
1155 Parent = PDB.getParent(Parent);
1156 else
1157 break;
1158 }
1159
1160 if (Parent) {
1161 switch (Parent->getStmtClass()) {
1162 case Stmt::DoStmtClass:
1163 case Stmt::ObjCAtSynchronizedStmtClass:
1164 addContext(Parent);
1165 default:
1166 break;
1167 }
1168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Ted Kremeneke1baed32009-05-05 23:13:38 +00001170 addContext(S);
1171}
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremenek14856d72009-04-06 23:06:54 +00001173void EdgeBuilder::addContext(const Stmt *S) {
1174 if (!S)
1175 return;
1176
Ted Kremenek59950d32012-02-24 07:12:52 +00001177 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Ted Kremenek14856d72009-04-06 23:06:54 +00001179 while (!CLocs.empty()) {
1180 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1181
1182 // Is the top location context the same as the one for the new location?
1183 if (TopContextLoc == L)
1184 return;
1185
1186 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001187 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001188 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001189 }
1190
1191 // Context does not contain the location. Flush it.
1192 popLocation();
1193 }
1194
1195 CLocs.push_back(L);
1196}
1197
1198static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1199 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001200 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001201 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001202 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001203
Ted Kremenek9c378f72011-08-12 23:37:29 +00001204 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001205 while (NextNode) {
1206 N = NextNode;
1207 NextNode = GetPredecessorNode(N);
1208 ProgramPoint P = N->getLocation();
1209
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001210 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001211 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1212 const StackFrameContext *LCtx =
1213 CE->getLocationContext()->getCurrentStackFrame();
1214 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1215 PDB.getSourceManager(),
1216 LCtx);
1217 EB.addEdge(Loc, true);
1218 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001219 PathDiagnosticCallPiece *C =
1220 PathDiagnosticCallPiece::construct(N, *CE, SM);
1221 PD.getActivePath().push_front(C);
1222 PD.pushActivePath(&C->path);
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001223 break;
1224 }
Ted Kremenek59950d32012-02-24 07:12:52 +00001225
1226 // Note that is important that we update the LocationContext
1227 // after looking at CallExits. CallExit basically adds an
1228 // edge in the *caller*, so we don't want to update the LocationContext
1229 // too soon.
1230 PDB.LC = N->getLocationContext();
1231
Ted Kremenek2042fc12012-02-24 06:00:00 +00001232 // Pop the call hierarchy if we are done walking the contents
1233 // of a function call.
1234 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
1235 PD.popActivePath();
1236 // The current active path should never be empty. Either we
1237 // just added a bunch of stuff to the top-level path, or
1238 // we have a previous CallExit. If the front of the active
1239 // path is not a PathDiagnosticCallPiece, it means that the
1240 // path terminated within a function call. We must then take the
1241 // current contents of the active path and place it within
1242 // a new PathDiagnosticCallPiece.
1243 assert(!PD.getActivePath().empty());
1244 PathDiagnosticCallPiece *C =
1245 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1246 if (!C)
1247 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
1248 C->setCallee(*CE, SM);
Ted Kremenek59950d32012-02-24 07:12:52 +00001249 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001250 EB.addContext(CE->getCallExpr());
1251 break;
1252 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001253
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001254 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001255 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001256 const CFGBlock &Blk = *BE->getSrc();
1257 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001259 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001260 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001261 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001262 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001264 if (!Term) {
1265 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1266 CS = dyn_cast<CompoundStmt>(FS->getBody());
1267 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001268 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001271 PathDiagnosticEventPiece *p =
1272 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001273 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001275 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001276 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001278 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001279 PathDiagnosticLocation BL =
1280 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001281 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001282 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001285 if (Term)
1286 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001288 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001289 }
1290
Mike Stump1eb44332009-09-09 15:08:12 +00001291 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001292 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1293 const Stmt *stmt = S->getStmt();
1294 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001295 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001296 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001297 }
1298 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001299 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001300 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001301
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001302 break;
1303 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001304
1305
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001306 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001308 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001309 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Anna Zaks8e6431a2011-08-18 22:37:56 +00001311 // Add pieces from custom visitors.
1312 BugReport *R = PDB.getBugReport();
1313 for (BugReport::visitor_iterator I = R->visitor_begin(),
1314 E = R->visitor_end(); I!=E; ++I) {
1315 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001316 const PathDiagnosticLocation &Loc = p->getLocation();
1317 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001318 PD.getActivePath().push_front(p);
Ted Kremenek8966bc12009-05-06 21:39:49 +00001319 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001320 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001321 }
Mike Stump1eb44332009-09-09 15:08:12 +00001322 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001323 }
1324}
1325
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001326//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001327// Methods for BugType and subclasses.
1328//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001329BugType::~BugType() { }
1330
Ted Kremenekcf118d42009-02-04 23:49:09 +00001331void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001332
David Blaikie99ba9e32011-12-20 02:48:34 +00001333void BuiltinBug::anchor() {}
1334
Ted Kremenekcf118d42009-02-04 23:49:09 +00001335//===----------------------------------------------------------------------===//
1336// Methods for BugReport and subclasses.
1337//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001338
David Blaikie99ba9e32011-12-20 02:48:34 +00001339void BugReport::NodeResolver::anchor() {}
1340
Anna Zaks8e6431a2011-08-18 22:37:56 +00001341void BugReport::addVisitor(BugReporterVisitor* visitor) {
1342 if (!visitor)
1343 return;
1344
1345 llvm::FoldingSetNodeID ID;
1346 visitor->Profile(ID);
1347 void *InsertPos;
1348
1349 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1350 delete visitor;
1351 return;
1352 }
1353
1354 CallbacksSet.InsertNode(visitor, InsertPos);
1355 Callbacks = F.add(visitor, Callbacks);
1356}
1357
1358BugReport::~BugReport() {
1359 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001360 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001361 }
1362}
Anna Zakse172e8b2011-08-17 23:00:25 +00001363
1364void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1365 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001366 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001367 if (UniqueingLocation.isValid()) {
1368 UniqueingLocation.Profile(hash);
1369 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001370 Location.Profile(hash);
1371 } else {
1372 assert(ErrorNode);
1373 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1374 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001375
1376 for (SmallVectorImpl<SourceRange>::const_iterator I =
1377 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1378 const SourceRange range = *I;
1379 if (!range.isValid())
1380 continue;
1381 hash.AddInteger(range.getBegin().getRawEncoding());
1382 hash.AddInteger(range.getEnd().getRawEncoding());
1383 }
1384}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001385
Ted Kremenek9c378f72011-08-12 23:37:29 +00001386const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001387 if (!ErrorNode)
1388 return 0;
1389
Tom Care212f6d32010-09-16 03:50:38 +00001390 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001391 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Ted Kremenek9c378f72011-08-12 23:37:29 +00001393 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001394 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001395 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001396 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001397 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001398 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001399 S = GetStmt(ProgP);
1400
1401 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001402}
1403
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001404std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001405BugReport::getRanges() {
1406 // If no custom ranges, add the range of the statement corresponding to
1407 // the error node.
1408 if (Ranges.empty()) {
1409 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1410 addRange(E->getSourceRange());
1411 else
1412 return std::make_pair(ranges_iterator(), ranges_iterator());
1413 }
1414
Anna Zaks14924262011-08-24 20:31:06 +00001415 // User-specified absence of range info.
1416 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1417 return std::make_pair(ranges_iterator(), ranges_iterator());
1418
Anna Zakse172e8b2011-08-17 23:00:25 +00001419 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001420}
1421
Anna Zaks590dd8e2011-09-20 21:38:35 +00001422PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001423 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001424 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001425 "Either Location or ErrorNode should be specified but not both.");
1426
Ted Kremenek9c378f72011-08-12 23:37:29 +00001427 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001428 const LocationContext *LC = ErrorNode->getLocationContext();
1429
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001430 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001431 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001432 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001433 // For binary operators, return the location of the operator.
1434 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001435 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001436
Anna Zaks590dd8e2011-09-20 21:38:35 +00001437 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001438 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001439 } else {
1440 assert(Location.isValid());
1441 return Location;
1442 }
1443
Anna Zaks590dd8e2011-09-20 21:38:35 +00001444 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001445}
1446
Ted Kremenekcf118d42009-02-04 23:49:09 +00001447//===----------------------------------------------------------------------===//
1448// Methods for BugReporter and subclasses.
1449//===----------------------------------------------------------------------===//
1450
1451BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001452 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001453}
1454
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001455GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001456BugReporterData::~BugReporterData() {}
1457
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001458ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001459
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001460ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001461GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1462
Anna Zaks3b030a22011-08-19 01:57:09 +00001463BugReporter::~BugReporter() {
1464 FlushReports();
1465
1466 // Free the bug reports we are tracking.
1467 typedef std::vector<BugReportEquivClass *> ContTy;
1468 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1469 I != E; ++I) {
1470 delete *I;
1471 }
1472}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001473
1474void BugReporter::FlushReports() {
1475 if (BugTypes.isEmpty())
1476 return;
1477
1478 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001479 // warnings and new BugTypes.
1480 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1481 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001482 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001483 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001484 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001485 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001486 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001487 const_cast<BugType*>(*I)->FlushReports(*this);
1488
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001489 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1490 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1491 BugReportEquivClass& EQ = *EI;
1492 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001493 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001494
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001495 // BugReporter owns and deletes only BugTypes created implicitly through
1496 // EmitBasicReport.
1497 // FIXME: There are leaks from checkers that assume that the BugTypes they
1498 // create will be destroyed by the BugReporter.
1499 for (llvm::StringMap<BugType*>::iterator
1500 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1501 delete I->second;
1502
Ted Kremenekcf118d42009-02-04 23:49:09 +00001503 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001504 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001505}
1506
1507//===----------------------------------------------------------------------===//
1508// PathDiagnostics generation.
1509//===----------------------------------------------------------------------===//
1510
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001511static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001512 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001513MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001514 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Ted Kremenekcf118d42009-02-04 23:49:09 +00001516 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001517 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001518 // error node unless there are two or more error nodes with the same minimum
1519 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001520 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001521 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001522
1523 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001524 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1525 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenekcf118d42009-02-04 23:49:09 +00001527 // Create owning pointers for GTrim and NMap just to ensure that they are
1528 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001529 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1530 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Ted Kremenekcf118d42009-02-04 23:49:09 +00001532 // Find the (first) error node in the trimmed graph. We just need to consult
1533 // the node map (NMap) which maps from nodes in the original graph to nodes
1534 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001535
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001536 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001537 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001538 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001539
Ted Kremenek40406fe2010-12-03 06:52:30 +00001540 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1541 const ExplodedNode *originalNode = nodes[nodeIndex];
1542 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001543 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001544 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001545 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Ted Kremenek938332c2009-05-16 01:11:58 +00001548 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001549
1550 // Create a new (third!) graph with a single path. This is the graph
1551 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001552 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Ted Kremenek10aa5542009-03-12 23:41:59 +00001554 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001555 // to the root node, and then construct a new graph that contains only
1556 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001557 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001559 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001560 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001562 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001563 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001564 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001566 if (Visited.find(Node) != Visited.end())
1567 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001569 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001571 if (Node->pred_empty()) {
1572 Root = Node;
1573 break;
1574 }
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001576 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001577 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001578 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek938332c2009-05-16 01:11:58 +00001581 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenek10aa5542009-03-12 23:41:59 +00001583 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001584 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001585 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001586 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001587 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001589 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001590 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001591 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001592 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001594 // Create the equivalent node in the new graph with the same state
1595 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001596 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001598 // Store the mapping to the original node.
1599 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1600 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001601 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001603 // Link up the new node with the previous node.
1604 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001605 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001607 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001609 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001610 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001611 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001612 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001613 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001614 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001615 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001618 // Find the next successor node. We choose the node that is marked
1619 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001620 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1621 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001622 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001624 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001626 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001628 if (I == Visited.end())
1629 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001631 if (!N || I->second < MinVal) {
1632 N = *SI;
1633 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001634 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Ted Kremenek938332c2009-05-16 01:11:58 +00001637 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Ted Kremenek938332c2009-05-16 01:11:58 +00001640 assert(First);
1641
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001642 return std::make_pair(std::make_pair(GNew, BM),
1643 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001644}
1645
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001646/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1647/// and collapses PathDiagosticPieces that are expanded by macros.
1648static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001649 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1650 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001652 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001653 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001655 MacroStackTy MacroStack;
1656 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremenek2042fc12012-02-24 06:00:00 +00001658 for (PathPieces::const_iterator I = PD.path.begin(), E = PD.path.end();
1659 I!=E; ++I) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001660 // Get the location of the PathDiagnosticPiece.
Ted Kremenek802e0242012-02-08 04:32:34 +00001661 const FullSourceLoc Loc = (*I)->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001663 // Determine the instantiation location, which is the location we group
1664 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001665 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001666 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001667 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001669 if (Loc.isFileID()) {
1670 MacroStack.clear();
Ted Kremenek802e0242012-02-08 04:32:34 +00001671 Pieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001672 continue;
1673 }
1674
1675 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001677 // Is the PathDiagnosticPiece within the same macro group?
1678 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek802e0242012-02-08 04:32:34 +00001679 MacroStack.back().first->subPieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001680 continue;
1681 }
1682
1683 // We aren't in the same group. Are we descending into a new macro
1684 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001685 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001686
1687 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001688 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001689 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001691 // Walk the entire macro stack.
1692 while (!MacroStack.empty()) {
1693 if (InstantiationLoc == MacroStack.back().second) {
1694 MacroGroup = MacroStack.back().first;
1695 break;
1696 }
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001698 if (ParentInstantiationLoc == MacroStack.back().second) {
1699 MacroGroup = MacroStack.back().first;
1700 break;
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001703 MacroStack.pop_back();
1704 }
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001706 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1707 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001708 PathDiagnosticMacroPiece *NewGroup =
1709 new PathDiagnosticMacroPiece(
Ted Kremenek802e0242012-02-08 04:32:34 +00001710 PathDiagnosticLocation::createSingleLocation((*I)->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001711
1712 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001713 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001714 else {
1715 assert(InstantiationLoc.isFileID());
1716 Pieces.push_back(NewGroup);
1717 }
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001719 MacroGroup = NewGroup;
1720 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1721 }
1722
1723 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek802e0242012-02-08 04:32:34 +00001724 MacroGroup->subPieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001725 }
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001727 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek2042fc12012-02-24 06:00:00 +00001728 PD.getMutablePieces().clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001730 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
Ted Kremenekaf84f8f2012-02-08 22:48:17 +00001731 if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
1732 if (!MP->containsEvent())
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001733 continue;
Ted Kremenek2042fc12012-02-24 06:00:00 +00001734 PD.getMutablePieces().push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001735 }
1736}
1737
Ted Kremenek7dc86642009-03-31 20:22:36 +00001738void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001739 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Ted Kremenek40406fe2010-12-03 06:52:30 +00001741 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001742 SmallVector<const ExplodedNode *, 10> errorNodes;
1743 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001744 E = bugReports.end(); I != E; ++I) {
1745 errorNodes.push_back((*I)->getErrorNode());
1746 }
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001748 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001749 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001750 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001751 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001752 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Ted Kremenekcf118d42009-02-04 23:49:09 +00001754 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001755 assert(GPair.second.second < bugReports.size());
1756 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001757 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001759 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1760 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001761 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001762
1763 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001764 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1765 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Anna Zaks8e6431a2011-08-18 22:37:56 +00001767 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001768 R->addVisitor(new NilReceiverBRVisitor());
1769 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Anna Zaks23f395e2011-08-20 01:27:22 +00001771 // Generate the very last diagnostic piece - the piece is visible before
1772 // the trace is expanded.
1773 PathDiagnosticPiece *LastPiece = 0;
1774 for (BugReport::visitor_iterator I = R->visitor_begin(),
1775 E = R->visitor_end(); I!=E; ++I) {
1776 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1777 assert (!LastPiece &&
1778 "There can only be one final piece in a diagnostic.");
1779 LastPiece = Piece;
1780 }
1781 }
1782 if (!LastPiece)
1783 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1784 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001785 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001786 else
1787 return;
1788
Ted Kremenek7dc86642009-03-31 20:22:36 +00001789 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001790 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001791 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001792 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001793 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001794 GenerateMinimalPathDiagnostic(PD, PDB, N);
1795 break;
1796 }
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001797
1798 // Finally, prune the diagnostic path of uninteresting stuff.
1799 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1800 assert(hasSomethingInteresting);
1801 (void) hasSomethingInteresting;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001802}
1803
Ted Kremenekcf118d42009-02-04 23:49:09 +00001804void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001805 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001806}
1807
Mike Stump1eb44332009-09-09 15:08:12 +00001808void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001809 // Compute the bug report's hash to determine its equivalence class.
1810 llvm::FoldingSetNodeID ID;
1811 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001812
1813 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001814 BugType& BT = R->getBugType();
1815 Register(&BT);
1816 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001817 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Ted Kremenekcf118d42009-02-04 23:49:09 +00001819 if (!EQ) {
1820 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001821 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001822 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001823 }
1824 else
1825 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001826}
1827
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001828
1829//===----------------------------------------------------------------------===//
1830// Emitting reports in equivalence classes.
1831//===----------------------------------------------------------------------===//
1832
1833namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001834struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001835 const ExplodedNode *N;
1836 ExplodedNode::const_succ_iterator I, E;
1837
1838 FRIEC_WLItem(const ExplodedNode *n)
1839 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1840};
1841}
1842
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001843static BugReport *
1844FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001845 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001846
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001847 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1848 assert(I != E);
1849 BugReport *R = *I;
1850 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001851
Ted Kremenek40406fe2010-12-03 06:52:30 +00001852 // If we don't need to suppress any of the nodes because they are
1853 // post-dominated by a sink, simply add all the nodes in the equivalence class
1854 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001855 if (!BT.isSuppressOnSink()) {
1856 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001857 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001858 if (N) {
1859 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001860 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001861 }
1862 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001863 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001864 }
1865
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001866 // For bug reports that should be suppressed when all paths are post-dominated
1867 // by a sink node, iterate through the reports in the equivalence class
1868 // until we find one that isn't post-dominated (if one exists). We use a
1869 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1870 // this as a recursive function, but we don't want to risk blowing out the
1871 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001872 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001873
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001874 for (; I != E; ++I) {
1875 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001876 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001877
Ted Kremenek40406fe2010-12-03 06:52:30 +00001878 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001879 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001880 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001881 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001882 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001883 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001884 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001885 if (errorNode->succ_empty()) {
1886 bugReports.push_back(R);
1887 if (!exampleReport)
1888 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001889 continue;
1890 }
1891
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001892 // At this point we know that 'N' is not a sink and it has at least one
1893 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1894 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001895 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001896 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1897
1898 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001899 WL.push_back(errorNode);
1900 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001901
1902 while (!WL.empty()) {
1903 WLItem &WI = WL.back();
1904 assert(!WI.N->succ_empty());
1905
1906 for (; WI.I != WI.E; ++WI.I) {
1907 const ExplodedNode *Succ = *WI.I;
1908 // End-of-path node?
1909 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001910 // If we found an end-of-path node that is not a sink.
1911 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001912 bugReports.push_back(R);
1913 if (!exampleReport)
1914 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001915 WL.clear();
1916 break;
1917 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001918 // Found a sink? Continue on to the next successor.
1919 continue;
1920 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001921 // Mark the successor as visited. If it hasn't been explored,
1922 // enqueue it to the DFS worklist.
1923 unsigned &mark = Visited[Succ];
1924 if (!mark) {
1925 mark = 1;
1926 WL.push_back(Succ);
1927 break;
1928 }
1929 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001930
1931 // The worklist may have been cleared at this point. First
1932 // check if it is empty before checking the last item.
1933 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001934 WL.pop_back();
1935 }
1936 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001937
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001938 // ExampleReport will be NULL if all the nodes in the equivalence class
1939 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001940 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001941}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001942
1943//===----------------------------------------------------------------------===//
1944// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1945// uses global state, which eventually should go elsewhere.
1946//===----------------------------------------------------------------------===//
1947namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001948class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001949 llvm::FoldingSetNodeID ID;
1950public:
1951 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001952 R->Profile(ID);
1953 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001954 }
1955
1956 void Profile(llvm::FoldingSetNodeID &id) {
1957 id = ID;
1958 }
1959
1960 llvm::FoldingSetNodeID &getID() { return ID; }
1961};
1962}
1963
1964static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1965 // FIXME: Eventually this diagnostic cache should reside in something
1966 // like AnalysisManager instead of being a static variable. This is
1967 // really unsafe in the long term.
1968 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1969 static DiagnosticCache DC;
1970
1971 void *InsertPos;
1972 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1973
1974 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1975 delete Item;
1976 return true;
1977 }
1978
1979 DC.InsertNode(Item, InsertPos);
1980 return false;
1981}
1982
Ted Kremenekcf118d42009-02-04 23:49:09 +00001983void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001984 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001985 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1986 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001987 return;
1988
David Blaikieef3643f2011-09-26 00:51:36 +00001989 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Ted Kremenekcf118d42009-02-04 23:49:09 +00001991 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001992 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001993 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001995 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001996 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001997 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001998 ? exampleReport->getDescription()
1999 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00002000 BT.getCategory()));
2001
Ted Kremenek40406fe2010-12-03 06:52:30 +00002002 if (!bugReports.empty())
2003 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Ted Kremenek40406fe2010-12-03 06:52:30 +00002005 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00002006 return;
2007
Ted Kremenek072192b2008-04-30 23:47:44 +00002008 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00002009 const BugReport::ExtraTextList &Meta =
2010 exampleReport->getExtraText();
2011 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
2012 e = Meta.end(); i != e; ++i) {
2013 D->addMeta(*i);
2014 }
Ted Kremenek75840e12008-04-18 01:56:37 +00002015
Ted Kremenek3148eb42009-01-24 00:55:43 +00002016 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00002017 BugReport::ranges_iterator Beg, End;
2018 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00002019 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00002020
2021 // Search the description for '%', as that will be interpretted as a
2022 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002023 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00002024 unsigned ErrorDiag;
2025 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002026 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00002027 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002028 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00002029 if (*I == '%')
2030 Out << "%%";
2031 else
2032 Out << *I;
2033
2034 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00002035 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00002036 }
Ted Kremenek57202072008-07-14 17:40:50 +00002037
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00002038 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002039 DiagnosticBuilder diagBuilder = Diag.Report(
2040 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00002041 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00002042 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00002043 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00002044
David Blaikieef3643f2011-09-26 00:51:36 +00002045 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00002046 if (!PD)
2047 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Ted Kremenek802e0242012-02-08 04:32:34 +00002049 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002050 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
2051 exampleReport->getLocation(getSourceManager()),
2052 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00002053
Ted Kremenek3148eb42009-01-24 00:55:43 +00002054 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00002055 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00002056 }
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Ted Kremenek3148eb42009-01-24 00:55:43 +00002058 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002059}
Ted Kremenek57202072008-07-14 17:40:50 +00002060
Chris Lattner5f9e2722011-07-23 10:55:15 +00002061void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002062 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002063 SourceRange* RBeg, unsigned NumRanges) {
2064 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
2065}
Ted Kremenekcf118d42009-02-04 23:49:09 +00002066
Chris Lattner5f9e2722011-07-23 10:55:15 +00002067void BugReporter::EmitBasicReport(StringRef name,
2068 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002069 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002070 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002072 // 'BT' is owned by BugReporter.
2073 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002074 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002075 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2076 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002077}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002078
Chris Lattner5f9e2722011-07-23 10:55:15 +00002079BugType *BugReporter::getBugTypeForName(StringRef name,
2080 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002081 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002082 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2083 llvm::StringMapEntry<BugType *> &
2084 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2085 BugType *BT = entry.getValue();
2086 if (!BT) {
2087 BT = new BugType(name, category);
2088 entry.setValue(BT);
2089 }
2090 return BT;
2091}