blob: d36951814867c91472b9a239c837e287ee5f4423 [file] [log] [blame]
Ted Kremenek56b1f712011-01-13 20:58:56 +00001//==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==//
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines a DeadStores, a flow-sensitive checker that looks for
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000011// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000017#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000018#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenekf96f16d2009-04-07 05:25:24 +000021#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000022#include "clang/Basic/Diagnostic.h"
Ted Kremenekce1cab92007-09-11 17:24:14 +000023#include "clang/AST/ASTContext.h"
Ted Kremenek1a654b62008-06-20 21:45:25 +000024#include "clang/AST/ParentMap.h"
Ted Kremenekf96f16d2009-04-07 05:25:24 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000026
27using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000028using namespace ento;
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000029
30namespace {
Ted Kremenek1a654b62008-06-20 21:45:25 +000031
Ted Kremenek848ec832011-02-11 23:24:26 +000032// FIXME: Eventually migrate into its own file, and have it managed by
33// AnalysisManager.
34class ReachableCode {
35 const CFG &cfg;
36 llvm::BitVector reachable;
37public:
38 ReachableCode(const CFG &cfg)
39 : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
40
41 void computeReachableBlocks();
42
43 bool isReachable(const CFGBlock *block) const {
44 return reachable[block->getBlockID()];
45 }
46};
47}
48
49void ReachableCode::computeReachableBlocks() {
50 if (!cfg.getNumBlockIDs())
51 return;
52
Chris Lattner5f9e2722011-07-23 10:55:15 +000053 SmallVector<const CFGBlock*, 10> worklist;
Ted Kremenek848ec832011-02-11 23:24:26 +000054 worklist.push_back(&cfg.getEntry());
55
56 while (!worklist.empty()) {
57 const CFGBlock *block = worklist.back();
58 worklist.pop_back();
59 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
60 if (isReachable)
61 continue;
62 isReachable = true;
63 for (CFGBlock::const_succ_iterator i = block->succ_begin(),
64 e = block->succ_end(); i != e; ++i)
65 if (const CFGBlock *succ = *i)
66 worklist.push_back(succ);
67 }
68}
69
70namespace {
Ted Kremenek88299892011-07-28 23:07:59 +000071class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek848ec832011-02-11 23:24:26 +000072 const CFG &cfg;
Chris Lattnerc0508f92007-09-15 23:21:08 +000073 ASTContext &Ctx;
Ted Kremenek8f269862008-07-14 20:56:04 +000074 BugReporter& BR;
Ted Kremenek1a654b62008-06-20 21:45:25 +000075 ParentMap& Parents;
Ted Kremenek88299892011-07-28 23:07:59 +000076 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek848ec832011-02-11 23:24:26 +000077 llvm::OwningPtr<ReachableCode> reachableCode;
78 const CFGBlock *currentBlock;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremenek2cfac222008-07-23 21:16:38 +000080 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000082public:
Ted Kremenek848ec832011-02-11 23:24:26 +000083 DeadStoreObs(const CFG &cfg, ASTContext &ctx,
84 BugReporter& br, ParentMap& parents,
Ted Kremenek88299892011-07-28 23:07:59 +000085 llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
Ted Kremenek848ec832011-02-11 23:24:26 +000086 : cfg(cfg), Ctx(ctx), BR(br), Parents(parents),
87 Escaped(escaped), currentBlock(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremenekfdd225e2007-09-25 04:31:27 +000089 virtual ~DeadStoreObs() {}
Ted Kremenekb930d7a2009-04-01 06:52:48 +000090
Ted Kremenek9c378f72011-08-12 23:37:29 +000091 void Report(const VarDecl *V, DeadStoreKind dsk,
Ted Kremenek88299892011-07-28 23:07:59 +000092 SourceLocation L, SourceRange R) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +000093 if (Escaped.count(V))
94 return;
Ted Kremenek848ec832011-02-11 23:24:26 +000095
96 // Compute reachable blocks within the CFG for trivial cases
97 // where a bogus dead store can be reported because itself is unreachable.
98 if (!reachableCode.get()) {
99 reachableCode.reset(new ReachableCode(cfg));
100 reachableCode->computeReachableBlocks();
101 }
102
103 if (!reachableCode->isReachable(currentBlock))
104 return;
Ted Kremenek8f269862008-07-14 20:56:04 +0000105
Ted Kremenek848ec832011-02-11 23:24:26 +0000106 const std::string &name = V->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek2cfac222008-07-23 21:16:38 +0000108 const char* BugType = 0;
109 std::string msg;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek2cfac222008-07-23 21:16:38 +0000111 switch (dsk) {
112 default:
113 assert(false && "Impossible dead store type.");
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek2cfac222008-07-23 21:16:38 +0000115 case DeadInit:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000116 BugType = "Dead initialization";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000117 msg = "Value stored to '" + name +
118 "' during its initialization is never read";
119 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek2cfac222008-07-23 21:16:38 +0000121 case DeadIncrement:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000122 BugType = "Dead increment";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000123 case Standard:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000124 if (!BugType) BugType = "Dead assignment";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000125 msg = "Value stored to '" + name + "' is never read";
126 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek2cfac222008-07-23 21:16:38 +0000128 case Enclosing:
Ted Kremenek56b1f712011-01-13 20:58:56 +0000129 // Don't report issues in this case, e.g.: "if (x = foo())",
130 // where 'x' is unused later. We have yet to see a case where
131 // this is a real bug.
132 return;
Ted Kremenekf9c2a5d2008-07-15 18:06:32 +0000133 }
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Benjamin Kramerf0171732009-11-29 18:27:55 +0000135 BR.EmitBasicReport(BugType, "Dead store", msg, L, R);
Ted Kremenek1a654b62008-06-20 21:45:25 +0000136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Ted Kremenek9c378f72011-08-12 23:37:29 +0000138 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenek2cfac222008-07-23 21:16:38 +0000139 DeadStoreKind dsk,
Ted Kremenek88299892011-07-28 23:07:59 +0000140 const LiveVariables::LivenessValues &Live) {
Ted Kremenek1a654b62008-06-20 21:45:25 +0000141
Ted Kremenek852274d2009-12-16 03:18:58 +0000142 if (!VD->hasLocalStorage())
143 return;
144 // Reference types confuse the dead stores checker. Skip them
145 // for now.
146 if (VD->getType()->getAs<ReferenceType>())
147 return;
148
Ted Kremenek88299892011-07-28 23:07:59 +0000149 if (!Live.isLive(VD) &&
Ted Kremenek74635d82009-12-03 00:46:16 +0000150 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>()))
Ted Kremenek2cfac222008-07-23 21:16:38 +0000151 Report(VD, dsk, Ex->getSourceRange().getBegin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000152 Val->getSourceRange());
Ted Kremenek3eb817e2008-05-21 22:59:16 +0000153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenek9c378f72011-08-12 23:37:29 +0000155 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremenek88299892011-07-28 23:07:59 +0000156 const LiveVariables::LivenessValues& Live) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000157 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek88299892011-07-28 23:07:59 +0000158 CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenek2cfac222008-07-23 21:16:38 +0000159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenek9c378f72011-08-12 23:37:29 +0000161 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenek2cfac222008-07-23 21:16:38 +0000162 if (B->isCompoundAssignmentOp())
163 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek9c378f72011-08-12 23:37:29 +0000165 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek88299892011-07-28 23:07:59 +0000166 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek2cfac222008-07-23 21:16:38 +0000168 if (!BRHS)
169 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek88299892011-07-28 23:07:59 +0000171 const DeclRefExpr *DR;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Ted Kremenek2cfac222008-07-23 21:16:38 +0000173 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
174 if (DR->getDecl() == VD)
175 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek2cfac222008-07-23 21:16:38 +0000177 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
178 if (DR->getDecl() == VD)
179 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek2cfac222008-07-23 21:16:38 +0000181 return false;
Ted Kremeneka23157e2008-05-05 23:12:21 +0000182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek9c378f72011-08-12 23:37:29 +0000184 virtual void observeStmt(const Stmt *S, const CFGBlock *block,
Ted Kremenek88299892011-07-28 23:07:59 +0000185 const LiveVariables::LivenessValues &Live) {
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenek848ec832011-02-11 23:24:26 +0000187 currentBlock = block;
188
Ted Kremenek1c86b152008-04-14 18:28:25 +0000189 // Skip statements in macros.
190 if (S->getLocStart().isMacroID())
191 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000193 // Only cover dead stores from regular assignments. ++/-- dead stores
194 // have never flagged a real bug.
Ted Kremenek88299892011-07-28 23:07:59 +0000195 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000196 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek9c378f72011-08-12 23:37:29 +0000198 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +0000199 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremeneke12691c2008-08-09 00:05:14 +0000200 // Special case: check for assigning null to a pointer.
Mike Stump1eb44332009-09-09 15:08:12 +0000201 // This is a common form of defensive programming.
Ted Kremenek89132202010-02-23 21:19:33 +0000202 QualType T = VD->getType();
203 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000204 if (B->getRHS()->isNullPointerConstant(Ctx,
205 Expr::NPC_ValueDependentIsNull))
206 return;
Ted Kremeneke12691c2008-08-09 00:05:14 +0000207 }
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000208
Ted Kremenek9c378f72011-08-12 23:37:29 +0000209 Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek3b587862009-01-09 22:15:01 +0000210 // Special case: self-assignments. These are often used to shut up
211 // "unused variable" compiler warnings.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000212 if (DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
Ted Kremenek3b587862009-01-09 22:15:01 +0000213 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
214 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek3b587862009-01-09 22:15:01 +0000216 // Otherwise, issue a warning.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000217 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump1eb44332009-09-09 15:08:12 +0000218 ? Enclosing
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000219 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek88299892011-07-28 23:07:59 +0000221 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000222 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000223 }
Ted Kremenek88299892011-07-28 23:07:59 +0000224 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000225 if (!U->isIncrementOp() || U->isPrefix())
Ted Kremeneka23157e2008-05-05 23:12:21 +0000226 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenek88299892011-07-28 23:07:59 +0000228 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000229 if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenek380277e2008-10-15 05:23:41 +0000230 return;
Ted Kremenekb0f36322008-07-24 17:01:17 +0000231
Ted Kremenek88299892011-07-28 23:07:59 +0000232 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek9c378f72011-08-12 23:37:29 +0000234 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek88299892011-07-28 23:07:59 +0000235 CheckDeclRef(DR, U, DeadIncrement, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000236 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000237 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000238 // Iterate through the decls. Warn if any initializers are complex
239 // expressions that are not live (never used).
Ted Kremenek88299892011-07-28 23:07:59 +0000240 for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000241 DI != DE; ++DI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenek9c378f72011-08-12 23:37:29 +0000243 VarDecl *V = dyn_cast<VarDecl>(*DI);
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000244
245 if (!V)
246 continue;
Ted Kremenek852274d2009-12-16 03:18:58 +0000247
248 if (V->hasLocalStorage()) {
249 // Reference types confuse the dead stores checker. Skip them
250 // for now.
251 if (V->getType()->getAs<ReferenceType>())
252 return;
253
Ted Kremenek9c378f72011-08-12 23:37:29 +0000254 if (Expr *E = V->getInit()) {
John McCallf85e1932011-06-15 23:02:42 +0000255 while (ExprWithCleanups *exprClean = dyn_cast<ExprWithCleanups>(E))
256 E = exprClean->getSubExpr();
257
Ted Kremenek43f19e32009-12-15 04:12:12 +0000258 // Don't warn on C++ objects (yet) until we can show that their
259 // constructors/destructors don't have side effects.
260 if (isa<CXXConstructExpr>(E))
261 return;
Ted Kremenek604d9392009-12-23 04:11:44 +0000262
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000263 // A dead initialization is a variable that is dead after it
264 // is initialized. We don't flag warnings for those variables
265 // marked 'unused'.
Ted Kremenek88299892011-07-28 23:07:59 +0000266 if (!Live.isLive(V) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000267 // Special case: check for initializations with constants.
268 //
269 // e.g. : int x = 0;
270 //
271 // If x is EVER assigned a new value later, don't issue
272 // a warning. This is because such initialization can be
273 // due to defensive programming.
John McCall4204f072010-08-02 21:13:48 +0000274 if (E->isConstantInitializer(Ctx, false))
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000275 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000277 if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
Ted Kremenekebd42f42010-03-18 01:22:39 +0000278 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
279 // Special case: check for initialization from constant
280 // variables.
281 //
282 // e.g. extern const int MyConstant;
283 // int x = MyConstant;
284 //
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000285 if (VD->hasGlobalStorage() &&
Ted Kremenekebd42f42010-03-18 01:22:39 +0000286 VD->getType().isConstQualified())
287 return;
288 // Special case: check for initialization from scalar
289 // parameters. This is often a form of defensive
290 // programming. Non-scalars are still an error since
291 // because it more likely represents an actual algorithmic
292 // bug.
293 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
294 return;
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000297 Report(V, DeadInit, V->getLocation(), E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000298 }
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000299 }
Ted Kremenek852274d2009-12-16 03:18:58 +0000300 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000301 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000302 }
303};
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000305} // end anonymous namespace
306
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000307//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000308// Driver function to invoke the Dead-Stores checker on a CFG.
309//===----------------------------------------------------------------------===//
310
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000311namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000312class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000313 CFG *cfg;
314public:
315 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000317 CFG& getCFG() { return *cfg; }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek88299892011-07-28 23:07:59 +0000319 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000320
321 void VisitUnaryOperator(UnaryOperator* U) {
322 // Check for '&'. Any VarDecl whose value has its address-taken we
323 // treat as escaped.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000324 Expr *E = U->getSubExpr()->IgnoreParenCasts();
John McCall2de56d12010-08-25 11:45:40 +0000325 if (U->getOpcode() == UO_AddrOf)
Ted Kremenek9c378f72011-08-12 23:37:29 +0000326 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
327 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000328 Escaped.insert(VD);
329 return;
330 }
331 Visit(E);
332 }
333};
334} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000336
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000337//===----------------------------------------------------------------------===//
338// DeadStoresChecker
339//===----------------------------------------------------------------------===//
340
341namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000342class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000343public:
344 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
345 BugReporter &BR) const {
346 if (LiveVariables *L = mgr.getLiveVariables(D)) {
347 CFG &cfg = *mgr.getCFG(D);
348 ParentMap &pmap = mgr.getParentMap(D);
349 FindEscaped FS(&cfg);
350 FS.getCFG().VisitBlockStmts(FS);
351 DeadStoreObs A(cfg, BR.getContext(), BR, pmap, FS.Escaped);
Ted Kremenek88299892011-07-28 23:07:59 +0000352 L->runOnAllBlocks(A);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000353 }
354 }
355};
356}
357
358void ento::registerDeadStoresChecker(CheckerManager &mgr) {
359 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000360}