blob: b10a62ca4950ff66ca8cdfcb05928dfdc85ef03c [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"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000024#include "clang/Basic/Diagnostic.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000026#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000027#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000028#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000029#include "llvm/ADT/DenseMap.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;
Ted Kremenek00605e02009-03-27 20:55:39 +0000135 llvm::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())) {
Jordy Rose7df12342011-08-21 05:25:15 +0000443 llvm::SmallString<64> buf;
444 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
929 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000931 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Ted Kremenek14856d72009-04-06 23:06:54 +0000933 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000934 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000935};
Ted Kremenek14856d72009-04-06 23:06:54 +0000936} // end anonymous namespace
937
938
939PathDiagnosticLocation
940EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
941 if (const Stmt *S = L.asStmt()) {
942 if (IsControlFlowExpr(S))
943 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000944
945 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Ted Kremenek14856d72009-04-06 23:06:54 +0000948 return L;
949}
950
951bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
952 const PathDiagnosticLocation &Containee) {
953
954 if (Container == Containee)
955 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Ted Kremenek14856d72009-04-06 23:06:54 +0000957 if (Container.asDecl())
958 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek14856d72009-04-06 23:06:54 +0000960 if (const Stmt *S = Containee.asStmt())
961 if (const Stmt *ContainerS = Container.asStmt()) {
962 while (S) {
963 if (S == ContainerS)
964 return true;
965 S = PDB.getParent(S);
966 }
967 return false;
968 }
969
970 // Less accurate: compare using source ranges.
971 SourceRange ContainerR = Container.asRange();
972 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Ted Kremenek14856d72009-04-06 23:06:54 +0000974 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000975 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
976 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
977 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
978 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chandler Carruth64211622011-07-25 21:09:52 +0000980 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
981 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
982 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
983 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremenek14856d72009-04-06 23:06:54 +0000985 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000986 assert(ContaineeBegLine <= ContaineeEndLine);
987
Ted Kremenek14856d72009-04-06 23:06:54 +0000988 return (ContainerBegLine <= ContaineeBegLine &&
989 ContainerEndLine >= ContaineeEndLine &&
990 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000991 SM.getExpansionColumnNumber(ContainerRBeg) <=
992 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000993 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000994 SM.getExpansionColumnNumber(ContainerREnd) >=
995 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000996}
997
Ted Kremenek14856d72009-04-06 23:06:54 +0000998void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
999 if (!PrevLoc.isValid()) {
1000 PrevLoc = NewLoc;
1001 return;
1002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001004 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
1005 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001007 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001008 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Ted Kremenek14856d72009-04-06 23:06:54 +00001010 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001011 if (NewLocClean.asLocation().getExpansionLoc() ==
1012 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001013 return;
1014
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001015 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1016 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001017}
1018
1019void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Ted Kremeneka301a672009-04-22 18:16:20 +00001021 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1022 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Ted Kremenek14856d72009-04-06 23:06:54 +00001024 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1025
1026 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001027 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek14856d72009-04-06 23:06:54 +00001029 // Is the top location context the same as the one for the new location?
1030 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001031 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001032 if (IsConsumedExpr(TopContextLoc) &&
1033 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001034 TopContextLoc.markDead();
1035
Ted Kremenek14856d72009-04-06 23:06:54 +00001036 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001037 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001038
1039 return;
1040 }
1041
1042 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001043 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001044 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001046 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001047 CLocs.push_back(ContextLocation(CLoc, true));
1048 return;
1049 }
1050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenek14856d72009-04-06 23:06:54 +00001052 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001053 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001054 }
1055
1056 // Context does not contain the location. Flush it.
1057 popLocation();
1058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001060 // If we reach here, there is no enclosing context. Just add the edge.
1061 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001062}
1063
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001064bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1065 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1066 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001068 return false;
1069}
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremeneke1baed32009-05-05 23:13:38 +00001071void EdgeBuilder::addExtendedContext(const Stmt *S) {
1072 if (!S)
1073 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001074
1075 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001076 while (Parent) {
1077 if (isa<CompoundStmt>(Parent))
1078 Parent = PDB.getParent(Parent);
1079 else
1080 break;
1081 }
1082
1083 if (Parent) {
1084 switch (Parent->getStmtClass()) {
1085 case Stmt::DoStmtClass:
1086 case Stmt::ObjCAtSynchronizedStmtClass:
1087 addContext(Parent);
1088 default:
1089 break;
1090 }
1091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Ted Kremeneke1baed32009-05-05 23:13:38 +00001093 addContext(S);
1094}
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Ted Kremenek14856d72009-04-06 23:06:54 +00001096void EdgeBuilder::addContext(const Stmt *S) {
1097 if (!S)
1098 return;
1099
Anna Zaks220ac8c2011-09-15 01:08:34 +00001100 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremenek14856d72009-04-06 23:06:54 +00001102 while (!CLocs.empty()) {
1103 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1104
1105 // Is the top location context the same as the one for the new location?
1106 if (TopContextLoc == L)
1107 return;
1108
1109 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001110 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001111 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001112 }
1113
1114 // Context does not contain the location. Flush it.
1115 popLocation();
1116 }
1117
1118 CLocs.push_back(L);
1119}
1120
1121static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1122 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001123 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001124 EdgeBuilder EB(PD, PDB);
Anna Zaks0cd59482011-09-16 19:18:30 +00001125 const SourceManager& SM = PDB.getSourceManager();
Ted Kremenek14856d72009-04-06 23:06:54 +00001126
Ted Kremenek9c378f72011-08-12 23:37:29 +00001127 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001128 while (NextNode) {
1129 N = NextNode;
1130 NextNode = GetPredecessorNode(N);
1131 ProgramPoint P = N->getLocation();
1132
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001133 do {
1134 // Block edges.
1135 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1136 const CFGBlock &Blk = *BE->getSrc();
1137 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001139 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001140 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001141 PathDiagnosticLocation L(Loop, SM, PDB.getLocationContext());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001142 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001144 if (!Term) {
1145 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1146 CS = dyn_cast<CompoundStmt>(FS->getBody());
1147 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001148 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001151 PathDiagnosticEventPiece *p =
1152 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001153 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001155 EB.addEdge(p->getLocation(), true);
1156 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001158 if (CS) {
Anna Zaks0cd59482011-09-16 19:18:30 +00001159 PathDiagnosticLocation BL =
1160 PathDiagnosticLocation::createEndBrace(CS, SM);
Ted Kremenek07c015c2009-05-15 02:46:13 +00001161 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001162 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001165 if (Term)
1166 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001168 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001169 }
1170
Mike Stump1eb44332009-09-09 15:08:12 +00001171 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001172 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1173 const Stmt *stmt = S->getStmt();
1174 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001175 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001176 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001177 }
1178 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001179 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001180 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001181
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001182 break;
1183 }
1184 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001186 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001187 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Anna Zaks8e6431a2011-08-18 22:37:56 +00001189 // Add pieces from custom visitors.
1190 BugReport *R = PDB.getBugReport();
1191 for (BugReport::visitor_iterator I = R->visitor_begin(),
1192 E = R->visitor_end(); I!=E; ++I) {
1193 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001194 const PathDiagnosticLocation &Loc = p->getLocation();
1195 EB.addEdge(Loc, true);
1196 PD.push_front(p);
1197 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001198 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001201 }
1202}
1203
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001204//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001205// Methods for BugType and subclasses.
1206//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001207BugType::~BugType() { }
1208
Ted Kremenekcf118d42009-02-04 23:49:09 +00001209void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001210
David Blaikie99ba9e32011-12-20 02:48:34 +00001211void BuiltinBug::anchor() {}
1212
Ted Kremenekcf118d42009-02-04 23:49:09 +00001213//===----------------------------------------------------------------------===//
1214// Methods for BugReport and subclasses.
1215//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001216
David Blaikie99ba9e32011-12-20 02:48:34 +00001217void BugReport::NodeResolver::anchor() {}
1218
Anna Zaks8e6431a2011-08-18 22:37:56 +00001219void BugReport::addVisitor(BugReporterVisitor* visitor) {
1220 if (!visitor)
1221 return;
1222
1223 llvm::FoldingSetNodeID ID;
1224 visitor->Profile(ID);
1225 void *InsertPos;
1226
1227 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1228 delete visitor;
1229 return;
1230 }
1231
1232 CallbacksSet.InsertNode(visitor, InsertPos);
1233 Callbacks = F.add(visitor, Callbacks);
1234}
1235
1236BugReport::~BugReport() {
1237 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001238 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001239 }
1240}
Anna Zakse172e8b2011-08-17 23:00:25 +00001241
1242void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1243 hash.AddPointer(&BT);
Anna Zakse172e8b2011-08-17 23:00:25 +00001244 hash.AddString(Description);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001245 if (Location.isValid()) {
1246 Location.Profile(hash);
1247 } else {
1248 assert(ErrorNode);
1249 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
1250 }
Anna Zakse172e8b2011-08-17 23:00:25 +00001251
1252 for (SmallVectorImpl<SourceRange>::const_iterator I =
1253 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1254 const SourceRange range = *I;
1255 if (!range.isValid())
1256 continue;
1257 hash.AddInteger(range.getBegin().getRawEncoding());
1258 hash.AddInteger(range.getEnd().getRawEncoding());
1259 }
1260}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001261
Ted Kremenek9c378f72011-08-12 23:37:29 +00001262const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001263 if (!ErrorNode)
1264 return 0;
1265
Tom Care212f6d32010-09-16 03:50:38 +00001266 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001267 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek9c378f72011-08-12 23:37:29 +00001269 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001270 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001271 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001272 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001273 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001274 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001275 S = GetStmt(ProgP);
1276
1277 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001278}
1279
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001280std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001281BugReport::getRanges() {
1282 // If no custom ranges, add the range of the statement corresponding to
1283 // the error node.
1284 if (Ranges.empty()) {
1285 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1286 addRange(E->getSourceRange());
1287 else
1288 return std::make_pair(ranges_iterator(), ranges_iterator());
1289 }
1290
Anna Zaks14924262011-08-24 20:31:06 +00001291 // User-specified absence of range info.
1292 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
1293 return std::make_pair(ranges_iterator(), ranges_iterator());
1294
Anna Zakse172e8b2011-08-17 23:00:25 +00001295 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001296}
1297
Anna Zaks590dd8e2011-09-20 21:38:35 +00001298PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001299 if (ErrorNode) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001300 assert(!Location.isValid() &&
Anna Zaksb7530a42011-08-17 23:21:23 +00001301 "Either Location or ErrorNode should be specified but not both.");
1302
Ted Kremenek9c378f72011-08-12 23:37:29 +00001303 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001304 const LocationContext *LC = ErrorNode->getLocationContext();
1305
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001306 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001307 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001308 return PathDiagnosticLocation::createMemberLoc(ME, SM);
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001309 // For binary operators, return the location of the operator.
1310 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
Anna Zaks590dd8e2011-09-20 21:38:35 +00001311 return PathDiagnosticLocation::createOperatorLoc(B, SM);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001312
Anna Zaks590dd8e2011-09-20 21:38:35 +00001313 return PathDiagnosticLocation::createBegin(S, SM, LC);
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001314 }
Anna Zaksb7530a42011-08-17 23:21:23 +00001315 } else {
1316 assert(Location.isValid());
1317 return Location;
1318 }
1319
Anna Zaks590dd8e2011-09-20 21:38:35 +00001320 return PathDiagnosticLocation();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001321}
1322
Ted Kremenekcf118d42009-02-04 23:49:09 +00001323//===----------------------------------------------------------------------===//
1324// Methods for BugReporter and subclasses.
1325//===----------------------------------------------------------------------===//
1326
1327BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001328 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001329}
1330
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001331GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001332BugReporterData::~BugReporterData() {}
1333
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001334ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001335
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001336ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001337GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1338
Anna Zaks3b030a22011-08-19 01:57:09 +00001339BugReporter::~BugReporter() {
1340 FlushReports();
1341
1342 // Free the bug reports we are tracking.
1343 typedef std::vector<BugReportEquivClass *> ContTy;
1344 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1345 I != E; ++I) {
1346 delete *I;
1347 }
1348}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001349
1350void BugReporter::FlushReports() {
1351 if (BugTypes.isEmpty())
1352 return;
1353
1354 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001355 // warnings and new BugTypes.
1356 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1357 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001358 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001359 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001360 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001361 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001362 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001363 const_cast<BugType*>(*I)->FlushReports(*this);
1364
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001365 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1366 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1367 BugReportEquivClass& EQ = *EI;
1368 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001369 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001370
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001371 // BugReporter owns and deletes only BugTypes created implicitly through
1372 // EmitBasicReport.
1373 // FIXME: There are leaks from checkers that assume that the BugTypes they
1374 // create will be destroyed by the BugReporter.
1375 for (llvm::StringMap<BugType*>::iterator
1376 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1377 delete I->second;
1378
Ted Kremenekcf118d42009-02-04 23:49:09 +00001379 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001380 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001381}
1382
1383//===----------------------------------------------------------------------===//
1384// PathDiagnostics generation.
1385//===----------------------------------------------------------------------===//
1386
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001387static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001388 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001389MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001390 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Ted Kremenekcf118d42009-02-04 23:49:09 +00001392 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001393 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001394 // error node unless there are two or more error nodes with the same minimum
1395 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001396 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001397 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001398
1399 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001400 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1401 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenekcf118d42009-02-04 23:49:09 +00001403 // Create owning pointers for GTrim and NMap just to ensure that they are
1404 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001405 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001406 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Ted Kremenekcf118d42009-02-04 23:49:09 +00001408 // Find the (first) error node in the trimmed graph. We just need to consult
1409 // the node map (NMap) which maps from nodes in the original graph to nodes
1410 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001411
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001412 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001413 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001414 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001415
Ted Kremenek40406fe2010-12-03 06:52:30 +00001416 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1417 const ExplodedNode *originalNode = nodes[nodeIndex];
1418 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001419 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001420 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001421 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001422 }
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Ted Kremenek938332c2009-05-16 01:11:58 +00001424 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001425
1426 // Create a new (third!) graph with a single path. This is the graph
1427 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001428 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenek10aa5542009-03-12 23:41:59 +00001430 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001431 // to the root node, and then construct a new graph that contains only
1432 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001433 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001435 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001436 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001438 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001439 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001440 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001442 if (Visited.find(Node) != Visited.end())
1443 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001445 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001447 if (Node->pred_empty()) {
1448 Root = Node;
1449 break;
1450 }
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001452 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001453 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001454 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek938332c2009-05-16 01:11:58 +00001457 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Ted Kremenek10aa5542009-03-12 23:41:59 +00001459 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001460 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001461 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001462 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001463 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001465 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001466 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001467 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001468 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001470 // Create the equivalent node in the new graph with the same state
1471 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001472 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001474 // Store the mapping to the original node.
1475 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1476 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001477 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001479 // Link up the new node with the previous node.
1480 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001481 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001483 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001485 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001486 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001487 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001488 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001489 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001490 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001491 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001492 }
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001494 // Find the next successor node. We choose the node that is marked
1495 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001496 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1497 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001498 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001500 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001502 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001504 if (I == Visited.end())
1505 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001507 if (!N || I->second < MinVal) {
1508 N = *SI;
1509 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001510 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001511 }
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Ted Kremenek938332c2009-05-16 01:11:58 +00001513 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001514 }
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Ted Kremenek938332c2009-05-16 01:11:58 +00001516 assert(First);
1517
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001518 return std::make_pair(std::make_pair(GNew, BM),
1519 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001520}
1521
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001522/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1523/// and collapses PathDiagosticPieces that are expanded by macros.
1524static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1525 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1526 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001528 typedef std::vector<PathDiagnosticPiece*>
1529 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001531 MacroStackTy MacroStack;
1532 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001534 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1535 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001536 const FullSourceLoc Loc = I->getLocation().asLocation();
1537
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001538 // Determine the instantiation location, which is the location we group
1539 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001540 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001541 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001542 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001544 if (Loc.isFileID()) {
1545 MacroStack.clear();
1546 Pieces.push_back(&*I);
1547 continue;
1548 }
1549
1550 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001552 // Is the PathDiagnosticPiece within the same macro group?
1553 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1554 MacroStack.back().first->push_back(&*I);
1555 continue;
1556 }
1557
1558 // We aren't in the same group. Are we descending into a new macro
1559 // or are part of an old one?
1560 PathDiagnosticMacroPiece *MacroGroup = 0;
1561
1562 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001563 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001564 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001566 // Walk the entire macro stack.
1567 while (!MacroStack.empty()) {
1568 if (InstantiationLoc == MacroStack.back().second) {
1569 MacroGroup = MacroStack.back().first;
1570 break;
1571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001573 if (ParentInstantiationLoc == MacroStack.back().second) {
1574 MacroGroup = MacroStack.back().first;
1575 break;
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001578 MacroStack.pop_back();
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001581 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1582 // Create a new macro group and add it to the stack.
Anna Zaks590dd8e2011-09-20 21:38:35 +00001583 PathDiagnosticMacroPiece *NewGroup =
1584 new PathDiagnosticMacroPiece(
1585 PathDiagnosticLocation::createSingleLocation(I->getLocation()));
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001586
1587 if (MacroGroup)
1588 MacroGroup->push_back(NewGroup);
1589 else {
1590 assert(InstantiationLoc.isFileID());
1591 Pieces.push_back(NewGroup);
1592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001594 MacroGroup = NewGroup;
1595 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1596 }
1597
1598 // Finally, add the PathDiagnosticPiece to the group.
1599 MacroGroup->push_back(&*I);
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001602 // Now take the pieces and construct a new PathDiagnostic.
1603 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001605 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1606 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1607 if (!MP->containsEvent()) {
1608 delete MP;
1609 continue;
1610 }
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001612 PD.push_back(*I);
1613 }
1614}
1615
Ted Kremenek7dc86642009-03-31 20:22:36 +00001616void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001617 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Ted Kremenek40406fe2010-12-03 06:52:30 +00001619 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001620 SmallVector<const ExplodedNode *, 10> errorNodes;
1621 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001622 E = bugReports.end(); I != E; ++I) {
1623 errorNodes.push_back((*I)->getErrorNode());
1624 }
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001626 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001627 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001628 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001629 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001630 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenekcf118d42009-02-04 23:49:09 +00001632 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001633 assert(GPair.second.second < bugReports.size());
1634 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001635 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001637 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001638 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001639 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001640
1641 // Start building the path diagnostic...
David Blaikieef3643f2011-09-26 00:51:36 +00001642 PathDiagnosticBuilder PDB(*this, R, BackMap.get(),
1643 getPathDiagnosticConsumer());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Anna Zaks8e6431a2011-08-18 22:37:56 +00001645 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001646 R->addVisitor(new NilReceiverBRVisitor());
1647 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Anna Zaks23f395e2011-08-20 01:27:22 +00001649 // Generate the very last diagnostic piece - the piece is visible before
1650 // the trace is expanded.
1651 PathDiagnosticPiece *LastPiece = 0;
1652 for (BugReport::visitor_iterator I = R->visitor_begin(),
1653 E = R->visitor_end(); I!=E; ++I) {
1654 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1655 assert (!LastPiece &&
1656 "There can only be one final piece in a diagnostic.");
1657 LastPiece = Piece;
1658 }
1659 }
1660 if (!LastPiece)
1661 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1662 if (LastPiece)
1663 PD.push_back(LastPiece);
1664 else
1665 return;
1666
Ted Kremenek7dc86642009-03-31 20:22:36 +00001667 switch (PDB.getGenerationScheme()) {
David Blaikieef3643f2011-09-26 00:51:36 +00001668 case PathDiagnosticConsumer::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001669 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001670 break;
David Blaikieef3643f2011-09-26 00:51:36 +00001671 case PathDiagnosticConsumer::Minimal:
Ted Kremenek7dc86642009-03-31 20:22:36 +00001672 GenerateMinimalPathDiagnostic(PD, PDB, N);
1673 break;
1674 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001675}
1676
Ted Kremenekcf118d42009-02-04 23:49:09 +00001677void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001678 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001679}
1680
Mike Stump1eb44332009-09-09 15:08:12 +00001681void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001682 // Compute the bug report's hash to determine its equivalence class.
1683 llvm::FoldingSetNodeID ID;
1684 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001685
1686 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001687 BugType& BT = R->getBugType();
1688 Register(&BT);
1689 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001690 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Ted Kremenekcf118d42009-02-04 23:49:09 +00001692 if (!EQ) {
1693 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001694 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001695 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001696 }
1697 else
1698 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001699}
1700
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001701
1702//===----------------------------------------------------------------------===//
1703// Emitting reports in equivalence classes.
1704//===----------------------------------------------------------------------===//
1705
1706namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001707struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001708 const ExplodedNode *N;
1709 ExplodedNode::const_succ_iterator I, E;
1710
1711 FRIEC_WLItem(const ExplodedNode *n)
1712 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1713};
1714}
1715
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001716static BugReport *
1717FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001718 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001719
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001720 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1721 assert(I != E);
1722 BugReport *R = *I;
1723 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001724
Ted Kremenek40406fe2010-12-03 06:52:30 +00001725 // If we don't need to suppress any of the nodes because they are
1726 // post-dominated by a sink, simply add all the nodes in the equivalence class
1727 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001728 if (!BT.isSuppressOnSink()) {
1729 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001730 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001731 if (N) {
1732 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001733 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001734 }
1735 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001736 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001737 }
1738
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001739 // For bug reports that should be suppressed when all paths are post-dominated
1740 // by a sink node, iterate through the reports in the equivalence class
1741 // until we find one that isn't post-dominated (if one exists). We use a
1742 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1743 // this as a recursive function, but we don't want to risk blowing out the
1744 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001745 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001746
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001747 for (; I != E; ++I) {
1748 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001749 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001750
Ted Kremenek40406fe2010-12-03 06:52:30 +00001751 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001752 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001753 if (errorNode->isSink()) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001754 llvm_unreachable(
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001755 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001756 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001757 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001758 if (errorNode->succ_empty()) {
1759 bugReports.push_back(R);
1760 if (!exampleReport)
1761 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001762 continue;
1763 }
1764
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001765 // At this point we know that 'N' is not a sink and it has at least one
1766 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1767 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001768 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001769 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1770
1771 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001772 WL.push_back(errorNode);
1773 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001774
1775 while (!WL.empty()) {
1776 WLItem &WI = WL.back();
1777 assert(!WI.N->succ_empty());
1778
1779 for (; WI.I != WI.E; ++WI.I) {
1780 const ExplodedNode *Succ = *WI.I;
1781 // End-of-path node?
1782 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001783 // If we found an end-of-path node that is not a sink.
1784 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001785 bugReports.push_back(R);
1786 if (!exampleReport)
1787 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001788 WL.clear();
1789 break;
1790 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001791 // Found a sink? Continue on to the next successor.
1792 continue;
1793 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001794 // Mark the successor as visited. If it hasn't been explored,
1795 // enqueue it to the DFS worklist.
1796 unsigned &mark = Visited[Succ];
1797 if (!mark) {
1798 mark = 1;
1799 WL.push_back(Succ);
1800 break;
1801 }
1802 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001803
1804 // The worklist may have been cleared at this point. First
1805 // check if it is empty before checking the last item.
1806 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001807 WL.pop_back();
1808 }
1809 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001810
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001811 // ExampleReport will be NULL if all the nodes in the equivalence class
1812 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001813 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001814}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001815
1816//===----------------------------------------------------------------------===//
1817// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1818// uses global state, which eventually should go elsewhere.
1819//===----------------------------------------------------------------------===//
1820namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001821class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001822 llvm::FoldingSetNodeID ID;
1823public:
1824 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
Anna Zaks4522e2a2011-09-19 23:44:31 +00001825 R->Profile(ID);
1826 PD->Profile(ID);
Ted Kremeneke0a58072009-09-18 22:37:37 +00001827 }
1828
1829 void Profile(llvm::FoldingSetNodeID &id) {
1830 id = ID;
1831 }
1832
1833 llvm::FoldingSetNodeID &getID() { return ID; }
1834};
1835}
1836
1837static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1838 // FIXME: Eventually this diagnostic cache should reside in something
1839 // like AnalysisManager instead of being a static variable. This is
1840 // really unsafe in the long term.
1841 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1842 static DiagnosticCache DC;
1843
1844 void *InsertPos;
1845 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1846
1847 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1848 delete Item;
1849 return true;
1850 }
1851
1852 DC.InsertNode(Item, InsertPos);
1853 return false;
1854}
1855
Ted Kremenekcf118d42009-02-04 23:49:09 +00001856void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001857 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001858 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1859 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001860 return;
1861
David Blaikieef3643f2011-09-26 00:51:36 +00001862 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer();
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Ted Kremenekcf118d42009-02-04 23:49:09 +00001864 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001865 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001866 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Ted Kremenekd49967f2009-04-29 21:58:13 +00001868 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001869 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001870 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001871 ? exampleReport->getDescription()
1872 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001873 BT.getCategory()));
1874
Ted Kremenek40406fe2010-12-03 06:52:30 +00001875 if (!bugReports.empty())
1876 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Ted Kremenek40406fe2010-12-03 06:52:30 +00001878 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001879 return;
1880
Ted Kremenek072192b2008-04-30 23:47:44 +00001881 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001882 const BugReport::ExtraTextList &Meta =
1883 exampleReport->getExtraText();
1884 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1885 e = Meta.end(); i != e; ++i) {
1886 D->addMeta(*i);
1887 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001888
Ted Kremenek3148eb42009-01-24 00:55:43 +00001889 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001890 BugReport::ranges_iterator Beg, End;
1891 llvm::tie(Beg, End) = exampleReport->getRanges();
David Blaikied6471f72011-09-25 23:23:43 +00001892 DiagnosticsEngine &Diag = getDiagnostic();
Ted Kremenekc213b482010-01-15 07:56:51 +00001893
1894 // Search the description for '%', as that will be interpretted as a
1895 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001896 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001897 unsigned ErrorDiag;
1898 {
1899 llvm::SmallString<512> TmpStr;
1900 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001901 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001902 if (*I == '%')
1903 Out << "%%";
1904 else
1905 Out << *I;
1906
1907 Out.flush();
David Blaikied6471f72011-09-25 23:23:43 +00001908 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr);
Ted Kremenekc213b482010-01-15 07:56:51 +00001909 }
Ted Kremenek57202072008-07-14 17:40:50 +00001910
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001911 {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001912 DiagnosticBuilder diagBuilder = Diag.Report(
1913 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001914 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001915 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001916 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001917
David Blaikieef3643f2011-09-26 00:51:36 +00001918 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001919 if (!PD)
1920 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001921
1922 if (D->empty()) {
Anna Zaks590dd8e2011-09-20 21:38:35 +00001923 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece(
1924 exampleReport->getLocation(getSourceManager()),
1925 exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001926
Ted Kremenek3148eb42009-01-24 00:55:43 +00001927 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1928 D->push_back(piece);
1929 }
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Ted Kremenek3148eb42009-01-24 00:55:43 +00001931 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001932}
Ted Kremenek57202072008-07-14 17:40:50 +00001933
Chris Lattner5f9e2722011-07-23 10:55:15 +00001934void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001935 PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001936 SourceRange* RBeg, unsigned NumRanges) {
1937 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1938}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001939
Chris Lattner5f9e2722011-07-23 10:55:15 +00001940void BugReporter::EmitBasicReport(StringRef name,
1941 StringRef category,
Anna Zaks590dd8e2011-09-20 21:38:35 +00001942 StringRef str, PathDiagnosticLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001943 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001945 // 'BT' is owned by BugReporter.
1946 BugType *BT = getBugTypeForName(name, category);
Anna Zaks590dd8e2011-09-20 21:38:35 +00001947 BugReport *R = new BugReport(*BT, str, Loc);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001948 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1949 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001950}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001951
Chris Lattner5f9e2722011-07-23 10:55:15 +00001952BugType *BugReporter::getBugTypeForName(StringRef name,
1953 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001954 llvm::SmallString<136> fullDesc;
1955 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1956 llvm::StringMapEntry<BugType *> &
1957 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1958 BugType *BT = entry.getValue();
1959 if (!BT) {
1960 BT = new BugType(name, category);
1961 entry.setValue(BT);
1962 }
1963 return BT;
1964}