blob: de3c8ed81a576e7645c4f0b986abd384abd8038d [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 Kremenek31061982009-03-31 23:00:32 +0000114// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000115//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000116
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000117typedef llvm::DenseMap<const ExplodedNode*,
118const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000119
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000120namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000121class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000122 NodeBackMap& M;
123public:
124 NodeMapClosure(NodeBackMap *m) : M(*m) {}
125 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Ted Kremenek9c378f72011-08-12 23:37:29 +0000127 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000128 NodeBackMap::iterator I = M.find(N);
129 return I == M.end() ? 0 : I->second;
130 }
131};
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000133class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000134 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000135 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000136 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000137 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000138public:
Ted Kremenek59950d32012-02-24 07:12:52 +0000139 const LocationContext *LC;
140
Ted Kremenek8966bc12009-05-06 21:39:49 +0000141 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000142 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000143 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000144 : BugReporterContext(br),
Ted Kremenek59950d32012-02-24 07:12:52 +0000145 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
146 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenek9c378f72011-08-12 23:37:29 +0000148 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenek9c378f72011-08-12 23:37:29 +0000150 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
151 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anna Zaks8e6431a2011-08-18 22:37:56 +0000153 BugReport *getBugReport() { return R; }
154
Tom Care212f6d32010-09-16 03:50:38 +0000155 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Ted Kremenek59950d32012-02-24 07:12:52 +0000156
157 ParentMap& getParentMap() { return LC->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000159 const Stmt *getParent(const Stmt *S) {
160 return getParentMap().getParent(S);
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek8966bc12009-05-06 21:39:49 +0000163 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000164
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000165 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
David Blaikieef3643f2011-09-26 00:51:36 +0000167 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
168 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000169 }
170
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000171 bool supportsLogicalOpControlFlow() const {
172 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000173 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000174};
175} // end anonymous namespace
176
Ted Kremenek00605e02009-03-27 20:55:39 +0000177PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000178PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000179 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek59950d32012-02-24 07:12:52 +0000180 return PathDiagnosticLocation(S, getSourceManager(), LC);
Ted Kremenek00605e02009-03-27 20:55:39 +0000181
Anna Zaks0cd59482011-09-16 19:18:30 +0000182 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
183 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000184}
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenek00605e02009-03-27 20:55:39 +0000186PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000187PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
188 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000189
Ted Kremenek143ca222008-05-06 18:11:09 +0000190 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000191 if (os.str().empty())
192 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenek00605e02009-03-27 20:55:39 +0000194 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek00605e02009-03-27 20:55:39 +0000196 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000197 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000198 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000199 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000200 else {
201 os << "Execution jumps to the end of the ";
202 const Decl *D = N->getLocationContext()->getDecl();
203 if (isa<ObjCMethodDecl>(D))
204 os << "method";
205 else if (isa<FunctionDecl>(D))
206 os << "function";
207 else {
208 assert(isa<BlockDecl>(D));
209 os << "anonymous block";
210 }
211 os << '.';
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000214 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000215}
216
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000217static bool IsNested(const Stmt *S, ParentMap &PM) {
218 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
219 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000221 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000223 if (Parent)
224 switch (Parent->getStmtClass()) {
225 case Stmt::ForStmtClass:
226 case Stmt::DoStmtClass:
227 case Stmt::WhileStmtClass:
228 return true;
229 default:
230 break;
231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
233 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000234}
235
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000236PathDiagnosticLocation
237PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000238 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000239 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000240 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000241
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000242 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000243 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000245 if (!Parent)
246 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000248 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000249 case Stmt::BinaryOperatorClass: {
250 const BinaryOperator *B = cast<BinaryOperator>(Parent);
251 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000252 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000253 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000254 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000255 case Stmt::CompoundStmtClass:
256 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000257 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000258 case Stmt::ChooseExprClass:
259 // Similar to '?' if we are referring to condition, just have the edge
260 // point to the entire choose expression.
261 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000262 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000263 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000264 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000265 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000266 case Stmt::ConditionalOperatorClass:
267 // For '?', if we are referring to condition, just have the edge point
268 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000269 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000270 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000271 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000272 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000273 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000274 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000275 case Stmt::ForStmtClass:
276 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000277 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000278 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000279 case Stmt::IfStmtClass:
280 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000281 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000282 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000283 case Stmt::ObjCForCollectionStmtClass:
284 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000285 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000286 break;
287 case Stmt::WhileStmtClass:
288 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000289 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000290 break;
291 default:
292 break;
293 }
294
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000295 S = Parent;
296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000298 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000299
300 // Special case: DeclStmts can appear in for statement declarations, in which
301 // case the ForStmt is the context.
302 if (isa<DeclStmt>(S)) {
303 if (const Stmt *Parent = P.getParent(S)) {
304 switch (Parent->getStmtClass()) {
305 case Stmt::ForStmtClass:
306 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000307 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000308 default:
309 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000310 }
311 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000312 }
313 else if (isa<BinaryOperator>(S)) {
314 // Special case: the binary operator represents the initialization
315 // code in a for statement (this can happen when the variable being
316 // initialized is an old variable.
317 if (const ForStmt *FS =
318 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
319 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000320 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000321 }
322 }
323
Anna Zaks220ac8c2011-09-15 01:08:34 +0000324 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000325}
326
Ted Kremenekcf118d42009-02-04 23:49:09 +0000327//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000328// ScanNotableSymbols: closure-like callback for scanning Store bindings.
329//===----------------------------------------------------------------------===//
330
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000331static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
332 ProgramStateManager& VMgr,
333 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek31061982009-03-31 23:00:32 +0000335 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenek31061982009-03-31 23:00:32 +0000337 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek31061982009-03-31 23:00:32 +0000339 if (!isa<PostStmt>(P))
340 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek9c378f72011-08-12 23:37:29 +0000342 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek31061982009-03-31 23:00:32 +0000344 if (!DR)
345 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek5eca4822012-01-06 22:09:28 +0000347 SVal Y = N->getState()->getSVal(DR, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek31061982009-03-31 23:00:32 +0000349 if (X != Y)
350 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek9c378f72011-08-12 23:37:29 +0000352 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek31061982009-03-31 23:00:32 +0000354 if (!VD)
355 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek31061982009-03-31 23:00:32 +0000357 return VD;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek31061982009-03-31 23:00:32 +0000360 return 0;
361}
362
363namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000364class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000365: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek31061982009-03-31 23:00:32 +0000367 SymbolRef Sym;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000368 ProgramStateRef PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000369 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000370 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000371 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000372 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000373 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek31061982009-03-31 23:00:32 +0000375public:
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000377 NotableSymbolHandler(SymbolRef sym,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000378 ProgramStateRef prevst,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000379 const Stmt *s,
380 ProgramStateManager& vmgr,
381 const ExplodedNode *pred,
382 PathDiagnostic& pd,
383 BugReporter& br)
384 : Sym(sym),
385 PrevSt(prevst),
386 S(s),
387 VMgr(vmgr),
388 Pred(pred),
389 PD(pd),
390 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek31061982009-03-31 23:00:32 +0000392 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
393 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek31061982009-03-31 23:00:32 +0000395 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek31061982009-03-31 23:00:32 +0000397 if (ScanSym != Sym)
398 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
400 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000401 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 if (X == V) // Same binding?
404 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek31061982009-03-31 23:00:32 +0000406 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000407 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000408 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremenek31061982009-03-31 23:00:32 +0000410 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek31061982009-03-31 23:00:32 +0000412 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
413 if (!B->isAssignmentOp())
414 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000417 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419 if (!DR)
420 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek31061982009-03-31 23:00:32 +0000422 VD = dyn_cast<VarDecl>(DR->getDecl());
423 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000424 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000425 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
426 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000427 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000428 VD = dyn_cast<VarDecl>(*DS->decl_begin());
429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek31061982009-03-31 23:00:32 +0000431 if (!VD)
432 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000435 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 if (!MostRecent)
438 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek31061982009-03-31 23:00:32 +0000440 // Create the diagnostic.
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000441 if (Loc::isLocType(VD->getType())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000442 SmallString<64> buf;
Jordy Rose7df12342011-08-21 05:25:15 +0000443 llvm::raw_svector_ostream os(buf);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000444 os << '\'' << *VD << "' now aliases '" << *MostRecent << '\'';
Anna Zaks590dd8e2011-09-20 21:38:35 +0000445 PathDiagnosticLocation L =
446 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(),
447 Pred->getLocationContext());
Ted Kremenek2042fc12012-02-24 06:00:00 +0000448 PD.getActivePath().push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek31061982009-03-31 23:00:32 +0000451 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000452 }
Ted Kremenek31061982009-03-31 23:00:32 +0000453};
454}
455
Ted Kremenek9c378f72011-08-12 23:37:29 +0000456static void HandleNotableSymbol(const ExplodedNode *N,
457 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000458 SymbolRef Sym, BugReporter& BR,
459 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek9c378f72011-08-12 23:37:29 +0000461 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000462 ProgramStateRef PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 if (!PrevSt)
465 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek31061982009-03-31 23:00:32 +0000467 // Look at the region bindings of the current state that map to the
468 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000469 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000470 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
471 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
472}
473
474namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000475class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000476: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek31061982009-03-31 23:00:32 +0000478 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000479 const ExplodedNode *N;
480 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000481 GRBugReporter& BR;
482 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek31061982009-03-31 23:00:32 +0000484public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000485 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000486 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000487 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 bool HandleBinding(StoreManager& SMgr, Store store,
490 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek31061982009-03-31 23:00:32 +0000492 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 if (!ScanSym)
495 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek31061982009-03-31 23:00:32 +0000497 if (!BR.isNotable(ScanSym))
498 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek31061982009-03-31 23:00:32 +0000500 if (AlreadyProcessed.count(ScanSym))
501 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Ted Kremenek31061982009-03-31 23:00:32 +0000503 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 HandleNotableSymbol(N, S, ScanSym, BR, PD);
506 return true;
507 }
508};
509} // end anonymous namespace
510
511//===----------------------------------------------------------------------===//
512// "Minimal" path diagnostic generation algorithm.
513//===----------------------------------------------------------------------===//
514
Ted Kremenek14856d72009-04-06 23:06:54 +0000515static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
516
Ted Kremenek31061982009-03-31 23:00:32 +0000517static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
518 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000519 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000520
Ted Kremenek31061982009-03-31 23:00:32 +0000521 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek59950d32012-02-24 07:12:52 +0000522 const LocationContext *LC = PDB.LC;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000523 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000524 ? NULL : *(N->pred_begin());
525 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000526 N = NextNode;
Ted Kremenek59950d32012-02-24 07:12:52 +0000527 PDB.LC = N->getLocationContext();
Ted Kremenek31061982009-03-31 23:00:32 +0000528 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek31061982009-03-31 23:00:32 +0000530 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000531
532 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
533 PathDiagnosticCallPiece *C =
534 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
535 PD.getActivePath().push_front(C);
536 PD.pushActivePath(&C->path);
537 continue;
538 }
539
540 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
541 PD.popActivePath();
542 // The current active path should never be empty. Either we
543 // just added a bunch of stuff to the top-level path, or
544 // we have a previous CallExit. If the front of the active
545 // path is not a PathDiagnosticCallPiece, it means that the
546 // path terminated within a function call. We must then take the
547 // current contents of the active path and place it within
548 // a new PathDiagnosticCallPiece.
549 assert(!PD.getActivePath().empty());
550 PathDiagnosticCallPiece *C =
551 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
552 if (!C)
553 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
554 C->setCallee(*CE, SMgr);
555 continue;
556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek9c378f72011-08-12 23:37:29 +0000558 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
559 const CFGBlock *Src = BE->getSrc();
560 const CFGBlock *Dst = BE->getDst();
561 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Ted Kremenek31061982009-03-31 23:00:32 +0000563 if (!T)
564 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Anna Zaks590dd8e2011-09-20 21:38:35 +0000566 PathDiagnosticLocation Start =
567 PathDiagnosticLocation::createBegin(T, SMgr,
568 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Ted Kremenek31061982009-03-31 23:00:32 +0000570 switch (T->getStmtClass()) {
571 default:
572 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek31061982009-03-31 23:00:32 +0000574 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000575 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000576 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremenek31061982009-03-31 23:00:32 +0000578 if (!S)
579 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremenek31061982009-03-31 23:00:32 +0000581 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000582 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000583 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000586 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000587 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000588 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000589 break;
590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
592 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000593 // Figure out what case arm we took.
594 std::string sbuf;
595 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Ted Kremenek9c378f72011-08-12 23:37:29 +0000597 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000598 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Ted Kremenek31061982009-03-31 23:00:32 +0000600 switch (S->getStmtClass()) {
601 default:
602 os << "No cases match in the switch statement. "
603 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000604 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000605 break;
606 case Stmt::DefaultStmtClass:
607 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000608 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000609 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Ted Kremenek31061982009-03-31 23:00:32 +0000611 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000612 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000613 const CaseStmt *Case = cast<CaseStmt>(S);
614 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000615
616 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000617 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek9c378f72011-08-12 23:37:29 +0000619 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000620 // FIXME: Maybe this should be an assertion. Are there cases
621 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000622 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000623 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Ted Kremenek31061982009-03-31 23:00:32 +0000625 if (D) {
626 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000627 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000628 }
629 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000630
631 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000632 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000633
Ted Kremenek31061982009-03-31 23:00:32 +0000634 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000635 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000636 break;
637 }
638 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000639 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000640 os.str()));
641 }
642 else {
643 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000644 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000645 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000646 os.str()));
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenek31061982009-03-31 23:00:32 +0000649 break;
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek31061982009-03-31 23:00:32 +0000652 case Stmt::BreakStmtClass:
653 case Stmt::ContinueStmtClass: {
654 std::string sbuf;
655 llvm::raw_string_ostream os(sbuf);
656 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000657 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000658 os.str()));
659 break;
660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek31061982009-03-31 23:00:32 +0000662 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000663 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000664 case Stmt::ConditionalOperatorClass: {
665 std::string sbuf;
666 llvm::raw_string_ostream os(sbuf);
667 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremenek31061982009-03-31 23:00:32 +0000669 if (*(Src->succ_begin()+1) == Dst)
670 os << "false";
671 else
672 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenek31061982009-03-31 23:00:32 +0000674 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek31061982009-03-31 23:00:32 +0000676 if (const Stmt *S = End.asStmt())
677 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek2042fc12012-02-24 06:00:00 +0000679 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000680 os.str()));
681 break;
682 }
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremenek31061982009-03-31 23:00:32 +0000684 // Determine control-flow for short-circuited '&&' and '||'.
685 case Stmt::BinaryOperatorClass: {
686 if (!PDB.supportsLogicalOpControlFlow())
687 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000689 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000690 std::string sbuf;
691 llvm::raw_string_ostream os(sbuf);
692 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000693
John McCall2de56d12010-08-25 11:45:40 +0000694 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000695 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek31061982009-03-31 23:00:32 +0000697 if (*(Src->succ_begin()+1) == Dst) {
698 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000699 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000700 PathDiagnosticLocation Start =
701 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000702 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000703 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000704 }
Ted Kremenek31061982009-03-31 23:00:32 +0000705 else {
706 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000707 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000708 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000709 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000710 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000711 }
Ted Kremenek31061982009-03-31 23:00:32 +0000712 }
713 else {
John McCall2de56d12010-08-25 11:45:40 +0000714 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000715 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenek31061982009-03-31 23:00:32 +0000717 if (*(Src->succ_begin()+1) == Dst) {
718 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000719 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000720 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000721 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000722 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000723 }
724 else {
725 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000726 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000727 PathDiagnosticLocation Start =
728 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000729 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000730 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000731 }
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek31061982009-03-31 23:00:32 +0000734 break;
735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
737 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000738 if (*(Src->succ_begin()) == Dst) {
739 std::string sbuf;
740 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek31061982009-03-31 23:00:32 +0000742 os << "Loop condition is true. ";
743 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek31061982009-03-31 23:00:32 +0000745 if (const Stmt *S = End.asStmt())
746 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremenek2042fc12012-02-24 06:00:00 +0000748 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000749 os.str()));
750 }
751 else {
752 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenek31061982009-03-31 23:00:32 +0000754 if (const Stmt *S = End.asStmt())
755 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek2042fc12012-02-24 06:00:00 +0000757 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000758 "Loop condition is false. Exiting loop"));
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek31061982009-03-31 23:00:32 +0000761 break;
762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek31061982009-03-31 23:00:32 +0000764 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000765 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000766 if (*(Src->succ_begin()+1) == Dst) {
767 std::string sbuf;
768 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek31061982009-03-31 23:00:32 +0000770 os << "Loop condition is false. ";
771 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
772 if (const Stmt *S = End.asStmt())
773 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek2042fc12012-02-24 06:00:00 +0000775 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000776 os.str()));
777 }
778 else {
779 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
780 if (const Stmt *S = End.asStmt())
781 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek2042fc12012-02-24 06:00:00 +0000783 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000784 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek31061982009-03-31 23:00:32 +0000787 break;
788 }
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek31061982009-03-31 23:00:32 +0000790 case Stmt::IfStmtClass: {
791 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenek31061982009-03-31 23:00:32 +0000793 if (const Stmt *S = End.asStmt())
794 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek31061982009-03-31 23:00:32 +0000796 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000797 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000798 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000799 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000800 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000801 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek31061982009-03-31 23:00:32 +0000803 break;
804 }
805 }
806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000808 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000809 // Add diagnostic pieces from custom visitors.
810 BugReport *R = PDB.getBugReport();
811 for (BugReport::visitor_iterator I = R->visitor_begin(),
812 E = R->visitor_end(); I!=E; ++I) {
813 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenek2042fc12012-02-24 06:00:00 +0000814 PD.getActivePath().push_front(p);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000815 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ted Kremenek9c378f72011-08-12 23:37:29 +0000818 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000819 // Scan the region bindings, and see if a "notable" symbol has a new
820 // lval binding.
821 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
822 PDB.getStateManager().iterBindings(N->getState(), SNS);
823 }
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenek14856d72009-04-06 23:06:54 +0000826 // After constructing the full PathDiagnostic, do a pass over it to compact
827 // PathDiagnosticPieces that occur within a macro.
828 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000829}
830
831//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000832// "Extensive" PathDiagnostic generation.
833//===----------------------------------------------------------------------===//
834
835static bool IsControlFlowExpr(const Stmt *S) {
836 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000838 if (!E)
839 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
841 E = E->IgnoreParenCasts();
842
John McCall56ca35d2011-02-17 10:25:35 +0000843 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000844 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000846 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
847 if (B->isLogicalOp())
848 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000849
850 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000851}
852
Ted Kremenek14856d72009-04-06 23:06:54 +0000853namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000854class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000855 bool IsDead;
856public:
857 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
858 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000859
860 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000861 bool isDead() const { return IsDead; }
862};
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000864class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000865 std::vector<ContextLocation> CLocs;
866 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000867 PathDiagnostic &PD;
868 PathDiagnosticBuilder &PDB;
869 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000871 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenek14856d72009-04-06 23:06:54 +0000873 bool containsLocation(const PathDiagnosticLocation &Container,
874 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Ted Kremenek14856d72009-04-06 23:06:54 +0000876 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek9650cf32009-05-11 21:42:34 +0000878 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
879 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000880 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000881 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000882 while (1) {
883 // Adjust the location for some expressions that are best referenced
884 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000885 switch (S->getStmtClass()) {
886 default:
887 break;
888 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000889 case Stmt::GenericSelectionExprClass:
890 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000891 firstCharOnly = true;
892 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000893 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000894 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000895 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000896 firstCharOnly = true;
897 continue;
898 case Stmt::ChooseExprClass:
899 S = cast<ChooseExpr>(S)->getCond();
900 firstCharOnly = true;
901 continue;
902 case Stmt::BinaryOperatorClass:
903 S = cast<BinaryOperator>(S)->getLHS();
904 firstCharOnly = true;
905 continue;
906 }
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremenek9650cf32009-05-11 21:42:34 +0000908 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek9650cf32009-05-11 21:42:34 +0000911 if (S != Original)
Ted Kremenek59950d32012-02-24 07:12:52 +0000912 L = PathDiagnosticLocation(S, L.getManager(), PDB.LC);
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek9650cf32009-05-11 21:42:34 +0000915 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000916 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000917
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000918 return L;
919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenek14856d72009-04-06 23:06:54 +0000921 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000922 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000923 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000924 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000925 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000926 CLocs.pop_back();
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremenek14856d72009-04-06 23:06:54 +0000929public:
930 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
931 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Ted Kremeneka301a672009-04-22 18:16:20 +0000933 // If the PathDiagnostic already has pieces, add the enclosing statement
934 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000935 if (!PD.path.empty()) {
936 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Ted Kremenek14856d72009-04-06 23:06:54 +0000938 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000939 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000940 }
941 }
942
943 ~EdgeBuilder() {
944 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000945
Ted Kremeneka301a672009-04-22 18:16:20 +0000946 // Finally, add an initial edge from the start location of the first
947 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000948 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
Ted Kremenek59950d32012-02-24 07:12:52 +0000949 PDB.LC,
Anna Zaks0cd59482011-09-16 19:18:30 +0000950 PDB.getSourceManager());
951 if (L.isValid())
952 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000953 }
954
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000955 void flushLocations() {
956 while (!CLocs.empty())
957 popLocation();
958 PrevLoc = PathDiagnosticLocation();
959 }
960
Ted Kremenek14856d72009-04-06 23:06:54 +0000961 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000963 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Ted Kremenek14856d72009-04-06 23:06:54 +0000965 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000966 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000967};
Ted Kremenek14856d72009-04-06 23:06:54 +0000968} // end anonymous namespace
969
970
971PathDiagnosticLocation
972EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
973 if (const Stmt *S = L.asStmt()) {
974 if (IsControlFlowExpr(S))
975 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
977 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Ted Kremenek14856d72009-04-06 23:06:54 +0000980 return L;
981}
982
983bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
984 const PathDiagnosticLocation &Containee) {
985
986 if (Container == Containee)
987 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Ted Kremenek14856d72009-04-06 23:06:54 +0000989 if (Container.asDecl())
990 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek14856d72009-04-06 23:06:54 +0000992 if (const Stmt *S = Containee.asStmt())
993 if (const Stmt *ContainerS = Container.asStmt()) {
994 while (S) {
995 if (S == ContainerS)
996 return true;
997 S = PDB.getParent(S);
998 }
999 return false;
1000 }
1001
1002 // Less accurate: compare using source ranges.
1003 SourceRange ContainerR = Container.asRange();
1004 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001007 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1008 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1009 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1010 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chandler Carruth64211622011-07-25 21:09:52 +00001012 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1013 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1014 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1015 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremenek14856d72009-04-06 23:06:54 +00001017 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001018 assert(ContaineeBegLine <= ContaineeEndLine);
1019
Ted Kremenek14856d72009-04-06 23:06:54 +00001020 return (ContainerBegLine <= ContaineeBegLine &&
1021 ContainerEndLine >= ContaineeEndLine &&
1022 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001023 SM.getExpansionColumnNumber(ContainerRBeg) <=
1024 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001025 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001026 SM.getExpansionColumnNumber(ContainerREnd) >=
1027 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001028}
1029
Ted Kremenek14856d72009-04-06 23:06:54 +00001030void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1031 if (!PrevLoc.isValid()) {
1032 PrevLoc = NewLoc;
1033 return;
1034 }
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001036 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1037 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001039 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001040 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Ted Kremenek14856d72009-04-06 23:06:54 +00001042 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001043 if (NewLocClean.asLocation().getExpansionLoc() ==
1044 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001045 return;
1046
Ted Kremenek2042fc12012-02-24 06:00:00 +00001047 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001048 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001049}
1050
1051void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Ted Kremeneka301a672009-04-22 18:16:20 +00001053 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1054 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Ted Kremenek14856d72009-04-06 23:06:54 +00001056 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1057
1058 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001059 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek14856d72009-04-06 23:06:54 +00001061 // Is the top location context the same as the one for the new location?
1062 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001063 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001064 if (IsConsumedExpr(TopContextLoc) &&
1065 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001066 TopContextLoc.markDead();
1067
Ted Kremenek14856d72009-04-06 23:06:54 +00001068 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001069 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001070
1071 return;
1072 }
1073
1074 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001075 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001076 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001078 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001079 CLocs.push_back(ContextLocation(CLoc, true));
1080 return;
1081 }
1082 }
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Ted Kremenek14856d72009-04-06 23:06:54 +00001084 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001085 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001086 }
1087
1088 // Context does not contain the location. Flush it.
1089 popLocation();
1090 }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001092 // If we reach here, there is no enclosing context. Just add the edge.
1093 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001094}
1095
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001096bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1097 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1098 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001100 return false;
1101}
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Ted Kremeneke1baed32009-05-05 23:13:38 +00001103void EdgeBuilder::addExtendedContext(const Stmt *S) {
1104 if (!S)
1105 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001106
1107 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001108 while (Parent) {
1109 if (isa<CompoundStmt>(Parent))
1110 Parent = PDB.getParent(Parent);
1111 else
1112 break;
1113 }
1114
1115 if (Parent) {
1116 switch (Parent->getStmtClass()) {
1117 case Stmt::DoStmtClass:
1118 case Stmt::ObjCAtSynchronizedStmtClass:
1119 addContext(Parent);
1120 default:
1121 break;
1122 }
1123 }
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Ted Kremeneke1baed32009-05-05 23:13:38 +00001125 addContext(S);
1126}
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Ted Kremenek14856d72009-04-06 23:06:54 +00001128void EdgeBuilder::addContext(const Stmt *S) {
1129 if (!S)
1130 return;
1131
Ted Kremenek59950d32012-02-24 07:12:52 +00001132 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Ted Kremenek14856d72009-04-06 23:06:54 +00001134 while (!CLocs.empty()) {
1135 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1136
1137 // Is the top location context the same as the one for the new location?
1138 if (TopContextLoc == L)
1139 return;
1140
1141 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001142 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001143 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001144 }
1145
1146 // Context does not contain the location. Flush it.
1147 popLocation();
1148 }
1149
1150 CLocs.push_back(L);
1151}
1152
1153static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1154 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001155 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001156 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001157 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001158
Ted Kremenek9c378f72011-08-12 23:37:29 +00001159 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001160 while (NextNode) {
1161 N = NextNode;
1162 NextNode = GetPredecessorNode(N);
1163 ProgramPoint P = N->getLocation();
1164
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001165 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001166 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1167 const StackFrameContext *LCtx =
1168 CE->getLocationContext()->getCurrentStackFrame();
1169 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1170 PDB.getSourceManager(),
1171 LCtx);
1172 EB.addEdge(Loc, true);
1173 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001174 PathDiagnosticCallPiece *C =
1175 PathDiagnosticCallPiece::construct(N, *CE, SM);
1176 PD.getActivePath().push_front(C);
1177 PD.pushActivePath(&C->path);
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001178 break;
1179 }
Ted Kremenek59950d32012-02-24 07:12:52 +00001180
1181 // Note that is important that we update the LocationContext
1182 // after looking at CallExits. CallExit basically adds an
1183 // edge in the *caller*, so we don't want to update the LocationContext
1184 // too soon.
1185 PDB.LC = N->getLocationContext();
1186
Ted Kremenek2042fc12012-02-24 06:00:00 +00001187 // Pop the call hierarchy if we are done walking the contents
1188 // of a function call.
1189 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
1190 PD.popActivePath();
1191 // The current active path should never be empty. Either we
1192 // just added a bunch of stuff to the top-level path, or
1193 // we have a previous CallExit. If the front of the active
1194 // path is not a PathDiagnosticCallPiece, it means that the
1195 // path terminated within a function call. We must then take the
1196 // current contents of the active path and place it within
1197 // a new PathDiagnosticCallPiece.
1198 assert(!PD.getActivePath().empty());
1199 PathDiagnosticCallPiece *C =
1200 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1201 if (!C)
1202 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
1203 C->setCallee(*CE, SM);
Ted Kremenek59950d32012-02-24 07:12:52 +00001204 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001205 EB.addContext(CE->getCallExpr());
1206 break;
1207 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001208
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001209 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001210 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001211 const CFGBlock &Blk = *BE->getSrc();
1212 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001214 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001215 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenek59950d32012-02-24 07:12:52 +00001216 PathDiagnosticLocation L(Loop, SM, PDB.LC);
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001217 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001219 if (!Term) {
1220 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1221 CS = dyn_cast<CompoundStmt>(FS->getBody());
1222 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001223 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001224 }
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001226 PathDiagnosticEventPiece *p =
1227 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001228 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001230 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001231 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001233 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001234 PathDiagnosticLocation BL =
1235 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001236 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001237 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001238 }
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001240 if (Term)
1241 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001243 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001244 }
1245
Mike Stump1eb44332009-09-09 15:08:12 +00001246 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001247 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1248 const Stmt *stmt = S->getStmt();
1249 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001250 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001251 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001252 }
1253 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001254 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001255 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001256
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001257 break;
1258 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001259
1260
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001261 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001263 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001264 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Anna Zaks8e6431a2011-08-18 22:37:56 +00001266 // Add pieces from custom visitors.
1267 BugReport *R = PDB.getBugReport();
1268 for (BugReport::visitor_iterator I = R->visitor_begin(),
1269 E = R->visitor_end(); I!=E; ++I) {
1270 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001271 const PathDiagnosticLocation &Loc = p->getLocation();
1272 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001273 PD.getActivePath().push_front(p);
Ted Kremenek8966bc12009-05-06 21:39:49 +00001274 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001275 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001276 }
Mike Stump1eb44332009-09-09 15:08:12 +00001277 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001278 }
1279}
1280
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001281//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001282// Methods for BugType and subclasses.
1283//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001284BugType::~BugType() { }
1285
Ted Kremenekcf118d42009-02-04 23:49:09 +00001286void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001287
David Blaikie99ba9e32011-12-20 02:48:34 +00001288void BuiltinBug::anchor() {}
1289
Ted Kremenekcf118d42009-02-04 23:49:09 +00001290//===----------------------------------------------------------------------===//
1291// Methods for BugReport and subclasses.
1292//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001293
David Blaikie99ba9e32011-12-20 02:48:34 +00001294void BugReport::NodeResolver::anchor() {}
1295
Anna Zaks8e6431a2011-08-18 22:37:56 +00001296void BugReport::addVisitor(BugReporterVisitor* visitor) {
1297 if (!visitor)
1298 return;
1299
1300 llvm::FoldingSetNodeID ID;
1301 visitor->Profile(ID);
1302 void *InsertPos;
1303
1304 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1305 delete visitor;
1306 return;
1307 }
1308
1309 CallbacksSet.InsertNode(visitor, InsertPos);
1310 Callbacks = F.add(visitor, Callbacks);
1311}
1312
1313BugReport::~BugReport() {
1314 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001315 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001316 }
1317}
Anna Zakse172e8b2011-08-17 23:00:25 +00001318
1319void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1320 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001321 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001322 if (UniqueingLocation.isValid()) {
1323 UniqueingLocation.Profile(hash);
1324 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001325 Location.Profile(hash);
1326 } else {
1327 assert(ErrorNode);
1328 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1329 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001330
1331 for (SmallVectorImpl<SourceRange>::const_iterator I =
1332 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1333 const SourceRange range = *I;
1334 if (!range.isValid())
1335 continue;
1336 hash.AddInteger(range.getBegin().getRawEncoding());
1337 hash.AddInteger(range.getEnd().getRawEncoding());
1338 }
1339}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001340
Ted Kremenek9c378f72011-08-12 23:37:29 +00001341const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001342 if (!ErrorNode)
1343 return 0;
1344
Tom Care212f6d32010-09-16 03:50:38 +00001345 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001346 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Ted Kremenek9c378f72011-08-12 23:37:29 +00001348 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001349 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001350 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001351 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001352 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001353 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001354 S = GetStmt(ProgP);
1355
1356 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001357}
1358
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001359std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001360BugReport::getRanges() {
1361 // If no custom ranges, add the range of the statement corresponding to
1362 // the error node.
1363 if (Ranges.empty()) {
1364 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1365 addRange(E->getSourceRange());
1366 else
1367 return std::make_pair(ranges_iterator(), ranges_iterator());
1368 }
1369
Anna Zaks14924262011-08-24 20:31:06 +00001370 // User-specified absence of range info.
1371 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1372 return std::make_pair(ranges_iterator(), ranges_iterator());
1373
Anna Zakse172e8b2011-08-17 23:00:25 +00001374 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001375}
1376
Anna Zaks590dd8e2011-09-20 21:38:35 +00001377PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001378 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001379 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001380 "Either Location or ErrorNode should be specified but not both.");
1381
Ted Kremenek9c378f72011-08-12 23:37:29 +00001382 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001383 const LocationContext *LC = ErrorNode->getLocationContext();
1384
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001385 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001386 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001387 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001388 // For binary operators, return the location of the operator.
1389 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001390 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001391
Anna Zaks590dd8e2011-09-20 21:38:35 +00001392 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001393 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001394 } else {
1395 assert(Location.isValid());
1396 return Location;
1397 }
1398
Anna Zaks590dd8e2011-09-20 21:38:35 +00001399 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001400}
1401
Ted Kremenekcf118d42009-02-04 23:49:09 +00001402//===----------------------------------------------------------------------===//
1403// Methods for BugReporter and subclasses.
1404//===----------------------------------------------------------------------===//
1405
1406BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001407 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001408}
1409
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001410GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001411BugReporterData::~BugReporterData() {}
1412
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001413ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001414
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001415ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001416GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1417
Anna Zaks3b030a22011-08-19 01:57:09 +00001418BugReporter::~BugReporter() {
1419 FlushReports();
1420
1421 // Free the bug reports we are tracking.
1422 typedef std::vector<BugReportEquivClass *> ContTy;
1423 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1424 I != E; ++I) {
1425 delete *I;
1426 }
1427}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001428
1429void BugReporter::FlushReports() {
1430 if (BugTypes.isEmpty())
1431 return;
1432
1433 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001434 // warnings and new BugTypes.
1435 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1436 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001437 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001438 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001439 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001440 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001441 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001442 const_cast<BugType*>(*I)->FlushReports(*this);
1443
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001444 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1445 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1446 BugReportEquivClass& EQ = *EI;
1447 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001448 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001449
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001450 // BugReporter owns and deletes only BugTypes created implicitly through
1451 // EmitBasicReport.
1452 // FIXME: There are leaks from checkers that assume that the BugTypes they
1453 // create will be destroyed by the BugReporter.
1454 for (llvm::StringMap<BugType*>::iterator
1455 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1456 delete I->second;
1457
Ted Kremenekcf118d42009-02-04 23:49:09 +00001458 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001459 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001460}
1461
1462//===----------------------------------------------------------------------===//
1463// PathDiagnostics generation.
1464//===----------------------------------------------------------------------===//
1465
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001466static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001467 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001468MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001469 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Ted Kremenekcf118d42009-02-04 23:49:09 +00001471 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001472 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001473 // error node unless there are two or more error nodes with the same minimum
1474 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001475 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001476 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001477
1478 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001479 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1480 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenekcf118d42009-02-04 23:49:09 +00001482 // Create owning pointers for GTrim and NMap just to ensure that they are
1483 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001484 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1485 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Ted Kremenekcf118d42009-02-04 23:49:09 +00001487 // Find the (first) error node in the trimmed graph. We just need to consult
1488 // the node map (NMap) which maps from nodes in the original graph to nodes
1489 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001490
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001491 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001492 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001493 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001494
Ted Kremenek40406fe2010-12-03 06:52:30 +00001495 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1496 const ExplodedNode *originalNode = nodes[nodeIndex];
1497 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001498 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001499 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001500 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenek938332c2009-05-16 01:11:58 +00001503 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001504
1505 // Create a new (third!) graph with a single path. This is the graph
1506 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001507 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Ted Kremenek10aa5542009-03-12 23:41:59 +00001509 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001510 // to the root node, and then construct a new graph that contains only
1511 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001512 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001514 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001515 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001517 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001518 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001519 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001521 if (Visited.find(Node) != Visited.end())
1522 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001524 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001526 if (Node->pred_empty()) {
1527 Root = Node;
1528 break;
1529 }
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001531 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001532 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001533 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001534 }
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Ted Kremenek938332c2009-05-16 01:11:58 +00001536 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Ted Kremenek10aa5542009-03-12 23:41:59 +00001538 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001539 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001540 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001541 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001542 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001544 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001545 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001546 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001547 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001549 // Create the equivalent node in the new graph with the same state
1550 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001551 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001553 // Store the mapping to the original node.
1554 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1555 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001556 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001558 // Link up the new node with the previous node.
1559 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001560 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001562 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001564 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001565 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001566 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001567 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001568 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001569 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001570 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001573 // Find the next successor node. We choose the node that is marked
1574 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001575 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1576 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001577 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001579 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001581 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001583 if (I == Visited.end())
1584 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001586 if (!N || I->second < MinVal) {
1587 N = *SI;
1588 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001589 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001590 }
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Ted Kremenek938332c2009-05-16 01:11:58 +00001592 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001593 }
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Ted Kremenek938332c2009-05-16 01:11:58 +00001595 assert(First);
1596
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001597 return std::make_pair(std::make_pair(GNew, BM),
1598 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001599}
1600
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001601/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1602/// and collapses PathDiagosticPieces that are expanded by macros.
1603static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001604 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1605 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001607 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001608 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001610 MacroStackTy MacroStack;
1611 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Ted Kremenek2042fc12012-02-24 06:00:00 +00001613 for (PathPieces::const_iterator I = PD.path.begin(), E = PD.path.end();
1614 I!=E; ++I) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001615 // Get the location of the PathDiagnosticPiece.
Ted Kremenek802e0242012-02-08 04:32:34 +00001616 const FullSourceLoc Loc = (*I)->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001618 // Determine the instantiation location, which is the location we group
1619 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001620 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001621 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001622 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001624 if (Loc.isFileID()) {
1625 MacroStack.clear();
Ted Kremenek802e0242012-02-08 04:32:34 +00001626 Pieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001627 continue;
1628 }
1629
1630 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001632 // Is the PathDiagnosticPiece within the same macro group?
1633 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek802e0242012-02-08 04:32:34 +00001634 MacroStack.back().first->subPieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001635 continue;
1636 }
1637
1638 // We aren't in the same group. Are we descending into a new macro
1639 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001640 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001641
1642 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001643 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001644 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001646 // Walk the entire macro stack.
1647 while (!MacroStack.empty()) {
1648 if (InstantiationLoc == MacroStack.back().second) {
1649 MacroGroup = MacroStack.back().first;
1650 break;
1651 }
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001653 if (ParentInstantiationLoc == MacroStack.back().second) {
1654 MacroGroup = MacroStack.back().first;
1655 break;
1656 }
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001658 MacroStack.pop_back();
1659 }
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001661 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1662 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001663 PathDiagnosticMacroPiece *NewGroup =
1664 new PathDiagnosticMacroPiece(
Ted Kremenek802e0242012-02-08 04:32:34 +00001665 PathDiagnosticLocation::createSingleLocation((*I)->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001666
1667 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001668 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001669 else {
1670 assert(InstantiationLoc.isFileID());
1671 Pieces.push_back(NewGroup);
1672 }
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001674 MacroGroup = NewGroup;
1675 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1676 }
1677
1678 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek802e0242012-02-08 04:32:34 +00001679 MacroGroup->subPieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001680 }
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001682 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek2042fc12012-02-24 06:00:00 +00001683 PD.getMutablePieces().clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001685 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
Ted Kremenekaf84f8f2012-02-08 22:48:17 +00001686 if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
1687 if (!MP->containsEvent())
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001688 continue;
Ted Kremenek2042fc12012-02-24 06:00:00 +00001689 PD.getMutablePieces().push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001690 }
1691}
1692
Ted Kremenek7dc86642009-03-31 20:22:36 +00001693void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001694 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Ted Kremenek40406fe2010-12-03 06:52:30 +00001696 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001697 SmallVector<const ExplodedNode *, 10> errorNodes;
1698 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001699 E = bugReports.end(); I != E; ++I) {
1700 errorNodes.push_back((*I)->getErrorNode());
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001703 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001704 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001705 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001706 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001707 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Ted Kremenekcf118d42009-02-04 23:49:09 +00001709 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001710 assert(GPair.second.second < bugReports.size());
1711 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001712 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001714 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1715 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001716 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001717
1718 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001719 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1720 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Anna Zaks8e6431a2011-08-18 22:37:56 +00001722 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001723 R->addVisitor(new NilReceiverBRVisitor());
1724 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Anna Zaks23f395e2011-08-20 01:27:22 +00001726 // Generate the very last diagnostic piece - the piece is visible before
1727 // the trace is expanded.
1728 PathDiagnosticPiece *LastPiece = 0;
1729 for (BugReport::visitor_iterator I = R->visitor_begin(),
1730 E = R->visitor_end(); I!=E; ++I) {
1731 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1732 assert (!LastPiece &&
1733 "There can only be one final piece in a diagnostic.");
1734 LastPiece = Piece;
1735 }
1736 }
1737 if (!LastPiece)
1738 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1739 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001740 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001741 else
1742 return;
1743
Ted Kremenek7dc86642009-03-31 20:22:36 +00001744 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001745 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001746 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001747 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001748 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001749 GenerateMinimalPathDiagnostic(PD, PDB, N);
1750 break;
1751 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001752}
1753
Ted Kremenekcf118d42009-02-04 23:49:09 +00001754void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001755 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001756}
1757
Mike Stump1eb44332009-09-09 15:08:12 +00001758void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001759 // Compute the bug report's hash to determine its equivalence class.
1760 llvm::FoldingSetNodeID ID;
1761 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001762
1763 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001764 BugType& BT = R->getBugType();
1765 Register(&BT);
1766 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001767 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Ted Kremenekcf118d42009-02-04 23:49:09 +00001769 if (!EQ) {
1770 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001771 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001772 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001773 }
1774 else
1775 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001776}
1777
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001778
1779//===----------------------------------------------------------------------===//
1780// Emitting reports in equivalence classes.
1781//===----------------------------------------------------------------------===//
1782
1783namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001784struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001785 const ExplodedNode *N;
1786 ExplodedNode::const_succ_iterator I, E;
1787
1788 FRIEC_WLItem(const ExplodedNode *n)
1789 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1790};
1791}
1792
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001793static BugReport *
1794FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001795 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001796
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001797 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1798 assert(I != E);
1799 BugReport *R = *I;
1800 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001801
Ted Kremenek40406fe2010-12-03 06:52:30 +00001802 // If we don't need to suppress any of the nodes because they are
1803 // post-dominated by a sink, simply add all the nodes in the equivalence class
1804 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001805 if (!BT.isSuppressOnSink()) {
1806 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001807 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001808 if (N) {
1809 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001810 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001811 }
1812 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001813 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001814 }
1815
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001816 // For bug reports that should be suppressed when all paths are post-dominated
1817 // by a sink node, iterate through the reports in the equivalence class
1818 // until we find one that isn't post-dominated (if one exists). We use a
1819 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1820 // this as a recursive function, but we don't want to risk blowing out the
1821 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001822 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001823
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001824 for (; I != E; ++I) {
1825 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001826 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001827
Ted Kremenek40406fe2010-12-03 06:52:30 +00001828 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001829 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001830 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001831 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001832 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001833 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001834 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001835 if (errorNode->succ_empty()) {
1836 bugReports.push_back(R);
1837 if (!exampleReport)
1838 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001839 continue;
1840 }
1841
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001842 // At this point we know that 'N' is not a sink and it has at least one
1843 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1844 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001845 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001846 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1847
1848 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001849 WL.push_back(errorNode);
1850 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001851
1852 while (!WL.empty()) {
1853 WLItem &WI = WL.back();
1854 assert(!WI.N->succ_empty());
1855
1856 for (; WI.I != WI.E; ++WI.I) {
1857 const ExplodedNode *Succ = *WI.I;
1858 // End-of-path node?
1859 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001860 // If we found an end-of-path node that is not a sink.
1861 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001862 bugReports.push_back(R);
1863 if (!exampleReport)
1864 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001865 WL.clear();
1866 break;
1867 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001868 // Found a sink? Continue on to the next successor.
1869 continue;
1870 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001871 // Mark the successor as visited. If it hasn't been explored,
1872 // enqueue it to the DFS worklist.
1873 unsigned &mark = Visited[Succ];
1874 if (!mark) {
1875 mark = 1;
1876 WL.push_back(Succ);
1877 break;
1878 }
1879 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001880
1881 // The worklist may have been cleared at this point. First
1882 // check if it is empty before checking the last item.
1883 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001884 WL.pop_back();
1885 }
1886 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001887
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001888 // ExampleReport will be NULL if all the nodes in the equivalence class
1889 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001890 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001891}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001892
1893//===----------------------------------------------------------------------===//
1894// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1895// uses global state, which eventually should go elsewhere.
1896//===----------------------------------------------------------------------===//
1897namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001898class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001899 llvm::FoldingSetNodeID ID;
1900public:
1901 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001902 R->Profile(ID);
1903 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001904 }
1905
1906 void Profile(llvm::FoldingSetNodeID &id) {
1907 id = ID;
1908 }
1909
1910 llvm::FoldingSetNodeID &getID() { return ID; }
1911};
1912}
1913
1914static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1915 // FIXME: Eventually this diagnostic cache should reside in something
1916 // like AnalysisManager instead of being a static variable. This is
1917 // really unsafe in the long term.
1918 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1919 static DiagnosticCache DC;
1920
1921 void *InsertPos;
1922 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1923
1924 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1925 delete Item;
1926 return true;
1927 }
1928
1929 DC.InsertNode(Item, InsertPos);
1930 return false;
1931}
1932
Ted Kremenekcf118d42009-02-04 23:49:09 +00001933void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001934 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001935 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1936 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001937 return;
1938
David Blaikieef3643f2011-09-26 00:51:36 +00001939 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Ted Kremenekcf118d42009-02-04 23:49:09 +00001941 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001942 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001943 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001945 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001946 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001947 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001948 ? exampleReport->getDescription()
1949 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001950 BT.getCategory()));
1951
Ted Kremenek40406fe2010-12-03 06:52:30 +00001952 if (!bugReports.empty())
1953 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Ted Kremenek40406fe2010-12-03 06:52:30 +00001955 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001956 return;
1957
Ted Kremenek072192b2008-04-30 23:47:44 +00001958 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001959 const BugReport::ExtraTextList &Meta =
1960 exampleReport->getExtraText();
1961 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1962 e = Meta.end(); i != e; ++i) {
1963 D->addMeta(*i);
1964 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001965
Ted Kremenek3148eb42009-01-24 00:55:43 +00001966 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001967 BugReport::ranges_iterator Beg, End;
1968 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001969 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001970
1971 // Search the description for '%', as that will be interpretted as a
1972 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001973 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001974 unsigned ErrorDiag;
1975 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001976 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001977 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001978 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001979 if (*I == '%')
1980 Out << "%%";
1981 else
1982 Out << *I;
1983
1984 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001985 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001986 }
Ted Kremenek57202072008-07-14 17:40:50 +00001987
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001988 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001989 DiagnosticBuilder diagBuilder = Diag.Report(
1990 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001991 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001992 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001993 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001994
David Blaikieef3643f2011-09-26 00:51:36 +00001995 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001996 if (!PD)
1997 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Ted Kremenek802e0242012-02-08 04:32:34 +00001999 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002000 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
2001 exampleReport->getLocation(getSourceManager()),
2002 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00002003
Ted Kremenek3148eb42009-01-24 00:55:43 +00002004 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00002005 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00002006 }
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Ted Kremenek3148eb42009-01-24 00:55:43 +00002008 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002009}
Ted Kremenek57202072008-07-14 17:40:50 +00002010
Chris Lattner5f9e2722011-07-23 10:55:15 +00002011void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002012 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002013 SourceRange* RBeg, unsigned NumRanges) {
2014 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
2015}
Ted Kremenekcf118d42009-02-04 23:49:09 +00002016
Chris Lattner5f9e2722011-07-23 10:55:15 +00002017void BugReporter::EmitBasicReport(StringRef name,
2018 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002019 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002020 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002022 // 'BT' is owned by BugReporter.
2023 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002024 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002025 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2026 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002027}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002028
Chris Lattner5f9e2722011-07-23 10:55:15 +00002029BugType *BugReporter::getBugTypeForName(StringRef name,
2030 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002031 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002032 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2033 llvm::StringMapEntry<BugType *> &
2034 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2035 BugType *BT = entry.getValue();
2036 if (!BT) {
2037 BT = new BugType(name, category);
2038 entry.setValue(BT);
2039 }
2040 return BT;
2041}