blob: fc28c587d212f394cbca38fafa5d818503ce35ac [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())) {
Jordy Rose7df12342011-08-21 05:25:15 +0000441 llvm::SmallString<64> buf;
442 llvm::raw_svector_ostream os(buf);
443 os << '\'' << VD << "' now aliases '" << MostRecent << '\'';
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Jordy Rose7df12342011-08-21 05:25:15 +0000445 PD.push_front(new PathDiagnosticEventPiece(L, os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek31061982009-03-31 23:00:32 +0000448 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000449 }
Ted Kremenek31061982009-03-31 23:00:32 +0000450};
451}
452
Ted Kremenek9c378f72011-08-12 23:37:29 +0000453static void HandleNotableSymbol(const ExplodedNode *N,
454 const Stmt *S,
Ted Kremenek31061982009-03-31 23:00:32 +0000455 SymbolRef Sym, BugReporter& BR,
456 PathDiagnostic& PD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek9c378f72011-08-12 23:37:29 +0000458 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000459 const ProgramState *PrevSt = Pred ? Pred->getState() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek31061982009-03-31 23:00:32 +0000461 if (!PrevSt)
462 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek31061982009-03-31 23:00:32 +0000464 // Look at the region bindings of the current state that map to the
465 // specified symbol. Are any of them not in the previous state?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000466 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek31061982009-03-31 23:00:32 +0000467 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR);
468 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H);
469}
470
471namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000472class ScanNotableSymbols
Ted Kremenek31061982009-03-31 23:00:32 +0000473: public StoreManager::BindingsHandler {
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremenek31061982009-03-31 23:00:32 +0000475 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000476 const ExplodedNode *N;
477 const Stmt *S;
Ted Kremenek31061982009-03-31 23:00:32 +0000478 GRBugReporter& BR;
479 PathDiagnostic& PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek31061982009-03-31 23:00:32 +0000481public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000482 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s,
Ted Kremenek5f85e172009-07-22 22:35:28 +0000483 GRBugReporter& br, PathDiagnostic& pd)
Ted Kremenek31061982009-03-31 23:00:32 +0000484 : N(n), S(s), BR(br), PD(pd) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek31061982009-03-31 23:00:32 +0000486 bool HandleBinding(StoreManager& SMgr, Store store,
487 const MemRegion* R, SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek31061982009-03-31 23:00:32 +0000489 SymbolRef ScanSym = V.getAsSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek31061982009-03-31 23:00:32 +0000491 if (!ScanSym)
492 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek31061982009-03-31 23:00:32 +0000494 if (!BR.isNotable(ScanSym))
495 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek31061982009-03-31 23:00:32 +0000497 if (AlreadyProcessed.count(ScanSym))
498 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek31061982009-03-31 23:00:32 +0000500 AlreadyProcessed.insert(ScanSym);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek31061982009-03-31 23:00:32 +0000502 HandleNotableSymbol(N, S, ScanSym, BR, PD);
503 return true;
504 }
505};
506} // end anonymous namespace
507
508//===----------------------------------------------------------------------===//
509// "Minimal" path diagnostic generation algorithm.
510//===----------------------------------------------------------------------===//
511
Ted Kremenek14856d72009-04-06 23:06:54 +0000512static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM);
513
Ted Kremenek31061982009-03-31 23:00:32 +0000514static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD,
515 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000516 const ExplodedNode *N) {
Ted Kremenek8966bc12009-05-06 21:39:49 +0000517
Ted Kremenek31061982009-03-31 23:00:32 +0000518 SourceManager& SMgr = PDB.getSourceManager();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000519 const ExplodedNode *NextNode = N->pred_empty()
Ted Kremenek31061982009-03-31 23:00:32 +0000520 ? NULL : *(N->pred_begin());
521 while (NextNode) {
Mike Stump1eb44332009-09-09 15:08:12 +0000522 N = NextNode;
Ted Kremenek31061982009-03-31 23:00:32 +0000523 NextNode = GetPredecessorNode(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Ted Kremenek31061982009-03-31 23:00:32 +0000525 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek9c378f72011-08-12 23:37:29 +0000527 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
528 const CFGBlock *Src = BE->getSrc();
529 const CFGBlock *Dst = BE->getDst();
530 const Stmt *T = Src->getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Ted Kremenek31061982009-03-31 23:00:32 +0000532 if (!T)
533 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Ted Kremenek31061982009-03-31 23:00:32 +0000535 FullSourceLoc Start(T->getLocStart(), SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Ted Kremenek31061982009-03-31 23:00:32 +0000537 switch (T->getStmtClass()) {
538 default:
539 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek31061982009-03-31 23:00:32 +0000541 case Stmt::GotoStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000542 case Stmt::IndirectGotoStmtClass: {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000543 const Stmt *S = GetNextStmt(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek31061982009-03-31 23:00:32 +0000545 if (!S)
546 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek31061982009-03-31 23:00:32 +0000548 std::string sbuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000549 llvm::raw_string_ostream os(sbuf);
Ted Kremenek31061982009-03-31 23:00:32 +0000550 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek31061982009-03-31 23:00:32 +0000552 os << "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000553 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000554 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
555 os.str()));
556 break;
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
559 case Stmt::SwitchStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000560 // Figure out what case arm we took.
561 std::string sbuf;
562 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Ted Kremenek9c378f72011-08-12 23:37:29 +0000564 if (const Stmt *S = Dst->getLabel()) {
Ted Kremenek31061982009-03-31 23:00:32 +0000565 PathDiagnosticLocation End(S, SMgr);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek31061982009-03-31 23:00:32 +0000567 switch (S->getStmtClass()) {
568 default:
569 os << "No cases match in the switch statement. "
570 "Control jumps to line "
Chandler Carruth64211622011-07-25 21:09:52 +0000571 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000572 break;
573 case Stmt::DefaultStmtClass:
574 os << "Control jumps to the 'default' case at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000575 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000576 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremenek31061982009-03-31 23:00:32 +0000578 case Stmt::CaseStmtClass: {
Mike Stump1eb44332009-09-09 15:08:12 +0000579 os << "Control jumps to 'case ";
Ted Kremenek9c378f72011-08-12 23:37:29 +0000580 const CaseStmt *Case = cast<CaseStmt>(S);
581 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 // Determine if it is an enum.
Ted Kremenek31061982009-03-31 23:00:32 +0000584 bool GetRawInt = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Ted Kremenek9c378f72011-08-12 23:37:29 +0000586 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000587 // FIXME: Maybe this should be an assertion. Are there cases
588 // were it is not an EnumConstantDecl?
Ted Kremenek9c378f72011-08-12 23:37:29 +0000589 const EnumConstantDecl *D =
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000590 dyn_cast<EnumConstantDecl>(DR->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Ted Kremenek31061982009-03-31 23:00:32 +0000592 if (D) {
593 GetRawInt = false;
Benjamin Kramer900fc632010-04-17 09:33:03 +0000594 os << D;
Ted Kremenek31061982009-03-31 23:00:32 +0000595 }
596 }
Eli Friedman9ec64d62009-04-26 19:04:51 +0000597
598 if (GetRawInt)
Ted Kremenek8966bc12009-05-06 21:39:49 +0000599 os << LHS->EvaluateAsInt(PDB.getASTContext());
Eli Friedman9ec64d62009-04-26 19:04:51 +0000600
Ted Kremenek31061982009-03-31 23:00:32 +0000601 os << ":' at line "
Chandler Carruth64211622011-07-25 21:09:52 +0000602 << End.asLocation().getExpansionLineNumber();
Ted Kremenek31061982009-03-31 23:00:32 +0000603 break;
604 }
605 }
606 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
607 os.str()));
608 }
609 else {
610 os << "'Default' branch taken. ";
Mike Stump1eb44332009-09-09 15:08:12 +0000611 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
Ted Kremenek31061982009-03-31 23:00:32 +0000612 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
613 os.str()));
614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Ted Kremenek31061982009-03-31 23:00:32 +0000616 break;
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek31061982009-03-31 23:00:32 +0000619 case Stmt::BreakStmtClass:
620 case Stmt::ContinueStmtClass: {
621 std::string sbuf;
622 llvm::raw_string_ostream os(sbuf);
623 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
624 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
625 os.str()));
626 break;
627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek31061982009-03-31 23:00:32 +0000629 // Determine control-flow for ternary '?'.
John McCall56ca35d2011-02-17 10:25:35 +0000630 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek31061982009-03-31 23:00:32 +0000631 case Stmt::ConditionalOperatorClass: {
632 std::string sbuf;
633 llvm::raw_string_ostream os(sbuf);
634 os << "'?' condition is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek31061982009-03-31 23:00:32 +0000636 if (*(Src->succ_begin()+1) == Dst)
637 os << "false";
638 else
639 os << "true";
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek31061982009-03-31 23:00:32 +0000641 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek31061982009-03-31 23:00:32 +0000643 if (const Stmt *S = End.asStmt())
644 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek31061982009-03-31 23:00:32 +0000646 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
647 os.str()));
648 break;
649 }
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenek31061982009-03-31 23:00:32 +0000651 // Determine control-flow for short-circuited '&&' and '||'.
652 case Stmt::BinaryOperatorClass: {
653 if (!PDB.supportsLogicalOpControlFlow())
654 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Zhongxing Xu03509ae2010-07-20 06:22:24 +0000656 const BinaryOperator *B = cast<BinaryOperator>(T);
Ted Kremenek31061982009-03-31 23:00:32 +0000657 std::string sbuf;
658 llvm::raw_string_ostream os(sbuf);
659 os << "Left side of '";
Mike Stump1eb44332009-09-09 15:08:12 +0000660
John McCall2de56d12010-08-25 11:45:40 +0000661 if (B->getOpcode() == BO_LAnd) {
Ted Kremenek31061982009-03-31 23:00:32 +0000662 os << "&&" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenek31061982009-03-31 23:00:32 +0000664 if (*(Src->succ_begin()+1) == Dst) {
665 os << "false";
666 PathDiagnosticLocation End(B->getLHS(), SMgr);
667 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
668 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
669 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000670 }
Ted Kremenek31061982009-03-31 23:00:32 +0000671 else {
672 os << "true";
673 PathDiagnosticLocation Start(B->getLHS(), SMgr);
674 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
675 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
676 os.str()));
Mike Stump1eb44332009-09-09 15:08:12 +0000677 }
Ted Kremenek31061982009-03-31 23:00:32 +0000678 }
679 else {
John McCall2de56d12010-08-25 11:45:40 +0000680 assert(B->getOpcode() == BO_LOr);
Ted Kremenek31061982009-03-31 23:00:32 +0000681 os << "||" << "' is ";
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremenek31061982009-03-31 23:00:32 +0000683 if (*(Src->succ_begin()+1) == Dst) {
684 os << "false";
685 PathDiagnosticLocation Start(B->getLHS(), SMgr);
686 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
687 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000688 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000689 }
690 else {
691 os << "true";
692 PathDiagnosticLocation End(B->getLHS(), SMgr);
693 PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr);
694 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Mike Stump1eb44332009-09-09 15:08:12 +0000695 os.str()));
Ted Kremenek31061982009-03-31 23:00:32 +0000696 }
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Ted Kremenek31061982009-03-31 23:00:32 +0000699 break;
700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
702 case Stmt::DoStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000703 if (*(Src->succ_begin()) == Dst) {
704 std::string sbuf;
705 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek31061982009-03-31 23:00:32 +0000707 os << "Loop condition is true. ";
708 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek31061982009-03-31 23:00:32 +0000710 if (const Stmt *S = End.asStmt())
711 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek31061982009-03-31 23:00:32 +0000713 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
714 os.str()));
715 }
716 else {
717 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek31061982009-03-31 23:00:32 +0000719 if (const Stmt *S = End.asStmt())
720 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek31061982009-03-31 23:00:32 +0000722 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
723 "Loop condition is false. Exiting loop"));
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek31061982009-03-31 23:00:32 +0000726 break;
727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek31061982009-03-31 23:00:32 +0000729 case Stmt::WhileStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000730 case Stmt::ForStmtClass: {
Ted Kremenek31061982009-03-31 23:00:32 +0000731 if (*(Src->succ_begin()+1) == Dst) {
732 std::string sbuf;
733 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek31061982009-03-31 23:00:32 +0000735 os << "Loop condition is false. ";
736 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
737 if (const Stmt *S = End.asStmt())
738 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek31061982009-03-31 23:00:32 +0000740 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
741 os.str()));
742 }
743 else {
744 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
745 if (const Stmt *S = End.asStmt())
746 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremenek31061982009-03-31 23:00:32 +0000748 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000749 "Loop condition is true. Entering loop body"));
Ted Kremenek31061982009-03-31 23:00:32 +0000750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Ted Kremenek31061982009-03-31 23:00:32 +0000752 break;
753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek31061982009-03-31 23:00:32 +0000755 case Stmt::IfStmtClass: {
756 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek31061982009-03-31 23:00:32 +0000758 if (const Stmt *S = End.asStmt())
759 End = PDB.getEnclosingStmtLocation(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek31061982009-03-31 23:00:32 +0000761 if (*(Src->succ_begin()+1) == Dst)
762 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000763 "Taking false branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000764 else
Ted Kremenek31061982009-03-31 23:00:32 +0000765 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000766 "Taking true branch"));
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek31061982009-03-31 23:00:32 +0000768 break;
769 }
770 }
771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000773 if (NextNode) {
Anna Zaks8e6431a2011-08-18 22:37:56 +0000774 // Add diagnostic pieces from custom visitors.
775 BugReport *R = PDB.getBugReport();
776 for (BugReport::visitor_iterator I = R->visitor_begin(),
777 E = R->visitor_end(); I!=E; ++I) {
778 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R))
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000779 PD.push_front(p);
780 }
Ted Kremenek8966bc12009-05-06 21:39:49 +0000781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek9c378f72011-08-12 23:37:29 +0000783 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
Ted Kremenek31061982009-03-31 23:00:32 +0000784 // Scan the region bindings, and see if a "notable" symbol has a new
785 // lval binding.
786 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD);
787 PDB.getStateManager().iterBindings(N->getState(), SNS);
788 }
789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek14856d72009-04-06 23:06:54 +0000791 // After constructing the full PathDiagnostic, do a pass over it to compact
792 // PathDiagnosticPieces that occur within a macro.
793 CompactPathDiagnostic(PD, PDB.getSourceManager());
Ted Kremenek31061982009-03-31 23:00:32 +0000794}
795
796//===----------------------------------------------------------------------===//
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000797// "Extensive" PathDiagnostic generation.
798//===----------------------------------------------------------------------===//
799
800static bool IsControlFlowExpr(const Stmt *S) {
801 const Expr *E = dyn_cast<Expr>(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000803 if (!E)
804 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
806 E = E->IgnoreParenCasts();
807
John McCall56ca35d2011-02-17 10:25:35 +0000808 if (isa<AbstractConditionalOperator>(E))
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000809 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000811 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
812 if (B->isLogicalOp())
813 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
815 return false;
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000816}
817
Ted Kremenek14856d72009-04-06 23:06:54 +0000818namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000819class ContextLocation : public PathDiagnosticLocation {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000820 bool IsDead;
821public:
822 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
823 : PathDiagnosticLocation(L), IsDead(isdead) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000824
825 void markDead() { IsDead = true; }
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000826 bool isDead() const { return IsDead; }
827};
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000829class EdgeBuilder {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000830 std::vector<ContextLocation> CLocs;
831 typedef std::vector<ContextLocation>::iterator iterator;
Ted Kremenek14856d72009-04-06 23:06:54 +0000832 PathDiagnostic &PD;
833 PathDiagnosticBuilder &PDB;
834 PathDiagnosticLocation PrevLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000836 bool IsConsumedExpr(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek14856d72009-04-06 23:06:54 +0000838 bool containsLocation(const PathDiagnosticLocation &Container,
839 const PathDiagnosticLocation &Containee);
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek14856d72009-04-06 23:06:54 +0000841 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek9650cf32009-05-11 21:42:34 +0000843 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
844 bool firstCharOnly = false) {
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000845 if (const Stmt *S = L.asStmt()) {
Ted Kremenek9650cf32009-05-11 21:42:34 +0000846 const Stmt *Original = S;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000847 while (1) {
848 // Adjust the location for some expressions that are best referenced
849 // by one of their subexpressions.
Ted Kremenek9650cf32009-05-11 21:42:34 +0000850 switch (S->getStmtClass()) {
851 default:
852 break;
853 case Stmt::ParenExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000854 case Stmt::GenericSelectionExprClass:
855 S = cast<Expr>(S)->IgnoreParens();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000856 firstCharOnly = true;
857 continue;
John McCall56ca35d2011-02-17 10:25:35 +0000858 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9650cf32009-05-11 21:42:34 +0000859 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000860 S = cast<AbstractConditionalOperator>(S)->getCond();
Ted Kremenek9650cf32009-05-11 21:42:34 +0000861 firstCharOnly = true;
862 continue;
863 case Stmt::ChooseExprClass:
864 S = cast<ChooseExpr>(S)->getCond();
865 firstCharOnly = true;
866 continue;
867 case Stmt::BinaryOperatorClass:
868 S = cast<BinaryOperator>(S)->getLHS();
869 firstCharOnly = true;
870 continue;
871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenek9650cf32009-05-11 21:42:34 +0000873 break;
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Ted Kremenek9650cf32009-05-11 21:42:34 +0000876 if (S != Original)
877 L = PathDiagnosticLocation(S, L.getManager());
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek9650cf32009-05-11 21:42:34 +0000880 if (firstCharOnly)
881 L = PathDiagnosticLocation(L.asLocation());
882
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000883 return L;
884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Ted Kremenek14856d72009-04-06 23:06:54 +0000886 void popLocation() {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +0000887 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000888 // For contexts, we only one the first character as the range.
Ted Kremenek07c015c2009-05-15 02:46:13 +0000889 rawAddEdge(cleanUpLocation(CLocs.back(), true));
Ted Kremenek5c7168c2009-04-22 20:36:26 +0000890 }
Ted Kremenek14856d72009-04-06 23:06:54 +0000891 CLocs.pop_back();
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek14856d72009-04-06 23:06:54 +0000894public:
895 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
896 : PD(pd), PDB(pdb) {
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremeneka301a672009-04-22 18:16:20 +0000898 // If the PathDiagnostic already has pieces, add the enclosing statement
899 // of the first piece as a context as well.
Ted Kremenek14856d72009-04-06 23:06:54 +0000900 if (!PD.empty()) {
901 PrevLoc = PD.begin()->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Ted Kremenek14856d72009-04-06 23:06:54 +0000903 if (const Stmt *S = PrevLoc.asStmt())
Ted Kremeneke1baed32009-05-05 23:13:38 +0000904 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek14856d72009-04-06 23:06:54 +0000905 }
906 }
907
908 ~EdgeBuilder() {
909 while (!CLocs.empty()) popLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremeneka301a672009-04-22 18:16:20 +0000911 // Finally, add an initial edge from the start location of the first
912 // statement (if it doesn't already exist).
Sebastian Redld3a413d2009-04-26 20:35:05 +0000913 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
914 if (const CompoundStmt *CS =
Argyrios Kyrtzidis5f1bfc12010-07-07 11:31:34 +0000915 dyn_cast_or_null<CompoundStmt>(PDB.getCodeDecl().getBody()))
Ted Kremeneka301a672009-04-22 18:16:20 +0000916 if (!CS->body_empty()) {
917 SourceLocation Loc = (*CS->body_begin())->getLocStart();
918 rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager()));
919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenek14856d72009-04-06 23:06:54 +0000921 }
922
923 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek8bd4d032009-04-28 04:23:15 +0000925 void rawAddEdge(PathDiagnosticLocation NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenek14856d72009-04-06 23:06:54 +0000927 void addContext(const Stmt *S);
Ted Kremeneke1baed32009-05-05 23:13:38 +0000928 void addExtendedContext(const Stmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +0000929};
Ted Kremenek14856d72009-04-06 23:06:54 +0000930} // end anonymous namespace
931
932
933PathDiagnosticLocation
934EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
935 if (const Stmt *S = L.asStmt()) {
936 if (IsControlFlowExpr(S))
937 return L;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
939 return PDB.getEnclosingStmtLocation(S);
Ted Kremenek14856d72009-04-06 23:06:54 +0000940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremenek14856d72009-04-06 23:06:54 +0000942 return L;
943}
944
945bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
946 const PathDiagnosticLocation &Containee) {
947
948 if (Container == Containee)
949 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenek14856d72009-04-06 23:06:54 +0000951 if (Container.asDecl())
952 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek14856d72009-04-06 23:06:54 +0000954 if (const Stmt *S = Containee.asStmt())
955 if (const Stmt *ContainerS = Container.asStmt()) {
956 while (S) {
957 if (S == ContainerS)
958 return true;
959 S = PDB.getParent(S);
960 }
961 return false;
962 }
963
964 // Less accurate: compare using source ranges.
965 SourceRange ContainerR = Container.asRange();
966 SourceRange ContaineeR = Containee.asRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Ted Kremenek14856d72009-04-06 23:06:54 +0000968 SourceManager &SM = PDB.getSourceManager();
Chandler Carruth40278532011-07-25 16:49:02 +0000969 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
970 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
971 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
972 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chandler Carruth64211622011-07-25 21:09:52 +0000974 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
975 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
976 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
977 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenek14856d72009-04-06 23:06:54 +0000979 assert(ContainerBegLine <= ContainerEndLine);
Mike Stump1eb44332009-09-09 15:08:12 +0000980 assert(ContaineeBegLine <= ContaineeEndLine);
981
Ted Kremenek14856d72009-04-06 23:06:54 +0000982 return (ContainerBegLine <= ContaineeBegLine &&
983 ContainerEndLine >= ContaineeEndLine &&
984 (ContainerBegLine != ContaineeBegLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000985 SM.getExpansionColumnNumber(ContainerRBeg) <=
986 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
Ted Kremenek14856d72009-04-06 23:06:54 +0000987 (ContainerEndLine != ContaineeEndLine ||
Chandler Carrutha77c0312011-07-25 20:57:57 +0000988 SM.getExpansionColumnNumber(ContainerREnd) >=
989 SM.getExpansionColumnNumber(ContainerREnd)));
Ted Kremenek14856d72009-04-06 23:06:54 +0000990}
991
Ted Kremenek14856d72009-04-06 23:06:54 +0000992void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
993 if (!PrevLoc.isValid()) {
994 PrevLoc = NewLoc;
995 return;
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +0000998 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc);
999 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001001 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
Ted Kremenek14856d72009-04-06 23:06:54 +00001002 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek14856d72009-04-06 23:06:54 +00001004 // FIXME: Ignore intra-macro edges for now.
Chandler Carruth40278532011-07-25 16:49:02 +00001005 if (NewLocClean.asLocation().getExpansionLoc() ==
1006 PrevLocClean.asLocation().getExpansionLoc())
Ted Kremenek14856d72009-04-06 23:06:54 +00001007 return;
1008
Ted Kremenek8c8b0ad2009-05-11 19:50:47 +00001009 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1010 PrevLoc = NewLoc;
Ted Kremenek14856d72009-04-06 23:06:54 +00001011}
1012
1013void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) {
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Ted Kremeneka301a672009-04-22 18:16:20 +00001015 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1016 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremenek14856d72009-04-06 23:06:54 +00001018 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1019
1020 while (!CLocs.empty()) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001021 ContextLocation &TopContextLoc = CLocs.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremenek14856d72009-04-06 23:06:54 +00001023 // Is the top location context the same as the one for the new location?
1024 if (TopContextLoc == CLoc) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001025 if (alwaysAdd) {
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001026 if (IsConsumedExpr(TopContextLoc) &&
1027 !IsControlFlowExpr(TopContextLoc.asStmt()))
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001028 TopContextLoc.markDead();
1029
Ted Kremenek14856d72009-04-06 23:06:54 +00001030 rawAddEdge(NewLoc);
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001031 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001032
1033 return;
1034 }
1035
1036 if (containsLocation(TopContextLoc, CLoc)) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001037 if (alwaysAdd) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001038 rawAddEdge(NewLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Ted Kremenek4c6f8d32009-05-04 18:15:17 +00001040 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) {
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001041 CLocs.push_back(ContextLocation(CLoc, true));
1042 return;
1043 }
1044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenek14856d72009-04-06 23:06:54 +00001046 CLocs.push_back(CLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001047 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001048 }
1049
1050 // Context does not contain the location. Flush it.
1051 popLocation();
1052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Ted Kremenek5c7168c2009-04-22 20:36:26 +00001054 // If we reach here, there is no enclosing context. Just add the edge.
1055 rawAddEdge(NewLoc);
Ted Kremenek14856d72009-04-06 23:06:54 +00001056}
1057
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001058bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1059 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1060 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek8f9b1b32009-05-01 16:08:09 +00001062 return false;
1063}
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Ted Kremeneke1baed32009-05-05 23:13:38 +00001065void EdgeBuilder::addExtendedContext(const Stmt *S) {
1066 if (!S)
1067 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001068
1069 const Stmt *Parent = PDB.getParent(S);
Ted Kremeneke1baed32009-05-05 23:13:38 +00001070 while (Parent) {
1071 if (isa<CompoundStmt>(Parent))
1072 Parent = PDB.getParent(Parent);
1073 else
1074 break;
1075 }
1076
1077 if (Parent) {
1078 switch (Parent->getStmtClass()) {
1079 case Stmt::DoStmtClass:
1080 case Stmt::ObjCAtSynchronizedStmtClass:
1081 addContext(Parent);
1082 default:
1083 break;
1084 }
1085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremeneke1baed32009-05-05 23:13:38 +00001087 addContext(S);
1088}
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremenek14856d72009-04-06 23:06:54 +00001090void EdgeBuilder::addContext(const Stmt *S) {
1091 if (!S)
1092 return;
1093
1094 PathDiagnosticLocation L(S, PDB.getSourceManager());
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Ted Kremenek14856d72009-04-06 23:06:54 +00001096 while (!CLocs.empty()) {
1097 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1098
1099 // Is the top location context the same as the one for the new location?
1100 if (TopContextLoc == L)
1101 return;
1102
1103 if (containsLocation(TopContextLoc, L)) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001104 CLocs.push_back(L);
Mike Stump1eb44332009-09-09 15:08:12 +00001105 return;
Ted Kremenek14856d72009-04-06 23:06:54 +00001106 }
1107
1108 // Context does not contain the location. Flush it.
1109 popLocation();
1110 }
1111
1112 CLocs.push_back(L);
1113}
1114
1115static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD,
1116 PathDiagnosticBuilder &PDB,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001117 const ExplodedNode *N) {
Ted Kremenek14856d72009-04-06 23:06:54 +00001118 EdgeBuilder EB(PD, PDB);
1119
Ted Kremenek9c378f72011-08-12 23:37:29 +00001120 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek14856d72009-04-06 23:06:54 +00001121 while (NextNode) {
1122 N = NextNode;
1123 NextNode = GetPredecessorNode(N);
1124 ProgramPoint P = N->getLocation();
1125
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001126 do {
1127 // Block edges.
1128 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
1129 const CFGBlock &Blk = *BE->getSrc();
1130 const Stmt *Term = Blk.getTerminator();
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001132 // Are we jumping to the head of a loop? Add a special diagnostic.
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001133 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001134 PathDiagnosticLocation L(Loop, PDB.getSourceManager());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001135 const CompoundStmt *CS = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001137 if (!Term) {
1138 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1139 CS = dyn_cast<CompoundStmt>(FS->getBody());
1140 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
Mike Stump1eb44332009-09-09 15:08:12 +00001141 CS = dyn_cast<CompoundStmt>(WS->getBody());
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001144 PathDiagnosticEventPiece *p =
1145 new PathDiagnosticEventPiece(L,
Ted Kremenek07c015c2009-05-15 02:46:13 +00001146 "Looping back to the head of the loop");
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001148 EB.addEdge(p->getLocation(), true);
1149 PD.push_front(p);
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001151 if (CS) {
Ted Kremenek07c015c2009-05-15 02:46:13 +00001152 PathDiagnosticLocation BL(CS->getRBracLoc(),
1153 PDB.getSourceManager());
1154 BL = PathDiagnosticLocation(BL.asLocation());
1155 EB.addEdge(BL);
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001156 }
Ted Kremenek8bd4d032009-04-28 04:23:15 +00001157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremenekddb7bab2009-05-15 01:50:15 +00001159 if (Term)
1160 EB.addContext(Term);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001162 break;
Ted Kremenek14856d72009-04-06 23:06:54 +00001163 }
1164
Mike Stump1eb44332009-09-09 15:08:12 +00001165 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001166 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) {
1167 const Stmt *stmt = S->getStmt();
1168 if (IsControlFlowExpr(stmt)) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001169 // Add the proper context for '&&', '||', and '?'.
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001170 EB.addContext(stmt);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001171 }
1172 else
Ted Kremenek3c0349e2011-03-01 03:15:10 +00001173 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001174 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00001175
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001176 break;
1177 }
1178 } while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenekdd986cc2009-05-07 00:45:33 +00001180 if (!NextNode)
Ted Kremenek14856d72009-04-06 23:06:54 +00001181 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Anna Zaks8e6431a2011-08-18 22:37:56 +00001183 // Add pieces from custom visitors.
1184 BugReport *R = PDB.getBugReport();
1185 for (BugReport::visitor_iterator I = R->visitor_begin(),
1186 E = R->visitor_end(); I!=E; ++I) {
1187 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001188 const PathDiagnosticLocation &Loc = p->getLocation();
1189 EB.addEdge(Loc, true);
1190 PD.push_front(p);
1191 if (const Stmt *S = Loc.asStmt())
Mike Stump1eb44332009-09-09 15:08:12 +00001192 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
Ted Kremenek8966bc12009-05-06 21:39:49 +00001193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194 }
Ted Kremenek14856d72009-04-06 23:06:54 +00001195 }
1196}
1197
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001198//===----------------------------------------------------------------------===//
Ted Kremenekcf118d42009-02-04 23:49:09 +00001199// Methods for BugType and subclasses.
1200//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001201BugType::~BugType() { }
1202
Ted Kremenekcf118d42009-02-04 23:49:09 +00001203void BugType::FlushReports(BugReporter &BR) {}
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001204
Ted Kremenekcf118d42009-02-04 23:49:09 +00001205//===----------------------------------------------------------------------===//
1206// Methods for BugReport and subclasses.
1207//===----------------------------------------------------------------------===//
Anna Zakse172e8b2011-08-17 23:00:25 +00001208
Anna Zaks8e6431a2011-08-18 22:37:56 +00001209void BugReport::addVisitor(BugReporterVisitor* visitor) {
1210 if (!visitor)
1211 return;
1212
1213 llvm::FoldingSetNodeID ID;
1214 visitor->Profile(ID);
1215 void *InsertPos;
1216
1217 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
1218 delete visitor;
1219 return;
1220 }
1221
1222 CallbacksSet.InsertNode(visitor, InsertPos);
1223 Callbacks = F.add(visitor, Callbacks);
1224}
1225
1226BugReport::~BugReport() {
1227 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
Anna Zaksdc757b02011-08-19 23:21:56 +00001228 delete *I;
Anna Zaks8e6431a2011-08-18 22:37:56 +00001229 }
1230}
Anna Zakse172e8b2011-08-17 23:00:25 +00001231
1232void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
1233 hash.AddPointer(&BT);
1234 hash.AddInteger(getLocation().getRawEncoding());
1235 hash.AddString(Description);
1236
1237 for (SmallVectorImpl<SourceRange>::const_iterator I =
1238 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1239 const SourceRange range = *I;
1240 if (!range.isValid())
1241 continue;
1242 hash.AddInteger(range.getBegin().getRawEncoding());
1243 hash.AddInteger(range.getEnd().getRawEncoding());
1244 }
1245}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001246
Ted Kremenek9c378f72011-08-12 23:37:29 +00001247const Stmt *BugReport::getStmt() const {
Anna Zakse172e8b2011-08-17 23:00:25 +00001248 if (!ErrorNode)
1249 return 0;
1250
Tom Care212f6d32010-09-16 03:50:38 +00001251 ProgramPoint ProgP = ErrorNode->getLocation();
Ted Kremenek5f85e172009-07-22 22:35:28 +00001252 const Stmt *S = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek9c378f72011-08-12 23:37:29 +00001254 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) {
Zhongxing Xufafd3832009-08-20 01:23:34 +00001255 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
Zhongxing Xu50d5bc42009-08-18 08:46:04 +00001256 if (BE->getBlock() == &Exit)
Tom Care212f6d32010-09-16 03:50:38 +00001257 S = GetPreviousStmt(ErrorNode);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001258 }
Ted Kremenek5f85e172009-07-22 22:35:28 +00001259 if (!S)
Mike Stump1eb44332009-09-09 15:08:12 +00001260 S = GetStmt(ProgP);
1261
1262 return S;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001263}
1264
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001265std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator>
Anna Zakse172e8b2011-08-17 23:00:25 +00001266BugReport::getRanges() {
1267 // If no custom ranges, add the range of the statement corresponding to
1268 // the error node.
1269 if (Ranges.empty()) {
1270 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
1271 addRange(E->getSourceRange());
1272 else
1273 return std::make_pair(ranges_iterator(), ranges_iterator());
1274 }
1275
1276 return std::make_pair(Ranges.begin(), Ranges.end());
Ted Kremenekf1ae7052008-04-03 17:57:38 +00001277}
1278
Mike Stump1eb44332009-09-09 15:08:12 +00001279SourceLocation BugReport::getLocation() const {
Anna Zaksb7530a42011-08-17 23:21:23 +00001280 if (ErrorNode) {
1281 (Location.isInvalid() &&
1282 "Either Location or ErrorNode should be specified but not both.");
1283
Ted Kremenek9c378f72011-08-12 23:37:29 +00001284 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) {
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001285 // For member expressions, return the location of the '.' or '->'.
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001286 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001287 return ME->getMemberLoc();
Ted Kremenek5b9bd212009-09-11 22:07:28 +00001288 // For binary operators, return the location of the operator.
1289 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
1290 return B->getOperatorLoc();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001291
Ted Kremenekcf118d42009-02-04 23:49:09 +00001292 return S->getLocStart();
Ted Kremenek9b5e5052009-02-27 20:05:10 +00001293 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001294
Anna Zaksb7530a42011-08-17 23:21:23 +00001295 } else {
1296 assert(Location.isValid());
1297 return Location;
1298 }
1299
Ted Kremenekcf118d42009-02-04 23:49:09 +00001300 return FullSourceLoc();
Ted Kremenekd2f642b2008-04-14 17:39:48 +00001301}
1302
Ted Kremenekcf118d42009-02-04 23:49:09 +00001303//===----------------------------------------------------------------------===//
1304// Methods for BugReporter and subclasses.
1305//===----------------------------------------------------------------------===//
1306
1307BugReportEquivClass::~BugReportEquivClass() {
Ted Kremenek8966bc12009-05-06 21:39:49 +00001308 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001309}
1310
Zhongxing Xua2f4ec02009-09-05 06:06:49 +00001311GRBugReporter::~GRBugReporter() { }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001312BugReporterData::~BugReporterData() {}
1313
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001314ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001315
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001316ProgramStateManager&
Ted Kremenekcf118d42009-02-04 23:49:09 +00001317GRBugReporter::getStateManager() { return Eng.getStateManager(); }
1318
Anna Zaks3b030a22011-08-19 01:57:09 +00001319BugReporter::~BugReporter() {
1320 FlushReports();
1321
1322 // Free the bug reports we are tracking.
1323 typedef std::vector<BugReportEquivClass *> ContTy;
1324 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
1325 I != E; ++I) {
1326 delete *I;
1327 }
1328}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001329
1330void BugReporter::FlushReports() {
1331 if (BugTypes.isEmpty())
1332 return;
1333
1334 // First flush the warnings for each BugType. This may end up creating new
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001335 // warnings and new BugTypes.
1336 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
1337 // Turn NSErrorChecker into a proper checker and remove this.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001338 SmallVector<const BugType*, 16> bugTypes;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001339 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001340 bugTypes.push_back(*I);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001341 for (SmallVector<const BugType*, 16>::iterator
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001342 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
Ted Kremenekcf118d42009-02-04 23:49:09 +00001343 const_cast<BugType*>(*I)->FlushReports(*this);
1344
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001345 typedef llvm::FoldingSet<BugReportEquivClass> SetTy;
1346 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){
1347 BugReportEquivClass& EQ = *EI;
1348 FlushReport(EQ);
Ted Kremenek94826a72008-04-03 04:59:14 +00001349 }
Ted Kremenekcf118d42009-02-04 23:49:09 +00001350
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001351 // BugReporter owns and deletes only BugTypes created implicitly through
1352 // EmitBasicReport.
1353 // FIXME: There are leaks from checkers that assume that the BugTypes they
1354 // create will be destroyed by the BugReporter.
1355 for (llvm::StringMap<BugType*>::iterator
1356 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I)
1357 delete I->second;
1358
Ted Kremenekcf118d42009-02-04 23:49:09 +00001359 // Remove all references to the BugType objects.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001360 BugTypes = F.getEmptySet();
Ted Kremenekcf118d42009-02-04 23:49:09 +00001361}
1362
1363//===----------------------------------------------------------------------===//
1364// PathDiagnostics generation.
1365//===----------------------------------------------------------------------===//
1366
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001367static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001368 std::pair<ExplodedNode*, unsigned> >
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001369MakeReportGraph(const ExplodedGraph* G,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001370 SmallVectorImpl<const ExplodedNode*> &nodes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Ted Kremenekcf118d42009-02-04 23:49:09 +00001372 // Create the trimmed graph. It will contain the shortest paths from the
Mike Stump1eb44332009-09-09 15:08:12 +00001373 // error nodes to the root. In the new graph we should only have one
Ted Kremenekcf118d42009-02-04 23:49:09 +00001374 // error node unless there are two or more error nodes with the same minimum
1375 // path length.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001376 ExplodedGraph* GTrim;
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001377 InterExplodedGraphMap* NMap;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001378
1379 llvm::DenseMap<const void*, const void*> InverseMap;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001380 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(),
1381 &InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Ted Kremenekcf118d42009-02-04 23:49:09 +00001383 // Create owning pointers for GTrim and NMap just to ensure that they are
1384 // released when this function exists.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001385 llvm::OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001386 llvm::OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Ted Kremenekcf118d42009-02-04 23:49:09 +00001388 // Find the (first) error node in the trimmed graph. We just need to consult
1389 // the node map (NMap) which maps from nodes in the original graph to nodes
1390 // in the new graph.
Ted Kremenek938332c2009-05-16 01:11:58 +00001391
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001392 std::queue<const ExplodedNode*> WS;
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001393 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy;
Ted Kremenek938332c2009-05-16 01:11:58 +00001394 IndexMapTy IndexMap;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001395
Ted Kremenek40406fe2010-12-03 06:52:30 +00001396 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) {
1397 const ExplodedNode *originalNode = nodes[nodeIndex];
1398 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) {
Ted Kremenek938332c2009-05-16 01:11:58 +00001399 WS.push(N);
Ted Kremenek40406fe2010-12-03 06:52:30 +00001400 IndexMap[originalNode] = nodeIndex;
Ted Kremenekcf118d42009-02-04 23:49:09 +00001401 }
Ted Kremenek40406fe2010-12-03 06:52:30 +00001402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Ted Kremenek938332c2009-05-16 01:11:58 +00001404 assert(!WS.empty() && "No error node found in the trimmed graph.");
Ted Kremenekcf118d42009-02-04 23:49:09 +00001405
1406 // Create a new (third!) graph with a single path. This is the graph
1407 // that will be returned to the caller.
Zhongxing Xuc77a5512010-07-01 07:10:59 +00001408 ExplodedGraph *GNew = new ExplodedGraph();
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Ted Kremenek10aa5542009-03-12 23:41:59 +00001410 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001411 // to the root node, and then construct a new graph that contains only
1412 // a single path.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001413 llvm::DenseMap<const void*,unsigned> Visited;
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001415 unsigned cnt = 0;
Ted Kremenek9c378f72011-08-12 23:37:29 +00001416 const ExplodedNode *Root = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001418 while (!WS.empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001419 const ExplodedNode *Node = WS.front();
Ted Kremenek10aa5542009-03-12 23:41:59 +00001420 WS.pop();
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001422 if (Visited.find(Node) != Visited.end())
1423 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001425 Visited[Node] = cnt++;
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001427 if (Node->pred_empty()) {
1428 Root = Node;
1429 break;
1430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001432 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(),
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001433 E=Node->pred_end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +00001434 WS.push(*I);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001435 }
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenek938332c2009-05-16 01:11:58 +00001437 assert(Root);
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremenek10aa5542009-03-12 23:41:59 +00001439 // Now walk from the root down the BFS path, always taking the successor
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001440 // with the lowest number.
Mike Stump1eb44332009-09-09 15:08:12 +00001441 ExplodedNode *Last = 0, *First = 0;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001442 NodeBackMap *BM = new NodeBackMap();
Ted Kremenek938332c2009-05-16 01:11:58 +00001443 unsigned NodeIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001445 for ( const ExplodedNode *N = Root ;;) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001446 // Lookup the number associated with the current node.
Ted Kremenek3148eb42009-01-24 00:55:43 +00001447 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N);
Ted Kremenek938332c2009-05-16 01:11:58 +00001448 assert(I != Visited.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001450 // Create the equivalent node in the new graph with the same state
1451 // and location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00001452 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState());
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001454 // Store the mapping to the original node.
1455 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N);
1456 assert(IMitr != InverseMap.end() && "No mapping to original node.");
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001457 (*BM)[NewN] = (const ExplodedNode*) IMitr->second;
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001459 // Link up the new node with the previous node.
1460 if (Last)
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +00001461 NewN->addPredecessor(Last, *GNew);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001463 Last = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001465 // Are we at the final node?
Ted Kremenek938332c2009-05-16 01:11:58 +00001466 IndexMapTy::iterator IMI =
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001467 IndexMap.find((const ExplodedNode*)(IMitr->second));
Ted Kremenek938332c2009-05-16 01:11:58 +00001468 if (IMI != IndexMap.end()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001469 First = NewN;
Ted Kremenek938332c2009-05-16 01:11:58 +00001470 NodeIndex = IMI->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001471 break;
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001474 // Find the next successor node. We choose the node that is marked
1475 // with the lowest DFS number.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001476 ExplodedNode::const_succ_iterator SI = N->succ_begin();
1477 ExplodedNode::const_succ_iterator SE = N->succ_end();
Ted Kremenekc1da4412008-06-17 19:14:06 +00001478 N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001480 for (unsigned MinVal = 0; SI != SE; ++SI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001482 I = Visited.find(*SI);
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001484 if (I == Visited.end())
1485 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001487 if (!N || I->second < MinVal) {
1488 N = *SI;
1489 MinVal = I->second;
Ted Kremenekc1da4412008-06-17 19:14:06 +00001490 }
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Ted Kremenek938332c2009-05-16 01:11:58 +00001493 assert(N);
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001494 }
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Ted Kremenek938332c2009-05-16 01:11:58 +00001496 assert(First);
1497
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001498 return std::make_pair(std::make_pair(GNew, BM),
1499 std::make_pair(First, NodeIndex));
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001500}
1501
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001502/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
1503/// and collapses PathDiagosticPieces that are expanded by macros.
1504static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) {
1505 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> >
1506 MacroStackTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001508 typedef std::vector<PathDiagnosticPiece*>
1509 PiecesTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001511 MacroStackTy MacroStack;
1512 PiecesTy Pieces;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001514 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) {
1515 // Get the location of the PathDiagnosticPiece.
Mike Stump1eb44332009-09-09 15:08:12 +00001516 const FullSourceLoc Loc = I->getLocation().asLocation();
1517
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001518 // Determine the instantiation location, which is the location we group
1519 // related PathDiagnosticPieces.
Mike Stump1eb44332009-09-09 15:08:12 +00001520 SourceLocation InstantiationLoc = Loc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001521 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001522 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001524 if (Loc.isFileID()) {
1525 MacroStack.clear();
1526 Pieces.push_back(&*I);
1527 continue;
1528 }
1529
1530 assert(Loc.isMacroID());
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001532 // Is the PathDiagnosticPiece within the same macro group?
1533 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
1534 MacroStack.back().first->push_back(&*I);
1535 continue;
1536 }
1537
1538 // We aren't in the same group. Are we descending into a new macro
1539 // or are part of an old one?
1540 PathDiagnosticMacroPiece *MacroGroup = 0;
1541
1542 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
Chandler Carruth40278532011-07-25 16:49:02 +00001543 SM.getExpansionLoc(Loc) :
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001544 SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001546 // Walk the entire macro stack.
1547 while (!MacroStack.empty()) {
1548 if (InstantiationLoc == MacroStack.back().second) {
1549 MacroGroup = MacroStack.back().first;
1550 break;
1551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001553 if (ParentInstantiationLoc == MacroStack.back().second) {
1554 MacroGroup = MacroStack.back().first;
1555 break;
1556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001558 MacroStack.pop_back();
1559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001561 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
1562 // Create a new macro group and add it to the stack.
1563 PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc);
1564
1565 if (MacroGroup)
1566 MacroGroup->push_back(NewGroup);
1567 else {
1568 assert(InstantiationLoc.isFileID());
1569 Pieces.push_back(NewGroup);
1570 }
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001572 MacroGroup = NewGroup;
1573 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
1574 }
1575
1576 // Finally, add the PathDiagnosticPiece to the group.
1577 MacroGroup->push_back(&*I);
1578 }
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001580 // Now take the pieces and construct a new PathDiagnostic.
1581 PD.resetPath(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001583 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) {
1584 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I))
1585 if (!MP->containsEvent()) {
1586 delete MP;
1587 continue;
1588 }
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Ted Kremenek0e5c8d42009-03-10 05:16:17 +00001590 PD.push_back(*I);
1591 }
1592}
1593
Ted Kremenek7dc86642009-03-31 20:22:36 +00001594void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001595 SmallVectorImpl<BugReport *> &bugReports) {
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Ted Kremenek40406fe2010-12-03 06:52:30 +00001597 assert(!bugReports.empty());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001598 SmallVector<const ExplodedNode *, 10> errorNodes;
1599 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(),
Ted Kremenek40406fe2010-12-03 06:52:30 +00001600 E = bugReports.end(); I != E; ++I) {
1601 errorNodes.push_back((*I)->getErrorNode());
1602 }
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Ted Kremeneka43a1eb2008-04-23 23:02:12 +00001604 // Construct a new graph that contains only a single path from the error
Mike Stump1eb44332009-09-09 15:08:12 +00001605 // node to a root.
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001606 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001607 std::pair<ExplodedNode*, unsigned> >&
Ted Kremenek40406fe2010-12-03 06:52:30 +00001608 GPair = MakeReportGraph(&getGraph(), errorNodes);
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Ted Kremenekcf118d42009-02-04 23:49:09 +00001610 // Find the BugReport with the original location.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001611 assert(GPair.second.second < bugReports.size());
1612 BugReport *R = bugReports[GPair.second.second];
Ted Kremenekcf118d42009-02-04 23:49:09 +00001613 assert(R && "No original report found for sliced graph.");
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Zhongxing Xu38b02b92009-08-06 06:28:40 +00001615 llvm::OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first);
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001616 llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001617 const ExplodedNode *N = GPair.second.first;
Mike Stump1eb44332009-09-09 15:08:12 +00001618
1619 // Start building the path diagnostic...
Ted Kremenek8966bc12009-05-06 21:39:49 +00001620 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), getPathDiagnosticClient());
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Anna Zaks8e6431a2011-08-18 22:37:56 +00001622 // Register additional node visitors.
Anna Zaks50bbc162011-08-19 22:33:38 +00001623 R->addVisitor(new NilReceiverBRVisitor());
1624 R->addVisitor(new ConditionBRVisitor());
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Anna Zaks23f395e2011-08-20 01:27:22 +00001626 // Generate the very last diagnostic piece - the piece is visible before
1627 // the trace is expanded.
1628 PathDiagnosticPiece *LastPiece = 0;
1629 for (BugReport::visitor_iterator I = R->visitor_begin(),
1630 E = R->visitor_end(); I!=E; ++I) {
1631 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) {
1632 assert (!LastPiece &&
1633 "There can only be one final piece in a diagnostic.");
1634 LastPiece = Piece;
1635 }
1636 }
1637 if (!LastPiece)
1638 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
1639 if (LastPiece)
1640 PD.push_back(LastPiece);
1641 else
1642 return;
1643
Ted Kremenek7dc86642009-03-31 20:22:36 +00001644 switch (PDB.getGenerationScheme()) {
1645 case PathDiagnosticClient::Extensive:
Ted Kremenek8966bc12009-05-06 21:39:49 +00001646 GenerateExtensivePathDiagnostic(PD, PDB, N);
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +00001647 break;
Ted Kremenek7dc86642009-03-31 20:22:36 +00001648 case PathDiagnosticClient::Minimal:
1649 GenerateMinimalPathDiagnostic(PD, PDB, N);
1650 break;
1651 }
Ted Kremenek7dc86642009-03-31 20:22:36 +00001652}
1653
Ted Kremenekcf118d42009-02-04 23:49:09 +00001654void BugReporter::Register(BugType *BT) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001655 BugTypes = F.add(BugTypes, BT);
Ted Kremenek76d90c82008-05-16 18:33:14 +00001656}
1657
Mike Stump1eb44332009-09-09 15:08:12 +00001658void BugReporter::EmitReport(BugReport* R) {
Ted Kremenekcf118d42009-02-04 23:49:09 +00001659 // Compute the bug report's hash to determine its equivalence class.
1660 llvm::FoldingSetNodeID ID;
1661 R->Profile(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00001662
1663 // Lookup the equivance class. If there isn't one, create it.
Ted Kremenekcf118d42009-02-04 23:49:09 +00001664 BugType& BT = R->getBugType();
1665 Register(&BT);
1666 void *InsertPos;
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001667 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenekcf118d42009-02-04 23:49:09 +00001669 if (!EQ) {
1670 EQ = new BugReportEquivClass(R);
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001671 EQClasses.InsertNode(EQ, InsertPos);
Anna Zaks3b030a22011-08-19 01:57:09 +00001672 EQClassesVector.push_back(EQ);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001673 }
1674 else
1675 EQ->AddReport(R);
Ted Kremenek61f3e052008-04-03 04:42:52 +00001676}
1677
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001678
1679//===----------------------------------------------------------------------===//
1680// Emitting reports in equivalence classes.
1681//===----------------------------------------------------------------------===//
1682
1683namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001684struct FRIEC_WLItem {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001685 const ExplodedNode *N;
1686 ExplodedNode::const_succ_iterator I, E;
1687
1688 FRIEC_WLItem(const ExplodedNode *n)
1689 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
1690};
1691}
1692
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001693static BugReport *
1694FindReportInEquivalenceClass(BugReportEquivClass& EQ,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001695 SmallVectorImpl<BugReport*> &bugReports) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001696
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001697 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
1698 assert(I != E);
1699 BugReport *R = *I;
1700 BugType& BT = R->getBugType();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001701
Ted Kremenek40406fe2010-12-03 06:52:30 +00001702 // If we don't need to suppress any of the nodes because they are
1703 // post-dominated by a sink, simply add all the nodes in the equivalence class
1704 // to 'Nodes'. Any of the reports will serve as a "representative" report.
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001705 if (!BT.isSuppressOnSink()) {
1706 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001707 const ExplodedNode *N = I->getErrorNode();
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001708 if (N) {
1709 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001710 bugReports.push_back(R);
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001711 }
1712 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001713 return R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001714 }
1715
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001716 // For bug reports that should be suppressed when all paths are post-dominated
1717 // by a sink node, iterate through the reports in the equivalence class
1718 // until we find one that isn't post-dominated (if one exists). We use a
1719 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
1720 // this as a recursive function, but we don't want to risk blowing out the
1721 // stack for very long paths.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001722 BugReport *exampleReport = 0;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001723
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001724 for (; I != E; ++I) {
1725 R = *I;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001726 const ExplodedNode *errorNode = R->getErrorNode();
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001727
Ted Kremenek40406fe2010-12-03 06:52:30 +00001728 if (!errorNode)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001729 continue;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001730 if (errorNode->isSink()) {
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001731 assert(false &&
1732 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001733 return 0;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001734 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001735 // No successors? By definition this nodes isn't post-dominated by a sink.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001736 if (errorNode->succ_empty()) {
1737 bugReports.push_back(R);
1738 if (!exampleReport)
1739 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001740 continue;
1741 }
1742
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001743 // At this point we know that 'N' is not a sink and it has at least one
1744 // successor. Use a DFS worklist to find a non-sink end-of-path node.
1745 typedef FRIEC_WLItem WLItem;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001746 typedef SmallVector<WLItem, 10> DFSWorkList;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001747 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
1748
1749 DFSWorkList WL;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001750 WL.push_back(errorNode);
1751 Visited[errorNode] = 1;
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001752
1753 while (!WL.empty()) {
1754 WLItem &WI = WL.back();
1755 assert(!WI.N->succ_empty());
1756
1757 for (; WI.I != WI.E; ++WI.I) {
1758 const ExplodedNode *Succ = *WI.I;
1759 // End-of-path node?
1760 if (Succ->succ_empty()) {
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001761 // If we found an end-of-path node that is not a sink.
1762 if (!Succ->isSink()) {
Ted Kremenek40406fe2010-12-03 06:52:30 +00001763 bugReports.push_back(R);
1764 if (!exampleReport)
1765 exampleReport = R;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001766 WL.clear();
1767 break;
1768 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001769 // Found a sink? Continue on to the next successor.
1770 continue;
1771 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001772 // Mark the successor as visited. If it hasn't been explored,
1773 // enqueue it to the DFS worklist.
1774 unsigned &mark = Visited[Succ];
1775 if (!mark) {
1776 mark = 1;
1777 WL.push_back(Succ);
1778 break;
1779 }
1780 }
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001781
1782 // The worklist may have been cleared at this point. First
1783 // check if it is empty before checking the last item.
1784 if (!WL.empty() && &WL.back() == &WI)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001785 WL.pop_back();
1786 }
1787 }
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001788
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001789 // ExampleReport will be NULL if all the nodes in the equivalence class
1790 // were post-dominated by sinks.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001791 return exampleReport;
Ted Kremenek61f52bd2010-09-09 19:05:34 +00001792}
Ted Kremeneke0a58072009-09-18 22:37:37 +00001793
1794//===----------------------------------------------------------------------===//
1795// DiagnosticCache. This is a hack to cache analyzer diagnostics. It
1796// uses global state, which eventually should go elsewhere.
1797//===----------------------------------------------------------------------===//
1798namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001799class DiagCacheItem : public llvm::FoldingSetNode {
Ted Kremeneke0a58072009-09-18 22:37:37 +00001800 llvm::FoldingSetNodeID ID;
1801public:
1802 DiagCacheItem(BugReport *R, PathDiagnostic *PD) {
1803 ID.AddString(R->getBugType().getName());
1804 ID.AddString(R->getBugType().getCategory());
1805 ID.AddString(R->getDescription());
1806 ID.AddInteger(R->getLocation().getRawEncoding());
1807 PD->Profile(ID);
1808 }
1809
1810 void Profile(llvm::FoldingSetNodeID &id) {
1811 id = ID;
1812 }
1813
1814 llvm::FoldingSetNodeID &getID() { return ID; }
1815};
1816}
1817
1818static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) {
1819 // FIXME: Eventually this diagnostic cache should reside in something
1820 // like AnalysisManager instead of being a static variable. This is
1821 // really unsafe in the long term.
1822 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache;
1823 static DiagnosticCache DC;
1824
1825 void *InsertPos;
1826 DiagCacheItem *Item = new DiagCacheItem(R, PD);
1827
1828 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) {
1829 delete Item;
1830 return true;
1831 }
1832
1833 DC.InsertNode(Item, InsertPos);
1834 return false;
1835}
1836
Ted Kremenekcf118d42009-02-04 23:49:09 +00001837void BugReporter::FlushReport(BugReportEquivClass& EQ) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001838 SmallVector<BugReport*, 10> bugReports;
Ted Kremenek40406fe2010-12-03 06:52:30 +00001839 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
1840 if (!exampleReport)
Ted Kremenek06c9cb42009-09-14 22:01:32 +00001841 return;
1842
Ted Kremenekd49967f2009-04-29 21:58:13 +00001843 PathDiagnosticClient* PD = getPathDiagnosticClient();
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Ted Kremenekcf118d42009-02-04 23:49:09 +00001845 // FIXME: Make sure we use the 'R' for the path that was actually used.
Mike Stump1eb44332009-09-09 15:08:12 +00001846 // Probably doesn't make a difference in practice.
Ted Kremenek40406fe2010-12-03 06:52:30 +00001847 BugType& BT = exampleReport->getBugType();
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Ted Kremenekd49967f2009-04-29 21:58:13 +00001849 llvm::OwningPtr<PathDiagnostic>
Ted Kremenek40406fe2010-12-03 06:52:30 +00001850 D(new PathDiagnostic(exampleReport->getBugType().getName(),
Ted Kremenekda0e8422009-04-29 22:05:03 +00001851 !PD || PD->useVerboseDescription()
Ted Kremenek40406fe2010-12-03 06:52:30 +00001852 ? exampleReport->getDescription()
1853 : exampleReport->getShortDescription(),
Ted Kremenekd49967f2009-04-29 21:58:13 +00001854 BT.getCategory()));
1855
Ted Kremenek40406fe2010-12-03 06:52:30 +00001856 if (!bugReports.empty())
1857 GeneratePathDiagnostic(*D.get(), bugReports);
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Ted Kremenek40406fe2010-12-03 06:52:30 +00001859 if (IsCachedDiagnostic(exampleReport, D.get()))
Ted Kremeneke0a58072009-09-18 22:37:37 +00001860 return;
1861
Ted Kremenek072192b2008-04-30 23:47:44 +00001862 // Get the meta data.
Anna Zaks7f2531c2011-08-22 20:31:28 +00001863 const BugReport::ExtraTextList &Meta =
1864 exampleReport->getExtraText();
1865 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
1866 e = Meta.end(); i != e; ++i) {
1867 D->addMeta(*i);
1868 }
Ted Kremenek75840e12008-04-18 01:56:37 +00001869
Ted Kremenek3148eb42009-01-24 00:55:43 +00001870 // Emit a summary diagnostic to the regular Diagnostics engine.
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001871 BugReport::ranges_iterator Beg, End;
1872 llvm::tie(Beg, End) = exampleReport->getRanges();
Ted Kremenek40406fe2010-12-03 06:52:30 +00001873 Diagnostic &Diag = getDiagnostic();
1874 FullSourceLoc L(exampleReport->getLocation(), getSourceManager());
Ted Kremenekc213b482010-01-15 07:56:51 +00001875
1876 // Search the description for '%', as that will be interpretted as a
1877 // format character by FormatDiagnostics.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001878 StringRef desc = exampleReport->getShortDescription();
Ted Kremenekc213b482010-01-15 07:56:51 +00001879 unsigned ErrorDiag;
1880 {
1881 llvm::SmallString<512> TmpStr;
1882 llvm::raw_svector_ostream Out(TmpStr);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001883 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I)
Ted Kremenekc213b482010-01-15 07:56:51 +00001884 if (*I == '%')
1885 Out << "%%";
1886 else
1887 Out << *I;
1888
1889 Out.flush();
1890 ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, TmpStr);
1891 }
Ted Kremenek57202072008-07-14 17:40:50 +00001892
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001893 {
1894 DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001895 for (BugReport::ranges_iterator I = Beg; I != End; ++I)
Argyrios Kyrtzidisb6b7e7b2010-12-03 00:58:10 +00001896 diagBuilder << *I;
Ted Kremenek2f0e89e2008-04-18 22:56:53 +00001897 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00001898
1899 // Emit a full diagnostic for the path if we have a PathDiagnosticClient.
1900 if (!PD)
1901 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001902
1903 if (D->empty()) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001904 PathDiagnosticPiece *piece =
Ted Kremenek40406fe2010-12-03 06:52:30 +00001905 new PathDiagnosticEventPiece(L, exampleReport->getDescription());
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +00001906
Ted Kremenek3148eb42009-01-24 00:55:43 +00001907 for ( ; Beg != End; ++Beg) piece->addRange(*Beg);
1908 D->push_back(piece);
1909 }
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Ted Kremenek3148eb42009-01-24 00:55:43 +00001911 PD->HandlePathDiagnostic(D.take());
Ted Kremenek61f3e052008-04-03 04:42:52 +00001912}
Ted Kremenek57202072008-07-14 17:40:50 +00001913
Chris Lattner5f9e2722011-07-23 10:55:15 +00001914void BugReporter::EmitBasicReport(StringRef name, StringRef str,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001915 SourceLocation Loc,
1916 SourceRange* RBeg, unsigned NumRanges) {
1917 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges);
1918}
Ted Kremenekcf118d42009-02-04 23:49:09 +00001919
Chris Lattner5f9e2722011-07-23 10:55:15 +00001920void BugReporter::EmitBasicReport(StringRef name,
1921 StringRef category,
1922 StringRef str, SourceLocation Loc,
Ted Kremenek8c036c72008-09-20 04:23:38 +00001923 SourceRange* RBeg, unsigned NumRanges) {
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001925 // 'BT' is owned by BugReporter.
1926 BugType *BT = getBugTypeForName(name, category);
Chris Lattner0a14eee2008-11-18 07:04:44 +00001927 FullSourceLoc L = getContext().getFullLoc(Loc);
Anna Zaksb7530a42011-08-17 23:21:23 +00001928 BugReport *R = new BugReport(*BT, str, L);
Ted Kremenekcf118d42009-02-04 23:49:09 +00001929 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg);
1930 EmitReport(R);
Ted Kremenek57202072008-07-14 17:40:50 +00001931}
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001932
Chris Lattner5f9e2722011-07-23 10:55:15 +00001933BugType *BugReporter::getBugTypeForName(StringRef name,
1934 StringRef category) {
Argyrios Kyrtzidis404fc3a2011-02-23 00:16:01 +00001935 llvm::SmallString<136> fullDesc;
1936 llvm::raw_svector_ostream(fullDesc) << name << ":" << category;
1937 llvm::StringMapEntry<BugType *> &
1938 entry = StrBugTypes.GetOrCreateValue(fullDesc);
1939 BugType *BT = entry.getValue();
1940 if (!BT) {
1941 BT = new BugType(name, category);
1942 entry.setValue(BT);
1943 }
1944 return BT;
1945}