blob: 2752e32e50862d51ec1f1e8c268eab82e41aec8b [file] [log] [blame]
Ted Kremenek61f3e052008-04-03 04:42:52 +00001// BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BugReporter, a utility class for generating
Ted Kremenek6c07bdb2009-06-26 00:05:51 +000011// PathDiagnostics.
Ted Kremenek61f3e052008-04-03 04:42:52 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000018#include "clang/AST/ASTContext.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000019#include "clang/Analysis/CFG.h"
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000022#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
24#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000025#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000028#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000031#include "llvm/ADT/OwningPtr.h"
Ted Kremenek802e0242012-02-08 04:32:34 +000032#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000033#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
35using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000036using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000037
Ted Kremenek8966bc12009-05-06 21:39:49 +000038BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000039
David Blaikie99ba9e32011-12-20 02:48:34 +000040void BugReporterContext::anchor() {}
41
Ted Kremenekcf118d42009-02-04 23:49:09 +000042//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000043// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000044//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000047 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
48 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000049 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000050 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenekb697b102009-02-23 22:44:26 +000052 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000053}
54
Zhongxing Xuc5619d92009-08-06 01:32:16 +000055static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000056GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000057 return N->pred_empty() ? NULL : *(N->pred_begin());
58}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000059
Zhongxing Xuc5619d92009-08-06 01:32:16 +000060static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000061GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000062 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000063}
64
Ted Kremenek9c378f72011-08-12 23:37:29 +000065static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000066 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000067 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000068 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekb697b102009-02-23 22:44:26 +000070 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000071}
72
Ted Kremenek9c378f72011-08-12 23:37:29 +000073static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000074 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000075 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000076 // Check if the statement is '?' or '&&'/'||'. These are "merges",
77 // not actual statement points.
78 switch (S->getStmtClass()) {
79 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000080 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000081 case Stmt::ConditionalOperatorClass: continue;
82 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000083 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
84 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000085 continue;
86 break;
87 }
88 default:
89 break;
90 }
Ted Kremenekb697b102009-02-23 22:44:26 +000091 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekb697b102009-02-23 22:44:26 +000094 return 0;
95}
96
Ted Kremenek5f85e172009-07-22 22:35:28 +000097static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000098GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000099 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000100 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekb697b102009-02-23 22:44:26 +0000102 return GetPreviousStmt(N);
103}
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek5f85e172009-07-22 22:35:28 +0000105static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000106GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000107 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000108 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return GetNextStmt(N);
111}
112
113//===----------------------------------------------------------------------===//
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000114// Diagnostic cleanup.
115//===----------------------------------------------------------------------===//
116
117/// Recursively scan through a path and prune out calls and macros pieces
118/// that aren't needed. Return true if afterwards the path contains
119/// "interesting stuff" which means it should be pruned from the parent path.
120static bool RemoveUneededCalls(PathPieces &pieces) {
121 bool containsSomethingInteresting = false;
122 const unsigned N = pieces.size();
123
124 for (unsigned i = 0 ; i < N ; ++i) {
125 // Remove the front piece from the path. If it is still something we
126 // want to keep once we are done, we will push it back on the end.
127 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
128 pieces.pop_front();
129
Ted Kremenek72516742012-03-01 00:05:06 +0000130 switch (piece->getKind()) {
131 case PathDiagnosticPiece::Call: {
132 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
133 // Recursively clean out the subclass. Keep this call around if
134 // it contains any informative diagnostics.
135 if (!RemoveUneededCalls(call->path))
136 continue;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000137 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000138 break;
139 }
140 case PathDiagnosticPiece::Macro: {
141 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
142 if (!RemoveUneededCalls(macro->subPieces))
143 continue;
144 containsSomethingInteresting = true;
145 break;
146 }
147 case PathDiagnosticPiece::Event: {
148 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
149 // We never throw away an event, but we do throw it away wholesale
150 // as part of a path if we throw the entire path away.
Ted Kremenek76aadc32012-03-09 01:13:14 +0000151 if (event->isPrunable())
152 continue;
153 containsSomethingInteresting = true;
Ted Kremenek72516742012-03-01 00:05:06 +0000154 break;
155 }
156 case PathDiagnosticPiece::ControlFlow:
157 break;
Ted Kremenekc89f4b02012-02-28 23:06:21 +0000158 }
159
160 pieces.push_back(piece);
161 }
162
163 return containsSomethingInteresting;
164}
165
166//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000167// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000168//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000169
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000170typedef llvm::DenseMap<const ExplodedNode*,
171const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000172
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000173namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000174class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000175 NodeBackMap& M;
176public:
177 NodeMapClosure(NodeBackMap *m) : M(*m) {}
178 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek9c378f72011-08-12 23:37:29 +0000180 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000181 NodeBackMap::iterator I = M.find(N);
182 return I == M.end() ? 0 : I->second;
183 }
184};
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000186class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000187 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000188 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000189 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000190 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000191public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000192 const LocationContext *LC;
193
Ted Kremenek8966bc12009-05-06 21:39:49 +0000194 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000195 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000196 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000197 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000198 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
199 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek9c378f72011-08-12 23:37:29 +0000201 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek9c378f72011-08-12 23:37:29 +0000203 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
204 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Anna Zaks8e6431a2011-08-18 22:37:56 +0000206 BugReport *getBugReport() { return R; }
207
Tom Care212f6d32010-09-16 03:50:38 +0000208 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000209
210 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000212 const Stmt *getParent(const Stmt *S) {
213 return getParentMap().getParent(S);
214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek8966bc12009-05-06 21:39:49 +0000216 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000217
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000218 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
David Blaikieef3643f2011-09-26 00:51:36 +0000220 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
221 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000222 }
223
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000224 bool supportsLogicalOpControlFlow() const {
225 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000226 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000227};
228} // end anonymous namespace
229
Ted Kremenek00605e02009-03-27 20:55:39 +0000230PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000231PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000232 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000233 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000234
Anna Zaks0cd59482011-09-16 19:18:30 +0000235 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
236 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000237}
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek00605e02009-03-27 20:55:39 +0000239PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000240PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
241 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000242
Ted Kremenek143ca222008-05-06 18:11:09 +0000243 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000244 if (os.str().empty())
245 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek00605e02009-03-27 20:55:39 +0000247 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek00605e02009-03-27 20:55:39 +0000249 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000250 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000251 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000252 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000253 else {
254 os << "Execution jumps to the end of the ";
255 const Decl *D = N->getLocationContext()->getDecl();
256 if (isa<ObjCMethodDecl>(D))
257 os << "method";
258 else if (isa<FunctionDecl>(D))
259 os << "function";
260 else {
261 assert(isa<BlockDecl>(D));
262 os << "anonymous block";
263 }
264 os << '.';
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000267 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000268}
269
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000270static bool IsNested(const Stmt *S, ParentMap &PM) {
271 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
272 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000274 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000276 if (Parent)
277 switch (Parent->getStmtClass()) {
278 case Stmt::ForStmtClass:
279 case Stmt::DoStmtClass:
280 case Stmt::WhileStmtClass:
281 return true;
282 default:
283 break;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000287}
288
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000289PathDiagnosticLocation
290PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000291 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000292 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000293 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000294
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000295 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000296 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000298 if (!Parent)
299 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000301 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000302 case Stmt::BinaryOperatorClass: {
303 const BinaryOperator *B = cast<BinaryOperator>(Parent);
304 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000305 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000306 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000307 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000308 case Stmt::CompoundStmtClass:
309 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000310 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000311 case Stmt::ChooseExprClass:
312 // Similar to '?' if we are referring to condition, just have the edge
313 // point to the entire choose expression.
314 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000315 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000316 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000317 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000318 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000319 case Stmt::ConditionalOperatorClass:
320 // For '?', if we are referring to condition, just have the edge point
321 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000322 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000323 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000324 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000325 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000326 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000327 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000328 case Stmt::ForStmtClass:
329 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000330 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000331 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000332 case Stmt::IfStmtClass:
333 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000334 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000335 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000336 case Stmt::ObjCForCollectionStmtClass:
337 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000338 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000339 break;
340 case Stmt::WhileStmtClass:
341 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000342 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000343 break;
344 default:
345 break;
346 }
347
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000348 S = Parent;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000351 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000352
353 // Special case: DeclStmts can appear in for statement declarations, in which
354 // case the ForStmt is the context.
355 if (isa<DeclStmt>(S)) {
356 if (const Stmt *Parent = P.getParent(S)) {
357 switch (Parent->getStmtClass()) {
358 case Stmt::ForStmtClass:
359 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000360 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000361 default:
362 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000363 }
364 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000365 }
366 else if (isa<BinaryOperator>(S)) {
367 // Special case: the binary operator represents the initialization
368 // code in a for statement (this can happen when the variable being
369 // initialized is an old variable.
370 if (const ForStmt *FS =
371 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
372 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000373 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000374 }
375 }
376
Anna Zaks220ac8c2011-09-15 01:08:34 +0000377 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000378}
379
Ted Kremenekcf118d42009-02-04 23:49:09 +0000380//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000381// "Minimal" path diagnostic generation algorithm.
382//===----------------------------------------------------------------------===//
Anna Zaks56a938f2012-03-16 23:24:20 +0000383typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
384typedef SmallVector<StackDiagPair, 6> StackDiagVector;
385
Anna Zaks368a0d52012-03-15 21:13:02 +0000386static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
Anna Zaks56a938f2012-03-16 23:24:20 +0000387 StackDiagVector &CallStack) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000388 // If the piece contains a special message, add it to all the call
389 // pieces on the active stack.
390 if (PathDiagnosticEventPiece *ep =
391 dyn_cast<PathDiagnosticEventPiece>(P)) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000392
Anna Zaks56a938f2012-03-16 23:24:20 +0000393 if (ep->hasCallStackHint())
394 for (StackDiagVector::iterator I = CallStack.begin(),
395 E = CallStack.end(); I != E; ++I) {
396 PathDiagnosticCallPiece *CP = I->first;
397 const ExplodedNode *N = I->second;
NAKAMURA Takumi8fe45252012-03-17 13:06:05 +0000398 std::string stackMsg = ep->getCallStackMessage(N);
Anna Zaks56a938f2012-03-16 23:24:20 +0000399
Anna Zaks368a0d52012-03-15 21:13:02 +0000400 // The last message on the path to final bug is the most important
401 // one. Since we traverse the path backwards, do not add the message
402 // if one has been previously added.
Anna Zaks56a938f2012-03-16 23:24:20 +0000403 if (!CP->hasCallStackMessage())
404 CP->setCallStackMessage(stackMsg);
405 }
Anna Zaks368a0d52012-03-15 21:13:02 +0000406 }
407}
Ted Kremenek31061982009-03-31 23:00:32 +0000408
Ted Kremenek77d09442012-03-02 01:27:31 +0000409static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
Ted Kremenek14856d72009-04-06 23:06:54 +0000410
Ted Kremenek31061982009-03-31 23:00:32 +0000411static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
412 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000413 const ExplodedNode *N,
414 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000417 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000418 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000419 ? NULL : *(N->pred_begin());
Anna Zaks368a0d52012-03-15 21:13:02 +0000420
Anna Zaks56a938f2012-03-16 23:24:20 +0000421 StackDiagVector CallStack;
Anna Zaks368a0d52012-03-15 21:13:02 +0000422
Ted Kremenek31061982009-03-31 23:00:32 +0000423 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000424 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000425 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000426 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek31061982009-03-31 23:00:32 +0000428 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000429
430 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
431 PathDiagnosticCallPiece *C =
432 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
433 PD.getActivePath().push_front(C);
434 PD.pushActivePath(&C->path);
Anna Zaks56a938f2012-03-16 23:24:20 +0000435 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek2042fc12012-02-24 06:00:00 +0000436 continue;
437 }
438
439 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
440 PD.popActivePath();
441 // The current active path should never be empty. Either we
442 // just added a bunch of stuff to the top-level path, or
443 // we have a previous CallExit. If the front of the active
444 // path is not a PathDiagnosticCallPiece, it means that the
445 // path terminated within a function call. We must then take the
446 // current contents of the active path and place it within
447 // a new PathDiagnosticCallPiece.
448 assert(!PD.getActivePath().empty());
449 PathDiagnosticCallPiece *C =
450 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +0000451 if (!C) {
452 const Decl *Caller = CE->getLocationContext()->getDecl();
453 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
454 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000455 C->setCallee(*CE, SMgr);
Anna Zaks368a0d52012-03-15 21:13:02 +0000456 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +0000457 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +0000458 CallStack.pop_back();
459 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000460 continue;
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek9c378f72011-08-12 23:37:29 +0000463 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
464 const CFGBlock *Src = BE->getSrc();
465 const CFGBlock *Dst = BE->getDst();
466 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek31061982009-03-31 23:00:32 +0000468 if (!T)
469 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anna Zaks590dd8e2011-09-20 21:38:35 +0000471 PathDiagnosticLocation Start =
472 PathDiagnosticLocation::createBegin(T, SMgr,
473 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremenek31061982009-03-31 23:00:32 +0000475 switch (T->getStmtClass()) {
476 default:
477 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek31061982009-03-31 23:00:32 +0000479 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000480 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000481 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek31061982009-03-31 23:00:32 +0000483 if (!S)
484 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek31061982009-03-31 23:00:32 +0000486 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000487 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000488 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek31061982009-03-31 23:00:32 +0000490 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000491 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000492 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000493 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000494 break;
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
497 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000498 // Figure out what case arm we took.
499 std::string sbuf;
500 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek9c378f72011-08-12 23:37:29 +0000502 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000503 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 switch (S->getStmtClass()) {
506 default:
507 os << "No cases match in the switch statement. "
508 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000509 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000510 break;
511 case Stmt::DefaultStmtClass:
512 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000513 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000514 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenek31061982009-03-31 23:00:32 +0000516 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000517 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000518 const CaseStmt *Case = cast<CaseStmt>(S);
519 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000520
521 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000522 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenek9c378f72011-08-12 23:37:29 +0000524 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000525 // FIXME: Maybe this should be an assertion. Are there cases
526 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000527 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000528 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek31061982009-03-31 23:00:32 +0000530 if (D) {
531 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000532 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000533 }
534 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000535
536 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000537 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000538
Ted Kremenek31061982009-03-31 23:00:32 +0000539 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000540 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000541 break;
542 }
543 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000544 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000545 os.str()));
546 }
547 else {
548 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000549 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000550 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000551 os.str()));
552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenek31061982009-03-31 23:00:32 +0000554 break;
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Ted Kremenek31061982009-03-31 23:00:32 +0000557 case Stmt::BreakStmtClass:
558 case Stmt::ContinueStmtClass: {
559 std::string sbuf;
560 llvm::raw_string_ostream os(sbuf);
561 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000562 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000563 os.str()));
564 break;
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek31061982009-03-31 23:00:32 +0000567 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000568 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000569 case Stmt::ConditionalOperatorClass: {
570 std::string sbuf;
571 llvm::raw_string_ostream os(sbuf);
572 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek31061982009-03-31 23:00:32 +0000574 if (*(Src->succ_begin()+1) == Dst)
575 os << "false";
576 else
577 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Ted Kremenek31061982009-03-31 23:00:32 +0000579 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremenek31061982009-03-31 23:00:32 +0000581 if (const Stmt *S = End.asStmt())
582 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Ted Kremenek2042fc12012-02-24 06:00:00 +0000584 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000585 os.str()));
586 break;
587 }
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Ted Kremenek31061982009-03-31 23:00:32 +0000589 // Determine control-flow for short-circuited '&&' and '||'.
590 case Stmt::BinaryOperatorClass: {
591 if (!PDB.supportsLogicalOpControlFlow())
592 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000594 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000595 std::string sbuf;
596 llvm::raw_string_ostream os(sbuf);
597 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000598
John McCall2de56d12010-08-25 11:45:40 +0000599 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000600 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Ted Kremenek31061982009-03-31 23:00:32 +0000602 if (*(Src->succ_begin()+1) == Dst) {
603 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000604 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000605 PathDiagnosticLocation Start =
606 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000607 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000608 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000609 }
Ted Kremenek31061982009-03-31 23:00:32 +0000610 else {
611 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000612 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000613 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000614 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000615 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000616 }
Ted Kremenek31061982009-03-31 23:00:32 +0000617 }
618 else {
John McCall2de56d12010-08-25 11:45:40 +0000619 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000620 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Ted Kremenek31061982009-03-31 23:00:32 +0000622 if (*(Src->succ_begin()+1) == Dst) {
623 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000624 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000625 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000626 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000627 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000628 }
629 else {
630 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000631 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000632 PathDiagnosticLocation Start =
633 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000634 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000635 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000636 }
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek31061982009-03-31 23:00:32 +0000639 break;
640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000643 if (*(Src->succ_begin()) == Dst) {
644 std::string sbuf;
645 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Ted Kremenek31061982009-03-31 23:00:32 +0000647 os << "Loop condition is true. ";
648 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek31061982009-03-31 23:00:32 +0000650 if (const Stmt *S = End.asStmt())
651 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek2042fc12012-02-24 06:00:00 +0000653 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000654 os.str()));
655 }
656 else {
657 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Ted Kremenek31061982009-03-31 23:00:32 +0000659 if (const Stmt *S = End.asStmt())
660 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek2042fc12012-02-24 06:00:00 +0000662 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000663 "Loop condition is false. Exiting loop"));
664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Ted Kremenek31061982009-03-31 23:00:32 +0000666 break;
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremenek31061982009-03-31 23:00:32 +0000669 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000670 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000671 if (*(Src->succ_begin()+1) == Dst) {
672 std::string sbuf;
673 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Ted Kremenek31061982009-03-31 23:00:32 +0000675 os << "Loop condition is false. ";
676 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
677 if (const Stmt *S = End.asStmt())
678 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremenek2042fc12012-02-24 06:00:00 +0000680 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000681 os.str()));
682 }
683 else {
684 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
685 if (const Stmt *S = End.asStmt())
686 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenek2042fc12012-02-24 06:00:00 +0000688 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000689 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenek31061982009-03-31 23:00:32 +0000692 break;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Ted Kremenek31061982009-03-31 23:00:32 +0000695 case Stmt::IfStmtClass: {
696 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek31061982009-03-31 23:00:32 +0000698 if (const Stmt *S = End.asStmt())
699 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek31061982009-03-31 23:00:32 +0000701 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000702 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000703 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000704 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000705 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000706 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek31061982009-03-31 23:00:32 +0000708 break;
709 }
710 }
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000713 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000714 // Add diagnostic pieces from custom visitors.
715 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +0000716 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
717 E = visitors.end();
718 I != E; ++I) {
Anna Zaks368a0d52012-03-15 21:13:02 +0000719 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek2042fc12012-02-24 06:00:00 +0000720 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +0000721 updateStackPiecesWithMessage(p, CallStack);
722 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000723 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000724 }
Ted Kremenek31061982009-03-31 23:00:32 +0000725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek14856d72009-04-06 23:06:54 +0000727 // After constructing the full PathDiagnostic, do a pass over it to compact
728 // PathDiagnosticPieces that occur within a macro.
Ted Kremenek77d09442012-03-02 01:27:31 +0000729 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000730}
731
732//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000733// "Extensive" PathDiagnostic generation.
734//===----------------------------------------------------------------------===//
735
736static bool IsControlFlowExpr(const Stmt *S) {
737 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000739 if (!E)
740 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
742 E = E->IgnoreParenCasts();
743
John McCall56ca35d2011-02-17 10:25:35 +0000744 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000745 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000747 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
748 if (B->isLogicalOp())
749 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
751 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000752}
753
Ted Kremenek14856d72009-04-06 23:06:54 +0000754namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000755class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000756 bool IsDead;
757public:
758 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
759 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000760
761 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000762 bool isDead() const { return IsDead; }
763};
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000765class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000766 std::vector<ContextLocation> CLocs;
767 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000768 PathDiagnostic &PD;
769 PathDiagnosticBuilder &PDB;
770 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000772 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Ted Kremenek14856d72009-04-06 23:06:54 +0000774 bool containsLocation(const PathDiagnosticLocation &Container,
775 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek14856d72009-04-06 23:06:54 +0000777 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek9650cf32009-05-11 21:42:34 +0000779 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
780 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000781 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000782 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000783 while (1) {
784 // Adjust the location for some expressions that are best referenced
785 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000786 switch (S->getStmtClass()) {
787 default:
788 break;
789 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000790 case Stmt::GenericSelectionExprClass:
791 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000792 firstCharOnly = true;
793 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000794 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000795 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000796 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000797 firstCharOnly = true;
798 continue;
799 case Stmt::ChooseExprClass:
800 S = cast<ChooseExpr>(S)->getCond();
801 firstCharOnly = true;
802 continue;
803 case Stmt::BinaryOperatorClass:
804 S = cast<BinaryOperator>(S)->getLHS();
805 firstCharOnly = true;
806 continue;
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek9650cf32009-05-11 21:42:34 +0000809 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000810 }
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek9650cf32009-05-11 21:42:34 +0000812 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000813 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek9650cf32009-05-11 21:42:34 +0000816 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000817 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000818
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000819 return L;
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek14856d72009-04-06 23:06:54 +0000822 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000823 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000824 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000825 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000826 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000827 CLocs.pop_back();
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek14856d72009-04-06 23:06:54 +0000830public:
831 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
832 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremeneka301a672009-04-22 18:16:20 +0000834 // If the PathDiagnostic already has pieces, add the enclosing statement
835 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000836 if (!PD.path.empty()) {
837 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek14856d72009-04-06 23:06:54 +0000839 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000840 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000841 }
842 }
843
844 ~EdgeBuilder() {
845 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000846
Ted Kremeneka301a672009-04-22 18:16:20 +0000847 // Finally, add an initial edge from the start location of the first
848 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000849 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000850 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000851 PDB.getSourceManager());
852 if (L.isValid())
853 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000854 }
855
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000856 void flushLocations() {
857 while (!CLocs.empty())
858 popLocation();
859 PrevLoc = PathDiagnosticLocation();
860 }
861
Ted Kremenek14856d72009-04-06 23:06:54 +0000862 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000864 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Ted Kremenek14856d72009-04-06 23:06:54 +0000866 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000867 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000868};
Ted Kremenek14856d72009-04-06 23:06:54 +0000869} // end anonymous namespace
870
871
872PathDiagnosticLocation
873EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
874 if (const Stmt *S = L.asStmt()) {
875 if (IsControlFlowExpr(S))
876 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
878 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek14856d72009-04-06 23:06:54 +0000881 return L;
882}
883
884bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
885 const PathDiagnosticLocation &Containee) {
886
887 if (Container == Containee)
888 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Ted Kremenek14856d72009-04-06 23:06:54 +0000890 if (Container.asDecl())
891 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek14856d72009-04-06 23:06:54 +0000893 if (const Stmt *S = Containee.asStmt())
894 if (const Stmt *ContainerS = Container.asStmt()) {
895 while (S) {
896 if (S == ContainerS)
897 return true;
898 S = PDB.getParent(S);
899 }
900 return false;
901 }
902
903 // Less accurate: compare using source ranges.
904 SourceRange ContainerR = Container.asRange();
905 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Ted Kremenek14856d72009-04-06 23:06:54 +0000907 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000908 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
909 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
910 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
911 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chandler Carruth64211622011-07-25 21:09:52 +0000913 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
914 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
915 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
916 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremenek14856d72009-04-06 23:06:54 +0000918 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000919 assert(ContaineeBegLine <= ContaineeEndLine);
920
Ted Kremenek14856d72009-04-06 23:06:54 +0000921 return (ContainerBegLine <= ContaineeBegLine &&
922 ContainerEndLine >= ContaineeEndLine &&
923 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000924 SM.getExpansionColumnNumber(ContainerRBeg) <=
925 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000926 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000927 SM.getExpansionColumnNumber(ContainerREnd) >=
Ted Kremenek6488dc32012-03-28 05:24:50 +0000928 SM.getExpansionColumnNumber(ContaineeREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000929}
930
Ted Kremenek14856d72009-04-06 23:06:54 +0000931void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
932 if (!PrevLoc.isValid()) {
933 PrevLoc = NewLoc;
934 return;
935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000937 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
938 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000940 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +0000941 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Ted Kremenek14856d72009-04-06 23:06:54 +0000943 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +0000944 if (NewLocClean.asLocation().getExpansionLoc() ==
945 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +0000946 return;
947
Ted Kremenek2042fc12012-02-24 06:00:00 +0000948 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000949 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +0000950}
951
952void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremeneka301a672009-04-22 18:16:20 +0000954 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
955 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Ted Kremenek14856d72009-04-06 23:06:54 +0000957 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
958
959 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000960 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Ted Kremenek14856d72009-04-06 23:06:54 +0000962 // Is the top location context the same as the one for the new location?
963 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000964 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000965 if (IsConsumedExpr(TopContextLoc) &&
966 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000967 TopContextLoc.markDead();
968
Ted Kremenek14856d72009-04-06 23:06:54 +0000969 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000970 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000971
972 return;
973 }
974
975 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000976 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +0000977 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenek4c6f8d32009-05-04 18:15:17 +0000979 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000980 CLocs.push_back(ContextLocation(CLoc, true));
981 return;
982 }
983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000986 return;
Ted Kremenek14856d72009-04-06 23:06:54 +0000987 }
988
989 // Context does not contain the location. Flush it.
990 popLocation();
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000993 // If we reach here, there is no enclosing context. Just add the edge.
994 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +0000995}
996
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000997bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
998 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
999 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001001 return false;
1002}
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremeneke1baed32009-05-05 23:13:38 +00001004void EdgeBuilder::addExtendedContext(const Stmt *S) {
1005 if (!S)
1006 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001007
1008 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001009 while (Parent) {
1010 if (isa<CompoundStmt>(Parent))
1011 Parent = PDB.getParent(Parent);
1012 else
1013 break;
1014 }
1015
1016 if (Parent) {
1017 switch (Parent->getStmtClass()) {
1018 case Stmt::DoStmtClass:
1019 case Stmt::ObjCAtSynchronizedStmtClass:
1020 addContext(Parent);
1021 default:
1022 break;
1023 }
1024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremeneke1baed32009-05-05 23:13:38 +00001026 addContext(S);
1027}
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek14856d72009-04-06 23:06:54 +00001029void EdgeBuilder::addContext(const Stmt *S) {
1030 if (!S)
1031 return;
1032
Ted Kremenek59950d32012-02-24 07:12:52 +00001033 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Ted Kremenek14856d72009-04-06 23:06:54 +00001035 while (!CLocs.empty()) {
1036 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1037
1038 // Is the top location context the same as the one for the new location?
1039 if (TopContextLoc == L)
1040 return;
1041
1042 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001043 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001044 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001045 }
1046
1047 // Context does not contain the location. Flush it.
1048 popLocation();
1049 }
1050
1051 CLocs.push_back(L);
1052}
1053
1054static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1055 PathDiagnosticBuilder &PDB,
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001056 const ExplodedNode *N,
1057 ArrayRef<BugReporterVisitor *> visitors) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001058 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001059 const SourceManager& SM = PDB.getSourceManager();
Anna Zaks56a938f2012-03-16 23:24:20 +00001060 StackDiagVector CallStack;
Ted Kremenek14856d72009-04-06 23:06:54 +00001061
Ted Kremenek9c378f72011-08-12 23:37:29 +00001062 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001063 while (NextNode) {
1064 N = NextNode;
1065 NextNode = GetPredecessorNode(N);
1066 ProgramPoint P = N->getLocation();
1067
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001068 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001069 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1070 const StackFrameContext *LCtx =
1071 CE->getLocationContext()->getCurrentStackFrame();
1072 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1073 PDB.getSourceManager(),
1074 LCtx);
1075 EB.addEdge(Loc, true);
1076 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001077 PathDiagnosticCallPiece *C =
1078 PathDiagnosticCallPiece::construct(N, *CE, SM);
1079 PD.getActivePath().push_front(C);
1080 PD.pushActivePath(&C->path);
Anna Zaks56a938f2012-03-16 23:24:20 +00001081 CallStack.push_back(StackDiagPair(C, N));
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001082 break;
1083 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001084
Ted Kremenek2042fc12012-02-24 06:00:00 +00001085 // Pop the call hierarchy if we are done walking the contents
1086 // of a function call.
1087 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
Ted Kremenek097ebb32012-03-06 01:25:01 +00001088 // Add an edge to the start of the function.
1089 const Decl *D = CE->getCalleeContext()->getDecl();
1090 PathDiagnosticLocation pos =
1091 PathDiagnosticLocation::createBegin(D, SM);
1092 EB.addEdge(pos);
1093
1094 // Flush all locations, and pop the active path.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001095 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001096 PD.popActivePath();
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001097 assert(!PD.getActivePath().empty());
1098 PDB.LC = N->getLocationContext();
Ted Kremenek097ebb32012-03-06 01:25:01 +00001099
Ted Kremenek2042fc12012-02-24 06:00:00 +00001100 // The current active path should never be empty. Either we
1101 // just added a bunch of stuff to the top-level path, or
1102 // we have a previous CallExit. If the front of the active
1103 // path is not a PathDiagnosticCallPiece, it means that the
1104 // path terminated within a function call. We must then take the
1105 // current contents of the active path and place it within
1106 // a new PathDiagnosticCallPiece.
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001107 PathDiagnosticCallPiece *C =
Ted Kremenek2042fc12012-02-24 06:00:00 +00001108 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
Anna Zaks93739372012-03-14 18:58:28 +00001109 if (!C) {
1110 const Decl * Caller = CE->getLocationContext()->getDecl();
1111 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1112 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001113 C->setCallee(*CE, SM);
1114 EB.addContext(CE->getCallExpr());
Anna Zaks368a0d52012-03-15 21:13:02 +00001115
1116 if (!CallStack.empty()) {
Anna Zaks56a938f2012-03-16 23:24:20 +00001117 assert(CallStack.back().first == C);
Anna Zaks368a0d52012-03-15 21:13:02 +00001118 CallStack.pop_back();
1119 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001120 break;
1121 }
Ted Kremenek4ba86bc2012-03-02 21:16:22 +00001122
1123 // Note that is important that we update the LocationContext
1124 // after looking at CallExits. CallExit basically adds an
1125 // edge in the *caller*, so we don't want to update the LocationContext
1126 // too soon.
1127 PDB.LC = N->getLocationContext();
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001128
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001129 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001130 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001131 const CFGBlock &Blk = *BE->getSrc();
1132 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001134 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001135 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001136 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001137 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001139 if (!Term) {
1140 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1141 CS = dyn_cast<CompoundStmt>(FS->getBody());
1142 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001143 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001146 PathDiagnosticEventPiece *p =
1147 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001148 "Looping back to the head of the loop");
Ted Kremenek2dd17ab2012-03-06 01:00:36 +00001149 p->setPrunable(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001152 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001154 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001155 PathDiagnosticLocation BL =
1156 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001157 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001158 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001161 if (Term)
1162 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001164 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001165 }
1166
Mike Stump1eb44332009-09-09 15:08:12 +00001167 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001168 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1169 const Stmt *stmt = S->getStmt();
1170 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001171 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001172 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001173 }
1174 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001175 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001176 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001177
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001178 break;
1179 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001180
1181
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001182 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001184 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001185 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Anna Zaks8e6431a2011-08-18 22:37:56 +00001187 // Add pieces from custom visitors.
1188 BugReport *R = PDB.getBugReport();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001189 for (ArrayRef<BugReporterVisitor *>::iterator I = visitors.begin(),
1190 E = visitors.end();
1191 I != E; ++I) {
Anna Zaks8e6431a2011-08-18 22:37:56 +00001192 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001193 const PathDiagnosticLocation &Loc = p->getLocation();
1194 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001195 PD.getActivePath().push_front(p);
Anna Zaks368a0d52012-03-15 21:13:02 +00001196 updateStackPiecesWithMessage(p, CallStack);
1197
Ted Kremenek8966bc12009-05-06 21:39:49 +00001198 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001199 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001200 }
Mike Stump1eb44332009-09-09 15:08:12 +00001201 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001202 }
1203}
1204
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001205//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001206// Methods for BugType and subclasses.
1207//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001208BugType::~BugType() { }
1209
Ted Kremenekcf118d42009-02-04 23:49:09 +00001210void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001211
David Blaikie99ba9e32011-12-20 02:48:34 +00001212void BuiltinBug::anchor() {}
1213
Ted Kremenekcf118d42009-02-04 23:49:09 +00001214//===----------------------------------------------------------------------===//
1215// Methods for BugReport and subclasses.
1216//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001217
David Blaikie99ba9e32011-12-20 02:48:34 +00001218void BugReport::NodeResolver::anchor() {}
1219
Anna Zaks8e6431a2011-08-18 22:37:56 +00001220void BugReport::addVisitor(BugReporterVisitor* visitor) {
1221 if (!visitor)
1222 return;
1223
1224 llvm::FoldingSetNodeID ID;
1225 visitor->Profile(ID);
1226 void *InsertPos;
1227
1228 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1229 delete visitor;
1230 return;
1231 }
1232
1233 CallbacksSet.InsertNode(visitor, InsertPos);
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001234 Callbacks.push_back(visitor);
1235 ++ConfigurationChangeToken;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001236}
1237
1238BugReport::~BugReport() {
1239 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001240 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001241 }
1242}
Anna Zakse172e8b2011-08-17 23:00:25 +00001243
Ted Kremenek07189522012-04-04 18:11:35 +00001244const Decl *BugReport::getDeclWithIssue() const {
1245 if (DeclWithIssue)
1246 return DeclWithIssue;
1247
1248 const ExplodedNode *N = getErrorNode();
1249 if (!N)
1250 return 0;
1251
1252 const LocationContext *LC = N->getLocationContext();
1253 return LC->getCurrentStackFrame()->getDecl();
1254}
1255
Anna Zakse172e8b2011-08-17 23:00:25 +00001256void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1257 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001258 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001259 if (UniqueingLocation.isValid()) {
1260 UniqueingLocation.Profile(hash);
1261 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001262 Location.Profile(hash);
1263 } else {
1264 assert(ErrorNode);
1265 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1266 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001267
1268 for (SmallVectorImpl<SourceRange>::const_iterator I =
1269 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1270 const SourceRange range = *I;
1271 if (!range.isValid())
1272 continue;
1273 hash.AddInteger(range.getBegin().getRawEncoding());
1274 hash.AddInteger(range.getEnd().getRawEncoding());
1275 }
1276}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001277
Ted Kremenek76aadc32012-03-09 01:13:14 +00001278void BugReport::markInteresting(SymbolRef sym) {
1279 if (!sym)
1280 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001281
1282 // If the symbol wasn't already in our set, note a configuration change.
1283 if (interestingSymbols.insert(sym).second)
1284 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001285
1286 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
1287 interestingRegions.insert(meta->getRegion());
Ted Kremenek76aadc32012-03-09 01:13:14 +00001288}
1289
1290void BugReport::markInteresting(const MemRegion *R) {
1291 if (!R)
1292 return;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001293
1294 // If the base region wasn't already in our set, note a configuration change.
Ted Kremenek76aadc32012-03-09 01:13:14 +00001295 R = R->getBaseRegion();
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001296 if (interestingRegions.insert(R).second)
1297 ++ConfigurationChangeToken;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001298
Ted Kremenek76aadc32012-03-09 01:13:14 +00001299 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1300 interestingSymbols.insert(SR->getSymbol());
1301}
1302
1303void BugReport::markInteresting(SVal V) {
1304 markInteresting(V.getAsRegion());
1305 markInteresting(V.getAsSymbol());
1306}
1307
1308bool BugReport::isInteresting(SVal V) const {
1309 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
1310}
1311
1312bool BugReport::isInteresting(SymbolRef sym) const {
1313 if (!sym)
1314 return false;
Jordy Rose8ec588e2012-03-15 22:45:29 +00001315 // We don't currently consider metadata symbols to be interesting
1316 // even if we know their region is interesting. Is that correct behavior?
Ted Kremenek76aadc32012-03-09 01:13:14 +00001317 return interestingSymbols.count(sym);
1318}
1319
1320bool BugReport::isInteresting(const MemRegion *R) const {
1321 if (!R)
1322 return false;
1323 R = R->getBaseRegion();
1324 bool b = interestingRegions.count(R);
1325 if (b)
1326 return true;
1327 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1328 return interestingSymbols.count(SR->getSymbol());
1329 return false;
1330}
1331
1332
Ted Kremenek9c378f72011-08-12 23:37:29 +00001333const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001334 if (!ErrorNode)
1335 return 0;
1336
Tom Care212f6d32010-09-16 03:50:38 +00001337 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001338 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Ted Kremenek9c378f72011-08-12 23:37:29 +00001340 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001341 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001342 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001343 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001344 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001345 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001346 S = GetStmt(ProgP);
1347
1348 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001349}
1350
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001351std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001352BugReport::getRanges() {
1353 // If no custom ranges, add the range of the statement corresponding to
1354 // the error node.
1355 if (Ranges.empty()) {
1356 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1357 addRange(E->getSourceRange());
1358 else
1359 return std::make_pair(ranges_iterator(), ranges_iterator());
1360 }
1361
Anna Zaks14924262011-08-24 20:31:06 +00001362 // User-specified absence of range info.
1363 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1364 return std::make_pair(ranges_iterator(), ranges_iterator());
1365
Anna Zakse172e8b2011-08-17 23:00:25 +00001366 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001367}
1368
Anna Zaks590dd8e2011-09-20 21:38:35 +00001369PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001370 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001371 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001372 "Either Location or ErrorNode should be specified but not both.");
1373
Ted Kremenek9c378f72011-08-12 23:37:29 +00001374 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001375 const LocationContext *LC = ErrorNode->getLocationContext();
1376
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001377 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001378 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001379 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001380 // For binary operators, return the location of the operator.
1381 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001382 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001383
Anna Zaks590dd8e2011-09-20 21:38:35 +00001384 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001385 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001386 } else {
1387 assert(Location.isValid());
1388 return Location;
1389 }
1390
Anna Zaks590dd8e2011-09-20 21:38:35 +00001391 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001392}
1393
Ted Kremenekcf118d42009-02-04 23:49:09 +00001394//===----------------------------------------------------------------------===//
1395// Methods for BugReporter and subclasses.
1396//===----------------------------------------------------------------------===//
1397
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001398BugReportEquivClass::~BugReportEquivClass() { }
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001399GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001400BugReporterData::~BugReporterData() {}
1401
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001402ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001403
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001404ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001405GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1406
Anna Zaks3b030a22011-08-19 01:57:09 +00001407BugReporter::~BugReporter() {
1408 FlushReports();
1409
1410 // Free the bug reports we are tracking.
1411 typedef std::vector<BugReportEquivClass *> ContTy;
1412 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1413 I != E; ++I) {
1414 delete *I;
1415 }
1416}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001417
1418void BugReporter::FlushReports() {
1419 if (BugTypes.isEmpty())
1420 return;
1421
1422 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001423 // warnings and new BugTypes.
1424 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1425 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001426 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001427 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001428 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001429 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001430 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001431 const_cast<BugType*>(*I)->FlushReports(*this);
1432
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001433 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1434 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1435 BugReportEquivClass& EQ = *EI;
1436 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001437 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001438
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001439 // BugReporter owns and deletes only BugTypes created implicitly through
1440 // EmitBasicReport.
1441 // FIXME: There are leaks from checkers that assume that the BugTypes they
1442 // create will be destroyed by the BugReporter.
1443 for (llvm::StringMap<BugType*>::iterator
1444 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1445 delete I->second;
1446
Ted Kremenekcf118d42009-02-04 23:49:09 +00001447 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001448 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001449}
1450
1451//===----------------------------------------------------------------------===//
1452// PathDiagnostics generation.
1453//===----------------------------------------------------------------------===//
1454
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001455static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001456 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001457MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001458 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Ted Kremenekcf118d42009-02-04 23:49:09 +00001460 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001461 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001462 // error node unless there are two or more error nodes with the same minimum
1463 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001464 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001465 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001466
1467 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001468 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1469 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Ted Kremenekcf118d42009-02-04 23:49:09 +00001471 // Create owning pointers for GTrim and NMap just to ensure that they are
1472 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001473 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1474 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Ted Kremenekcf118d42009-02-04 23:49:09 +00001476 // Find the (first) error node in the trimmed graph. We just need to consult
1477 // the node map (NMap) which maps from nodes in the original graph to nodes
1478 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001479
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001480 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001481 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001482 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001483
Ted Kremenek40406fe2010-12-03 06:52:30 +00001484 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1485 const ExplodedNode *originalNode = nodes[nodeIndex];
1486 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001487 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001488 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001489 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001490 }
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenek938332c2009-05-16 01:11:58 +00001492 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001493
1494 // Create a new (third!) graph with a single path. This is the graph
1495 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001496 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek10aa5542009-03-12 23:41:59 +00001498 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001499 // to the root node, and then construct a new graph that contains only
1500 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001501 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001503 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001504 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001506 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001507 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001508 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001510 if (Visited.find(Node) != Visited.end())
1511 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001513 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001515 if (Node->pred_empty()) {
1516 Root = Node;
1517 break;
1518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001520 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001521 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001522 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Ted Kremenek938332c2009-05-16 01:11:58 +00001525 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenek10aa5542009-03-12 23:41:59 +00001527 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001528 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001529 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001530 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001531 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001533 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001534 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001535 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001536 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001538 // Create the equivalent node in the new graph with the same state
1539 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001540 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001542 // Store the mapping to the original node.
1543 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1544 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001545 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001547 // Link up the new node with the previous node.
1548 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001549 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001551 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001553 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001554 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001555 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001556 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001557 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001558 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001559 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001560 }
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001562 // Find the next successor node. We choose the node that is marked
1563 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001564 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1565 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001566 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001568 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001570 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001572 if (I == Visited.end())
1573 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001575 if (!N || I->second < MinVal) {
1576 N = *SI;
1577 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001578 }
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(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Ted Kremenek938332c2009-05-16 01:11:58 +00001584 assert(First);
1585
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001586 return std::make_pair(std::make_pair(GNew, BM),
1587 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001588}
1589
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001590/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1591/// and collapses PathDiagosticPieces that are expanded by macros.
Ted Kremenek77d09442012-03-02 01:27:31 +00001592static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001593 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1594 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001596 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001597 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001599 MacroStackTy MacroStack;
1600 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenek77d09442012-03-02 01:27:31 +00001602 for (PathPieces::const_iterator I = path.begin(), E = path.end();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001603 I!=E; ++I) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001604
1605 PathDiagnosticPiece *piece = I->getPtr();
1606
1607 // Recursively compact calls.
1608 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
1609 CompactPathDiagnostic(call->path, SM);
1610 }
1611
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001612 // Get the location of the PathDiagnosticPiece.
Ted Kremenek77d09442012-03-02 01:27:31 +00001613 const FullSourceLoc Loc = piece->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001615 // Determine the instantiation location, which is the location we group
1616 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001617 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001618 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001619 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001621 if (Loc.isFileID()) {
1622 MacroStack.clear();
Ted Kremenek77d09442012-03-02 01:27:31 +00001623 Pieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001624 continue;
1625 }
1626
1627 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001629 // Is the PathDiagnosticPiece within the same macro group?
1630 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek77d09442012-03-02 01:27:31 +00001631 MacroStack.back().first->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001632 continue;
1633 }
1634
1635 // We aren't in the same group. Are we descending into a new macro
1636 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001637 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001638
1639 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001640 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001641 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001643 // Walk the entire macro stack.
1644 while (!MacroStack.empty()) {
1645 if (InstantiationLoc == MacroStack.back().second) {
1646 MacroGroup = MacroStack.back().first;
1647 break;
1648 }
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001650 if (ParentInstantiationLoc == MacroStack.back().second) {
1651 MacroGroup = MacroStack.back().first;
1652 break;
1653 }
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001655 MacroStack.pop_back();
1656 }
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001658 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1659 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001660 PathDiagnosticMacroPiece *NewGroup =
1661 new PathDiagnosticMacroPiece(
Ted Kremenek77d09442012-03-02 01:27:31 +00001662 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001663
1664 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001665 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001666 else {
1667 assert(InstantiationLoc.isFileID());
1668 Pieces.push_back(NewGroup);
1669 }
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001671 MacroGroup = NewGroup;
1672 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1673 }
1674
1675 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek77d09442012-03-02 01:27:31 +00001676 MacroGroup->subPieces.push_back(piece);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001677 }
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001679 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek77d09442012-03-02 01:27:31 +00001680 path.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Ted Kremenek77d09442012-03-02 01:27:31 +00001682 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I)
1683 path.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001684}
1685
Ted Kremenek7dc86642009-03-31 20:22:36 +00001686void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001687 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Ted Kremenek40406fe2010-12-03 06:52:30 +00001689 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001690 SmallVector<const ExplodedNode *, 10> errorNodes;
1691 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001692 E = bugReports.end(); I != E; ++I) {
1693 errorNodes.push_back((*I)->getErrorNode());
1694 }
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001696 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001697 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001698 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001699 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001700 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Ted Kremenekcf118d42009-02-04 23:49:09 +00001702 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001703 assert(GPair.second.second < bugReports.size());
1704 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001705 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001707 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1708 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001709 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001710
1711 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001712 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1713 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Anna Zaks8e6431a2011-08-18 22:37:56 +00001715 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001716 R->addVisitor(new NilReceiverBRVisitor());
1717 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001719 BugReport::VisitorList visitors;
1720 unsigned originalReportConfigToken, finalReportConfigToken;
Anna Zaks23f395e2011-08-20 01:27:22 +00001721
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001722 // While generating diagnostics, it's possible the visitors will decide
1723 // new symbols and regions are interesting, or add other visitors based on
1724 // the information they find. If they do, we need to regenerate the path
1725 // based on our new report configuration.
1726 do {
1727 // Get a clean copy of all the visitors.
1728 for (BugReport::visitor_iterator I = R->visitor_begin(),
1729 E = R->visitor_end(); I != E; ++I)
1730 visitors.push_back((*I)->clone());
1731
1732 // Clear out the active path from any previous work.
1733 PD.getActivePath().clear();
1734 originalReportConfigToken = R->getConfigurationChangeToken();
1735
1736 // Generate the very last diagnostic piece - the piece is visible before
1737 // the trace is expanded.
1738 PathDiagnosticPiece *LastPiece = 0;
1739 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
1740 I != E; ++I) {
1741 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1742 assert (!LastPiece &&
1743 "There can only be one final piece in a diagnostic.");
1744 LastPiece = Piece;
1745 }
1746 }
1747 if (!LastPiece)
1748 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1749 if (LastPiece)
1750 PD.getActivePath().push_back(LastPiece);
1751 else
1752 return;
1753
1754 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001755 case PathDiagnosticConsumer::Extensive:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001756 GenerateExtensivePathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001757 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001758 case PathDiagnosticConsumer::Minimal:
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001759 GenerateMinimalPathDiagnostic(PD, PDB, N, visitors);
Ted Kremenek7dc86642009-03-31 20:22:36 +00001760 break;
Jordy Rose3bc75ca2012-03-24 03:03:29 +00001761 }
1762
1763 // Clean up the visitors we used.
1764 llvm::DeleteContainerPointers(visitors);
1765
1766 // Did anything change while generating this path?
1767 finalReportConfigToken = R->getConfigurationChangeToken();
1768 } while(finalReportConfigToken != originalReportConfigToken);
1769
Ted Kremenekc89f4b02012-02-28 23:06:21 +00001770 // Finally, prune the diagnostic path of uninteresting stuff.
1771 bool hasSomethingInteresting = RemoveUneededCalls(PD.getMutablePieces());
1772 assert(hasSomethingInteresting);
1773 (void) hasSomethingInteresting;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001774}
1775
Ted Kremenekcf118d42009-02-04 23:49:09 +00001776void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001777 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001778}
1779
Mike Stump1eb44332009-09-09 15:08:12 +00001780void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001781 // Compute the bug report's hash to determine its equivalence class.
1782 llvm::FoldingSetNodeID ID;
1783 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001784
1785 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001786 BugType& BT = R->getBugType();
1787 Register(&BT);
1788 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001789 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Ted Kremenekcf118d42009-02-04 23:49:09 +00001791 if (!EQ) {
1792 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001793 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001794 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001795 }
1796 else
1797 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001798}
1799
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001800
1801//===----------------------------------------------------------------------===//
1802// Emitting reports in equivalence classes.
1803//===----------------------------------------------------------------------===//
1804
1805namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001806struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001807 const ExplodedNode *N;
1808 ExplodedNode::const_succ_iterator I, E;
1809
1810 FRIEC_WLItem(const ExplodedNode *n)
1811 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1812};
1813}
1814
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001815static BugReport *
1816FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001817 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001818
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001819 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1820 assert(I != E);
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001821 BugType& BT = I->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001822
Ted Kremenek40406fe2010-12-03 06:52:30 +00001823 // If we don't need to suppress any of the nodes because they are
1824 // post-dominated by a sink, simply add all the nodes in the equivalence class
1825 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001826 if (!BT.isSuppressOnSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001827 BugReport *R = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001828 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001829 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001830 if (N) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001831 R = I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001832 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001833 }
1834 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001835 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001836 }
1837
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001838 // For bug reports that should be suppressed when all paths are post-dominated
1839 // by a sink node, iterate through the reports in the equivalence class
1840 // until we find one that isn't post-dominated (if one exists). We use a
1841 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1842 // this as a recursive function, but we don't want to risk blowing out the
1843 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001844 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001845
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001846 for (; I != E; ++I) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001847 const ExplodedNode *errorNode = I->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001848
Ted Kremenek40406fe2010-12-03 06:52:30 +00001849 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001850 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001851 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001852 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001853 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001854 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001855 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001856 if (errorNode->succ_empty()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001857 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001858 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001859 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001860 continue;
1861 }
1862
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001863 // At this point we know that 'N' is not a sink and it has at least one
1864 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1865 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001866 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001867 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1868
1869 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001870 WL.push_back(errorNode);
1871 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001872
1873 while (!WL.empty()) {
1874 WLItem &WI = WL.back();
1875 assert(!WI.N->succ_empty());
1876
1877 for (; WI.I != WI.E; ++WI.I) {
1878 const ExplodedNode *Succ = *WI.I;
1879 // End-of-path node?
1880 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001881 // If we found an end-of-path node that is not a sink.
1882 if (!Succ->isSink()) {
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001883 bugReports.push_back(I);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001884 if (!exampleReport)
Benjamin Kramer4a5f7242012-04-01 19:30:51 +00001885 exampleReport = I;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001886 WL.clear();
1887 break;
1888 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001889 // Found a sink? Continue on to the next successor.
1890 continue;
1891 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001892 // Mark the successor as visited. If it hasn't been explored,
1893 // enqueue it to the DFS worklist.
1894 unsigned &mark = Visited[Succ];
1895 if (!mark) {
1896 mark = 1;
1897 WL.push_back(Succ);
1898 break;
1899 }
1900 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001901
1902 // The worklist may have been cleared at this point. First
1903 // check if it is empty before checking the last item.
1904 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001905 WL.pop_back();
1906 }
1907 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001908
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001909 // ExampleReport will be NULL if all the nodes in the equivalence class
1910 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001911 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001912}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001913
1914//===----------------------------------------------------------------------===//
1915// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1916// uses global state, which eventually should go elsewhere.
1917//===----------------------------------------------------------------------===//
1918namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001919class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001920 llvm::FoldingSetNodeID ID;
1921public:
1922 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001923 R->Profile(ID);
1924 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001925 }
1926
1927 void Profile(llvm::FoldingSetNodeID &id) {
1928 id = ID;
1929 }
1930
1931 llvm::FoldingSetNodeID &getID() { return ID; }
1932};
1933}
1934
1935static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1936 // FIXME: Eventually this diagnostic cache should reside in something
1937 // like AnalysisManager instead of being a static variable. This is
1938 // really unsafe in the long term.
1939 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1940 static DiagnosticCache DC;
1941
1942 void *InsertPos;
1943 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1944
1945 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1946 delete Item;
1947 return true;
1948 }
1949
1950 DC.InsertNode(Item, InsertPos);
1951 return false;
1952}
1953
Ted Kremenekcf118d42009-02-04 23:49:09 +00001954void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001955 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001956 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1957 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001958 return;
1959
David Blaikieef3643f2011-09-26 00:51:36 +00001960 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Ted Kremenekcf118d42009-02-04 23:49:09 +00001962 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001963 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001964 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001966 OwningPtr<PathDiagnostic>
Ted Kremenek07189522012-04-04 18:11:35 +00001967 D(new PathDiagnostic(exampleReport->getDeclWithIssue(),
1968 exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001969 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001970 ? exampleReport->getDescription()
1971 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001972 BT.getCategory()));
1973
Ted Kremenek40406fe2010-12-03 06:52:30 +00001974 if (!bugReports.empty())
1975 GeneratePathDiagnostic(*D.get(), bugReports);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001976
Ted Kremenek072192b2008-04-30 23:47:44 +00001977 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001978 const BugReport::ExtraTextList &Meta =
1979 exampleReport->getExtraText();
1980 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1981 e = Meta.end(); i != e; ++i) {
1982 D->addMeta(*i);
1983 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001984
Ted Kremenek3148eb42009-01-24 00:55:43 +00001985 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001986 BugReport::ranges_iterator Beg, End;
1987 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001988 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001989
Ted Kremenek88fc1812012-04-04 00:55:29 +00001990 if (!IsCachedDiagnostic(exampleReport, D.get())) {
1991 // Search the description for '%', as that will be interpretted as a
1992 // format character by FormatDiagnostics.
1993 StringRef desc = exampleReport->getShortDescription();
1994
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001995 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001996 llvm::raw_svector_ostream Out(TmpStr);
Ted Kremenek88fc1812012-04-04 00:55:29 +00001997 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I) {
Ted Kremenekc213b482010-01-15 07:56:51 +00001998 if (*I == '%')
1999 Out << "%%";
2000 else
2001 Out << *I;
Ted Kremenek88fc1812012-04-04 00:55:29 +00002002 }
Ted Kremenekc213b482010-01-15 07:56:51 +00002003
2004 Out.flush();
Ted Kremenek88fc1812012-04-04 00:55:29 +00002005 unsigned ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenek57202072008-07-14 17:40:50 +00002006
Anna Zaks590dd8e2011-09-20 21:38:35 +00002007 DiagnosticBuilder diagBuilder = Diag.Report(
2008 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00002009 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00002010 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00002011 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00002012
David Blaikieef3643f2011-09-26 00:51:36 +00002013 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00002014 if (!PD)
2015 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Ted Kremenek802e0242012-02-08 04:32:34 +00002017 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002018 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
2019 exampleReport->getLocation(getSourceManager()),
2020 exampleReport->getDescription());
Ted Kremenek07189522012-04-04 18:11:35 +00002021 for ( ; Beg != End; ++Beg)
2022 piece->addRange(*Beg);
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00002023
Ted Kremenek2042fc12012-02-24 06:00:00 +00002024 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00002025 }
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Ted Kremenek3148eb42009-01-24 00:55:43 +00002027 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002028}
Ted Kremenek57202072008-07-14 17:40:50 +00002029
Ted Kremenek07189522012-04-04 18:11:35 +00002030void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
2031 StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002032 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002033 SourceRange* RBeg, unsigned NumRanges) {
Ted Kremenek07189522012-04-04 18:11:35 +00002034 EmitBasicReport(DeclWithIssue, name, "", str, Loc, RBeg, NumRanges);
Ted Kremenek8c036c72008-09-20 04:23:38 +00002035}
Ted Kremenekcf118d42009-02-04 23:49:09 +00002036
Ted Kremenek07189522012-04-04 18:11:35 +00002037void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
2038 StringRef name,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002039 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002040 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002041 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002043 // 'BT' is owned by BugReporter.
2044 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002045 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenek07189522012-04-04 18:11:35 +00002046 R->setDeclWithIssue(DeclWithIssue);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002047 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2048 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002049}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002050
Chris Lattner5f9e2722011-07-23 10:55:15 +00002051BugType *BugReporter::getBugTypeForName(StringRef name,
2052 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002053 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002054 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2055 llvm::StringMapEntry<BugType *> &
2056 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2057 BugType *BT = entry.getValue();
2058 if (!BT) {
2059 BT = new BugType(name, category);
2060 entry.setValue(BT);
2061 }
2062 return BT;
2063}