blob: 8e5ea361097d22ba44aa1043893c9a29d8a6889c [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 Kremenek10aa5542009-03-12 23:41:59 +000032#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000033
34using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000035using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000036
Ted Kremenek8966bc12009-05-06 21:39:49 +000037BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000038
David Blaikie99ba9e32011-12-20 02:48:34 +000039void BugReporterContext::anchor() {}
40
Ted Kremenekcf118d42009-02-04 23:49:09 +000041//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000042// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000043//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000044
Ted Kremenek9c378f72011-08-12 23:37:29 +000045static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000046 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
47 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000048 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000049 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000050
Ted Kremenekb697b102009-02-23 22:44:26 +000051 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000052}
53
Zhongxing Xuc5619d92009-08-06 01:32:16 +000054static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000055GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000056 return N->pred_empty() ? NULL : *(N->pred_begin());
57}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000058
Zhongxing Xuc5619d92009-08-06 01:32:16 +000059static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000060GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000061 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000062}
63
Ted Kremenek9c378f72011-08-12 23:37:29 +000064static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000065 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000066 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000067 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenekb697b102009-02-23 22:44:26 +000069 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000070}
71
Ted Kremenek9c378f72011-08-12 23:37:29 +000072static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000073 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000074 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000075 // Check if the statement is '?' or '&&'/'||'. These are "merges",
76 // not actual statement points.
77 switch (S->getStmtClass()) {
78 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000079 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 case Stmt::ConditionalOperatorClass: continue;
81 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000082 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
83 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000084 continue;
85 break;
86 }
87 default:
88 break;
89 }
Ted Kremenekb697b102009-02-23 22:44:26 +000090 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000091 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenekb697b102009-02-23 22:44:26 +000093 return 0;
94}
95
Ted Kremenek5f85e172009-07-22 22:35:28 +000096static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000097GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000098 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000099 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenekb697b102009-02-23 22:44:26 +0000101 return GetPreviousStmt(N);
102}
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremenek5f85e172009-07-22 22:35:28 +0000104static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000105GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000106 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000107 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenekb697b102009-02-23 22:44:26 +0000109 return GetNextStmt(N);
110}
111
112//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000113// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000114//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000115
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000116typedef llvm::DenseMap<const ExplodedNode*,
117const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000118
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000119namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000120class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000121 NodeBackMap& M;
122public:
123 NodeMapClosure(NodeBackMap *m) : M(*m) {}
124 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenek9c378f72011-08-12 23:37:29 +0000126 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000127 NodeBackMap::iterator I = M.find(N);
128 return I == M.end() ? 0 : I->second;
129 }
130};
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000132class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000133 BugReport *R;
David Blaikieef3643f2011-09-26 00:51:36 +0000134 PathDiagnosticConsumer *PDC;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000135 OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000136 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000137public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000138 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000139 BugReport *r, NodeBackMap *Backmap,
David Blaikieef3643f2011-09-26 00:51:36 +0000140 PathDiagnosticConsumer *pdc)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000141 : BugReporterContext(br),
Anna Zaks8e6431a2011-08-18 22:37:56 +0000142 R(r), PDC(pdc), NMC(Backmap) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek9c378f72011-08-12 23:37:29 +0000144 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Ted Kremenek9c378f72011-08-12 23:37:29 +0000146 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
147 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Anna Zaks8e6431a2011-08-18 22:37:56 +0000149 BugReport *getBugReport() { return R; }
150
Tom Care212f6d32010-09-16 03:50:38 +0000151 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000152
Anna Zaks220ac8c2011-09-15 01:08:34 +0000153 const LocationContext* getLocationContext() {
154 return R->getErrorNode()->getLocationContext();
155 }
156
Tom Care212f6d32010-09-16 03:50:38 +0000157 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000159 const Stmt *getParent(const Stmt *S) {
160 return getParentMap().getParent(S);
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek8966bc12009-05-06 21:39:49 +0000163 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000164
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000165 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
David Blaikieef3643f2011-09-26 00:51:36 +0000167 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
168 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000169 }
170
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000171 bool supportsLogicalOpControlFlow() const {
172 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000173 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000174};
175} // end anonymous namespace
176
Ted Kremenek00605e02009-03-27 20:55:39 +0000177PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000178PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000179 if (const Stmt *S = GetNextStmt(N))
Anna Zaks220ac8c2011-09-15 01:08:34 +0000180 return PathDiagnosticLocation(S, getSourceManager(), getLocationContext());
Ted Kremenek00605e02009-03-27 20:55:39 +0000181
Anna Zaks0cd59482011-09-16 19:18:30 +0000182 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
183 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000184}
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenek00605e02009-03-27 20:55:39 +0000186PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000187PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
188 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000189
Ted Kremenek143ca222008-05-06 18:11:09 +0000190 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000191 if (os.str().empty())
192 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenek00605e02009-03-27 20:55:39 +0000194 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek00605e02009-03-27 20:55:39 +0000196 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000197 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000198 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000199 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000200 else {
201 os << "Execution jumps to the end of the ";
202 const Decl *D = N->getLocationContext()->getDecl();
203 if (isa<ObjCMethodDecl>(D))
204 os << "method";
205 else if (isa<FunctionDecl>(D))
206 os << "function";
207 else {
208 assert(isa<BlockDecl>(D));
209 os << "anonymous block";
210 }
211 os << '.';
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000214 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000215}
216
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000217static bool IsNested(const Stmt *S, ParentMap &PM) {
218 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
219 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000221 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000223 if (Parent)
224 switch (Parent->getStmtClass()) {
225 case Stmt::ForStmtClass:
226 case Stmt::DoStmtClass:
227 case Stmt::WhileStmtClass:
228 return true;
229 default:
230 break;
231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
233 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000234}
235
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000236PathDiagnosticLocation
237PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000238 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000239 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000240 SourceManager &SMgr = getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000241 const LocationContext *LC = getLocationContext();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000242
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000243 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000244 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000246 if (!Parent)
247 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000249 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000250 case Stmt::BinaryOperatorClass: {
251 const BinaryOperator *B = cast<BinaryOperator>(Parent);
252 if (B->isLogicalOp())
Anna Zaks220ac8c2011-09-15 01:08:34 +0000253 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000254 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000255 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000256 case Stmt::CompoundStmtClass:
257 case Stmt::StmtExprClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000258 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000259 case Stmt::ChooseExprClass:
260 // Similar to '?' if we are referring to condition, just have the edge
261 // point to the entire choose expression.
262 if (cast<ChooseExpr>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000263 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000264 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000265 return PathDiagnosticLocation(S, SMgr, LC);
John McCall56ca35d2011-02-17 10:25:35 +0000266 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000267 case Stmt::ConditionalOperatorClass:
268 // For '?', if we are referring to condition, just have the edge point
269 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000270 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000271 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000272 else
Anna Zaks220ac8c2011-09-15 01:08:34 +0000273 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000274 case Stmt::DoStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000275 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000276 case Stmt::ForStmtClass:
277 if (cast<ForStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000278 return PathDiagnosticLocation(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000279 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000280 case Stmt::IfStmtClass:
281 if (cast<IfStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000282 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000283 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000284 case Stmt::ObjCForCollectionStmtClass:
285 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000286 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000287 break;
288 case Stmt::WhileStmtClass:
289 if (cast<WhileStmt>(Parent)->getCond() != S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000290 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000291 break;
292 default:
293 break;
294 }
295
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000296 S = Parent;
297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000299 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000300
301 // Special case: DeclStmts can appear in for statement declarations, in which
302 // case the ForStmt is the context.
303 if (isa<DeclStmt>(S)) {
304 if (const Stmt *Parent = P.getParent(S)) {
305 switch (Parent->getStmtClass()) {
306 case Stmt::ForStmtClass:
307 case Stmt::ObjCForCollectionStmtClass:
Anna Zaks220ac8c2011-09-15 01:08:34 +0000308 return PathDiagnosticLocation(Parent, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000309 default:
310 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000311 }
312 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000313 }
314 else if (isa<BinaryOperator>(S)) {
315 // Special case: the binary operator represents the initialization
316 // code in a for statement (this can happen when the variable being
317 // initialized is an old variable.
318 if (const ForStmt *FS =
319 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
320 if (FS->getInit() == S)
Anna Zaks220ac8c2011-09-15 01:08:34 +0000321 return PathDiagnosticLocation(FS, SMgr, LC);
Ted Kremeneke88a1702009-05-11 22:19:32 +0000322 }
323 }
324
Anna Zaks220ac8c2011-09-15 01:08:34 +0000325 return PathDiagnosticLocation(S, SMgr, LC);
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000326}
327
Ted Kremenekcf118d42009-02-04 23:49:09 +0000328//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000329// ScanNotableSymbols: closure-like callback for scanning Store bindings.
330//===----------------------------------------------------------------------===//
331
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000332static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
333 ProgramStateManager& VMgr,
334 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek31061982009-03-31 23:00:32 +0000336 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek31061982009-03-31 23:00:32 +0000338 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek31061982009-03-31 23:00:32 +0000340 if (!isa<PostStmt>(P))
341 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek9c378f72011-08-12 23:37:29 +0000343 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek31061982009-03-31 23:00:32 +0000345 if (!DR)
346 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek5eca4822012-01-06 22:09:28 +0000348 SVal Y = N->getState()->getSVal(DR, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek31061982009-03-31 23:00:32 +0000350 if (X != Y)
351 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek9c378f72011-08-12 23:37:29 +0000353 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek31061982009-03-31 23:00:32 +0000355 if (!VD)
356 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek31061982009-03-31 23:00:32 +0000358 return VD;
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek31061982009-03-31 23:00:32 +0000361 return 0;
362}
363
364namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000365class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000366: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek31061982009-03-31 23:00:32 +0000368 SymbolRef Sym;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000369 ProgramStateRef PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000370 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000371 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000372 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000373 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000374 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek31061982009-03-31 23:00:32 +0000376public:
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000378 NotableSymbolHandler(SymbolRef sym,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000379 ProgramStateRef prevst,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000380 const Stmt *s,
381 ProgramStateManager& vmgr,
382 const ExplodedNode *pred,
383 PathDiagnostic& pd,
384 BugReporter& br)
385 : Sym(sym),
386 PrevSt(prevst),
387 S(s),
388 VMgr(vmgr),
389 Pred(pred),
390 PD(pd),
391 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek31061982009-03-31 23:00:32 +0000393 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
394 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek31061982009-03-31 23:00:32 +0000396 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek31061982009-03-31 23:00:32 +0000398 if (ScanSym != Sym)
399 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000400
401 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000402 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremenek31061982009-03-31 23:00:32 +0000404 if (X == V) // Same binding?
405 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenek31061982009-03-31 23:00:32 +0000407 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000408 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000409 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek31061982009-03-31 23:00:32 +0000411 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek31061982009-03-31 23:00:32 +0000413 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
414 if (!B->isAssignmentOp())
415 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek31061982009-03-31 23:00:32 +0000417 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000418 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek31061982009-03-31 23:00:32 +0000420 if (!DR)
421 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek31061982009-03-31 23:00:32 +0000423 VD = dyn_cast<VarDecl>(DR->getDecl());
424 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000425 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000426 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
427 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000428 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000429 VD = dyn_cast<VarDecl>(*DS->decl_begin());
430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek31061982009-03-31 23:00:32 +0000432 if (!VD)
433 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek31061982009-03-31 23:00:32 +0000435 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000436 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek31061982009-03-31 23:00:32 +0000438 if (!MostRecent)
439 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek31061982009-03-31 23:00:32 +0000441 // Create the diagnostic.
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000442 if (Loc::isLocType(VD->getType())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000443 SmallString<64> buf;
Jordy Rose7df12342011-08-21 05:25:15 +0000444 llvm::raw_svector_ostream os(buf);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000445 os << '\'' << *VD << "' now aliases '" << *MostRecent << '\'';
Anna Zaks590dd8e2011-09-20 21:38:35 +0000446 PathDiagnosticLocation L =
447 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(),
448 Pred->getLocationContext());
Jordy Rose7df12342011-08-21 05:25:15 +0000449 PD.push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek31061982009-03-31 23:00:32 +0000452 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000453 }
Ted Kremenek31061982009-03-31 23:00:32 +0000454};
455}
456
Ted Kremenek9c378f72011-08-12 23:37:29 +0000457static void HandleNotableSymbol(const ExplodedNode *N,
458 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000459 SymbolRef Sym, BugReporter& BR,
460 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Ted Kremenek9c378f72011-08-12 23:37:29 +0000462 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000463 ProgramStateRef PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek31061982009-03-31 23:00:32 +0000465 if (!PrevSt)
466 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek31061982009-03-31 23:00:32 +0000468 // Look at the region bindings of the current state that map to the
469 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000470 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000471 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
472 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
473}
474
475namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000476class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000477: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek31061982009-03-31 23:00:32 +0000479 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000480 const ExplodedNode *N;
481 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000482 GRBugReporter& BR;
483 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek31061982009-03-31 23:00:32 +0000485public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000486 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000487 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000488 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek31061982009-03-31 23:00:32 +0000490 bool HandleBinding(StoreManager& SMgr, Store store,
491 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek31061982009-03-31 23:00:32 +0000493 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremenek31061982009-03-31 23:00:32 +0000495 if (!ScanSym)
496 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek31061982009-03-31 23:00:32 +0000498 if (!BR.isNotable(ScanSym))
499 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501 if (AlreadyProcessed.count(ScanSym))
502 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek31061982009-03-31 23:00:32 +0000504 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Ted Kremenek31061982009-03-31 23:00:32 +0000506 HandleNotableSymbol(N, S, ScanSym, BR, PD);
507 return true;
508 }
509};
510} // end anonymous namespace
511
512//===----------------------------------------------------------------------===//
513// "Minimal" path diagnostic generation algorithm.
514//===----------------------------------------------------------------------===//
515
Ted Kremenek14856d72009-04-06 23:06:54 +0000516static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
517
Ted Kremenek31061982009-03-31 23:00:32 +0000518static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
519 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000520 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000521
Ted Kremenek31061982009-03-31 23:00:32 +0000522 SourceManager& SMgr = PDB.getSourceManager();
Anna Zaks220ac8c2011-09-15 01:08:34 +0000523 const LocationContext *LC = PDB.getLocationContext();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000524 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000525 ? NULL : *(N->pred_begin());
526 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000528 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek31061982009-03-31 23:00:32 +0000530 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Ted Kremenek9c378f72011-08-12 23:37:29 +0000532 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
533 const CFGBlock *Src = BE->getSrc();
534 const CFGBlock *Dst = BE->getDst();
535 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Ted Kremenek31061982009-03-31 23:00:32 +0000537 if (!T)
538 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Anna Zaks590dd8e2011-09-20 21:38:35 +0000540 PathDiagnosticLocation Start =
541 PathDiagnosticLocation::createBegin(T, SMgr,
542 N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek31061982009-03-31 23:00:32 +0000544 switch (T->getStmtClass()) {
545 default:
546 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek31061982009-03-31 23:00:32 +0000548 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000549 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000550 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek31061982009-03-31 23:00:32 +0000552 if (!S)
553 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek31061982009-03-31 23:00:32 +0000555 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000556 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000557 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek31061982009-03-31 23:00:32 +0000559 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000560 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000561 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
562 os.str()));
563 break;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
566 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000567 // Figure out what case arm we took.
568 std::string sbuf;
569 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Ted Kremenek9c378f72011-08-12 23:37:29 +0000571 if (const Stmt *S = Dst->getLabel()) {
Anna Zaks220ac8c2011-09-15 01:08:34 +0000572 PathDiagnosticLocation End(S, SMgr, LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek31061982009-03-31 23:00:32 +0000574 switch (S->getStmtClass()) {
575 default:
576 os << "No cases match in the switch statement. "
577 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000578 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000579 break;
580 case Stmt::DefaultStmtClass:
581 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000582 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000583 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek31061982009-03-31 23:00:32 +0000585 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000586 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000587 const CaseStmt *Case = cast<CaseStmt>(S);
588 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000589
590 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000591 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Ted Kremenek9c378f72011-08-12 23:37:29 +0000593 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000594 // FIXME: Maybe this should be an assertion. Are there cases
595 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000596 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000597 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Ted Kremenek31061982009-03-31 23:00:32 +0000599 if (D) {
600 GetRawInt = false;
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000601 os << *D;
Ted Kremenek31061982009-03-31 23:00:32 +0000602 }
603 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000604
605 if (GetRawInt)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000606 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000607
Ted Kremenek31061982009-03-31 23:00:32 +0000608 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000609 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000610 break;
611 }
612 }
613 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
614 os.str()));
615 }
616 else {
617 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000618 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000619 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
620 os.str()));
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenek31061982009-03-31 23:00:32 +0000623 break;
624 }
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Ted Kremenek31061982009-03-31 23:00:32 +0000626 case Stmt::BreakStmtClass:
627 case Stmt::ContinueStmtClass: {
628 std::string sbuf;
629 llvm::raw_string_ostream os(sbuf);
630 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
631 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
632 os.str()));
633 break;
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek31061982009-03-31 23:00:32 +0000636 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000637 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000638 case Stmt::ConditionalOperatorClass: {
639 std::string sbuf;
640 llvm::raw_string_ostream os(sbuf);
641 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek31061982009-03-31 23:00:32 +0000643 if (*(Src->succ_begin()+1) == Dst)
644 os << "false";
645 else
646 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremenek31061982009-03-31 23:00:32 +0000648 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek31061982009-03-31 23:00:32 +0000650 if (const Stmt *S = End.asStmt())
651 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek31061982009-03-31 23:00:32 +0000653 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
654 os.str()));
655 break;
656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek31061982009-03-31 23:00:32 +0000658 // Determine control-flow for short-circuited '&&' and '||'.
659 case Stmt::BinaryOperatorClass: {
660 if (!PDB.supportsLogicalOpControlFlow())
661 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000663 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000664 std::string sbuf;
665 llvm::raw_string_ostream os(sbuf);
666 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000667
John McCall2de56d12010-08-25 11:45:40 +0000668 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000669 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek31061982009-03-31 23:00:32 +0000671 if (*(Src->succ_begin()+1) == Dst) {
672 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000673 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
Anna Zaks0cd59482011-09-16 19:18:30 +0000674 PathDiagnosticLocation Start =
675 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
Ted Kremenek31061982009-03-31 23:00:32 +0000676 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
677 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000678 }
Ted Kremenek31061982009-03-31 23:00:32 +0000679 else {
680 os << "true";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000681 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000682 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
683 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
684 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000685 }
Ted Kremenek31061982009-03-31 23:00:32 +0000686 }
687 else {
John McCall2de56d12010-08-25 11:45:40 +0000688 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000689 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Ted Kremenek31061982009-03-31 23:00:32 +0000691 if (*(Src->succ_begin()+1) == Dst) {
692 os << "false";
Anna Zaks220ac8c2011-09-15 01:08:34 +0000693 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
Ted Kremenek31061982009-03-31 23:00:32 +0000694 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
695 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000696 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000697 }
698 else {
699 os << "true";
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 Kremenek31061982009-03-31 23:00:32 +0000703 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000704 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000705 }
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek31061982009-03-31 23:00:32 +0000708 break;
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
711 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000712 if (*(Src->succ_begin()) == Dst) {
713 std::string sbuf;
714 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremenek31061982009-03-31 23:00:32 +0000716 os << "Loop condition is true. ";
717 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 if (const Stmt *S = End.asStmt())
720 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek31061982009-03-31 23:00:32 +0000722 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
723 os.str()));
724 }
725 else {
726 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremenek31061982009-03-31 23:00:32 +0000728 if (const Stmt *S = End.asStmt())
729 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenek31061982009-03-31 23:00:32 +0000731 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
732 "Loop condition is false. Exiting loop"));
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
Ted Kremenek31061982009-03-31 23:00:32 +0000738 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000739 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000740 if (*(Src->succ_begin()+1) == Dst) {
741 std::string sbuf;
742 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Ted Kremenek31061982009-03-31 23:00:32 +0000744 os << "Loop condition is false. ";
745 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
746 if (const Stmt *S = End.asStmt())
747 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek31061982009-03-31 23:00:32 +0000749 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
750 os.str()));
751 }
752 else {
753 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
754 if (const Stmt *S = End.asStmt())
755 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek31061982009-03-31 23:00:32 +0000757 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000758 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek31061982009-03-31 23:00:32 +0000761 break;
762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek31061982009-03-31 23:00:32 +0000764 case Stmt::IfStmtClass: {
765 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek31061982009-03-31 23:00:32 +0000767 if (const Stmt *S = End.asStmt())
768 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek31061982009-03-31 23:00:32 +0000770 if (*(Src->succ_begin()+1) == Dst)
771 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000772 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000773 else
Ted Kremenek31061982009-03-31 23:00:32 +0000774 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000775 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek31061982009-03-31 23:00:32 +0000777 break;
778 }
779 }
780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000782 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000783 // Add diagnostic pieces from custom visitors.
784 BugReport *R = PDB.getBugReport();
785 for (BugReport::visitor_iterator I = R->visitor_begin(),
786 E = R->visitor_end(); I!=E; ++I) {
787 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000788 PD.push_front(p);
789 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenek9c378f72011-08-12 23:37:29 +0000792 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000793 // Scan the region bindings, and see if a "notable" symbol has a new
794 // lval binding.
795 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
796 PDB.getStateManager().iterBindings(N->getState(), SNS);
797 }
798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek14856d72009-04-06 23:06:54 +0000800 // After constructing the full PathDiagnostic, do a pass over it to compact
801 // PathDiagnosticPieces that occur within a macro.
802 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000803}
804
805//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000806// "Extensive" PathDiagnostic generation.
807//===----------------------------------------------------------------------===//
808
809static bool IsControlFlowExpr(const Stmt *S) {
810 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000812 if (!E)
813 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
815 E = E->IgnoreParenCasts();
816
John McCall56ca35d2011-02-17 10:25:35 +0000817 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000818 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000820 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
821 if (B->isLogicalOp())
822 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
824 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000825}
826
Ted Kremenek14856d72009-04-06 23:06:54 +0000827namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000828class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000829 bool IsDead;
830public:
831 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
832 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000833
834 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000835 bool isDead() const { return IsDead; }
836};
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000838class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000839 std::vector<ContextLocation> CLocs;
840 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000841 PathDiagnostic &PD;
842 PathDiagnosticBuilder &PDB;
843 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000845 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Ted Kremenek14856d72009-04-06 23:06:54 +0000847 bool containsLocation(const PathDiagnosticLocation &Container,
848 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Ted Kremenek14856d72009-04-06 23:06:54 +0000850 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Ted Kremenek9650cf32009-05-11 21:42:34 +0000852 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
853 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000854 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000855 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000856 while (1) {
857 // Adjust the location for some expressions that are best referenced
858 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000859 switch (S->getStmtClass()) {
860 default:
861 break;
862 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000863 case Stmt::GenericSelectionExprClass:
864 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000865 firstCharOnly = true;
866 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000867 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000868 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000869 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000870 firstCharOnly = true;
871 continue;
872 case Stmt::ChooseExprClass:
873 S = cast<ChooseExpr>(S)->getCond();
874 firstCharOnly = true;
875 continue;
876 case Stmt::BinaryOperatorClass:
877 S = cast<BinaryOperator>(S)->getLHS();
878 firstCharOnly = true;
879 continue;
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Ted Kremenek9650cf32009-05-11 21:42:34 +0000882 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Ted Kremenek9650cf32009-05-11 21:42:34 +0000885 if (S != Original)
Anna Zaks23803372011-09-20 16:23:37 +0000886 L = PathDiagnosticLocation(S, L.getManager(), PDB.getLocationContext());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Ted Kremenek9650cf32009-05-11 21:42:34 +0000889 if (firstCharOnly)
Anna Zaks1531bb02011-09-20 01:38:47 +0000890 L = PathDiagnosticLocation::createSingleLocation(L);
Ted Kremenek9650cf32009-05-11 21:42:34 +0000891
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000892 return L;
893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Ted Kremenek14856d72009-04-06 23:06:54 +0000895 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000896 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000897 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000898 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000899 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000900 CLocs.pop_back();
901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Ted Kremenek14856d72009-04-06 23:06:54 +0000903public:
904 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
905 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Ted Kremeneka301a672009-04-22 18:16:20 +0000907 // If the PathDiagnostic already has pieces, add the enclosing statement
908 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000909 if (!PD.empty()) {
910 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Ted Kremenek14856d72009-04-06 23:06:54 +0000912 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000913 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000914 }
915 }
916
917 ~EdgeBuilder() {
918 while (!CLocs.empty()) popLocation();
Anna Zaks0cd59482011-09-16 19:18:30 +0000919
Ted Kremeneka301a672009-04-22 18:16:20 +0000920 // Finally, add an initial edge from the start location of the first
921 // statement (if it doesn't already exist).
Anna Zaks0cd59482011-09-16 19:18:30 +0000922 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
923 PDB.getLocationContext(),
924 PDB.getSourceManager());
925 if (L.isValid())
926 rawAddEdge(L);
Ted Kremenek14856d72009-04-06 23:06:54 +0000927 }
928
Ted Kremenek5de4fdb2012-02-07 02:26:17 +0000929 void flushLocations() {
930 while (!CLocs.empty())
931 popLocation();
932 PrevLoc = PathDiagnosticLocation();
933 }
934
Ted Kremenek14856d72009-04-06 23:06:54 +0000935 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000937 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Ted Kremenek14856d72009-04-06 23:06:54 +0000939 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000940 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000941};
Ted Kremenek14856d72009-04-06 23:06:54 +0000942} // end anonymous namespace
943
944
945PathDiagnosticLocation
946EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
947 if (const Stmt *S = L.asStmt()) {
948 if (IsControlFlowExpr(S))
949 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
951 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000952 }
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek14856d72009-04-06 23:06:54 +0000954 return L;
955}
956
957bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
958 const PathDiagnosticLocation &Containee) {
959
960 if (Container == Containee)
961 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Ted Kremenek14856d72009-04-06 23:06:54 +0000963 if (Container.asDecl())
964 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Ted Kremenek14856d72009-04-06 23:06:54 +0000966 if (const Stmt *S = Containee.asStmt())
967 if (const Stmt *ContainerS = Container.asStmt()) {
968 while (S) {
969 if (S == ContainerS)
970 return true;
971 S = PDB.getParent(S);
972 }
973 return false;
974 }
975
976 // Less accurate: compare using source ranges.
977 SourceRange ContainerR = Container.asRange();
978 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Ted Kremenek14856d72009-04-06 23:06:54 +0000980 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000981 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
982 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
983 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
984 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chandler Carruth64211622011-07-25 21:09:52 +0000986 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
987 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
988 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
989 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Ted Kremenek14856d72009-04-06 23:06:54 +0000991 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000992 assert(ContaineeBegLine <= ContaineeEndLine);
993
Ted Kremenek14856d72009-04-06 23:06:54 +0000994 return (ContainerBegLine <= ContaineeBegLine &&
995 ContainerEndLine >= ContaineeEndLine &&
996 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000997 SM.getExpansionColumnNumber(ContainerRBeg) <=
998 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000999 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +00001000 SM.getExpansionColumnNumber(ContainerREnd) >=
1001 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +00001002}
1003
Ted Kremenek14856d72009-04-06 23:06:54 +00001004void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1005 if (!PrevLoc.isValid()) {
1006 PrevLoc = NewLoc;
1007 return;
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001010 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1011 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001013 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001014 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Ted Kremenek14856d72009-04-06 23:06:54 +00001016 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001017 if (NewLocClean.asLocation().getExpansionLoc() ==
1018 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001019 return;
1020
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001021 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1022 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001023}
1024
1025void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Ted Kremeneka301a672009-04-22 18:16:20 +00001027 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1028 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremenek14856d72009-04-06 23:06:54 +00001030 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1031
1032 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001033 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Ted Kremenek14856d72009-04-06 23:06:54 +00001035 // Is the top location context the same as the one for the new location?
1036 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001037 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001038 if (IsConsumedExpr(TopContextLoc) &&
1039 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001040 TopContextLoc.markDead();
1041
Ted Kremenek14856d72009-04-06 23:06:54 +00001042 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001043 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001044
1045 return;
1046 }
1047
1048 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001049 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001050 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001052 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001053 CLocs.push_back(ContextLocation(CLoc, true));
1054 return;
1055 }
1056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Ted Kremenek14856d72009-04-06 23:06:54 +00001058 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001059 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001060 }
1061
1062 // Context does not contain the location. Flush it.
1063 popLocation();
1064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001066 // If we reach here, there is no enclosing context. Just add the edge.
1067 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001068}
1069
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001070bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1071 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1072 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001074 return false;
1075}
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Ted Kremeneke1baed32009-05-05 23:13:38 +00001077void EdgeBuilder::addExtendedContext(const Stmt *S) {
1078 if (!S)
1079 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001080
1081 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001082 while (Parent) {
1083 if (isa<CompoundStmt>(Parent))
1084 Parent = PDB.getParent(Parent);
1085 else
1086 break;
1087 }
1088
1089 if (Parent) {
1090 switch (Parent->getStmtClass()) {
1091 case Stmt::DoStmtClass:
1092 case Stmt::ObjCAtSynchronizedStmtClass:
1093 addContext(Parent);
1094 default:
1095 break;
1096 }
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Ted Kremeneke1baed32009-05-05 23:13:38 +00001099 addContext(S);
1100}
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremenek14856d72009-04-06 23:06:54 +00001102void EdgeBuilder::addContext(const Stmt *S) {
1103 if (!S)
1104 return;
1105
Anna Zaks220ac8c2011-09-15 01:08:34 +00001106 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Ted Kremenek14856d72009-04-06 23:06:54 +00001108 while (!CLocs.empty()) {
1109 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1110
1111 // Is the top location context the same as the one for the new location?
1112 if (TopContextLoc == L)
1113 return;
1114
1115 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001116 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001117 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001118 }
1119
1120 // Context does not contain the location. Flush it.
1121 popLocation();
1122 }
1123
1124 CLocs.push_back(L);
1125}
1126
1127static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1128 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001129 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001130 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001131 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001132
Ted Kremenek9c378f72011-08-12 23:37:29 +00001133 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001134 while (NextNode) {
1135 N = NextNode;
1136 NextNode = GetPredecessorNode(N);
1137 ProgramPoint P = N->getLocation();
1138
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001139 do {
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001140 if (const CallExit *CE = dyn_cast<CallExit>(&P)) {
1141 const StackFrameContext *LCtx =
1142 CE->getLocationContext()->getCurrentStackFrame();
1143 PathDiagnosticLocation Loc(LCtx->getCallSite(),
1144 PDB.getSourceManager(),
1145 LCtx);
1146 EB.addEdge(Loc, true);
1147 EB.flushLocations();
1148 break;
1149 }
1150
1151 // Was the predecessor in a different stack frame?
1152 if (NextNode &&
1153 !isa<CallExit>(NextNode->getLocation()) &&
1154 NextNode->getLocationContext()->getCurrentStackFrame() !=
1155 N->getLocationContext()->getCurrentStackFrame()) {
1156 EB.flushLocations();
1157 }
1158
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001159 // Block edges.
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001160 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001161 const CFGBlock &Blk = *BE->getSrc();
1162 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001164 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001165 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001166 PathDiagnosticLocation L(Loop, SM, PDB.getLocationContext());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001167 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001169 if (!Term) {
1170 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1171 CS = dyn_cast<CompoundStmt>(FS->getBody());
1172 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001173 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001174 }
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001176 PathDiagnosticEventPiece *p =
1177 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001178 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001180 EB.addEdge(p->getLocation(), true);
1181 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001183 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001184 PathDiagnosticLocation BL =
1185 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001186 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001187 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001188 }
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001190 if (Term)
1191 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001193 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001194 }
1195
Mike Stump1eb44332009-09-09 15:08:12 +00001196 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001197 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1198 const Stmt *stmt = S->getStmt();
1199 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001200 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001201 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001202 }
1203 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001204 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001205 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001206
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001207 break;
1208 }
Ted Kremenek5de4fdb2012-02-07 02:26:17 +00001209
1210
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001211 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001213 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001214 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Anna Zaks8e6431a2011-08-18 22:37:56 +00001216 // Add pieces from custom visitors.
1217 BugReport *R = PDB.getBugReport();
1218 for (BugReport::visitor_iterator I = R->visitor_begin(),
1219 E = R->visitor_end(); I!=E; ++I) {
1220 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001221 const PathDiagnosticLocation &Loc = p->getLocation();
1222 EB.addEdge(Loc, true);
1223 PD.push_front(p);
1224 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001225 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001226 }
Mike Stump1eb44332009-09-09 15:08:12 +00001227 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001228 }
1229}
1230
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001231//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001232// Methods for BugType and subclasses.
1233//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001234BugType::~BugType() { }
1235
Ted Kremenekcf118d42009-02-04 23:49:09 +00001236void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001237
David Blaikie99ba9e32011-12-20 02:48:34 +00001238void BuiltinBug::anchor() {}
1239
Ted Kremenekcf118d42009-02-04 23:49:09 +00001240//===----------------------------------------------------------------------===//
1241// Methods for BugReport and subclasses.
1242//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001243
David Blaikie99ba9e32011-12-20 02:48:34 +00001244void BugReport::NodeResolver::anchor() {}
1245
Anna Zaks8e6431a2011-08-18 22:37:56 +00001246void BugReport::addVisitor(BugReporterVisitor* visitor) {
1247 if (!visitor)
1248 return;
1249
1250 llvm::FoldingSetNodeID ID;
1251 visitor->Profile(ID);
1252 void *InsertPos;
1253
1254 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1255 delete visitor;
1256 return;
1257 }
1258
1259 CallbacksSet.InsertNode(visitor, InsertPos);
1260 Callbacks = F.add(visitor, Callbacks);
1261}
1262
1263BugReport::~BugReport() {
1264 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001265 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001266 }
1267}
Anna Zakse172e8b2011-08-17 23:00:25 +00001268
1269void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1270 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001271 hash.AddString(Description);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001272 if (Location.isValid()) {
1273 Location.Profile(hash);
1274 } else {
1275 assert(ErrorNode);
1276 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1277 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001278
1279 for (SmallVectorImpl<SourceRange>::const_iterator I =
1280 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1281 const SourceRange range = *I;
1282 if (!range.isValid())
1283 continue;
1284 hash.AddInteger(range.getBegin().getRawEncoding());
1285 hash.AddInteger(range.getEnd().getRawEncoding());
1286 }
1287}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001288
Ted Kremenek9c378f72011-08-12 23:37:29 +00001289const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001290 if (!ErrorNode)
1291 return 0;
1292
Tom Care212f6d32010-09-16 03:50:38 +00001293 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001294 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Ted Kremenek9c378f72011-08-12 23:37:29 +00001296 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001297 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001298 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001299 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001300 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001301 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001302 S = GetStmt(ProgP);
1303
1304 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001305}
1306
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001307std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001308BugReport::getRanges() {
1309 // If no custom ranges, add the range of the statement corresponding to
1310 // the error node.
1311 if (Ranges.empty()) {
1312 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1313 addRange(E->getSourceRange());
1314 else
1315 return std::make_pair(ranges_iterator(), ranges_iterator());
1316 }
1317
Anna Zaks14924262011-08-24 20:31:06 +00001318 // User-specified absence of range info.
1319 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1320 return std::make_pair(ranges_iterator(), ranges_iterator());
1321
Anna Zakse172e8b2011-08-17 23:00:25 +00001322 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001323}
1324
Anna Zaks590dd8e2011-09-20 21:38:35 +00001325PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001326 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001327 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001328 "Either Location or ErrorNode should be specified but not both.");
1329
Ted Kremenek9c378f72011-08-12 23:37:29 +00001330 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001331 const LocationContext *LC = ErrorNode->getLocationContext();
1332
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001333 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001334 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001335 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001336 // For binary operators, return the location of the operator.
1337 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001338 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001339
Anna Zaks590dd8e2011-09-20 21:38:35 +00001340 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001341 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001342 } else {
1343 assert(Location.isValid());
1344 return Location;
1345 }
1346
Anna Zaks590dd8e2011-09-20 21:38:35 +00001347 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001348}
1349
Ted Kremenekcf118d42009-02-04 23:49:09 +00001350//===----------------------------------------------------------------------===//
1351// Methods for BugReporter and subclasses.
1352//===----------------------------------------------------------------------===//
1353
1354BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001355 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001356}
1357
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001358GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001359BugReporterData::~BugReporterData() {}
1360
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001361ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001362
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001363ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001364GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1365
Anna Zaks3b030a22011-08-19 01:57:09 +00001366BugReporter::~BugReporter() {
1367 FlushReports();
1368
1369 // Free the bug reports we are tracking.
1370 typedef std::vector<BugReportEquivClass *> ContTy;
1371 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1372 I != E; ++I) {
1373 delete *I;
1374 }
1375}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376
1377void BugReporter::FlushReports() {
1378 if (BugTypes.isEmpty())
1379 return;
1380
1381 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001382 // warnings and new BugTypes.
1383 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1384 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001385 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001386 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001387 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001388 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001389 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001390 const_cast<BugType*>(*I)->FlushReports(*this);
1391
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001392 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1393 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1394 BugReportEquivClass& EQ = *EI;
1395 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001396 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001397
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001398 // BugReporter owns and deletes only BugTypes created implicitly through
1399 // EmitBasicReport.
1400 // FIXME: There are leaks from checkers that assume that the BugTypes they
1401 // create will be destroyed by the BugReporter.
1402 for (llvm::StringMap<BugType*>::iterator
1403 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1404 delete I->second;
1405
Ted Kremenekcf118d42009-02-04 23:49:09 +00001406 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001407 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001408}
1409
1410//===----------------------------------------------------------------------===//
1411// PathDiagnostics generation.
1412//===----------------------------------------------------------------------===//
1413
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001414static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001415 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001416MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001417 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Ted Kremenekcf118d42009-02-04 23:49:09 +00001419 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001420 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001421 // error node unless there are two or more error nodes with the same minimum
1422 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001423 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001424 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001425
1426 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001427 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1428 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenekcf118d42009-02-04 23:49:09 +00001430 // Create owning pointers for GTrim and NMap just to ensure that they are
1431 // released when this function exists.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001432 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
1433 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenekcf118d42009-02-04 23:49:09 +00001435 // Find the (first) error node in the trimmed graph. We just need to consult
1436 // the node map (NMap) which maps from nodes in the original graph to nodes
1437 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001438
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001439 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001440 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001441 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001442
Ted Kremenek40406fe2010-12-03 06:52:30 +00001443 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1444 const ExplodedNode *originalNode = nodes[nodeIndex];
1445 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001446 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001447 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001448 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Ted Kremenek938332c2009-05-16 01:11:58 +00001451 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001452
1453 // Create a new (third!) graph with a single path. This is the graph
1454 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001455 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek10aa5542009-03-12 23:41:59 +00001457 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001458 // to the root node, and then construct a new graph that contains only
1459 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001460 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001462 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001463 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001466 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001467 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001469 if (Visited.find(Node) != Visited.end())
1470 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001474 if (Node->pred_empty()) {
1475 Root = Node;
1476 break;
1477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001479 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001480 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001481 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek938332c2009-05-16 01:11:58 +00001484 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek10aa5542009-03-12 23:41:59 +00001486 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001487 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001488 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001489 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001490 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001492 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001493 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001494 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001495 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001497 // Create the equivalent node in the new graph with the same state
1498 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001499 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001501 // Store the mapping to the original node.
1502 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1503 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001504 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001506 // Link up the new node with the previous node.
1507 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001508 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001510 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001512 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001513 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001514 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001515 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001516 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001517 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001518 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001521 // Find the next successor node. We choose the node that is marked
1522 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001523 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1524 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001525 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001527 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001529 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001531 if (I == Visited.end())
1532 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001534 if (!N || I->second < MinVal) {
1535 N = *SI;
1536 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001537 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Ted Kremenek938332c2009-05-16 01:11:58 +00001540 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001541 }
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Ted Kremenek938332c2009-05-16 01:11:58 +00001543 assert(First);
1544
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001545 return std::make_pair(std::make_pair(GNew, BM),
1546 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001547}
1548
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001549/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1550/// and collapses PathDiagosticPieces that are expanded by macros.
1551static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1552 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1553 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001555 typedef std::vector<PathDiagnosticPiece*>
1556 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001558 MacroStackTy MacroStack;
1559 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001561 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1562 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001563 const FullSourceLoc Loc = I->getLocation().asLocation();
1564
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001565 // Determine the instantiation location, which is the location we group
1566 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001567 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001568 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001569 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001571 if (Loc.isFileID()) {
1572 MacroStack.clear();
1573 Pieces.push_back(&*I);
1574 continue;
1575 }
1576
1577 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001579 // Is the PathDiagnosticPiece within the same macro group?
1580 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1581 MacroStack.back().first->push_back(&*I);
1582 continue;
1583 }
1584
1585 // We aren't in the same group. Are we descending into a new macro
1586 // or are part of an old one?
1587 PathDiagnosticMacroPiece *MacroGroup = 0;
1588
1589 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001590 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001591 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001593 // Walk the entire macro stack.
1594 while (!MacroStack.empty()) {
1595 if (InstantiationLoc == MacroStack.back().second) {
1596 MacroGroup = MacroStack.back().first;
1597 break;
1598 }
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001600 if (ParentInstantiationLoc == MacroStack.back().second) {
1601 MacroGroup = MacroStack.back().first;
1602 break;
1603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001605 MacroStack.pop_back();
1606 }
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001608 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1609 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001610 PathDiagnosticMacroPiece *NewGroup =
1611 new PathDiagnosticMacroPiece(
1612 PathDiagnosticLocation::createSingleLocation(I->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001613
1614 if (MacroGroup)
1615 MacroGroup->push_back(NewGroup);
1616 else {
1617 assert(InstantiationLoc.isFileID());
1618 Pieces.push_back(NewGroup);
1619 }
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001621 MacroGroup = NewGroup;
1622 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1623 }
1624
1625 // Finally, add the PathDiagnosticPiece to the group.
1626 MacroGroup->push_back(&*I);
1627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001629 // Now take the pieces and construct a new PathDiagnostic.
1630 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001632 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1633 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1634 if (!MP->containsEvent()) {
1635 delete MP;
1636 continue;
1637 }
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001639 PD.push_back(*I);
1640 }
1641}
1642
Ted Kremenek7dc86642009-03-31 20:22:36 +00001643void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001644 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Ted Kremenek40406fe2010-12-03 06:52:30 +00001646 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001647 SmallVector<const ExplodedNode *, 10> errorNodes;
1648 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001649 E = bugReports.end(); I != E; ++I) {
1650 errorNodes.push_back((*I)->getErrorNode());
1651 }
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001653 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001654 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001655 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001656 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001657 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenekcf118d42009-02-04 23:49:09 +00001659 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001660 assert(GPair.second.second < bugReports.size());
1661 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001662 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001664 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
1665 OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001666 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001667
1668 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001669 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1670 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Anna Zaks8e6431a2011-08-18 22:37:56 +00001672 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001673 R->addVisitor(new NilReceiverBRVisitor());
1674 R->addVisitor(new ConditionBRVisitor());
Ted Kremenek0cf3d472012-02-07 00:24:33 +00001675
1676 // If inlining is turning out, emit diagnostics for CallEnter and
1677 // CallExit at the top level.
1678 bool showTopLevel = Eng.getAnalysisManager().shouldInlineCall();
1679 R->addVisitor(new CallEnterExitBRVisitor(showTopLevel));
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Anna Zaks23f395e2011-08-20 01:27:22 +00001681 // Generate the very last diagnostic piece - the piece is visible before
1682 // the trace is expanded.
1683 PathDiagnosticPiece *LastPiece = 0;
1684 for (BugReport::visitor_iterator I = R->visitor_begin(),
1685 E = R->visitor_end(); I!=E; ++I) {
1686 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1687 assert (!LastPiece &&
1688 "There can only be one final piece in a diagnostic.");
1689 LastPiece = Piece;
1690 }
1691 }
1692 if (!LastPiece)
1693 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1694 if (LastPiece)
1695 PD.push_back(LastPiece);
1696 else
1697 return;
1698
Ted Kremenek7dc86642009-03-31 20:22:36 +00001699 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001700 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001701 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001702 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001703 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001704 GenerateMinimalPathDiagnostic(PD, PDB, N);
1705 break;
1706 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001707}
1708
Ted Kremenekcf118d42009-02-04 23:49:09 +00001709void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001710 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001711}
1712
Mike Stump1eb44332009-09-09 15:08:12 +00001713void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001714 // Compute the bug report's hash to determine its equivalence class.
1715 llvm::FoldingSetNodeID ID;
1716 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001717
1718 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001719 BugType& BT = R->getBugType();
1720 Register(&BT);
1721 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001722 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Ted Kremenekcf118d42009-02-04 23:49:09 +00001724 if (!EQ) {
1725 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001726 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001727 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001728 }
1729 else
1730 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001731}
1732
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001733
1734//===----------------------------------------------------------------------===//
1735// Emitting reports in equivalence classes.
1736//===----------------------------------------------------------------------===//
1737
1738namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001739struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001740 const ExplodedNode *N;
1741 ExplodedNode::const_succ_iterator I, E;
1742
1743 FRIEC_WLItem(const ExplodedNode *n)
1744 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1745};
1746}
1747
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001748static BugReport *
1749FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001750 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001751
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001752 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1753 assert(I != E);
1754 BugReport *R = *I;
1755 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001756
Ted Kremenek40406fe2010-12-03 06:52:30 +00001757 // If we don't need to suppress any of the nodes because they are
1758 // post-dominated by a sink, simply add all the nodes in the equivalence class
1759 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001760 if (!BT.isSuppressOnSink()) {
1761 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001762 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001763 if (N) {
1764 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001765 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001766 }
1767 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001768 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001769 }
1770
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001771 // For bug reports that should be suppressed when all paths are post-dominated
1772 // by a sink node, iterate through the reports in the equivalence class
1773 // until we find one that isn't post-dominated (if one exists). We use a
1774 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1775 // this as a recursive function, but we don't want to risk blowing out the
1776 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001777 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001778
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001779 for (; I != E; ++I) {
1780 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001781 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001782
Ted Kremenek40406fe2010-12-03 06:52:30 +00001783 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001784 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001785 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001786 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001787 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001788 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001789 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001790 if (errorNode->succ_empty()) {
1791 bugReports.push_back(R);
1792 if (!exampleReport)
1793 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001794 continue;
1795 }
1796
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001797 // At this point we know that 'N' is not a sink and it has at least one
1798 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1799 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001800 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001801 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1802
1803 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001804 WL.push_back(errorNode);
1805 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001806
1807 while (!WL.empty()) {
1808 WLItem &WI = WL.back();
1809 assert(!WI.N->succ_empty());
1810
1811 for (; WI.I != WI.E; ++WI.I) {
1812 const ExplodedNode *Succ = *WI.I;
1813 // End-of-path node?
1814 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001815 // If we found an end-of-path node that is not a sink.
1816 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001817 bugReports.push_back(R);
1818 if (!exampleReport)
1819 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001820 WL.clear();
1821 break;
1822 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001823 // Found a sink? Continue on to the next successor.
1824 continue;
1825 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001826 // Mark the successor as visited. If it hasn't been explored,
1827 // enqueue it to the DFS worklist.
1828 unsigned &mark = Visited[Succ];
1829 if (!mark) {
1830 mark = 1;
1831 WL.push_back(Succ);
1832 break;
1833 }
1834 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001835
1836 // The worklist may have been cleared at this point. First
1837 // check if it is empty before checking the last item.
1838 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001839 WL.pop_back();
1840 }
1841 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001842
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001843 // ExampleReport will be NULL if all the nodes in the equivalence class
1844 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001845 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001846}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001847
1848//===----------------------------------------------------------------------===//
1849// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1850// uses global state, which eventually should go elsewhere.
1851//===----------------------------------------------------------------------===//
1852namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001853class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001854 llvm::FoldingSetNodeID ID;
1855public:
1856 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001857 R->Profile(ID);
1858 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001859 }
1860
1861 void Profile(llvm::FoldingSetNodeID &id) {
1862 id = ID;
1863 }
1864
1865 llvm::FoldingSetNodeID &getID() { return ID; }
1866};
1867}
1868
1869static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1870 // FIXME: Eventually this diagnostic cache should reside in something
1871 // like AnalysisManager instead of being a static variable. This is
1872 // really unsafe in the long term.
1873 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1874 static DiagnosticCache DC;
1875
1876 void *InsertPos;
1877 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1878
1879 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1880 delete Item;
1881 return true;
1882 }
1883
1884 DC.InsertNode(Item, InsertPos);
1885 return false;
1886}
1887
Ted Kremenekcf118d42009-02-04 23:49:09 +00001888void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001889 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001890 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1891 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001892 return;
1893
David Blaikieef3643f2011-09-26 00:51:36 +00001894 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Ted Kremenekcf118d42009-02-04 23:49:09 +00001896 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001897 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001898 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001900 OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001901 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001902 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001903 ? exampleReport->getDescription()
1904 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001905 BT.getCategory()));
1906
Ted Kremenek40406fe2010-12-03 06:52:30 +00001907 if (!bugReports.empty())
1908 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Ted Kremenek40406fe2010-12-03 06:52:30 +00001910 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001911 return;
1912
Ted Kremenek072192b2008-04-30 23:47:44 +00001913 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001914 const BugReport::ExtraTextList &Meta =
1915 exampleReport->getExtraText();
1916 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1917 e = Meta.end(); i != e; ++i) {
1918 D->addMeta(*i);
1919 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001920
Ted Kremenek3148eb42009-01-24 00:55:43 +00001921 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001922 BugReport::ranges_iterator Beg, End;
1923 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001924 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001925
1926 // Search the description for '%', as that will be interpretted as a
1927 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001928 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001929 unsigned ErrorDiag;
1930 {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001931 SmallString<512> TmpStr;
Ted Kremenekc213b482010-01-15 07:56:51 +00001932 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001933 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001934 if (*I == '%')
1935 Out << "%%";
1936 else
1937 Out << *I;
1938
1939 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001940 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001941 }
Ted Kremenek57202072008-07-14 17:40:50 +00001942
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001943 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001944 DiagnosticBuilder diagBuilder = Diag.Report(
1945 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001946 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001947 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001948 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001949
David Blaikieef3643f2011-09-26 00:51:36 +00001950 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001951 if (!PD)
1952 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001953
1954 if (D->empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001955 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1956 exampleReport->getLocation(getSourceManager()),
1957 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001958
Ted Kremenek3148eb42009-01-24 00:55:43 +00001959 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1960 D->push_back(piece);
1961 }
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Ted Kremenek3148eb42009-01-24 00:55:43 +00001963 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001964}
Ted Kremenek57202072008-07-14 17:40:50 +00001965
Chris Lattner5f9e2722011-07-23 10:55:15 +00001966void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001967 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001968 SourceRange* RBeg, unsigned NumRanges) {
1969 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1970}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001971
Chris Lattner5f9e2722011-07-23 10:55:15 +00001972void BugReporter::EmitBasicReport(StringRef name,
1973 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001974 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001975 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001977 // 'BT' is owned by BugReporter.
1978 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001979 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001980 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1981 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001982}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001983
Chris Lattner5f9e2722011-07-23 10:55:15 +00001984BugType *BugReporter::getBugTypeForName(StringRef name,
1985 StringRef category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001986 SmallString<136> fullDesc;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001987 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1988 llvm::StringMapEntry<BugType *> &
1989 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1990 BugType *BT = entry.getValue();
1991 if (!BT) {
1992 BT = new BugType(name, category);
1993 entry.setValue(BT);
1994 }
1995 return BT;
1996}