blob: df3ebb860d997cd5d3dcb8e4ee1f26cf1785bcfa [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"
Ted Kremenek61f3e052008-04-03 04:42:52 +000020#include "clang/AST/Expr.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000021#include "clang/AST/ParentMap.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
23#include "clang/Basic/SourceManager.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000024#include "clang/Analysis/ProgramPoint.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000025#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chris Lattner405674c2008-08-23 22:23:37 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek331b0ac2008-06-18 05:34:07 +000027#include "llvm/ADT/DenseMap.h"
Ted Kremenekcf118d42009-02-04 23:49:09 +000028#include "llvm/ADT/STLExtras.h"
Ted Kremenek00605e02009-03-27 20:55:39 +000029#include "llvm/ADT/OwningPtr.h"
Ted Kremenek10aa5542009-03-12 23:41:59 +000030#include <queue>
Ted Kremenek61f3e052008-04-03 04:42:52 +000031
32using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000033using namespace ento;
Ted Kremenek61f3e052008-04-03 04:42:52 +000034
Ted Kremenek8966bc12009-05-06 21:39:49 +000035BugReporterVisitor::~BugReporterVisitor() {}
Ted Kremenek1b431022010-03-20 18:01:57 +000036
Ted Kremenekcf118d42009-02-04 23:49:09 +000037//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +000038// Helper routines for walking the ExplodedGraph and fetching statements.
Ted Kremenekcf118d42009-02-04 23:49:09 +000039//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000040
Ted Kremenek9c378f72011-08-12 23:37:29 +000041static inline const Stmt *GetStmt(const ProgramPoint &P) {
Ted Kremenek592362b2009-08-18 01:05:30 +000042 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P))
43 return SP->getStmt();
Ted Kremenek9c378f72011-08-12 23:37:29 +000044 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P))
Ted Kremenek61f3e052008-04-03 04:42:52 +000045 return BE->getSrc()->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +000046
Ted Kremenekb697b102009-02-23 22:44:26 +000047 return 0;
Ted Kremenek706e3cf2008-04-07 23:35:17 +000048}
49
Zhongxing Xuc5619d92009-08-06 01:32:16 +000050static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000051GetPredecessorNode(const ExplodedNode *N) {
Ted Kremenekbd7efa82008-04-17 23:44:37 +000052 return N->pred_empty() ? NULL : *(N->pred_begin());
53}
Ted Kremenek2673c9f2008-04-25 19:01:27 +000054
Zhongxing Xuc5619d92009-08-06 01:32:16 +000055static inline const ExplodedNode*
Ted Kremenek9c378f72011-08-12 23:37:29 +000056GetSuccessorNode(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000057 return N->succ_empty() ? NULL : *(N->succ_begin());
Ted Kremenekbd7efa82008-04-17 23:44:37 +000058}
59
Ted Kremenek9c378f72011-08-12 23:37:29 +000060static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000061 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000062 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +000063 return S;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Ted Kremenekb697b102009-02-23 22:44:26 +000065 return 0;
Ted Kremenek3148eb42009-01-24 00:55:43 +000066}
67
Ted Kremenek9c378f72011-08-12 23:37:29 +000068static const Stmt *GetNextStmt(const ExplodedNode *N) {
Ted Kremenekb697b102009-02-23 22:44:26 +000069 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N))
Ted Kremenek5f85e172009-07-22 22:35:28 +000070 if (const Stmt *S = GetStmt(N->getLocation())) {
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000071 // Check if the statement is '?' or '&&'/'||'. These are "merges",
72 // not actual statement points.
73 switch (S->getStmtClass()) {
74 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +000075 case Stmt::BinaryConditionalOperatorClass: continue;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000076 case Stmt::ConditionalOperatorClass: continue;
77 case Stmt::BinaryOperatorClass: {
John McCall2de56d12010-08-25 11:45:40 +000078 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
79 if (Op == BO_LAnd || Op == BO_LOr)
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000080 continue;
81 break;
82 }
83 default:
84 break;
85 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenekb7c51522009-07-28 00:07:15 +000087 // Some expressions don't have locations.
88 if (S->getLocStart().isInvalid())
89 continue;
Mike Stump1eb44332009-09-09 15:08:12 +000090
Ted Kremenekb697b102009-02-23 22:44:26 +000091 return S;
Ted Kremenekf5ab8e62009-03-28 17:33:57 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekb697b102009-02-23 22:44:26 +000094 return 0;
95}
96
Ted Kremenek5f85e172009-07-22 22:35:28 +000097static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +000098GetCurrentOrPreviousStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +000099 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000100 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekb697b102009-02-23 22:44:26 +0000102 return GetPreviousStmt(N);
103}
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek5f85e172009-07-22 22:35:28 +0000105static inline const Stmt*
Ted Kremenek9c378f72011-08-12 23:37:29 +0000106GetCurrentOrNextStmt(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000107 if (const Stmt *S = GetStmt(N->getLocation()))
Ted Kremenekb697b102009-02-23 22:44:26 +0000108 return S;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekb697b102009-02-23 22:44:26 +0000110 return GetNextStmt(N);
111}
112
113//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000114// PathDiagnosticBuilder and its associated routines and helper objects.
Ted Kremenekb697b102009-02-23 22:44:26 +0000115//===----------------------------------------------------------------------===//
Ted Kremenekb479dad2009-02-23 23:13:51 +0000116
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000117typedef llvm::DenseMap<const ExplodedNode*,
118const ExplodedNode*> NodeBackMap;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000119
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000120namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000121class NodeMapClosure : public BugReport::NodeResolver {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000122 NodeBackMap& M;
123public:
124 NodeMapClosure(NodeBackMap *m) : M(*m) {}
125 ~NodeMapClosure() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Ted Kremenek9c378f72011-08-12 23:37:29 +0000127 const ExplodedNode *getOriginalNode(const ExplodedNode *N) {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000128 NodeBackMap::iterator I = M.find(N);
129 return I == M.end() ? 0 : I->second;
130 }
131};
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000133class PathDiagnosticBuilder : public BugReporterContext {
Ted Kremenek7dc86642009-03-31 20:22:36 +0000134 BugReport *R;
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000135 PathDiagnosticClient *PDC;
Ted Kremenek00605e02009-03-27 20:55:39 +0000136 llvm::OwningPtr<ParentMap> PM;
Ted Kremenek7dc86642009-03-31 20:22:36 +0000137 NodeMapClosure NMC;
Mike Stump1eb44332009-09-09 15:08:12 +0000138public:
Ted Kremenek8966bc12009-05-06 21:39:49 +0000139 PathDiagnosticBuilder(GRBugReporter &br,
Mike Stump1eb44332009-09-09 15:08:12 +0000140 BugReport *r, NodeBackMap *Backmap,
Ted Kremenek8966bc12009-05-06 21:39:49 +0000141 PathDiagnosticClient *pdc)
142 : BugReporterContext(br),
Anna Zaks8e6431a2011-08-18 22:37:56 +0000143 R(r), PDC(pdc), NMC(Backmap) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Ted Kremenek9c378f72011-08-12 23:37:29 +0000145 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Ted Kremenek9c378f72011-08-12 23:37:29 +0000147 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
148 const ExplodedNode *N);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Anna Zaks8e6431a2011-08-18 22:37:56 +0000150 BugReport *getBugReport() { return R; }
151
Tom Care212f6d32010-09-16 03:50:38 +0000152 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000153
Tom Care212f6d32010-09-16 03:50:38 +0000154 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenekc3f83ad2009-04-01 17:18:21 +0000156 const Stmt *getParent(const Stmt *S) {
157 return getParentMap().getParent(S);
158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremenek8966bc12009-05-06 21:39:49 +0000160 virtual NodeMapClosure& getNodeResolver() { return NMC; }
Douglas Gregor72971342009-04-18 00:02:19 +0000161
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000162 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek7dc86642009-03-31 20:22:36 +0000164 PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const {
165 return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive;
166 }
167
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000168 bool supportsLogicalOpControlFlow() const {
169 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
Mike Stump1eb44332009-09-09 15:08:12 +0000170 }
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000171};
172} // end anonymous namespace
173
Ted Kremenek00605e02009-03-27 20:55:39 +0000174PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000175PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
Ted Kremenek5f85e172009-07-22 22:35:28 +0000176 if (const Stmt *S = GetNextStmt(N))
Ted Kremenek8966bc12009-05-06 21:39:49 +0000177 return PathDiagnosticLocation(S, getSourceManager());
Ted Kremenek00605e02009-03-27 20:55:39 +0000178
Mike Stump1eb44332009-09-09 15:08:12 +0000179 return FullSourceLoc(N->getLocationContext()->getDecl()->getBodyRBrace(),
Zhongxing Xuf7a50a42009-08-19 12:50:00 +0000180 getSourceManager());
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000181}
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek00605e02009-03-27 20:55:39 +0000183PathDiagnosticLocation
Ted Kremenek9c378f72011-08-12 23:37:29 +0000184PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
185 const ExplodedNode *N) {
Ted Kremenekbabdd7b2009-03-27 05:06:10 +0000186
Ted Kremenek143ca222008-05-06 18:11:09 +0000187 // Slow, but probably doesn't matter.
Ted Kremenekb697b102009-02-23 22:44:26 +0000188 if (os.str().empty())
189 os << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenek00605e02009-03-27 20:55:39 +0000191 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenek00605e02009-03-27 20:55:39 +0000193 if (Loc.asStmt())
Ted Kremenekb697b102009-02-23 22:44:26 +0000194 os << "Execution continues on line "
Chandler Carruth64211622011-07-25 21:09:52 +0000195 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
Ted Kremenek8966bc12009-05-06 21:39:49 +0000196 << '.';
Ted Kremenek4f1db532009-12-04 20:34:31 +0000197 else {
198 os << "Execution jumps to the end of the ";
199 const Decl *D = N->getLocationContext()->getDecl();
200 if (isa<ObjCMethodDecl>(D))
201 os << "method";
202 else if (isa<FunctionDecl>(D))
203 os << "function";
204 else {
205 assert(isa<BlockDecl>(D));
206 os << "anonymous block";
207 }
208 os << '.';
209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000211 return Loc;
Ted Kremenek143ca222008-05-06 18:11:09 +0000212}
213
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000214static bool IsNested(const Stmt *S, ParentMap &PM) {
215 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
216 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000218 const Stmt *Parent = PM.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000220 if (Parent)
221 switch (Parent->getStmtClass()) {
222 case Stmt::ForStmtClass:
223 case Stmt::DoStmtClass:
224 case Stmt::WhileStmtClass:
225 return true;
226 default:
227 break;
228 }
Mike Stump1eb44332009-09-09 15:08:12 +0000229
230 return false;
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000231}
232
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000233PathDiagnosticLocation
234PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000235 assert(S && "Null Stmt *passed to getEnclosingStmtLocation");
Mike Stump1eb44332009-09-09 15:08:12 +0000236 ParentMap &P = getParentMap();
Ted Kremenek8966bc12009-05-06 21:39:49 +0000237 SourceManager &SMgr = getSourceManager();
Ted Kremeneke88a1702009-05-11 22:19:32 +0000238
Ted Kremenekddb7bab2009-05-15 01:50:15 +0000239 while (IsNested(S, P)) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000240 const Stmt *Parent = P.getParentIgnoreParens(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000242 if (!Parent)
243 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000245 switch (Parent->getStmtClass()) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000246 case Stmt::BinaryOperatorClass: {
247 const BinaryOperator *B = cast<BinaryOperator>(Parent);
248 if (B->isLogicalOp())
249 return PathDiagnosticLocation(S, SMgr);
250 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000251 }
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000252 case Stmt::CompoundStmtClass:
253 case Stmt::StmtExprClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000254 return PathDiagnosticLocation(S, SMgr);
255 case Stmt::ChooseExprClass:
256 // Similar to '?' if we are referring to condition, just have the edge
257 // point to the entire choose expression.
258 if (cast<ChooseExpr>(Parent)->getCond() == S)
259 return PathDiagnosticLocation(Parent, SMgr);
260 else
Mike Stump1eb44332009-09-09 15:08:12 +0000261 return PathDiagnosticLocation(S, SMgr);
John McCall56ca35d2011-02-17 10:25:35 +0000262 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000263 case Stmt::ConditionalOperatorClass:
264 // For '?', if we are referring to condition, just have the edge point
265 // to the entire '?' expression.
John McCall56ca35d2011-02-17 10:25:35 +0000266 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S)
Ted Kremenek1d9a23a2009-03-28 04:08:14 +0000267 return PathDiagnosticLocation(Parent, SMgr);
268 else
Mike Stump1eb44332009-09-09 15:08:12 +0000269 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000270 case Stmt::DoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000271 return PathDiagnosticLocation(S, SMgr);
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000272 case Stmt::ForStmtClass:
273 if (cast<ForStmt>(Parent)->getBody() == S)
Mike Stump1eb44332009-09-09 15:08:12 +0000274 return PathDiagnosticLocation(S, SMgr);
275 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000276 case Stmt::IfStmtClass:
277 if (cast<IfStmt>(Parent)->getCond() != S)
278 return PathDiagnosticLocation(S, SMgr);
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000279 break;
Ted Kremenekaf3e3d52009-03-28 03:37:59 +0000280 case Stmt::ObjCForCollectionStmtClass:
281 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
282 return PathDiagnosticLocation(S, SMgr);
283 break;
284 case Stmt::WhileStmtClass:
285 if (cast<WhileStmt>(Parent)->getCond() != S)
286 return PathDiagnosticLocation(S, SMgr);
287 break;
288 default:
289 break;
290 }
291
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000292 S = Parent;
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000295 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
Ted Kremeneke88a1702009-05-11 22:19:32 +0000296
297 // Special case: DeclStmts can appear in for statement declarations, in which
298 // case the ForStmt is the context.
299 if (isa<DeclStmt>(S)) {
300 if (const Stmt *Parent = P.getParent(S)) {
301 switch (Parent->getStmtClass()) {
302 case Stmt::ForStmtClass:
303 case Stmt::ObjCForCollectionStmtClass:
304 return PathDiagnosticLocation(Parent, SMgr);
305 default:
306 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000307 }
308 }
Ted Kremeneke88a1702009-05-11 22:19:32 +0000309 }
310 else if (isa<BinaryOperator>(S)) {
311 // Special case: the binary operator represents the initialization
312 // code in a for statement (this can happen when the variable being
313 // initialized is an old variable.
314 if (const ForStmt *FS =
315 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) {
316 if (FS->getInit() == S)
317 return PathDiagnosticLocation(FS, SMgr);
318 }
319 }
320
Ted Kremenekd8c938b2009-03-27 21:16:25 +0000321 return PathDiagnosticLocation(S, SMgr);
322}
323
Ted Kremenekcf118d42009-02-04 23:49:09 +0000324//===----------------------------------------------------------------------===//
Ted Kremenek31061982009-03-31 23:00:32 +0000325// ScanNotableSymbols: closure-like callback for scanning Store bindings.
326//===----------------------------------------------------------------------===//
327
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000328static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N,
329 ProgramStateManager& VMgr,
330 SVal X) {
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek31061982009-03-31 23:00:32 +0000332 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek31061982009-03-31 23:00:32 +0000334 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek31061982009-03-31 23:00:32 +0000336 if (!isa<PostStmt>(P))
337 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek9c378f72011-08-12 23:37:29 +0000339 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt());
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek31061982009-03-31 23:00:32 +0000341 if (!DR)
342 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek13976632010-02-08 16:18:51 +0000344 SVal Y = N->getState()->getSVal(DR);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek31061982009-03-31 23:00:32 +0000346 if (X != Y)
347 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek9c378f72011-08-12 23:37:29 +0000349 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek31061982009-03-31 23:00:32 +0000351 if (!VD)
352 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek31061982009-03-31 23:00:32 +0000354 return VD;
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek31061982009-03-31 23:00:32 +0000357 return 0;
358}
359
360namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000361class NotableSymbolHandler
Ted Kremenek31061982009-03-31 23:00:32 +0000362: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek31061982009-03-31 23:00:32 +0000364 SymbolRef Sym;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000365 const ProgramState *PrevSt;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000366 const Stmt *S;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000367 ProgramStateManager& VMgr;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000368 const ExplodedNode *Pred;
Mike Stump1eb44332009-09-09 15:08:12 +0000369 PathDiagnostic& PD;
Ted Kremenek31061982009-03-31 23:00:32 +0000370 BugReporter& BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek31061982009-03-31 23:00:32 +0000372public:
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000374 NotableSymbolHandler(SymbolRef sym,
375 const ProgramState *prevst,
376 const Stmt *s,
377 ProgramStateManager& vmgr,
378 const ExplodedNode *pred,
379 PathDiagnostic& pd,
380 BugReporter& br)
381 : Sym(sym),
382 PrevSt(prevst),
383 S(s),
384 VMgr(vmgr),
385 Pred(pred),
386 PD(pd),
387 BR(br) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek31061982009-03-31 23:00:32 +0000389 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
390 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek31061982009-03-31 23:00:32 +0000392 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek31061982009-03-31 23:00:32 +0000394 if (ScanSym != Sym)
395 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
397 // Check if the previous state has this binding.
Ted Kremenek13976632010-02-08 16:18:51 +0000398 SVal X = PrevSt->getSVal(loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek31061982009-03-31 23:00:32 +0000400 if (X == V) // Same binding?
401 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek31061982009-03-31 23:00:32 +0000403 // Different binding. Only handle assignments for now. We don't pull
Mike Stump1eb44332009-09-09 15:08:12 +0000404 // this check out of the loop because we will eventually handle other
Ted Kremenek31061982009-03-31 23:00:32 +0000405 // cases.
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenek31061982009-03-31 23:00:32 +0000407 VarDecl *VD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek31061982009-03-31 23:00:32 +0000409 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
410 if (!B->isAssignmentOp())
411 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek31061982009-03-31 23:00:32 +0000413 // What variable did we assign to?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000414 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts());
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek31061982009-03-31 23:00:32 +0000416 if (!DR)
417 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek31061982009-03-31 23:00:32 +0000419 VD = dyn_cast<VarDecl>(DR->getDecl());
420 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000421 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000422 // FIXME: Eventually CFGs won't have DeclStmts. Right now we
423 // assume that each DeclStmt has a single Decl. This invariant
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000424 // holds by construction in the CFG.
Ted Kremenek31061982009-03-31 23:00:32 +0000425 VD = dyn_cast<VarDecl>(*DS->decl_begin());
426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek31061982009-03-31 23:00:32 +0000428 if (!VD)
429 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek31061982009-03-31 23:00:32 +0000431 // What is the most recently referenced variable with this binding?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000432 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek31061982009-03-31 23:00:32 +0000434 if (!MostRecent)
435 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek31061982009-03-31 23:00:32 +0000437 // Create the diagnostic.
438 FullSourceLoc L(S->getLocStart(), BR.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Zhanyong Wan7dfc9422011-02-16 21:13:32 +0000440 if (Loc::isLocType(VD->getType())) {
Ted Kremenek31061982009-03-31 23:00:32 +0000441 std::string msg = "'" + std::string(VD->getNameAsString()) +
442 "' now aliases '" + MostRecent->getNameAsString() + "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek31061982009-03-31 23:00:32 +0000444 PD.push_front(new PathDiagnosticEventPiece(L, msg));
445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek31061982009-03-31 23:00:32 +0000447 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000448 }
Ted Kremenek31061982009-03-31 23:00:32 +0000449};
450}
451
Ted Kremenek9c378f72011-08-12 23:37:29 +0000452static void HandleNotableSymbol(const ExplodedNode *N,
453 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000454 SymbolRef Sym, BugReporter& BR,
455 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek9c378f72011-08-12 23:37:29 +0000457 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000458 const ProgramState *PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek31061982009-03-31 23:00:32 +0000460 if (!PrevSt)
461 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek31061982009-03-31 23:00:32 +0000463 // Look at the region bindings of the current state that map to the
464 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000465 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000466 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
467 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
468}
469
470namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000471class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000472: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek31061982009-03-31 23:00:32 +0000474 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000475 const ExplodedNode *N;
476 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000477 GRBugReporter& BR;
478 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek31061982009-03-31 23:00:32 +0000480public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000481 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000482 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000483 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek31061982009-03-31 23:00:32 +0000485 bool HandleBinding(StoreManager& SMgr, Store store,
486 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Ted Kremenek31061982009-03-31 23:00:32 +0000488 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek31061982009-03-31 23:00:32 +0000490 if (!ScanSym)
491 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek31061982009-03-31 23:00:32 +0000493 if (!BR.isNotable(ScanSym))
494 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek31061982009-03-31 23:00:32 +0000496 if (AlreadyProcessed.count(ScanSym))
497 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Ted Kremenek31061982009-03-31 23:00:32 +0000499 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek31061982009-03-31 23:00:32 +0000501 HandleNotableSymbol(N, S, ScanSym, BR, PD);
502 return true;
503 }
504};
505} // end anonymous namespace
506
507//===----------------------------------------------------------------------===//
508// "Minimal" path diagnostic generation algorithm.
509//===----------------------------------------------------------------------===//
510
Ted Kremenek14856d72009-04-06 23:06:54 +0000511static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
512
Ted Kremenek31061982009-03-31 23:00:32 +0000513static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
514 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000515 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000516
Ted Kremenek31061982009-03-31 23:00:32 +0000517 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000518 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000519 ? NULL : *(N->pred_begin());
520 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000521 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000522 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenek31061982009-03-31 23:00:32 +0000524 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Ted Kremenek9c378f72011-08-12 23:37:29 +0000526 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
527 const CFGBlock *Src = BE->getSrc();
528 const CFGBlock *Dst = BE->getDst();
529 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Ted Kremenek31061982009-03-31 23:00:32 +0000531 if (!T)
532 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Ted Kremenek31061982009-03-31 23:00:32 +0000534 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek31061982009-03-31 23:00:32 +0000536 switch (T->getStmtClass()) {
537 default:
538 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek31061982009-03-31 23:00:32 +0000540 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000541 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000542 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek31061982009-03-31 23:00:32 +0000544 if (!S)
545 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek31061982009-03-31 23:00:32 +0000547 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000548 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000549 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek31061982009-03-31 23:00:32 +0000551 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000552 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000553 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
554 os.str()));
555 break;
556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
558 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000559 // Figure out what case arm we took.
560 std::string sbuf;
561 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Ted Kremenek9c378f72011-08-12 23:37:29 +0000563 if (const Stmt *S = Dst->getLabel()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000564 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Ted Kremenek31061982009-03-31 23:00:32 +0000566 switch (S->getStmtClass()) {
567 default:
568 os << "No cases match in the switch statement. "
569 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000570 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000571 break;
572 case Stmt::DefaultStmtClass:
573 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000574 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000575 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenek31061982009-03-31 23:00:32 +0000577 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000578 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000579 const CaseStmt *Case = cast<CaseStmt>(S);
580 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000583 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek9c378f72011-08-12 23:37:29 +0000585 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000586 // FIXME: Maybe this should be an assertion. Are there cases
587 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000588 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000589 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Ted Kremenek31061982009-03-31 23:00:32 +0000591 if (D) {
592 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000593 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000594 }
595 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000596
597 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000598 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000599
Ted Kremenek31061982009-03-31 23:00:32 +0000600 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000601 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000602 break;
603 }
604 }
605 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
606 os.str()));
607 }
608 else {
609 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000610 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000611 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
612 os.str()));
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Ted Kremenek31061982009-03-31 23:00:32 +0000615 break;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenek31061982009-03-31 23:00:32 +0000618 case Stmt::BreakStmtClass:
619 case Stmt::ContinueStmtClass: {
620 std::string sbuf;
621 llvm::raw_string_ostream os(sbuf);
622 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
623 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
624 os.str()));
625 break;
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Ted Kremenek31061982009-03-31 23:00:32 +0000628 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000629 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000630 case Stmt::ConditionalOperatorClass: {
631 std::string sbuf;
632 llvm::raw_string_ostream os(sbuf);
633 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek31061982009-03-31 23:00:32 +0000635 if (*(Src->succ_begin()+1) == Dst)
636 os << "false";
637 else
638 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek31061982009-03-31 23:00:32 +0000640 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek31061982009-03-31 23:00:32 +0000642 if (const Stmt *S = End.asStmt())
643 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Ted Kremenek31061982009-03-31 23:00:32 +0000645 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
646 os.str()));
647 break;
648 }
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek31061982009-03-31 23:00:32 +0000650 // Determine control-flow for short-circuited '&&' and '||'.
651 case Stmt::BinaryOperatorClass: {
652 if (!PDB.supportsLogicalOpControlFlow())
653 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000655 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000656 std::string sbuf;
657 llvm::raw_string_ostream os(sbuf);
658 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000659
John McCall2de56d12010-08-25 11:45:40 +0000660 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000661 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek31061982009-03-31 23:00:32 +0000663 if (*(Src->succ_begin()+1) == Dst) {
664 os << "false";
665 PathDiagnosticLocation End(B->getLHS(), SMgr);
666 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
667 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
668 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000669 }
Ted Kremenek31061982009-03-31 23:00:32 +0000670 else {
671 os << "true";
672 PathDiagnosticLocation Start(B->getLHS(), SMgr);
673 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
674 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
675 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000676 }
Ted Kremenek31061982009-03-31 23:00:32 +0000677 }
678 else {
John McCall2de56d12010-08-25 11:45:40 +0000679 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000680 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Ted Kremenek31061982009-03-31 23:00:32 +0000682 if (*(Src->succ_begin()+1) == Dst) {
683 os << "false";
684 PathDiagnosticLocation Start(B->getLHS(), SMgr);
685 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
686 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000687 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000688 }
689 else {
690 os << "true";
691 PathDiagnosticLocation End(B->getLHS(), SMgr);
692 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
693 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000694 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000695 }
696 }
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek31061982009-03-31 23:00:32 +0000698 break;
699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
701 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000702 if (*(Src->succ_begin()) == Dst) {
703 std::string sbuf;
704 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Ted Kremenek31061982009-03-31 23:00:32 +0000706 os << "Loop condition is true. ";
707 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenek31061982009-03-31 23:00:32 +0000709 if (const Stmt *S = End.asStmt())
710 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek31061982009-03-31 23:00:32 +0000712 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
713 os.str()));
714 }
715 else {
716 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek31061982009-03-31 23:00:32 +0000718 if (const Stmt *S = End.asStmt())
719 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenek31061982009-03-31 23:00:32 +0000721 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
722 "Loop condition is false. Exiting loop"));
723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek31061982009-03-31 23:00:32 +0000725 break;
726 }
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremenek31061982009-03-31 23:00:32 +0000728 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000729 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000730 if (*(Src->succ_begin()+1) == Dst) {
731 std::string sbuf;
732 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek31061982009-03-31 23:00:32 +0000734 os << "Loop condition is false. ";
735 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
736 if (const Stmt *S = End.asStmt())
737 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek31061982009-03-31 23:00:32 +0000739 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
740 os.str()));
741 }
742 else {
743 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
744 if (const Stmt *S = End.asStmt())
745 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Ted Kremenek31061982009-03-31 23:00:32 +0000747 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000748 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremenek31061982009-03-31 23:00:32 +0000751 break;
752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenek31061982009-03-31 23:00:32 +0000754 case Stmt::IfStmtClass: {
755 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek31061982009-03-31 23:00:32 +0000757 if (const Stmt *S = End.asStmt())
758 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek31061982009-03-31 23:00:32 +0000760 if (*(Src->succ_begin()+1) == Dst)
761 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000762 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000763 else
Ted Kremenek31061982009-03-31 23:00:32 +0000764 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000765 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek31061982009-03-31 23:00:32 +0000767 break;
768 }
769 }
770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000772 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000773 // Add diagnostic pieces from custom visitors.
774 BugReport *R = PDB.getBugReport();
775 for (BugReport::visitor_iterator I = R->visitor_begin(),
776 E = R->visitor_end(); I!=E; ++I) {
777 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000778 PD.push_front(p);
779 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Ted Kremenek9c378f72011-08-12 23:37:29 +0000782 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000783 // Scan the region bindings, and see if a "notable" symbol has a new
784 // lval binding.
785 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
786 PDB.getStateManager().iterBindings(N->getState(), SNS);
787 }
788 }
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek14856d72009-04-06 23:06:54 +0000790 // After constructing the full PathDiagnostic, do a pass over it to compact
791 // PathDiagnosticPieces that occur within a macro.
792 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000793}
794
795//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000796// "Extensive" PathDiagnostic generation.
797//===----------------------------------------------------------------------===//
798
799static bool IsControlFlowExpr(const Stmt *S) {
800 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000802 if (!E)
803 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000804
805 E = E->IgnoreParenCasts();
806
John McCall56ca35d2011-02-17 10:25:35 +0000807 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000808 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000810 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
811 if (B->isLogicalOp())
812 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
814 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000815}
816
Ted Kremenek14856d72009-04-06 23:06:54 +0000817namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000818class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000819 bool IsDead;
820public:
821 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
822 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000823
824 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000825 bool isDead() const { return IsDead; }
826};
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000828class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000829 std::vector<ContextLocation> CLocs;
830 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000831 PathDiagnostic &PD;
832 PathDiagnosticBuilder &PDB;
833 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000835 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenek14856d72009-04-06 23:06:54 +0000837 bool containsLocation(const PathDiagnosticLocation &Container,
838 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Ted Kremenek14856d72009-04-06 23:06:54 +0000840 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek9650cf32009-05-11 21:42:34 +0000842 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
843 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000844 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000845 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000846 while (1) {
847 // Adjust the location for some expressions that are best referenced
848 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000849 switch (S->getStmtClass()) {
850 default:
851 break;
852 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000853 case Stmt::GenericSelectionExprClass:
854 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000855 firstCharOnly = true;
856 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000857 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000858 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000859 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000860 firstCharOnly = true;
861 continue;
862 case Stmt::ChooseExprClass:
863 S = cast<ChooseExpr>(S)->getCond();
864 firstCharOnly = true;
865 continue;
866 case Stmt::BinaryOperatorClass:
867 S = cast<BinaryOperator>(S)->getLHS();
868 firstCharOnly = true;
869 continue;
870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek9650cf32009-05-11 21:42:34 +0000872 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek9650cf32009-05-11 21:42:34 +0000875 if (S != Original)
876 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek9650cf32009-05-11 21:42:34 +0000879 if (firstCharOnly)
880 L = PathDiagnosticLocation(L.asLocation());
881
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000882 return L;
883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Ted Kremenek14856d72009-04-06 23:06:54 +0000885 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000886 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000887 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000888 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000889 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000890 CLocs.pop_back();
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek14856d72009-04-06 23:06:54 +0000893public:
894 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
895 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremeneka301a672009-04-22 18:16:20 +0000897 // If the PathDiagnostic already has pieces, add the enclosing statement
898 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000899 if (!PD.empty()) {
900 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Ted Kremenek14856d72009-04-06 23:06:54 +0000902 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000903 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000904 }
905 }
906
907 ~EdgeBuilder() {
908 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremeneka301a672009-04-22 18:16:20 +0000910 // Finally, add an initial edge from the start location of the first
911 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000912 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
913 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000914 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000915 if (!CS->body_empty()) {
916 SourceLocation Loc = (*CS->body_begin())->getLocStart();
917 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
918 }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Ted Kremenek14856d72009-04-06 23:06:54 +0000920 }
921
922 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000924 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Ted Kremenek14856d72009-04-06 23:06:54 +0000926 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000927 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000928};
Ted Kremenek14856d72009-04-06 23:06:54 +0000929} // end anonymous namespace
930
931
932PathDiagnosticLocation
933EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
934 if (const Stmt *S = L.asStmt()) {
935 if (IsControlFlowExpr(S))
936 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
938 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek14856d72009-04-06 23:06:54 +0000941 return L;
942}
943
944bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
945 const PathDiagnosticLocation &Containee) {
946
947 if (Container == Containee)
948 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Ted Kremenek14856d72009-04-06 23:06:54 +0000950 if (Container.asDecl())
951 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Ted Kremenek14856d72009-04-06 23:06:54 +0000953 if (const Stmt *S = Containee.asStmt())
954 if (const Stmt *ContainerS = Container.asStmt()) {
955 while (S) {
956 if (S == ContainerS)
957 return true;
958 S = PDB.getParent(S);
959 }
960 return false;
961 }
962
963 // Less accurate: compare using source ranges.
964 SourceRange ContainerR = Container.asRange();
965 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Ted Kremenek14856d72009-04-06 23:06:54 +0000967 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000968 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
969 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
970 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
971 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chandler Carruth64211622011-07-25 21:09:52 +0000973 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
974 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
975 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
976 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Ted Kremenek14856d72009-04-06 23:06:54 +0000978 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000979 assert(ContaineeBegLine <= ContaineeEndLine);
980
Ted Kremenek14856d72009-04-06 23:06:54 +0000981 return (ContainerBegLine <= ContaineeBegLine &&
982 ContainerEndLine >= ContaineeEndLine &&
983 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000984 SM.getExpansionColumnNumber(ContainerRBeg) <=
985 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000986 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000987 SM.getExpansionColumnNumber(ContainerREnd) >=
988 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000989}
990
Ted Kremenek14856d72009-04-06 23:06:54 +0000991void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
992 if (!PrevLoc.isValid()) {
993 PrevLoc = NewLoc;
994 return;
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000997 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
998 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001000 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001001 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek14856d72009-04-06 23:06:54 +00001003 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001004 if (NewLocClean.asLocation().getExpansionLoc() ==
1005 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001006 return;
1007
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001008 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1009 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001010}
1011
1012void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Ted Kremeneka301a672009-04-22 18:16:20 +00001014 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1015 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremenek14856d72009-04-06 23:06:54 +00001017 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1018
1019 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001020 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Ted Kremenek14856d72009-04-06 23:06:54 +00001022 // Is the top location context the same as the one for the new location?
1023 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001024 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001025 if (IsConsumedExpr(TopContextLoc) &&
1026 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001027 TopContextLoc.markDead();
1028
Ted Kremenek14856d72009-04-06 23:06:54 +00001029 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001030 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001031
1032 return;
1033 }
1034
1035 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001036 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001037 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001039 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001040 CLocs.push_back(ContextLocation(CLoc, true));
1041 return;
1042 }
1043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremenek14856d72009-04-06 23:06:54 +00001045 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001046 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001047 }
1048
1049 // Context does not contain the location. Flush it.
1050 popLocation();
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001053 // If we reach here, there is no enclosing context. Just add the edge.
1054 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001055}
1056
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001057bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1058 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1059 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001061 return false;
1062}
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremeneke1baed32009-05-05 23:13:38 +00001064void EdgeBuilder::addExtendedContext(const Stmt *S) {
1065 if (!S)
1066 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001067
1068 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001069 while (Parent) {
1070 if (isa<CompoundStmt>(Parent))
1071 Parent = PDB.getParent(Parent);
1072 else
1073 break;
1074 }
1075
1076 if (Parent) {
1077 switch (Parent->getStmtClass()) {
1078 case Stmt::DoStmtClass:
1079 case Stmt::ObjCAtSynchronizedStmtClass:
1080 addContext(Parent);
1081 default:
1082 break;
1083 }
1084 }
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Ted Kremeneke1baed32009-05-05 23:13:38 +00001086 addContext(S);
1087}
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Ted Kremenek14856d72009-04-06 23:06:54 +00001089void EdgeBuilder::addContext(const Stmt *S) {
1090 if (!S)
1091 return;
1092
1093 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremenek14856d72009-04-06 23:06:54 +00001095 while (!CLocs.empty()) {
1096 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1097
1098 // Is the top location context the same as the one for the new location?
1099 if (TopContextLoc == L)
1100 return;
1101
1102 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001103 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001104 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001105 }
1106
1107 // Context does not contain the location. Flush it.
1108 popLocation();
1109 }
1110
1111 CLocs.push_back(L);
1112}
1113
1114static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1115 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001116 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001117 EdgeBuilder EB(PD, PDB);
1118
Ted Kremenek9c378f72011-08-12 23:37:29 +00001119 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001120 while (NextNode) {
1121 N = NextNode;
1122 NextNode = GetPredecessorNode(N);
1123 ProgramPoint P = N->getLocation();
1124
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001125 do {
1126 // Block edges.
1127 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1128 const CFGBlock &Blk = *BE->getSrc();
1129 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001131 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001132 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001133 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001134 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001136 if (!Term) {
1137 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1138 CS = dyn_cast<CompoundStmt>(FS->getBody());
1139 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001140 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001143 PathDiagnosticEventPiece *p =
1144 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001145 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001147 EB.addEdge(p->getLocation(), true);
1148 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001150 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001151 PathDiagnosticLocation BL(CS->getRBracLoc(),
1152 PDB.getSourceManager());
1153 BL = PathDiagnosticLocation(BL.asLocation());
1154 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001155 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001158 if (Term)
1159 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001161 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001162 }
1163
Mike Stump1eb44332009-09-09 15:08:12 +00001164 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001165 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1166 const Stmt *stmt = S->getStmt();
1167 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001168 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001169 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001170 }
1171 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001172 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001173 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001174
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001175 break;
1176 }
1177 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001179 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001180 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Anna Zaks8e6431a2011-08-18 22:37:56 +00001182 // Add pieces from custom visitors.
1183 BugReport *R = PDB.getBugReport();
1184 for (BugReport::visitor_iterator I = R->visitor_begin(),
1185 E = R->visitor_end(); I!=E; ++I) {
1186 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001187 const PathDiagnosticLocation &Loc = p->getLocation();
1188 EB.addEdge(Loc, true);
1189 PD.push_front(p);
1190 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001191 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001192 }
Mike Stump1eb44332009-09-09 15:08:12 +00001193 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001194 }
1195}
1196
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001197//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001198// Methods for BugType and subclasses.
1199//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001200BugType::~BugType() { }
1201
Ted Kremenekcf118d42009-02-04 23:49:09 +00001202void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001203
Ted Kremenekcf118d42009-02-04 23:49:09 +00001204//===----------------------------------------------------------------------===//
1205// Methods for BugReport and subclasses.
1206//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001207
Anna Zaks8e6431a2011-08-18 22:37:56 +00001208void BugReport::addVisitor(BugReporterVisitor* visitor) {
1209 if (!visitor)
1210 return;
1211
1212 llvm::FoldingSetNodeID ID;
1213 visitor->Profile(ID);
1214 void *InsertPos;
1215
1216 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1217 delete visitor;
1218 return;
1219 }
1220
1221 CallbacksSet.InsertNode(visitor, InsertPos);
1222 Callbacks = F.add(visitor, Callbacks);
1223}
1224
1225BugReport::~BugReport() {
1226 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001227 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001228 }
1229}
Anna Zakse172e8b2011-08-17 23:00:25 +00001230
1231void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1232 hash.AddPointer(&BT);
1233 hash.AddInteger(getLocation().getRawEncoding());
1234 hash.AddString(Description);
1235
1236 for (SmallVectorImpl<SourceRange>::const_iterator I =
1237 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1238 const SourceRange range = *I;
1239 if (!range.isValid())
1240 continue;
1241 hash.AddInteger(range.getBegin().getRawEncoding());
1242 hash.AddInteger(range.getEnd().getRawEncoding());
1243 }
1244}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001245
Ted Kremenek9c378f72011-08-12 23:37:29 +00001246const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001247 if (!ErrorNode)
1248 return 0;
1249
Tom Care212f6d32010-09-16 03:50:38 +00001250 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001251 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenek9c378f72011-08-12 23:37:29 +00001253 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001254 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001255 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001256 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001257 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001258 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001259 S = GetStmt(ProgP);
1260
1261 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001262}
1263
1264PathDiagnosticPiece*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001265BugReport::getEndPath(BugReporterContext &BRC,
1266 const ExplodedNode *EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Anna Zakseb3058a2011-08-03 01:57:49 +00001268 const ProgramPoint &PP = EndPathNode->getLocation();
1269 PathDiagnosticLocation L;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Anna Zakseb3058a2011-08-03 01:57:49 +00001271 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
1272 const CFGBlock *block = BE->getBlock();
1273 if (block->getBlockID() == 0) {
1274 L = PathDiagnosticLocation(
1275 EndPathNode->getLocationContext()->getDecl()->getBodyRBrace(),
1276 BRC.getSourceManager());
1277 }
1278 }
1279
1280 if (!L.isValid()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001281 const Stmt *S = getStmt();
Anna Zakseb3058a2011-08-03 01:57:49 +00001282
1283 if (!S)
1284 return NULL;
1285
1286 L = PathDiagnosticLocation(S, BRC.getSourceManager());
1287 }
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001288
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001289 BugReport::ranges_iterator Beg, End;
1290 llvm::tie(Beg, End) = getRanges();
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001292 // Only add the statement itself as a range if we didn't specify any
1293 // special ranges for this report.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001294 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L, getDescription(),
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001295 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001297 for (; Beg != End; ++Beg)
1298 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Ted Kremenek61f3e052008-04-03 04:42:52 +00001300 return P;
1301}
1302
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001303std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001304BugReport::getRanges() {
1305 // If no custom ranges, add the range of the statement corresponding to
1306 // the error node.
1307 if (Ranges.empty()) {
1308 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1309 addRange(E->getSourceRange());
1310 else
1311 return std::make_pair(ranges_iterator(), ranges_iterator());
1312 }
1313
1314 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001315}
1316
Mike Stump1eb44332009-09-09 15:08:12 +00001317SourceLocation BugReport::getLocation() const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001318 if (ErrorNode) {
1319 (Location.isInvalid() &&
1320 "Either Location or ErrorNode should be specified but not both.");
1321
Ted Kremenek9c378f72011-08-12 23:37:29 +00001322 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001323 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001324 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001325 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001326 // For binary operators, return the location of the operator.
1327 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1328 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001329
Ted Kremenekcf118d42009-02-04 23:49:09 +00001330 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001331 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001332
Anna Zaksb7530a42011-08-17 23:21:23 +00001333 } else {
1334 assert(Location.isValid());
1335 return Location;
1336 }
1337
Ted Kremenekcf118d42009-02-04 23:49:09 +00001338 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001339}
1340
Ted Kremenekcf118d42009-02-04 23:49:09 +00001341//===----------------------------------------------------------------------===//
1342// Methods for BugReporter and subclasses.
1343//===----------------------------------------------------------------------===//
1344
1345BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001346 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001347}
1348
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001349GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001350BugReporterData::~BugReporterData() {}
1351
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001352ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001353
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001354ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001355GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1356
Anna Zaks3b030a22011-08-19 01:57:09 +00001357BugReporter::~BugReporter() {
1358 FlushReports();
1359
1360 // Free the bug reports we are tracking.
1361 typedef std::vector<BugReportEquivClass *> ContTy;
1362 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1363 I != E; ++I) {
1364 delete *I;
1365 }
1366}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001367
1368void BugReporter::FlushReports() {
1369 if (BugTypes.isEmpty())
1370 return;
1371
1372 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001373 // warnings and new BugTypes.
1374 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1375 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001376 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001377 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001378 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001379 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001380 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001381 const_cast<BugType*>(*I)->FlushReports(*this);
1382
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001383 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1384 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1385 BugReportEquivClass& EQ = *EI;
1386 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001387 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001388
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001389 // BugReporter owns and deletes only BugTypes created implicitly through
1390 // EmitBasicReport.
1391 // FIXME: There are leaks from checkers that assume that the BugTypes they
1392 // create will be destroyed by the BugReporter.
1393 for (llvm::StringMap<BugType*>::iterator
1394 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1395 delete I->second;
1396
Ted Kremenekcf118d42009-02-04 23:49:09 +00001397 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001398 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001399}
1400
1401//===----------------------------------------------------------------------===//
1402// PathDiagnostics generation.
1403//===----------------------------------------------------------------------===//
1404
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001405static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001406 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001407MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001408 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Ted Kremenekcf118d42009-02-04 23:49:09 +00001410 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001411 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001412 // error node unless there are two or more error nodes with the same minimum
1413 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001414 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001415 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001416
1417 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001418 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1419 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Ted Kremenekcf118d42009-02-04 23:49:09 +00001421 // Create owning pointers for GTrim and NMap just to ensure that they are
1422 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001423 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001424 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Ted Kremenekcf118d42009-02-04 23:49:09 +00001426 // Find the (first) error node in the trimmed graph. We just need to consult
1427 // the node map (NMap) which maps from nodes in the original graph to nodes
1428 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001429
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001430 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001431 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001432 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001433
Ted Kremenek40406fe2010-12-03 06:52:30 +00001434 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1435 const ExplodedNode *originalNode = nodes[nodeIndex];
1436 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001437 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001438 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001439 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001440 }
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Ted Kremenek938332c2009-05-16 01:11:58 +00001442 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001443
1444 // Create a new (third!) graph with a single path. This is the graph
1445 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001446 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Ted Kremenek10aa5542009-03-12 23:41:59 +00001448 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001449 // to the root node, and then construct a new graph that contains only
1450 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001451 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001453 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001454 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001456 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001457 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001458 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001460 if (Visited.find(Node) != Visited.end())
1461 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001463 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 if (Node->pred_empty()) {
1466 Root = Node;
1467 break;
1468 }
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001470 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001471 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001472 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001473 }
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Ted Kremenek938332c2009-05-16 01:11:58 +00001475 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Ted Kremenek10aa5542009-03-12 23:41:59 +00001477 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001478 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001479 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001480 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001481 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001483 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001485 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001486 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001488 // Create the equivalent node in the new graph with the same state
1489 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001490 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001492 // Store the mapping to the original node.
1493 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1494 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001495 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001497 // Link up the new node with the previous node.
1498 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001499 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001501 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001503 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001504 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001505 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001506 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001507 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001508 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001509 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001512 // Find the next successor node. We choose the node that is marked
1513 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001514 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1515 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001516 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001518 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001520 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001522 if (I == Visited.end())
1523 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001525 if (!N || I->second < MinVal) {
1526 N = *SI;
1527 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001528 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001529 }
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremenek938332c2009-05-16 01:11:58 +00001531 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek938332c2009-05-16 01:11:58 +00001534 assert(First);
1535
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001536 return std::make_pair(std::make_pair(GNew, BM),
1537 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001538}
1539
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001540/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1541/// and collapses PathDiagosticPieces that are expanded by macros.
1542static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1543 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1544 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001546 typedef std::vector<PathDiagnosticPiece*>
1547 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001549 MacroStackTy MacroStack;
1550 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001552 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1553 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001554 const FullSourceLoc Loc = I->getLocation().asLocation();
1555
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001556 // Determine the instantiation location, which is the location we group
1557 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001558 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001559 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001560 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001562 if (Loc.isFileID()) {
1563 MacroStack.clear();
1564 Pieces.push_back(&*I);
1565 continue;
1566 }
1567
1568 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001570 // Is the PathDiagnosticPiece within the same macro group?
1571 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1572 MacroStack.back().first->push_back(&*I);
1573 continue;
1574 }
1575
1576 // We aren't in the same group. Are we descending into a new macro
1577 // or are part of an old one?
1578 PathDiagnosticMacroPiece *MacroGroup = 0;
1579
1580 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001581 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001582 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001584 // Walk the entire macro stack.
1585 while (!MacroStack.empty()) {
1586 if (InstantiationLoc == MacroStack.back().second) {
1587 MacroGroup = MacroStack.back().first;
1588 break;
1589 }
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001591 if (ParentInstantiationLoc == MacroStack.back().second) {
1592 MacroGroup = MacroStack.back().first;
1593 break;
1594 }
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001596 MacroStack.pop_back();
1597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001599 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1600 // Create a new macro group and add it to the stack.
1601 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1602
1603 if (MacroGroup)
1604 MacroGroup->push_back(NewGroup);
1605 else {
1606 assert(InstantiationLoc.isFileID());
1607 Pieces.push_back(NewGroup);
1608 }
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001610 MacroGroup = NewGroup;
1611 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1612 }
1613
1614 // Finally, add the PathDiagnosticPiece to the group.
1615 MacroGroup->push_back(&*I);
1616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001618 // Now take the pieces and construct a new PathDiagnostic.
1619 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001621 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1622 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1623 if (!MP->containsEvent()) {
1624 delete MP;
1625 continue;
1626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001628 PD.push_back(*I);
1629 }
1630}
1631
Ted Kremenek7dc86642009-03-31 20:22:36 +00001632void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001633 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Ted Kremenek40406fe2010-12-03 06:52:30 +00001635 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001636 SmallVector<const ExplodedNode *, 10> errorNodes;
1637 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001638 E = bugReports.end(); I != E; ++I) {
1639 errorNodes.push_back((*I)->getErrorNode());
1640 }
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001642 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001643 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001644 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001645 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001646 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Ted Kremenekcf118d42009-02-04 23:49:09 +00001648 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001649 assert(GPair.second.second < bugReports.size());
1650 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001651 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001653 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001654 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001655 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001656
1657 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001658 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Ted Kremenek9c378f72011-08-12 23:37:29 +00001660 if (PathDiagnosticPiece *Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001661 PD.push_back(Piece);
1662 else
1663 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Anna Zaks8e6431a2011-08-18 22:37:56 +00001665 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001666 R->addVisitor(new NilReceiverBRVisitor());
1667 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenek7dc86642009-03-31 20:22:36 +00001669 switch (PDB.getGenerationScheme()) {
1670 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001671 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001672 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001673 case PathDiagnosticClient::Minimal:
1674 GenerateMinimalPathDiagnostic(PD, PDB, N);
1675 break;
1676 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001677}
1678
Ted Kremenekcf118d42009-02-04 23:49:09 +00001679void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001680 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001681}
1682
Mike Stump1eb44332009-09-09 15:08:12 +00001683void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001684 // Compute the bug report's hash to determine its equivalence class.
1685 llvm::FoldingSetNodeID ID;
1686 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001687
1688 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001689 BugType& BT = R->getBugType();
1690 Register(&BT);
1691 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001692 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Ted Kremenekcf118d42009-02-04 23:49:09 +00001694 if (!EQ) {
1695 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001696 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001697 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001698 }
1699 else
1700 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001701}
1702
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001703
1704//===----------------------------------------------------------------------===//
1705// Emitting reports in equivalence classes.
1706//===----------------------------------------------------------------------===//
1707
1708namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001709struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001710 const ExplodedNode *N;
1711 ExplodedNode::const_succ_iterator I, E;
1712
1713 FRIEC_WLItem(const ExplodedNode *n)
1714 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1715};
1716}
1717
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001718static BugReport *
1719FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001720 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001721
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001722 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1723 assert(I != E);
1724 BugReport *R = *I;
1725 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001726
Ted Kremenek40406fe2010-12-03 06:52:30 +00001727 // If we don't need to suppress any of the nodes because they are
1728 // post-dominated by a sink, simply add all the nodes in the equivalence class
1729 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001730 if (!BT.isSuppressOnSink()) {
1731 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001732 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001733 if (N) {
1734 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001735 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001736 }
1737 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001738 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001739 }
1740
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001741 // For bug reports that should be suppressed when all paths are post-dominated
1742 // by a sink node, iterate through the reports in the equivalence class
1743 // until we find one that isn't post-dominated (if one exists). We use a
1744 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1745 // this as a recursive function, but we don't want to risk blowing out the
1746 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001747 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001748
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001749 for (; I != E; ++I) {
1750 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001751 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001752
Ted Kremenek40406fe2010-12-03 06:52:30 +00001753 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001754 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001755 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001756 assert(false &&
1757 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001758 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001759 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001760 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001761 if (errorNode->succ_empty()) {
1762 bugReports.push_back(R);
1763 if (!exampleReport)
1764 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001765 continue;
1766 }
1767
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001768 // At this point we know that 'N' is not a sink and it has at least one
1769 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1770 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001771 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001772 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1773
1774 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001775 WL.push_back(errorNode);
1776 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001777
1778 while (!WL.empty()) {
1779 WLItem &WI = WL.back();
1780 assert(!WI.N->succ_empty());
1781
1782 for (; WI.I != WI.E; ++WI.I) {
1783 const ExplodedNode *Succ = *WI.I;
1784 // End-of-path node?
1785 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001786 // If we found an end-of-path node that is not a sink.
1787 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001788 bugReports.push_back(R);
1789 if (!exampleReport)
1790 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001791 WL.clear();
1792 break;
1793 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001794 // Found a sink? Continue on to the next successor.
1795 continue;
1796 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001797 // Mark the successor as visited. If it hasn't been explored,
1798 // enqueue it to the DFS worklist.
1799 unsigned &mark = Visited[Succ];
1800 if (!mark) {
1801 mark = 1;
1802 WL.push_back(Succ);
1803 break;
1804 }
1805 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001806
1807 // The worklist may have been cleared at this point. First
1808 // check if it is empty before checking the last item.
1809 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001810 WL.pop_back();
1811 }
1812 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001813
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001814 // ExampleReport will be NULL if all the nodes in the equivalence class
1815 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001816 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001817}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001818
1819//===----------------------------------------------------------------------===//
1820// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1821// uses global state, which eventually should go elsewhere.
1822//===----------------------------------------------------------------------===//
1823namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001824class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001825 llvm::FoldingSetNodeID ID;
1826public:
1827 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1828 ID.AddString(R->getBugType().getName());
1829 ID.AddString(R->getBugType().getCategory());
1830 ID.AddString(R->getDescription());
1831 ID.AddInteger(R->getLocation().getRawEncoding());
1832 PD->Profile(ID);
1833 }
1834
1835 void Profile(llvm::FoldingSetNodeID &id) {
1836 id = ID;
1837 }
1838
1839 llvm::FoldingSetNodeID &getID() { return ID; }
1840};
1841}
1842
1843static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1844 // FIXME: Eventually this diagnostic cache should reside in something
1845 // like AnalysisManager instead of being a static variable. This is
1846 // really unsafe in the long term.
1847 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1848 static DiagnosticCache DC;
1849
1850 void *InsertPos;
1851 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1852
1853 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1854 delete Item;
1855 return true;
1856 }
1857
1858 DC.InsertNode(Item, InsertPos);
1859 return false;
1860}
1861
Ted Kremenekcf118d42009-02-04 23:49:09 +00001862void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001863 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001864 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1865 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001866 return;
1867
Ted Kremenekd49967f2009-04-29 21:58:13 +00001868 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Ted Kremenekcf118d42009-02-04 23:49:09 +00001870 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001871 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001872 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Ted Kremenekd49967f2009-04-29 21:58:13 +00001874 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001875 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001876 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001877 ? exampleReport->getDescription()
1878 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001879 BT.getCategory()));
1880
Ted Kremenek40406fe2010-12-03 06:52:30 +00001881 if (!bugReports.empty())
1882 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Ted Kremenek40406fe2010-12-03 06:52:30 +00001884 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001885 return;
1886
Ted Kremenek072192b2008-04-30 23:47:44 +00001887 // Get the meta data.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001888 std::pair<const char**, const char**> Meta =
1889 exampleReport->getExtraDescriptiveText();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001890 for (const char** s = Meta.first; s != Meta.second; ++s)
1891 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001892
Ted Kremenek3148eb42009-01-24 00:55:43 +00001893 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001894 BugReport::ranges_iterator Beg, End;
1895 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001896 Diagnostic &Diag = getDiagnostic();
1897 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001898
1899 // Search the description for '%', as that will be interpretted as a
1900 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001901 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001902 unsigned ErrorDiag;
1903 {
1904 llvm::SmallString<512> TmpStr;
1905 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001906 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001907 if (*I == '%')
1908 Out << "%%";
1909 else
1910 Out << *I;
1911
1912 Out.flush();
1913 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1914 }
Ted Kremenek57202072008-07-14 17:40:50 +00001915
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001916 {
1917 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001918 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001919 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001920 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001921
1922 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1923 if (!PD)
1924 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001925
1926 if (D->empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001927 PathDiagnosticPiece *piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001928 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001929
Ted Kremenek3148eb42009-01-24 00:55:43 +00001930 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1931 D->push_back(piece);
1932 }
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Ted Kremenek3148eb42009-01-24 00:55:43 +00001934 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001935}
Ted Kremenek57202072008-07-14 17:40:50 +00001936
Chris Lattner5f9e2722011-07-23 10:55:15 +00001937void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001938 SourceLocation Loc,
1939 SourceRange* RBeg, unsigned NumRanges) {
1940 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1941}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001942
Chris Lattner5f9e2722011-07-23 10:55:15 +00001943void BugReporter::EmitBasicReport(StringRef name,
1944 StringRef category,
1945 StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001946 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001948 // 'BT' is owned by BugReporter.
1949 BugType *BT = getBugTypeForName(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001950 FullSourceLoc L = getContext().getFullLoc(Loc);
Anna Zaksb7530a42011-08-17 23:21:23 +00001951 BugReport *R = new BugReport(*BT, str, L);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001952 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1953 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001954}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001955
Chris Lattner5f9e2722011-07-23 10:55:15 +00001956BugType *BugReporter::getBugTypeForName(StringRef name,
1957 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001958 llvm::SmallString<136> fullDesc;
1959 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1960 llvm::StringMapEntry<BugType *> &
1961 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1962 BugType *BT = entry.getValue();
1963 if (!BT) {
1964 BT = new BugType(name, category);
1965 entry.setValue(BT);
1966 }
1967 return BT;
1968}