blob: 0c91164045f5f9dfb18bbe98d64c3f7ca73cfc18 [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 Kremenek8966bc12009-05-06 21:39:49 +0000139 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000140 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000141 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000142 : BugReporterContext(br),
Anna Zaks8e6431a2011-08-18 22:37:56 +0000143 R(r), PDC(pdc), NMC(Backmap) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Ted Kremenek9c378f72011-08-12 23:37:29 +0000145 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Ted Kremenek9c378f72011-08-12 23:37:29 +0000147 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
148 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Anna Zaks8e6431a2011-08-18 22:37:56 +0000150 BugReport *getBugReport() { return R; }
151
Tom Care212f6d32010-09-16 03:50:38 +0000152 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000153
Anna Zaks220ac8c2011-09-15 01:08:34 +0000154 const LocationContext* getLocationContext() {
155 return R->getErrorNode()->getLocationContext();
156 }
157
Tom Care212f6d32010-09-16 03:50:38 +0000158 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000160 const Stmt *getParent(const Stmt *S) {
161 return getParentMap().getParent(S);
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek8966bc12009-05-06 21:39:49 +0000164 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000165
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000166 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
David Blaikieef3643f2011-09-26 00:51:36 +0000168 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
169 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000170 }
171
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000172 bool supportsLogicalOpControlFlow() const {
173 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000174 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000175};
176} // end anonymous namespace
177
Ted Kremenek00605e02009-03-27 20:55:39 +0000178PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000179PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000180 if (const Stmt *S = GetNextStmt(N))
Anna Zaks220ac8c2011-09-15 01:08:34 +0000181 return PathDiagnosticLocation(S, getSourceManager(), getLocationContext());
Ted Kremenek00605e02009-03-27 20:55:39 +0000182
Anna Zaks0cd59482011-09-16 19:18:30 +0000183 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
184 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000185}
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenek00605e02009-03-27 20:55:39 +0000187PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000188PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
189 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000190
Ted Kremenek143ca222008-05-06 18:11:09 +0000191 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000192 if (os.str().empty())
193 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek00605e02009-03-27 20:55:39 +0000195 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek00605e02009-03-27 20:55:39 +0000197 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000198 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000199 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000200 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000201 else {
202 os << "Execution jumps to the end of the ";
203 const Decl *D = N->getLocationContext()->getDecl();
204 if (isa<ObjCMethodDecl>(D))
205 os << "method";
206 else if (isa<FunctionDecl>(D))
207 os << "function";
208 else {
209 assert(isa<BlockDecl>(D));
210 os << "anonymous block";
211 }
212 os << '.';
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000215 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000216}
217
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000218static bool IsNested(const Stmt *S, ParentMap &PM) {
219 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
220 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000222 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000224 if (Parent)
225 switch (Parent->getStmtClass()) {
226 case Stmt::ForStmtClass:
227 case Stmt::DoStmtClass:
228 case Stmt::WhileStmtClass:
229 return true;
230 default:
231 break;
232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
234 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000235}
236
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000237PathDiagnosticLocation
238PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000239 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000240 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000241 SourceManager &SMgr = getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000242 const LocationContext *LC = getLocationContext();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000243
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000244 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000245 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000247 if (!Parent)
248 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000250 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000251 case Stmt::BinaryOperatorClass: {
252 const BinaryOperator *B = cast<BinaryOperator>(Parent);
253 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000254 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000255 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000256 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000257 case Stmt::CompoundStmtClass:
258 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000259 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000260 case Stmt::ChooseExprClass:
261 // Similar to '?' if we are referring to condition, just have the edge
262 // point to the entire choose expression.
263 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000264 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000265 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000266 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000267 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000268 case Stmt::ConditionalOperatorClass:
269 // For '?', if we are referring to condition, just have the edge point
270 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000271 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000272 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000273 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000274 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000275 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000276 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000277 case Stmt::ForStmtClass:
278 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000279 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000280 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000281 case Stmt::IfStmtClass:
282 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000283 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000284 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000285 case Stmt::ObjCForCollectionStmtClass:
286 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000287 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000288 break;
289 case Stmt::WhileStmtClass:
290 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000291 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000292 break;
293 default:
294 break;
295 }
296
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000297 S = Parent;
298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000300 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000301
302 // Special case: DeclStmts can appear in for statement declarations, in which
303 // case the ForStmt is the context.
304 if (isa<DeclStmt>(S)) {
305 if (const Stmt *Parent = P.getParent(S)) {
306 switch (Parent->getStmtClass()) {
307 case Stmt::ForStmtClass:
308 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000309 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000310 default:
311 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
313 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000314 }
315 else if (isa<BinaryOperator>(S)) {
316 // Special case: the binary operator represents the initialization
317 // code in a for statement (this can happen when the variable being
318 // initialized is an old variable.
319 if (const ForStmt *FS =
320 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
321 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000322 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000323 }
324 }
325
Anna Zaks220ac8c2011-09-15 01:08:34 +0000326 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000327}
328
Ted Kremenekcf118d42009-02-04 23:49:09 +0000329//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000330// ScanNotableSymbols: closure-like callback for scanning Store bindings.
331//===----------------------------------------------------------------------===//
332
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000333static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
334 ProgramStateManager& VMgr,
335 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenek31061982009-03-31 23:00:32 +0000337 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek31061982009-03-31 23:00:32 +0000339 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek31061982009-03-31 23:00:32 +0000341 if (!isa<PostStmt>(P))
342 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek9c378f72011-08-12 23:37:29 +0000344 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek31061982009-03-31 23:00:32 +0000346 if (!DR)
347 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek5eca4822012-01-06 22:09:28 +0000349 SVal Y = N->getState()->getSVal(DR, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek31061982009-03-31 23:00:32 +0000351 if (X != Y)
352 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek9c378f72011-08-12 23:37:29 +0000354 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek31061982009-03-31 23:00:32 +0000356 if (!VD)
357 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek31061982009-03-31 23:00:32 +0000359 return VD;
360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek31061982009-03-31 23:00:32 +0000362 return 0;
363}
364
365namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000366class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000367: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek31061982009-03-31 23:00:32 +0000369 SymbolRef Sym;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000370 ProgramStateRef PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000371 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000372 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000373 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000374 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000375 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek31061982009-03-31 23:00:32 +0000377public:
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000379 NotableSymbolHandler(SymbolRef sym,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000380 ProgramStateRef prevst,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000381 const Stmt *s,
382 ProgramStateManager& vmgr,
383 const ExplodedNode *pred,
384 PathDiagnostic& pd,
385 BugReporter& br)
386 : Sym(sym),
387 PrevSt(prevst),
388 S(s),
389 VMgr(vmgr),
390 Pred(pred),
391 PD(pd),
392 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek31061982009-03-31 23:00:32 +0000394 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
395 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek31061982009-03-31 23:00:32 +0000397 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek31061982009-03-31 23:00:32 +0000399 if (ScanSym != Sym)
400 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
402 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000403 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek31061982009-03-31 23:00:32 +0000405 if (X == V) // Same binding?
406 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek31061982009-03-31 23:00:32 +0000408 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000409 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000410 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek31061982009-03-31 23:00:32 +0000412 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremenek31061982009-03-31 23:00:32 +0000414 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
415 if (!B->isAssignmentOp())
416 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek31061982009-03-31 23:00:32 +0000418 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000419 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek31061982009-03-31 23:00:32 +0000421 if (!DR)
422 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek31061982009-03-31 23:00:32 +0000424 VD = dyn_cast<VarDecl>(DR->getDecl());
425 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000426 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000427 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
428 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000429 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000430 VD = dyn_cast<VarDecl>(*DS->decl_begin());
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek31061982009-03-31 23:00:32 +0000433 if (!VD)
434 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek31061982009-03-31 23:00:32 +0000436 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000437 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek31061982009-03-31 23:00:32 +0000439 if (!MostRecent)
440 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek31061982009-03-31 23:00:32 +0000442 // Create the diagnostic.
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000443 if (Loc::isLocType(VD->getType())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000444 SmallString<64> buf;
Jordy Rose7df12342011-08-21 05:25:15 +0000445 llvm::raw_svector_ostream os(buf);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000446 os << '\'' << *VD << "' now aliases '" << *MostRecent << '\'';
Anna Zaks590dd8e2011-09-20 21:38:35 +0000447 PathDiagnosticLocation L =
448 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(),
449 Pred->getLocationContext());
Ted Kremenek2042fc12012-02-24 06:00:00 +0000450 PD.getActivePath().push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenek31061982009-03-31 23:00:32 +0000453 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000454 }
Ted Kremenek31061982009-03-31 23:00:32 +0000455};
456}
457
Ted Kremenek9c378f72011-08-12 23:37:29 +0000458static void HandleNotableSymbol(const ExplodedNode *N,
459 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000460 SymbolRef Sym, BugReporter& BR,
461 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek9c378f72011-08-12 23:37:29 +0000463 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000464 ProgramStateRef PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Ted Kremenek31061982009-03-31 23:00:32 +0000466 if (!PrevSt)
467 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek31061982009-03-31 23:00:32 +0000469 // Look at the region bindings of the current state that map to the
470 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000471 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000472 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
473 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
474}
475
476namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000477class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000478: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek31061982009-03-31 23:00:32 +0000480 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000481 const ExplodedNode *N;
482 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000483 GRBugReporter& BR;
484 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek31061982009-03-31 23:00:32 +0000486public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000487 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000488 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000489 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek31061982009-03-31 23:00:32 +0000491 bool HandleBinding(StoreManager& SMgr, Store store,
492 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 if (!ScanSym)
497 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Ted Kremenek31061982009-03-31 23:00:32 +0000499 if (!BR.isNotable(ScanSym))
500 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek31061982009-03-31 23:00:32 +0000502 if (AlreadyProcessed.count(ScanSym))
503 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek31061982009-03-31 23:00:32 +0000505 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek31061982009-03-31 23:00:32 +0000507 HandleNotableSymbol(N, S, ScanSym, BR, PD);
508 return true;
509 }
510};
511} // end anonymous namespace
512
513//===----------------------------------------------------------------------===//
514// "Minimal" path diagnostic generation algorithm.
515//===----------------------------------------------------------------------===//
516
Ted Kremenek14856d72009-04-06 23:06:54 +0000517static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
518
Ted Kremenek31061982009-03-31 23:00:32 +0000519static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
520 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000521 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000522
Ted Kremenek31061982009-03-31 23:00:32 +0000523 SourceManager& SMgr = PDB.getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000524 const LocationContext *LC = PDB.getLocationContext();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000525 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000526 ? NULL : *(N->pred_begin());
527 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000528 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000529 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Ted Kremenek31061982009-03-31 23:00:32 +0000531 ProgramPoint P = N->getLocation();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000532
533 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
534 PathDiagnosticCallPiece *C =
535 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
536 PD.getActivePath().push_front(C);
537 PD.pushActivePath(&C->path);
538 continue;
539 }
540
541 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
542 PD.popActivePath();
543 // The current active path should never be empty. Either we
544 // just added a bunch of stuff to the top-level path, or
545 // we have a previous CallExit. If the front of the active
546 // path is not a PathDiagnosticCallPiece, it means that the
547 // path terminated within a function call. We must then take the
548 // current contents of the active path and place it within
549 // a new PathDiagnosticCallPiece.
550 assert(!PD.getActivePath().empty());
551 PathDiagnosticCallPiece *C =
552 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
553 if (!C)
554 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
555 C->setCallee(*CE, SMgr);
556 continue;
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek9c378f72011-08-12 23:37:29 +0000559 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
560 const CFGBlock *Src = BE->getSrc();
561 const CFGBlock *Dst = BE->getDst();
562 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Ted Kremenek31061982009-03-31 23:00:32 +0000564 if (!T)
565 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Anna Zaks590dd8e2011-09-20 21:38:35 +0000567 PathDiagnosticLocation Start =
568 PathDiagnosticLocation::createBegin(T, SMgr,
569 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Ted Kremenek31061982009-03-31 23:00:32 +0000571 switch (T->getStmtClass()) {
572 default:
573 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Ted Kremenek31061982009-03-31 23:00:32 +0000575 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000576 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000577 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Ted Kremenek31061982009-03-31 23:00:32 +0000579 if (!S)
580 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Ted Kremenek31061982009-03-31 23:00:32 +0000582 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000583 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000584 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Ted Kremenek31061982009-03-31 23:00:32 +0000586 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000587 << End.asLocation().getExpansionLineNumber();
Ted Kremenek2042fc12012-02-24 06:00:00 +0000588 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek802e0242012-02-08 04:32:34 +0000589 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000590 break;
591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
593 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000594 // Figure out what case arm we took.
595 std::string sbuf;
596 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremenek9c378f72011-08-12 23:37:29 +0000598 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000599 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Ted Kremenek31061982009-03-31 23:00:32 +0000601 switch (S->getStmtClass()) {
602 default:
603 os << "No cases match in the switch statement. "
604 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000605 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000606 break;
607 case Stmt::DefaultStmtClass:
608 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000609 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000610 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Ted Kremenek31061982009-03-31 23:00:32 +0000612 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000613 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000614 const CaseStmt *Case = cast<CaseStmt>(S);
615 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000616
617 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000618 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Ted Kremenek9c378f72011-08-12 23:37:29 +0000620 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000621 // FIXME: Maybe this should be an assertion. Are there cases
622 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000623 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000624 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek31061982009-03-31 23:00:32 +0000626 if (D) {
627 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000628 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000629 }
630 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000631
632 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000633 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000634
Ted Kremenek31061982009-03-31 23:00:32 +0000635 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000636 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000637 break;
638 }
639 }
Ted Kremenek2042fc12012-02-24 06:00:00 +0000640 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000641 os.str()));
642 }
643 else {
644 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000645 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000646 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000647 os.str()));
648 }
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek31061982009-03-31 23:00:32 +0000650 break;
651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 case Stmt::BreakStmtClass:
654 case Stmt::ContinueStmtClass: {
655 std::string sbuf;
656 llvm::raw_string_ostream os(sbuf);
657 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000658 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000659 os.str()));
660 break;
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek31061982009-03-31 23:00:32 +0000663 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000664 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000665 case Stmt::ConditionalOperatorClass: {
666 std::string sbuf;
667 llvm::raw_string_ostream os(sbuf);
668 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek31061982009-03-31 23:00:32 +0000670 if (*(Src->succ_begin()+1) == Dst)
671 os << "false";
672 else
673 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Ted Kremenek31061982009-03-31 23:00:32 +0000675 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Ted Kremenek31061982009-03-31 23:00:32 +0000677 if (const Stmt *S = End.asStmt())
678 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremenek2042fc12012-02-24 06:00:00 +0000680 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000681 os.str()));
682 break;
683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremenek31061982009-03-31 23:00:32 +0000685 // Determine control-flow for short-circuited '&&' and '||'.
686 case Stmt::BinaryOperatorClass: {
687 if (!PDB.supportsLogicalOpControlFlow())
688 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000690 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000691 std::string sbuf;
692 llvm::raw_string_ostream os(sbuf);
693 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000694
John McCall2de56d12010-08-25 11:45:40 +0000695 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000696 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek31061982009-03-31 23:00:32 +0000698 if (*(Src->succ_begin()+1) == Dst) {
699 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000700 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000701 PathDiagnosticLocation Start =
702 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000703 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000704 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000705 }
Ted Kremenek31061982009-03-31 23:00:32 +0000706 else {
707 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000708 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000709 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000710 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000711 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000712 }
Ted Kremenek31061982009-03-31 23:00:32 +0000713 }
714 else {
John McCall2de56d12010-08-25 11:45:40 +0000715 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000716 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek31061982009-03-31 23:00:32 +0000718 if (*(Src->succ_begin()+1) == Dst) {
719 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000720 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000721 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000722 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000723 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000724 }
725 else {
726 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000727 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000728 PathDiagnosticLocation Start =
729 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek2042fc12012-02-24 06:00:00 +0000730 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000731 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000732 }
733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek31061982009-03-31 23:00:32 +0000735 break;
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
738 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000739 if (*(Src->succ_begin()) == Dst) {
740 std::string sbuf;
741 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek31061982009-03-31 23:00:32 +0000743 os << "Loop condition is true. ";
744 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek31061982009-03-31 23:00:32 +0000746 if (const Stmt *S = End.asStmt())
747 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek2042fc12012-02-24 06:00:00 +0000749 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000750 os.str()));
751 }
752 else {
753 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek31061982009-03-31 23:00:32 +0000755 if (const Stmt *S = End.asStmt())
756 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek2042fc12012-02-24 06:00:00 +0000758 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000759 "Loop condition is false. Exiting loop"));
760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek31061982009-03-31 23:00:32 +0000762 break;
763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek31061982009-03-31 23:00:32 +0000765 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000766 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000767 if (*(Src->succ_begin()+1) == Dst) {
768 std::string sbuf;
769 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Ted Kremenek31061982009-03-31 23:00:32 +0000771 os << "Loop condition is false. ";
772 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
773 if (const Stmt *S = End.asStmt())
774 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek2042fc12012-02-24 06:00:00 +0000776 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek31061982009-03-31 23:00:32 +0000777 os.str()));
778 }
779 else {
780 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
781 if (const Stmt *S = End.asStmt())
782 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Ted Kremenek2042fc12012-02-24 06:00:00 +0000784 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000785 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenek31061982009-03-31 23:00:32 +0000788 break;
789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek31061982009-03-31 23:00:32 +0000791 case Stmt::IfStmtClass: {
792 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek31061982009-03-31 23:00:32 +0000794 if (const Stmt *S = End.asStmt())
795 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek31061982009-03-31 23:00:32 +0000797 if (*(Src->succ_begin()+1) == Dst)
Ted Kremenek2042fc12012-02-24 06:00:00 +0000798 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000799 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000800 else
Ted Kremenek2042fc12012-02-24 06:00:00 +0000801 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000802 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek31061982009-03-31 23:00:32 +0000804 break;
805 }
806 }
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000809 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000810 // Add diagnostic pieces from custom visitors.
811 BugReport *R = PDB.getBugReport();
812 for (BugReport::visitor_iterator I = R->visitor_begin(),
813 E = R->visitor_end(); I!=E; ++I) {
814 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenek2042fc12012-02-24 06:00:00 +0000815 PD.getActivePath().push_front(p);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000816 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Ted Kremenek9c378f72011-08-12 23:37:29 +0000819 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000820 // Scan the region bindings, and see if a "notable" symbol has a new
821 // lval binding.
822 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
823 PDB.getStateManager().iterBindings(N->getState(), SNS);
824 }
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek14856d72009-04-06 23:06:54 +0000827 // After constructing the full PathDiagnostic, do a pass over it to compact
828 // PathDiagnosticPieces that occur within a macro.
829 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000830}
831
832//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000833// "Extensive" PathDiagnostic generation.
834//===----------------------------------------------------------------------===//
835
836static bool IsControlFlowExpr(const Stmt *S) {
837 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000839 if (!E)
840 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000841
842 E = E->IgnoreParenCasts();
843
John McCall56ca35d2011-02-17 10:25:35 +0000844 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000845 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000847 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
848 if (B->isLogicalOp())
849 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000850
851 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000852}
853
Ted Kremenek14856d72009-04-06 23:06:54 +0000854namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000855class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000856 bool IsDead;
857public:
858 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
859 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000860
861 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000862 bool isDead() const { return IsDead; }
863};
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000865class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000866 std::vector<ContextLocation> CLocs;
867 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000868 PathDiagnostic &PD;
869 PathDiagnosticBuilder &PDB;
870 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000872 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Ted Kremenek14856d72009-04-06 23:06:54 +0000874 bool containsLocation(const PathDiagnosticLocation &Container,
875 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Ted Kremenek14856d72009-04-06 23:06:54 +0000877 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek9650cf32009-05-11 21:42:34 +0000879 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
880 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000881 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000882 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000883 while (1) {
884 // Adjust the location for some expressions that are best referenced
885 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000886 switch (S->getStmtClass()) {
887 default:
888 break;
889 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000890 case Stmt::GenericSelectionExprClass:
891 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000892 firstCharOnly = true;
893 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000894 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000895 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000896 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000897 firstCharOnly = true;
898 continue;
899 case Stmt::ChooseExprClass:
900 S = cast<ChooseExpr>(S)->getCond();
901 firstCharOnly = true;
902 continue;
903 case Stmt::BinaryOperatorClass:
904 S = cast<BinaryOperator>(S)->getLHS();
905 firstCharOnly = true;
906 continue;
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Ted Kremenek9650cf32009-05-11 21:42:34 +0000909 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000910 }
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Ted Kremenek9650cf32009-05-11 21:42:34 +0000912 if (S != Original)
Anna Zaks23803372011-09-20 16:23:37 +0000913 L = PathDiagnosticLocation(S, L.getManager(), PDB.getLocationContext());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000914 }
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Ted Kremenek9650cf32009-05-11 21:42:34 +0000916 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000917 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000918
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000919 return L;
920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Ted Kremenek14856d72009-04-06 23:06:54 +0000922 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000923 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000924 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000925 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000926 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000927 CLocs.pop_back();
928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek14856d72009-04-06 23:06:54 +0000930public:
931 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
932 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremeneka301a672009-04-22 18:16:20 +0000934 // If the PathDiagnostic already has pieces, add the enclosing statement
935 // of the first piece as a context as well.
Ted Kremenek802e0242012-02-08 04:32:34 +0000936 if (!PD.path.empty()) {
937 PrevLoc = (*PD.path.begin())->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Ted Kremenek14856d72009-04-06 23:06:54 +0000939 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000940 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000941 }
942 }
943
944 ~EdgeBuilder() {
945 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000946
Ted Kremeneka301a672009-04-22 18:16:20 +0000947 // Finally, add an initial edge from the start location of the first
948 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000949 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
950 PDB.getLocationContext(),
951 PDB.getSourceManager());
952 if (L.isValid())
953 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000954 }
955
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000956 void flushLocations() {
957 while (!CLocs.empty())
958 popLocation();
959 PrevLoc = PathDiagnosticLocation();
960 }
961
Ted Kremenek14856d72009-04-06 23:06:54 +0000962 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000964 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Ted Kremenek14856d72009-04-06 23:06:54 +0000966 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000967 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000968};
Ted Kremenek14856d72009-04-06 23:06:54 +0000969} // end anonymous namespace
970
971
972PathDiagnosticLocation
973EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
974 if (const Stmt *S = L.asStmt()) {
975 if (IsControlFlowExpr(S))
976 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
978 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 return L;
982}
983
984bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
985 const PathDiagnosticLocation &Containee) {
986
987 if (Container == Containee)
988 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Ted Kremenek14856d72009-04-06 23:06:54 +0000990 if (Container.asDecl())
991 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Ted Kremenek14856d72009-04-06 23:06:54 +0000993 if (const Stmt *S = Containee.asStmt())
994 if (const Stmt *ContainerS = Container.asStmt()) {
995 while (S) {
996 if (S == ContainerS)
997 return true;
998 S = PDB.getParent(S);
999 }
1000 return false;
1001 }
1002
1003 // Less accurate: compare using source ranges.
1004 SourceRange ContainerR = Container.asRange();
1005 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek14856d72009-04-06 23:06:54 +00001007 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +00001008 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1009 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1010 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1011 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Chandler Carruth64211622011-07-25 21:09:52 +00001013 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1014 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1015 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1016 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremenek14856d72009-04-06 23:06:54 +00001018 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +00001019 assert(ContaineeBegLine <= ContaineeEndLine);
1020
Ted Kremenek14856d72009-04-06 23:06:54 +00001021 return (ContainerBegLine <= ContaineeBegLine &&
1022 ContainerEndLine >= ContaineeEndLine &&
1023 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001024 SM.getExpansionColumnNumber(ContainerRBeg) <=
1025 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +00001026 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001027 SM.getExpansionColumnNumber(ContainerREnd) >=
1028 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001029}
1030
Ted Kremenek14856d72009-04-06 23:06:54 +00001031void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1032 if (!PrevLoc.isValid()) {
1033 PrevLoc = NewLoc;
1034 return;
1035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001037 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1038 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001040 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001041 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremenek14856d72009-04-06 23:06:54 +00001043 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001044 if (NewLocClean.asLocation().getExpansionLoc() ==
1045 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001046 return;
1047
Ted Kremenek2042fc12012-02-24 06:00:00 +00001048 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001049 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001050}
1051
1052void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Ted Kremeneka301a672009-04-22 18:16:20 +00001054 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1055 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Ted Kremenek14856d72009-04-06 23:06:54 +00001057 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1058
1059 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001060 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek14856d72009-04-06 23:06:54 +00001062 // Is the top location context the same as the one for the new location?
1063 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001064 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001065 if (IsConsumedExpr(TopContextLoc) &&
1066 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001067 TopContextLoc.markDead();
1068
Ted Kremenek14856d72009-04-06 23:06:54 +00001069 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001070 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001071
1072 return;
1073 }
1074
1075 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001076 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001077 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001079 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001080 CLocs.push_back(ContextLocation(CLoc, true));
1081 return;
1082 }
1083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Ted Kremenek14856d72009-04-06 23:06:54 +00001085 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001086 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001087 }
1088
1089 // Context does not contain the location. Flush it.
1090 popLocation();
1091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001093 // If we reach here, there is no enclosing context. Just add the edge.
1094 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001095}
1096
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001097bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1098 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1099 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001101 return false;
1102}
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Ted Kremeneke1baed32009-05-05 23:13:38 +00001104void EdgeBuilder::addExtendedContext(const Stmt *S) {
1105 if (!S)
1106 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
1108 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001109 while (Parent) {
1110 if (isa<CompoundStmt>(Parent))
1111 Parent = PDB.getParent(Parent);
1112 else
1113 break;
1114 }
1115
1116 if (Parent) {
1117 switch (Parent->getStmtClass()) {
1118 case Stmt::DoStmtClass:
1119 case Stmt::ObjCAtSynchronizedStmtClass:
1120 addContext(Parent);
1121 default:
1122 break;
1123 }
1124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Ted Kremeneke1baed32009-05-05 23:13:38 +00001126 addContext(S);
1127}
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Ted Kremenek14856d72009-04-06 23:06:54 +00001129void EdgeBuilder::addContext(const Stmt *S) {
1130 if (!S)
1131 return;
1132
Anna Zaks220ac8c2011-09-15 01:08:34 +00001133 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Ted Kremenek14856d72009-04-06 23:06:54 +00001135 while (!CLocs.empty()) {
1136 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1137
1138 // Is the top location context the same as the one for the new location?
1139 if (TopContextLoc == L)
1140 return;
1141
1142 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001143 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001144 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001145 }
1146
1147 // Context does not contain the location. Flush it.
1148 popLocation();
1149 }
1150
1151 CLocs.push_back(L);
1152}
1153
1154static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1155 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001156 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001157 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001158 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001159
Ted Kremenek9c378f72011-08-12 23:37:29 +00001160 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001161 while (NextNode) {
1162 N = NextNode;
1163 NextNode = GetPredecessorNode(N);
1164 ProgramPoint P = N->getLocation();
1165
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001166 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001167 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1168 const StackFrameContext *LCtx =
1169 CE->getLocationContext()->getCurrentStackFrame();
1170 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1171 PDB.getSourceManager(),
1172 LCtx);
1173 EB.addEdge(Loc, true);
1174 EB.flushLocations();
Ted Kremenek2042fc12012-02-24 06:00:00 +00001175 PathDiagnosticCallPiece *C =
1176 PathDiagnosticCallPiece::construct(N, *CE, SM);
1177 PD.getActivePath().push_front(C);
1178 PD.pushActivePath(&C->path);
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001179 break;
1180 }
1181
1182 // Was the predecessor in a different stack frame?
1183 if (NextNode &&
1184 !isa<CallExit>(NextNode->getLocation()) &&
1185 NextNode->getLocationContext()->getCurrentStackFrame() !=
1186 N->getLocationContext()->getCurrentStackFrame()) {
1187 EB.flushLocations();
1188 }
Ted Kremenek2042fc12012-02-24 06:00:00 +00001189
1190 // Pop the call hierarchy if we are done walking the contents
1191 // of a function call.
1192 if (const CallEnter *CE = dyn_cast<CallEnter>(&P)) {
1193 PD.popActivePath();
1194 // The current active path should never be empty. Either we
1195 // just added a bunch of stuff to the top-level path, or
1196 // we have a previous CallExit. If the front of the active
1197 // path is not a PathDiagnosticCallPiece, it means that the
1198 // path terminated within a function call. We must then take the
1199 // current contents of the active path and place it within
1200 // a new PathDiagnosticCallPiece.
1201 assert(!PD.getActivePath().empty());
1202 PathDiagnosticCallPiece *C =
1203 dyn_cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1204 if (!C)
1205 C = PathDiagnosticCallPiece::construct(PD.getActivePath());
1206 C->setCallee(*CE, SM);
1207 EB.addContext(CE->getCallExpr());
1208 break;
1209 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001210
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001211 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001212 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001213 const CFGBlock &Blk = *BE->getSrc();
1214 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001216 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001217 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001218 PathDiagnosticLocation L(Loop, SM, PDB.getLocationContext());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001219 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001221 if (!Term) {
1222 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1223 CS = dyn_cast<CompoundStmt>(FS->getBody());
1224 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001225 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001226 }
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001228 PathDiagnosticEventPiece *p =
1229 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001230 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001232 EB.addEdge(p->getLocation(), true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001233 PD.getActivePath().push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001235 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001236 PathDiagnosticLocation BL =
1237 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001238 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001239 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001242 if (Term)
1243 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001245 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001246 }
1247
Mike Stump1eb44332009-09-09 15:08:12 +00001248 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001249 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1250 const Stmt *stmt = S->getStmt();
1251 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001252 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001253 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001254 }
1255 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001256 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001257 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001258
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001259 break;
1260 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001261
1262
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001263 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001265 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001266 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Anna Zaks8e6431a2011-08-18 22:37:56 +00001268 // Add pieces from custom visitors.
1269 BugReport *R = PDB.getBugReport();
1270 for (BugReport::visitor_iterator I = R->visitor_begin(),
1271 E = R->visitor_end(); I!=E; ++I) {
1272 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001273 const PathDiagnosticLocation &Loc = p->getLocation();
1274 EB.addEdge(Loc, true);
Ted Kremenek2042fc12012-02-24 06:00:00 +00001275 PD.getActivePath().push_front(p);
Ted Kremenek8966bc12009-05-06 21:39:49 +00001276 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001277 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001278 }
Mike Stump1eb44332009-09-09 15:08:12 +00001279 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001280 }
1281}
1282
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001283//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001284// Methods for BugType and subclasses.
1285//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001286BugType::~BugType() { }
1287
Ted Kremenekcf118d42009-02-04 23:49:09 +00001288void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001289
David Blaikie99ba9e32011-12-20 02:48:34 +00001290void BuiltinBug::anchor() {}
1291
Ted Kremenekcf118d42009-02-04 23:49:09 +00001292//===----------------------------------------------------------------------===//
1293// Methods for BugReport and subclasses.
1294//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001295
David Blaikie99ba9e32011-12-20 02:48:34 +00001296void BugReport::NodeResolver::anchor() {}
1297
Anna Zaks8e6431a2011-08-18 22:37:56 +00001298void BugReport::addVisitor(BugReporterVisitor* visitor) {
1299 if (!visitor)
1300 return;
1301
1302 llvm::FoldingSetNodeID ID;
1303 visitor->Profile(ID);
1304 void *InsertPos;
1305
1306 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1307 delete visitor;
1308 return;
1309 }
1310
1311 CallbacksSet.InsertNode(visitor, InsertPos);
1312 Callbacks = F.add(visitor, Callbacks);
1313}
1314
1315BugReport::~BugReport() {
1316 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001317 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001318 }
1319}
Anna Zakse172e8b2011-08-17 23:00:25 +00001320
1321void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1322 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001323 hash.AddString(Description);
Anna Zaksca8e36e2012-02-23 21:38:21 +00001324 if (UniqueingLocation.isValid()) {
1325 UniqueingLocation.Profile(hash);
1326 } else if (Location.isValid()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001327 Location.Profile(hash);
1328 } else {
1329 assert(ErrorNode);
1330 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1331 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001332
1333 for (SmallVectorImpl<SourceRange>::const_iterator I =
1334 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1335 const SourceRange range = *I;
1336 if (!range.isValid())
1337 continue;
1338 hash.AddInteger(range.getBegin().getRawEncoding());
1339 hash.AddInteger(range.getEnd().getRawEncoding());
1340 }
1341}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001342
Ted Kremenek9c378f72011-08-12 23:37:29 +00001343const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001344 if (!ErrorNode)
1345 return 0;
1346
Tom Care212f6d32010-09-16 03:50:38 +00001347 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001348 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Ted Kremenek9c378f72011-08-12 23:37:29 +00001350 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001351 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001352 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001353 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001354 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001355 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001356 S = GetStmt(ProgP);
1357
1358 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001359}
1360
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001361std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001362BugReport::getRanges() {
1363 // If no custom ranges, add the range of the statement corresponding to
1364 // the error node.
1365 if (Ranges.empty()) {
1366 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1367 addRange(E->getSourceRange());
1368 else
1369 return std::make_pair(ranges_iterator(), ranges_iterator());
1370 }
1371
Anna Zaks14924262011-08-24 20:31:06 +00001372 // User-specified absence of range info.
1373 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1374 return std::make_pair(ranges_iterator(), ranges_iterator());
1375
Anna Zakse172e8b2011-08-17 23:00:25 +00001376 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001377}
1378
Anna Zaks590dd8e2011-09-20 21:38:35 +00001379PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001380 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001381 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001382 "Either Location or ErrorNode should be specified but not both.");
1383
Ted Kremenek9c378f72011-08-12 23:37:29 +00001384 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001385 const LocationContext *LC = ErrorNode->getLocationContext();
1386
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001387 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001388 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001389 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001390 // For binary operators, return the location of the operator.
1391 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001392 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001393
Anna Zaks590dd8e2011-09-20 21:38:35 +00001394 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001395 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001396 } else {
1397 assert(Location.isValid());
1398 return Location;
1399 }
1400
Anna Zaks590dd8e2011-09-20 21:38:35 +00001401 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001402}
1403
Ted Kremenekcf118d42009-02-04 23:49:09 +00001404//===----------------------------------------------------------------------===//
1405// Methods for BugReporter and subclasses.
1406//===----------------------------------------------------------------------===//
1407
1408BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001409 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001410}
1411
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001412GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001413BugReporterData::~BugReporterData() {}
1414
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001415ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001416
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001417ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001418GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1419
Anna Zaks3b030a22011-08-19 01:57:09 +00001420BugReporter::~BugReporter() {
1421 FlushReports();
1422
1423 // Free the bug reports we are tracking.
1424 typedef std::vector<BugReportEquivClass *> ContTy;
1425 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1426 I != E; ++I) {
1427 delete *I;
1428 }
1429}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001430
1431void BugReporter::FlushReports() {
1432 if (BugTypes.isEmpty())
1433 return;
1434
1435 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001436 // warnings and new BugTypes.
1437 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1438 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001439 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001440 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001441 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001442 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001443 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001444 const_cast<BugType*>(*I)->FlushReports(*this);
1445
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001446 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1447 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1448 BugReportEquivClass& EQ = *EI;
1449 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001450 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001451
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001452 // BugReporter owns and deletes only BugTypes created implicitly through
1453 // EmitBasicReport.
1454 // FIXME: There are leaks from checkers that assume that the BugTypes they
1455 // create will be destroyed by the BugReporter.
1456 for (llvm::StringMap<BugType*>::iterator
1457 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1458 delete I->second;
1459
Ted Kremenekcf118d42009-02-04 23:49:09 +00001460 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001461 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001462}
1463
1464//===----------------------------------------------------------------------===//
1465// PathDiagnostics generation.
1466//===----------------------------------------------------------------------===//
1467
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001468static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001469 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001470MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001471 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenekcf118d42009-02-04 23:49:09 +00001473 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001474 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001475 // error node unless there are two or more error nodes with the same minimum
1476 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001477 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001478 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001479
1480 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001481 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1482 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenekcf118d42009-02-04 23:49:09 +00001484 // Create owning pointers for GTrim and NMap just to ensure that they are
1485 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001486 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1487 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Ted Kremenekcf118d42009-02-04 23:49:09 +00001489 // Find the (first) error node in the trimmed graph. We just need to consult
1490 // the node map (NMap) which maps from nodes in the original graph to nodes
1491 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001492
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001493 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001494 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001495 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001496
Ted Kremenek40406fe2010-12-03 06:52:30 +00001497 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1498 const ExplodedNode *originalNode = nodes[nodeIndex];
1499 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001500 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001501 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001502 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Ted Kremenek938332c2009-05-16 01:11:58 +00001505 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001506
1507 // Create a new (third!) graph with a single path. This is the graph
1508 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001509 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ted Kremenek10aa5542009-03-12 23:41:59 +00001511 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001512 // to the root node, and then construct a new graph that contains only
1513 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001514 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001516 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001517 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001519 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001520 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001521 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001523 if (Visited.find(Node) != Visited.end())
1524 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001526 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001528 if (Node->pred_empty()) {
1529 Root = Node;
1530 break;
1531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001533 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001534 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001535 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001536 }
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Ted Kremenek938332c2009-05-16 01:11:58 +00001538 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Ted Kremenek10aa5542009-03-12 23:41:59 +00001540 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001541 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001542 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001543 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001544 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001546 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001547 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001548 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001549 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001551 // Create the equivalent node in the new graph with the same state
1552 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001553 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001555 // Store the mapping to the original node.
1556 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1557 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001558 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001560 // Link up the new node with the previous node.
1561 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001562 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001564 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001566 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001567 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001568 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001569 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001570 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001571 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001572 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001573 }
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001575 // Find the next successor node. We choose the node that is marked
1576 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001577 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1578 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001579 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001581 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001583 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001585 if (I == Visited.end())
1586 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001588 if (!N || I->second < MinVal) {
1589 N = *SI;
1590 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001591 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenek938332c2009-05-16 01:11:58 +00001594 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001595 }
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Ted Kremenek938332c2009-05-16 01:11:58 +00001597 assert(First);
1598
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001599 return std::make_pair(std::make_pair(GNew, BM),
1600 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001601}
1602
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001603/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1604/// and collapses PathDiagosticPieces that are expanded by macros.
1605static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
Ted Kremenek2042fc12012-02-24 06:00:00 +00001606 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
1607 SourceLocation> > MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001609 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001610 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001612 MacroStackTy MacroStack;
1613 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Ted Kremenek2042fc12012-02-24 06:00:00 +00001615 for (PathPieces::const_iterator I = PD.path.begin(), E = PD.path.end();
1616 I!=E; ++I) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001617 // Get the location of the PathDiagnosticPiece.
Ted Kremenek802e0242012-02-08 04:32:34 +00001618 const FullSourceLoc Loc = (*I)->getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001620 // Determine the instantiation location, which is the location we group
1621 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001622 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001623 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001624 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001626 if (Loc.isFileID()) {
1627 MacroStack.clear();
Ted Kremenek802e0242012-02-08 04:32:34 +00001628 Pieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001629 continue;
1630 }
1631
1632 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001634 // Is the PathDiagnosticPiece within the same macro group?
1635 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
Ted Kremenek802e0242012-02-08 04:32:34 +00001636 MacroStack.back().first->subPieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001637 continue;
1638 }
1639
1640 // We aren't in the same group. Are we descending into a new macro
1641 // or are part of an old one?
Dylan Noblesmithc93dc782012-02-20 14:00:23 +00001642 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001643
1644 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001645 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001646 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001648 // Walk the entire macro stack.
1649 while (!MacroStack.empty()) {
1650 if (InstantiationLoc == MacroStack.back().second) {
1651 MacroGroup = MacroStack.back().first;
1652 break;
1653 }
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001655 if (ParentInstantiationLoc == MacroStack.back().second) {
1656 MacroGroup = MacroStack.back().first;
1657 break;
1658 }
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001660 MacroStack.pop_back();
1661 }
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001663 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1664 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001665 PathDiagnosticMacroPiece *NewGroup =
1666 new PathDiagnosticMacroPiece(
Ted Kremenek802e0242012-02-08 04:32:34 +00001667 PathDiagnosticLocation::createSingleLocation((*I)->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001668
1669 if (MacroGroup)
Ted Kremenek802e0242012-02-08 04:32:34 +00001670 MacroGroup->subPieces.push_back(NewGroup);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001671 else {
1672 assert(InstantiationLoc.isFileID());
1673 Pieces.push_back(NewGroup);
1674 }
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001676 MacroGroup = NewGroup;
1677 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1678 }
1679
1680 // Finally, add the PathDiagnosticPiece to the group.
Ted Kremenek802e0242012-02-08 04:32:34 +00001681 MacroGroup->subPieces.push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001684 // Now take the pieces and construct a new PathDiagnostic.
Ted Kremenek2042fc12012-02-24 06:00:00 +00001685 PD.getMutablePieces().clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001687 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
Ted Kremenekaf84f8f2012-02-08 22:48:17 +00001688 if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
1689 if (!MP->containsEvent())
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001690 continue;
Ted Kremenek2042fc12012-02-24 06:00:00 +00001691 PD.getMutablePieces().push_back(*I);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001692 }
1693}
1694
Ted Kremenek7dc86642009-03-31 20:22:36 +00001695void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001696 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Ted Kremenek40406fe2010-12-03 06:52:30 +00001698 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001699 SmallVector<const ExplodedNode *, 10> errorNodes;
1700 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001701 E = bugReports.end(); I != E; ++I) {
1702 errorNodes.push_back((*I)->getErrorNode());
1703 }
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001705 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001706 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001707 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001708 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001709 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Ted Kremenekcf118d42009-02-04 23:49:09 +00001711 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001712 assert(GPair.second.second < bugReports.size());
1713 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001714 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001716 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1717 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001718 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001719
1720 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001721 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1722 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Anna Zaks8e6431a2011-08-18 22:37:56 +00001724 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001725 R->addVisitor(new NilReceiverBRVisitor());
1726 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Anna Zaks23f395e2011-08-20 01:27:22 +00001728 // Generate the very last diagnostic piece - the piece is visible before
1729 // the trace is expanded.
1730 PathDiagnosticPiece *LastPiece = 0;
1731 for (BugReport::visitor_iterator I = R->visitor_begin(),
1732 E = R->visitor_end(); I!=E; ++I) {
1733 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1734 assert (!LastPiece &&
1735 "There can only be one final piece in a diagnostic.");
1736 LastPiece = Piece;
1737 }
1738 }
1739 if (!LastPiece)
1740 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1741 if (LastPiece)
Ted Kremenek2042fc12012-02-24 06:00:00 +00001742 PD.getActivePath().push_back(LastPiece);
Anna Zaks23f395e2011-08-20 01:27:22 +00001743 else
1744 return;
1745
Ted Kremenek7dc86642009-03-31 20:22:36 +00001746 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001747 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001748 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001749 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001750 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001751 GenerateMinimalPathDiagnostic(PD, PDB, N);
1752 break;
1753 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001754}
1755
Ted Kremenekcf118d42009-02-04 23:49:09 +00001756void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001757 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001758}
1759
Mike Stump1eb44332009-09-09 15:08:12 +00001760void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001761 // Compute the bug report's hash to determine its equivalence class.
1762 llvm::FoldingSetNodeID ID;
1763 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001764
1765 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001766 BugType& BT = R->getBugType();
1767 Register(&BT);
1768 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001769 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Ted Kremenekcf118d42009-02-04 23:49:09 +00001771 if (!EQ) {
1772 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001773 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001774 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001775 }
1776 else
1777 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001778}
1779
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001780
1781//===----------------------------------------------------------------------===//
1782// Emitting reports in equivalence classes.
1783//===----------------------------------------------------------------------===//
1784
1785namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001786struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001787 const ExplodedNode *N;
1788 ExplodedNode::const_succ_iterator I, E;
1789
1790 FRIEC_WLItem(const ExplodedNode *n)
1791 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1792};
1793}
1794
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001795static BugReport *
1796FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001797 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001798
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001799 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1800 assert(I != E);
1801 BugReport *R = *I;
1802 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001803
Ted Kremenek40406fe2010-12-03 06:52:30 +00001804 // If we don't need to suppress any of the nodes because they are
1805 // post-dominated by a sink, simply add all the nodes in the equivalence class
1806 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001807 if (!BT.isSuppressOnSink()) {
1808 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001809 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001810 if (N) {
1811 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001812 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001813 }
1814 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001815 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001816 }
1817
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001818 // For bug reports that should be suppressed when all paths are post-dominated
1819 // by a sink node, iterate through the reports in the equivalence class
1820 // until we find one that isn't post-dominated (if one exists). We use a
1821 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1822 // this as a recursive function, but we don't want to risk blowing out the
1823 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001824 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001825
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001826 for (; I != E; ++I) {
1827 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001828 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001829
Ted Kremenek40406fe2010-12-03 06:52:30 +00001830 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001831 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001832 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001833 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001834 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001835 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001836 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001837 if (errorNode->succ_empty()) {
1838 bugReports.push_back(R);
1839 if (!exampleReport)
1840 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001841 continue;
1842 }
1843
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001844 // At this point we know that 'N' is not a sink and it has at least one
1845 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1846 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001847 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001848 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1849
1850 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001851 WL.push_back(errorNode);
1852 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001853
1854 while (!WL.empty()) {
1855 WLItem &WI = WL.back();
1856 assert(!WI.N->succ_empty());
1857
1858 for (; WI.I != WI.E; ++WI.I) {
1859 const ExplodedNode *Succ = *WI.I;
1860 // End-of-path node?
1861 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001862 // If we found an end-of-path node that is not a sink.
1863 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001864 bugReports.push_back(R);
1865 if (!exampleReport)
1866 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001867 WL.clear();
1868 break;
1869 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001870 // Found a sink? Continue on to the next successor.
1871 continue;
1872 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001873 // Mark the successor as visited. If it hasn't been explored,
1874 // enqueue it to the DFS worklist.
1875 unsigned &mark = Visited[Succ];
1876 if (!mark) {
1877 mark = 1;
1878 WL.push_back(Succ);
1879 break;
1880 }
1881 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001882
1883 // The worklist may have been cleared at this point. First
1884 // check if it is empty before checking the last item.
1885 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001886 WL.pop_back();
1887 }
1888 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001889
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001890 // ExampleReport will be NULL if all the nodes in the equivalence class
1891 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001892 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001893}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001894
1895//===----------------------------------------------------------------------===//
1896// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1897// uses global state, which eventually should go elsewhere.
1898//===----------------------------------------------------------------------===//
1899namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001900class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001901 llvm::FoldingSetNodeID ID;
1902public:
1903 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001904 R->Profile(ID);
1905 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001906 }
1907
1908 void Profile(llvm::FoldingSetNodeID &id) {
1909 id = ID;
1910 }
1911
1912 llvm::FoldingSetNodeID &getID() { return ID; }
1913};
1914}
1915
1916static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1917 // FIXME: Eventually this diagnostic cache should reside in something
1918 // like AnalysisManager instead of being a static variable. This is
1919 // really unsafe in the long term.
1920 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1921 static DiagnosticCache DC;
1922
1923 void *InsertPos;
1924 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1925
1926 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1927 delete Item;
1928 return true;
1929 }
1930
1931 DC.InsertNode(Item, InsertPos);
1932 return false;
1933}
1934
Ted Kremenekcf118d42009-02-04 23:49:09 +00001935void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001936 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001937 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1938 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001939 return;
1940
David Blaikieef3643f2011-09-26 00:51:36 +00001941 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Ted Kremenekcf118d42009-02-04 23:49:09 +00001943 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001944 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001945 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001947 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001948 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001949 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001950 ? exampleReport->getDescription()
1951 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001952 BT.getCategory()));
1953
Ted Kremenek40406fe2010-12-03 06:52:30 +00001954 if (!bugReports.empty())
1955 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Ted Kremenek40406fe2010-12-03 06:52:30 +00001957 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001958 return;
1959
Ted Kremenek072192b2008-04-30 23:47:44 +00001960 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001961 const BugReport::ExtraTextList &Meta =
1962 exampleReport->getExtraText();
1963 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1964 e = Meta.end(); i != e; ++i) {
1965 D->addMeta(*i);
1966 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001967
Ted Kremenek3148eb42009-01-24 00:55:43 +00001968 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001969 BugReport::ranges_iterator Beg, End;
1970 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001971 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001972
1973 // Search the description for '%', as that will be interpretted as a
1974 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001975 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001976 unsigned ErrorDiag;
1977 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001978 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001979 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001980 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001981 if (*I == '%')
1982 Out << "%%";
1983 else
1984 Out << *I;
1985
1986 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001987 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001988 }
Ted Kremenek57202072008-07-14 17:40:50 +00001989
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001990 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001991 DiagnosticBuilder diagBuilder = Diag.Report(
1992 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001993 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001994 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001995 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001996
David Blaikieef3643f2011-09-26 00:51:36 +00001997 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001998 if (!PD)
1999 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Ted Kremenek802e0242012-02-08 04:32:34 +00002001 if (D->path.empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00002002 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
2003 exampleReport->getLocation(getSourceManager()),
2004 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00002005
Ted Kremenek3148eb42009-01-24 00:55:43 +00002006 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
Ted Kremenek2042fc12012-02-24 06:00:00 +00002007 D->getActivePath().push_back(piece);
Ted Kremenek3148eb42009-01-24 00:55:43 +00002008 }
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Ted Kremenek3148eb42009-01-24 00:55:43 +00002010 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00002011}
Ted Kremenek57202072008-07-14 17:40:50 +00002012
Chris Lattner5f9e2722011-07-23 10:55:15 +00002013void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002014 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002015 SourceRange* RBeg, unsigned NumRanges) {
2016 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
2017}
Ted Kremenekcf118d42009-02-04 23:49:09 +00002018
Chris Lattner5f9e2722011-07-23 10:55:15 +00002019void BugReporter::EmitBasicReport(StringRef name,
2020 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00002021 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00002022 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002024 // 'BT' is owned by BugReporter.
2025 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002026 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00002027 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
2028 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00002029}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002030
Chris Lattner5f9e2722011-07-23 10:55:15 +00002031BugType *BugReporter::getBugTypeForName(StringRef name,
2032 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002033 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00002034 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
2035 llvm::StringMapEntry<BugType *> &
2036 entry = StrBugTypes.GetOrCreateValue(fullDesc);
2037 BugType *BT = entry.getValue();
2038 if (!BT) {
2039 BT = new BugType(name, category);
2040 entry.setValue(BT);
2041 }
2042 return BT;
2043}