blob: 6821806c8467715e4b8c1e921866f5a2395f13f9 [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 Zaks3b030a22011-08-19 01:57:09 +00001227 if ((*I)->isOwnedByReporterContext()) {
1228 delete *I;
1229 }
Anna Zaks8e6431a2011-08-18 22:37:56 +00001230 }
1231}
Anna Zakse172e8b2011-08-17 23:00:25 +00001232
1233void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1234 hash.AddPointer(&BT);
1235 hash.AddInteger(getLocation().getRawEncoding());
1236 hash.AddString(Description);
1237
1238 for (SmallVectorImpl<SourceRange>::const_iterator I =
1239 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1240 const SourceRange range = *I;
1241 if (!range.isValid())
1242 continue;
1243 hash.AddInteger(range.getBegin().getRawEncoding());
1244 hash.AddInteger(range.getEnd().getRawEncoding());
1245 }
1246}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001247
Ted Kremenek9c378f72011-08-12 23:37:29 +00001248const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001249 if (!ErrorNode)
1250 return 0;
1251
Tom Care212f6d32010-09-16 03:50:38 +00001252 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001253 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Ted Kremenek9c378f72011-08-12 23:37:29 +00001255 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001256 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001257 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001258 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001259 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001260 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001261 S = GetStmt(ProgP);
1262
1263 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001264}
1265
1266PathDiagnosticPiece*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001267BugReport::getEndPath(BugReporterContext &BRC,
1268 const ExplodedNode *EndPathNode) {
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Anna Zakseb3058a2011-08-03 01:57:49 +00001270 const ProgramPoint &PP = EndPathNode->getLocation();
1271 PathDiagnosticLocation L;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Anna Zakseb3058a2011-08-03 01:57:49 +00001273 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
1274 const CFGBlock *block = BE->getBlock();
1275 if (block->getBlockID() == 0) {
1276 L = PathDiagnosticLocation(
1277 EndPathNode->getLocationContext()->getDecl()->getBodyRBrace(),
1278 BRC.getSourceManager());
1279 }
1280 }
1281
1282 if (!L.isValid()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001283 const Stmt *S = getStmt();
Anna Zakseb3058a2011-08-03 01:57:49 +00001284
1285 if (!S)
1286 return NULL;
1287
1288 L = PathDiagnosticLocation(S, BRC.getSourceManager());
1289 }
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001290
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001291 BugReport::ranges_iterator Beg, End;
1292 llvm::tie(Beg, End) = getRanges();
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001294 // Only add the statement itself as a range if we didn't specify any
1295 // special ranges for this report.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001296 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L, getDescription(),
Ted Kremenek3ef538d2009-05-11 23:50:59 +00001297 Beg == End);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001299 for (; Beg != End; ++Beg)
1300 P->addRange(*Beg);
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Ted Kremenek61f3e052008-04-03 04:42:52 +00001302 return P;
1303}
1304
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001305std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001306BugReport::getRanges() {
1307 // If no custom ranges, add the range of the statement corresponding to
1308 // the error node.
1309 if (Ranges.empty()) {
1310 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1311 addRange(E->getSourceRange());
1312 else
1313 return std::make_pair(ranges_iterator(), ranges_iterator());
1314 }
1315
1316 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001317}
1318
Mike Stump1eb44332009-09-09 15:08:12 +00001319SourceLocation BugReport::getLocation() const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001320 if (ErrorNode) {
1321 (Location.isInvalid() &&
1322 "Either Location or ErrorNode should be specified but not both.");
1323
Ted Kremenek9c378f72011-08-12 23:37:29 +00001324 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001325 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001326 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001327 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001328 // For binary operators, return the location of the operator.
1329 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1330 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001331
Ted Kremenekcf118d42009-02-04 23:49:09 +00001332 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001333 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001334
Anna Zaksb7530a42011-08-17 23:21:23 +00001335 } else {
1336 assert(Location.isValid());
1337 return Location;
1338 }
1339
Ted Kremenekcf118d42009-02-04 23:49:09 +00001340 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001341}
1342
Ted Kremenek9c378f72011-08-12 23:37:29 +00001343PathDiagnosticPiece *BugReport::VisitNode(const ExplodedNode *N,
1344 const ExplodedNode *PrevN,
Anna Zaks8e6431a2011-08-18 22:37:56 +00001345 BugReporterContext &BRC,
1346 BugReport &BR) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +00001347 return NULL;
1348}
1349
Ted Kremenekcf118d42009-02-04 23:49:09 +00001350//===----------------------------------------------------------------------===//
1351// Methods for BugReporter and subclasses.
1352//===----------------------------------------------------------------------===//
1353
1354BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001355 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001356}
1357
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001358GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001359BugReporterData::~BugReporterData() {}
1360
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001361ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001362
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001363ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001364GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1365
Anna Zaks3b030a22011-08-19 01:57:09 +00001366BugReporter::~BugReporter() {
1367 FlushReports();
1368
1369 // Free the bug reports we are tracking.
1370 typedef std::vector<BugReportEquivClass *> ContTy;
1371 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1372 I != E; ++I) {
1373 delete *I;
1374 }
1375}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001376
1377void BugReporter::FlushReports() {
1378 if (BugTypes.isEmpty())
1379 return;
1380
1381 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001382 // warnings and new BugTypes.
1383 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1384 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001385 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001386 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001387 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001388 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001389 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001390 const_cast<BugType*>(*I)->FlushReports(*this);
1391
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001392 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1393 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1394 BugReportEquivClass& EQ = *EI;
1395 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001396 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001397
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001398 // BugReporter owns and deletes only BugTypes created implicitly through
1399 // EmitBasicReport.
1400 // FIXME: There are leaks from checkers that assume that the BugTypes they
1401 // create will be destroyed by the BugReporter.
1402 for (llvm::StringMap<BugType*>::iterator
1403 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1404 delete I->second;
1405
Ted Kremenekcf118d42009-02-04 23:49:09 +00001406 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001407 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001408}
1409
1410//===----------------------------------------------------------------------===//
1411// PathDiagnostics generation.
1412//===----------------------------------------------------------------------===//
1413
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001414static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001415 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001416MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001417 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Ted Kremenekcf118d42009-02-04 23:49:09 +00001419 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001420 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001421 // error node unless there are two or more error nodes with the same minimum
1422 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001423 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001424 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001425
1426 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001427 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1428 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Ted Kremenekcf118d42009-02-04 23:49:09 +00001430 // Create owning pointers for GTrim and NMap just to ensure that they are
1431 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001432 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001433 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenekcf118d42009-02-04 23:49:09 +00001435 // Find the (first) error node in the trimmed graph. We just need to consult
1436 // the node map (NMap) which maps from nodes in the original graph to nodes
1437 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001438
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001439 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001440 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001441 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001442
Ted Kremenek40406fe2010-12-03 06:52:30 +00001443 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1444 const ExplodedNode *originalNode = nodes[nodeIndex];
1445 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001446 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001447 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001448 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Ted Kremenek938332c2009-05-16 01:11:58 +00001451 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001452
1453 // Create a new (third!) graph with a single path. This is the graph
1454 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001455 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Ted Kremenek10aa5542009-03-12 23:41:59 +00001457 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001458 // to the root node, and then construct a new graph that contains only
1459 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001460 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001462 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001463 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001466 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001467 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001469 if (Visited.find(Node) != Visited.end())
1470 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001474 if (Node->pred_empty()) {
1475 Root = Node;
1476 break;
1477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001479 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001480 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001481 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek938332c2009-05-16 01:11:58 +00001484 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek10aa5542009-03-12 23:41:59 +00001486 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001487 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001488 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001489 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001490 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001492 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001493 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001494 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001495 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001497 // Create the equivalent node in the new graph with the same state
1498 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001499 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001501 // Store the mapping to the original node.
1502 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1503 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001504 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001506 // Link up the new node with the previous node.
1507 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001508 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001510 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001512 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001513 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001514 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001515 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001516 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001517 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001518 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001521 // Find the next successor node. We choose the node that is marked
1522 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001523 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1524 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001525 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001527 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001529 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001531 if (I == Visited.end())
1532 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001534 if (!N || I->second < MinVal) {
1535 N = *SI;
1536 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001537 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Ted Kremenek938332c2009-05-16 01:11:58 +00001540 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001541 }
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Ted Kremenek938332c2009-05-16 01:11:58 +00001543 assert(First);
1544
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001545 return std::make_pair(std::make_pair(GNew, BM),
1546 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001547}
1548
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001549/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1550/// and collapses PathDiagosticPieces that are expanded by macros.
1551static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1552 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1553 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001555 typedef std::vector<PathDiagnosticPiece*>
1556 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001558 MacroStackTy MacroStack;
1559 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001561 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1562 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001563 const FullSourceLoc Loc = I->getLocation().asLocation();
1564
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001565 // Determine the instantiation location, which is the location we group
1566 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001567 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001568 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001569 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001571 if (Loc.isFileID()) {
1572 MacroStack.clear();
1573 Pieces.push_back(&*I);
1574 continue;
1575 }
1576
1577 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001579 // Is the PathDiagnosticPiece within the same macro group?
1580 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1581 MacroStack.back().first->push_back(&*I);
1582 continue;
1583 }
1584
1585 // We aren't in the same group. Are we descending into a new macro
1586 // or are part of an old one?
1587 PathDiagnosticMacroPiece *MacroGroup = 0;
1588
1589 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001590 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001591 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001593 // Walk the entire macro stack.
1594 while (!MacroStack.empty()) {
1595 if (InstantiationLoc == MacroStack.back().second) {
1596 MacroGroup = MacroStack.back().first;
1597 break;
1598 }
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001600 if (ParentInstantiationLoc == MacroStack.back().second) {
1601 MacroGroup = MacroStack.back().first;
1602 break;
1603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001605 MacroStack.pop_back();
1606 }
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001608 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1609 // Create a new macro group and add it to the stack.
1610 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1611
1612 if (MacroGroup)
1613 MacroGroup->push_back(NewGroup);
1614 else {
1615 assert(InstantiationLoc.isFileID());
1616 Pieces.push_back(NewGroup);
1617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001619 MacroGroup = NewGroup;
1620 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1621 }
1622
1623 // Finally, add the PathDiagnosticPiece to the group.
1624 MacroGroup->push_back(&*I);
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001627 // Now take the pieces and construct a new PathDiagnostic.
1628 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001630 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1631 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1632 if (!MP->containsEvent()) {
1633 delete MP;
1634 continue;
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001637 PD.push_back(*I);
1638 }
1639}
1640
Ted Kremenek7dc86642009-03-31 20:22:36 +00001641void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001642 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Ted Kremenek40406fe2010-12-03 06:52:30 +00001644 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001645 SmallVector<const ExplodedNode *, 10> errorNodes;
1646 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001647 E = bugReports.end(); I != E; ++I) {
1648 errorNodes.push_back((*I)->getErrorNode());
1649 }
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001651 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001652 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001653 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001654 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001655 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Ted Kremenekcf118d42009-02-04 23:49:09 +00001657 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001658 assert(GPair.second.second < bugReports.size());
1659 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001660 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001662 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001663 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001664 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001665
1666 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001667 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenek9c378f72011-08-12 23:37:29 +00001669 if (PathDiagnosticPiece *Piece = R->getEndPath(PDB, N))
Ted Kremenekbd7efa82008-04-17 23:44:37 +00001670 PD.push_back(Piece);
1671 else
1672 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Anna Zaks8e6431a2011-08-18 22:37:56 +00001674 // Register additional node visitors.
1675 bugreporter::registerNilReceiverVisitor(*R);
1676 bugreporter::registerConditionVisitor(*R);
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Ted Kremenek7dc86642009-03-31 20:22:36 +00001678 switch (PDB.getGenerationScheme()) {
1679 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001680 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001681 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001682 case PathDiagnosticClient::Minimal:
1683 GenerateMinimalPathDiagnostic(PD, PDB, N);
1684 break;
1685 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001686}
1687
Ted Kremenekcf118d42009-02-04 23:49:09 +00001688void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001689 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001690}
1691
Mike Stump1eb44332009-09-09 15:08:12 +00001692void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001693 // Compute the bug report's hash to determine its equivalence class.
1694 llvm::FoldingSetNodeID ID;
1695 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001696
1697 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001698 BugType& BT = R->getBugType();
1699 Register(&BT);
1700 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001701 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Ted Kremenekcf118d42009-02-04 23:49:09 +00001703 if (!EQ) {
1704 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001705 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001706 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001707 }
1708 else
1709 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001710}
1711
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001712
1713//===----------------------------------------------------------------------===//
1714// Emitting reports in equivalence classes.
1715//===----------------------------------------------------------------------===//
1716
1717namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001718struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001719 const ExplodedNode *N;
1720 ExplodedNode::const_succ_iterator I, E;
1721
1722 FRIEC_WLItem(const ExplodedNode *n)
1723 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1724};
1725}
1726
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001727static BugReport *
1728FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001729 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001730
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001731 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1732 assert(I != E);
1733 BugReport *R = *I;
1734 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001735
Ted Kremenek40406fe2010-12-03 06:52:30 +00001736 // If we don't need to suppress any of the nodes because they are
1737 // post-dominated by a sink, simply add all the nodes in the equivalence class
1738 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001739 if (!BT.isSuppressOnSink()) {
1740 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001741 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001742 if (N) {
1743 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001744 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001745 }
1746 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001747 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001748 }
1749
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001750 // For bug reports that should be suppressed when all paths are post-dominated
1751 // by a sink node, iterate through the reports in the equivalence class
1752 // until we find one that isn't post-dominated (if one exists). We use a
1753 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1754 // this as a recursive function, but we don't want to risk blowing out the
1755 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001756 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001757
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001758 for (; I != E; ++I) {
1759 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001760 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001761
Ted Kremenek40406fe2010-12-03 06:52:30 +00001762 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001763 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001764 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001765 assert(false &&
1766 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001767 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001768 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001769 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001770 if (errorNode->succ_empty()) {
1771 bugReports.push_back(R);
1772 if (!exampleReport)
1773 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001774 continue;
1775 }
1776
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001777 // At this point we know that 'N' is not a sink and it has at least one
1778 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1779 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001780 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001781 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1782
1783 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001784 WL.push_back(errorNode);
1785 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001786
1787 while (!WL.empty()) {
1788 WLItem &WI = WL.back();
1789 assert(!WI.N->succ_empty());
1790
1791 for (; WI.I != WI.E; ++WI.I) {
1792 const ExplodedNode *Succ = *WI.I;
1793 // End-of-path node?
1794 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001795 // If we found an end-of-path node that is not a sink.
1796 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001797 bugReports.push_back(R);
1798 if (!exampleReport)
1799 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001800 WL.clear();
1801 break;
1802 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001803 // Found a sink? Continue on to the next successor.
1804 continue;
1805 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001806 // Mark the successor as visited. If it hasn't been explored,
1807 // enqueue it to the DFS worklist.
1808 unsigned &mark = Visited[Succ];
1809 if (!mark) {
1810 mark = 1;
1811 WL.push_back(Succ);
1812 break;
1813 }
1814 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001815
1816 // The worklist may have been cleared at this point. First
1817 // check if it is empty before checking the last item.
1818 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001819 WL.pop_back();
1820 }
1821 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001822
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001823 // ExampleReport will be NULL if all the nodes in the equivalence class
1824 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001825 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001826}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001827
1828//===----------------------------------------------------------------------===//
1829// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1830// uses global state, which eventually should go elsewhere.
1831//===----------------------------------------------------------------------===//
1832namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001833class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001834 llvm::FoldingSetNodeID ID;
1835public:
1836 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1837 ID.AddString(R->getBugType().getName());
1838 ID.AddString(R->getBugType().getCategory());
1839 ID.AddString(R->getDescription());
1840 ID.AddInteger(R->getLocation().getRawEncoding());
1841 PD->Profile(ID);
1842 }
1843
1844 void Profile(llvm::FoldingSetNodeID &id) {
1845 id = ID;
1846 }
1847
1848 llvm::FoldingSetNodeID &getID() { return ID; }
1849};
1850}
1851
1852static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1853 // FIXME: Eventually this diagnostic cache should reside in something
1854 // like AnalysisManager instead of being a static variable. This is
1855 // really unsafe in the long term.
1856 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1857 static DiagnosticCache DC;
1858
1859 void *InsertPos;
1860 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1861
1862 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1863 delete Item;
1864 return true;
1865 }
1866
1867 DC.InsertNode(Item, InsertPos);
1868 return false;
1869}
1870
Ted Kremenekcf118d42009-02-04 23:49:09 +00001871void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001872 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001873 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1874 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001875 return;
1876
Ted Kremenekd49967f2009-04-29 21:58:13 +00001877 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Ted Kremenekcf118d42009-02-04 23:49:09 +00001879 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001880 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001881 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Ted Kremenekd49967f2009-04-29 21:58:13 +00001883 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001884 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001885 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001886 ? exampleReport->getDescription()
1887 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001888 BT.getCategory()));
1889
Ted Kremenek40406fe2010-12-03 06:52:30 +00001890 if (!bugReports.empty())
1891 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Ted Kremenek40406fe2010-12-03 06:52:30 +00001893 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001894 return;
1895
Ted Kremenek072192b2008-04-30 23:47:44 +00001896 // Get the meta data.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001897 std::pair<const char**, const char**> Meta =
1898 exampleReport->getExtraDescriptiveText();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001899 for (const char** s = Meta.first; s != Meta.second; ++s)
1900 D->addMeta(*s);
Ted Kremenek75840e12008-04-18 01:56:37 +00001901
Ted Kremenek3148eb42009-01-24 00:55:43 +00001902 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001903 BugReport::ranges_iterator Beg, End;
1904 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001905 Diagnostic &Diag = getDiagnostic();
1906 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001907
1908 // Search the description for '%', as that will be interpretted as a
1909 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001910 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001911 unsigned ErrorDiag;
1912 {
1913 llvm::SmallString<512> TmpStr;
1914 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001915 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001916 if (*I == '%')
1917 Out << "%%";
1918 else
1919 Out << *I;
1920
1921 Out.flush();
1922 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1923 }
Ted Kremenek57202072008-07-14 17:40:50 +00001924
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001925 {
1926 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001927 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001928 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001929 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001930
1931 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1932 if (!PD)
1933 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001934
1935 if (D->empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001936 PathDiagnosticPiece *piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001937 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001938
Ted Kremenek3148eb42009-01-24 00:55:43 +00001939 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1940 D->push_back(piece);
1941 }
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Ted Kremenek3148eb42009-01-24 00:55:43 +00001943 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001944}
Ted Kremenek57202072008-07-14 17:40:50 +00001945
Chris Lattner5f9e2722011-07-23 10:55:15 +00001946void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001947 SourceLocation Loc,
1948 SourceRange* RBeg, unsigned NumRanges) {
1949 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1950}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001951
Chris Lattner5f9e2722011-07-23 10:55:15 +00001952void BugReporter::EmitBasicReport(StringRef name,
1953 StringRef category,
1954 StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001955 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001957 // 'BT' is owned by BugReporter.
1958 BugType *BT = getBugTypeForName(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001959 FullSourceLoc L = getContext().getFullLoc(Loc);
Anna Zaksb7530a42011-08-17 23:21:23 +00001960 BugReport *R = new BugReport(*BT, str, L);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001961 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1962 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001963}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001964
Chris Lattner5f9e2722011-07-23 10:55:15 +00001965BugType *BugReporter::getBugTypeForName(StringRef name,
1966 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001967 llvm::SmallString<136> fullDesc;
1968 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1969 llvm::StringMapEntry<BugType *> &
1970 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1971 BugType *BT = entry.getValue();
1972 if (!BT) {
1973 BT = new BugType(name, category);
1974 entry.setValue(BT);
1975 }
1976 return BT;
1977}