blob: 901af43c5f26a9f361d14e5b30a6c2ba6dbd99a9 [file] [log] [blame]
Ted Kremenekf2248202011-01-13 20:58:56 +00001//==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==//
Ted Kremenek1bb9f252007-09-06 23:01:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenek1bb9f252007-09-06 23:01:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif3a8edd82008-03-06 10:40:09 +000010// This file defines a DeadStores, a flow-sensitive checker that looks for
Ted Kremenek1bb9f252007-09-06 23:01:46 +000011// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenekbf593f82007-12-21 21:42:19 +000017#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekad8bce02007-09-25 04:31:27 +000018#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek4d947fa2009-04-07 05:25:24 +000021#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1bb9f252007-09-06 23:01:46 +000022#include "clang/Basic/Diagnostic.h"
Ted Kremenek2f1a79d2007-09-11 17:24:14 +000023#include "clang/AST/ASTContext.h"
Ted Kremenek34a69172008-06-20 21:45:25 +000024#include "clang/AST/ParentMap.h"
Ted Kremenek4d947fa2009-04-07 05:25:24 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek1bb9f252007-09-06 23:01:46 +000026
27using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000028using namespace ento;
Ted Kremenek1bb9f252007-09-06 23:01:46 +000029
30namespace {
Ted Kremenek34a69172008-06-20 21:45:25 +000031
Ted Kremenek9865d7f2011-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 Lattner0e62c1c2011-07-23 10:55:15 +000053 SmallVector<const CFGBlock*, 10> worklist;
Ted Kremenek9865d7f2011-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 Kremeneke9fda1e2011-07-28 23:07:59 +000071class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek9865d7f2011-02-11 23:24:26 +000072 const CFG &cfg;
Chris Lattner254987c2007-09-15 23:21:08 +000073 ASTContext &Ctx;
Ted Kremenekc18255d2008-07-14 20:56:04 +000074 BugReporter& BR;
Anna Zaksc29bed32011-09-20 21:38:35 +000075 AnalysisContext* AC;
Ted Kremenek34a69172008-06-20 21:45:25 +000076 ParentMap& Parents;
Ted Kremeneke9fda1e2011-07-28 23:07:59 +000077 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek9865d7f2011-02-11 23:24:26 +000078 llvm::OwningPtr<ReachableCode> reachableCode;
79 const CFGBlock *currentBlock;
Mike Stump11289f42009-09-09 15:08:12 +000080
Ted Kremenekecc851b2008-07-23 21:16:38 +000081 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump11289f42009-09-09 15:08:12 +000082
Ted Kremenek1bb9f252007-09-06 23:01:46 +000083public:
Ted Kremenek9865d7f2011-02-11 23:24:26 +000084 DeadStoreObs(const CFG &cfg, ASTContext &ctx,
Anna Zaksc29bed32011-09-20 21:38:35 +000085 BugReporter& br, AnalysisContext* ac, ParentMap& parents,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +000086 llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
Anna Zaksc29bed32011-09-20 21:38:35 +000087 : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
Ted Kremenek9865d7f2011-02-11 23:24:26 +000088 Escaped(escaped), currentBlock(0) {}
Mike Stump11289f42009-09-09 15:08:12 +000089
Ted Kremenekad8bce02007-09-25 04:31:27 +000090 virtual ~DeadStoreObs() {}
Ted Kremenek8b0dba32009-04-01 06:52:48 +000091
Ted Kremenek5ef32db2011-08-12 23:37:29 +000092 void Report(const VarDecl *V, DeadStoreKind dsk,
Anna Zaksc29bed32011-09-20 21:38:35 +000093 PathDiagnosticLocation L, SourceRange R) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +000094 if (Escaped.count(V))
95 return;
Ted Kremenek9865d7f2011-02-11 23:24:26 +000096
97 // Compute reachable blocks within the CFG for trivial cases
98 // where a bogus dead store can be reported because itself is unreachable.
99 if (!reachableCode.get()) {
100 reachableCode.reset(new ReachableCode(cfg));
101 reachableCode->computeReachableBlocks();
102 }
103
104 if (!reachableCode->isReachable(currentBlock))
105 return;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000106
Jordy Rose82c673d2011-08-21 05:25:15 +0000107 llvm::SmallString<64> buf;
108 llvm::raw_svector_ostream os(buf);
109 const char *BugType = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000110
Ted Kremenekecc851b2008-07-23 21:16:38 +0000111 switch (dsk) {
112 default:
Jordy Rose82c673d2011-08-21 05:25:15 +0000113 llvm_unreachable("Impossible dead store type.");
Mike Stump11289f42009-09-09 15:08:12 +0000114
Ted Kremenekecc851b2008-07-23 21:16:38 +0000115 case DeadInit:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000116 BugType = "Dead initialization";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000117 os << "Value stored to '" << *V
Jordy Rose82c673d2011-08-21 05:25:15 +0000118 << "' during its initialization is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000119 break;
Mike Stump11289f42009-09-09 15:08:12 +0000120
Ted Kremenekecc851b2008-07-23 21:16:38 +0000121 case DeadIncrement:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000122 BugType = "Dead increment";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000123 case Standard:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000124 if (!BugType) BugType = "Dead assignment";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000125 os << "Value stored to '" << *V << "' is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000126 break;
Mike Stump11289f42009-09-09 15:08:12 +0000127
Ted Kremenekecc851b2008-07-23 21:16:38 +0000128 case Enclosing:
Ted Kremenekf2248202011-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 Kremenek81bfc072008-07-15 18:06:32 +0000133 }
Mike Stump11289f42009-09-09 15:08:12 +0000134
Jordy Rose82c673d2011-08-21 05:25:15 +0000135 BR.EmitBasicReport(BugType, "Dead store", os.str(), L, R);
Ted Kremenek34a69172008-06-20 21:45:25 +0000136 }
Mike Stump11289f42009-09-09 15:08:12 +0000137
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000138 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenekecc851b2008-07-23 21:16:38 +0000139 DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000140 const LiveVariables::LivenessValues &Live) {
Ted Kremenek34a69172008-06-20 21:45:25 +0000141
Ted Kremenek4cad5fc2009-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 Kremeneke9fda1e2011-07-28 23:07:59 +0000149 if (!Live.isLive(VD) &&
Anna Zaksc29bed32011-09-20 21:38:35 +0000150 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) {
151
152 PathDiagnosticLocation ExLoc =
153 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
154 Report(VD, dsk, ExLoc, Val->getSourceRange());
155 }
Ted Kremenek91f035c2008-05-21 22:59:16 +0000156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000158 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000159 const LiveVariables::LivenessValues& Live) {
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000160 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000161 CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenekecc851b2008-07-23 21:16:38 +0000162 }
Mike Stump11289f42009-09-09 15:08:12 +0000163
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000164 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000165 if (B->isCompoundAssignmentOp())
166 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000168 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000169 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump11289f42009-09-09 15:08:12 +0000170
Ted Kremenekecc851b2008-07-23 21:16:38 +0000171 if (!BRHS)
172 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000174 const DeclRefExpr *DR;
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenekecc851b2008-07-23 21:16:38 +0000176 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
177 if (DR->getDecl() == VD)
178 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000179
Ted Kremenekecc851b2008-07-23 21:16:38 +0000180 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
181 if (DR->getDecl() == VD)
182 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Ted Kremenekecc851b2008-07-23 21:16:38 +0000184 return false;
Ted Kremenekf15cd142008-05-05 23:12:21 +0000185 }
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000187 virtual void observeStmt(const Stmt *S, const CFGBlock *block,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000188 const LiveVariables::LivenessValues &Live) {
Mike Stump11289f42009-09-09 15:08:12 +0000189
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000190 currentBlock = block;
191
Ted Kremenek87bfc032008-04-14 18:28:25 +0000192 // Skip statements in macros.
193 if (S->getLocStart().isMacroID())
194 return;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000196 // Only cover dead stores from regular assignments. ++/-- dead stores
197 // have never flagged a real bug.
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000198 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000199 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump11289f42009-09-09 15:08:12 +0000200
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000201 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek34a69172008-06-20 21:45:25 +0000202 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek0216b832008-08-09 00:05:14 +0000203 // Special case: check for assigning null to a pointer.
Mike Stump11289f42009-09-09 15:08:12 +0000204 // This is a common form of defensive programming.
Ted Kremenekb4331a92010-02-23 21:19:33 +0000205 QualType T = VD->getType();
206 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenek12b64952009-11-22 20:26:21 +0000207 if (B->getRHS()->isNullPointerConstant(Ctx,
208 Expr::NPC_ValueDependentIsNull))
209 return;
Ted Kremenek0216b832008-08-09 00:05:14 +0000210 }
Ted Kremenek12b64952009-11-22 20:26:21 +0000211
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000212 Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek890d44e2009-01-09 22:15:01 +0000213 // Special case: self-assignments. These are often used to shut up
214 // "unused variable" compiler warnings.
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000215 if (DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
Ted Kremenek890d44e2009-01-09 22:15:01 +0000216 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
217 return;
Mike Stump11289f42009-09-09 15:08:12 +0000218
Ted Kremenek890d44e2009-01-09 22:15:01 +0000219 // Otherwise, issue a warning.
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000220 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump11289f42009-09-09 15:08:12 +0000221 ? Enclosing
Ted Kremeneke5fe6172009-01-20 00:47:45 +0000222 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump11289f42009-09-09 15:08:12 +0000223
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000224 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000225 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000226 }
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000227 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000228 if (!U->isIncrementOp() || U->isPrefix())
Ted Kremenekf15cd142008-05-05 23:12:21 +0000229 return;
Mike Stump11289f42009-09-09 15:08:12 +0000230
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000231 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000232 if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenekbb7818b2008-10-15 05:23:41 +0000233 return;
Ted Kremenek87b16f42008-07-24 17:01:17 +0000234
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000235 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000236
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000237 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000238 CheckDeclRef(DR, U, DeadIncrement, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000239 }
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000240 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
Ted Kremenekad8bce02007-09-25 04:31:27 +0000241 // Iterate through the decls. Warn if any initializers are complex
242 // expressions that are not live (never used).
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000243 for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000244 DI != DE; ++DI) {
Mike Stump11289f42009-09-09 15:08:12 +0000245
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000246 VarDecl *V = dyn_cast<VarDecl>(*DI);
Ted Kremenek092ec762008-07-25 04:47:34 +0000247
248 if (!V)
249 continue;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000250
251 if (V->hasLocalStorage()) {
252 // Reference types confuse the dead stores checker. Skip them
253 // for now.
254 if (V->getType()->getAs<ReferenceType>())
255 return;
256
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000257 if (Expr *E = V->getInit()) {
John McCall31168b02011-06-15 23:02:42 +0000258 while (ExprWithCleanups *exprClean = dyn_cast<ExprWithCleanups>(E))
259 E = exprClean->getSubExpr();
260
Ted Kremenek29f38082009-12-15 04:12:12 +0000261 // Don't warn on C++ objects (yet) until we can show that their
262 // constructors/destructors don't have side effects.
263 if (isa<CXXConstructExpr>(E))
264 return;
Ted Kremenek857f41c2009-12-23 04:11:44 +0000265
Ted Kremenek092ec762008-07-25 04:47:34 +0000266 // A dead initialization is a variable that is dead after it
267 // is initialized. We don't flag warnings for those variables
268 // marked 'unused'.
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000269 if (!Live.isLive(V) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremeneka6ef56e2007-09-28 20:48:41 +0000270 // Special case: check for initializations with constants.
271 //
272 // e.g. : int x = 0;
273 //
274 // If x is EVER assigned a new value later, don't issue
275 // a warning. This is because such initialization can be
276 // due to defensive programming.
John McCall8b0f4ff2010-08-02 21:13:48 +0000277 if (E->isConstantInitializer(Ctx, false))
Ted Kremenek0203db72009-02-09 18:01:00 +0000278 return;
Mike Stump11289f42009-09-09 15:08:12 +0000279
Ted Kremenek0203db72009-02-09 18:01:00 +0000280 if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
Ted Kremeneke174fda2010-03-18 01:22:39 +0000281 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
282 // Special case: check for initialization from constant
283 // variables.
284 //
285 // e.g. extern const int MyConstant;
286 // int x = MyConstant;
287 //
Ted Kremenek0203db72009-02-09 18:01:00 +0000288 if (VD->hasGlobalStorage() &&
Ted Kremeneke174fda2010-03-18 01:22:39 +0000289 VD->getType().isConstQualified())
290 return;
291 // Special case: check for initialization from scalar
292 // parameters. This is often a form of defensive
293 // programming. Non-scalars are still an error since
294 // because it more likely represents an actual algorithmic
295 // bug.
296 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
297 return;
298 }
Mike Stump11289f42009-09-09 15:08:12 +0000299
Anna Zaksc29bed32011-09-20 21:38:35 +0000300 PathDiagnosticLocation Loc =
301 PathDiagnosticLocation::create(V, BR.getSourceManager());
302 Report(V, DeadInit, Loc, E->getSourceRange());
Ted Kremenek2f1a79d2007-09-11 17:24:14 +0000303 }
Ted Kremenek092ec762008-07-25 04:47:34 +0000304 }
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000305 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000306 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000307 }
308};
Mike Stump11289f42009-09-09 15:08:12 +0000309
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000310} // end anonymous namespace
311
Ted Kremenek7e151302008-04-14 17:39:48 +0000312//===----------------------------------------------------------------------===//
Ted Kremenekc7efb532008-07-02 23:16:33 +0000313// Driver function to invoke the Dead-Stores checker on a CFG.
314//===----------------------------------------------------------------------===//
315
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000316namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000317class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000318 CFG *cfg;
319public:
320 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000322 CFG& getCFG() { return *cfg; }
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000324 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000325
326 void VisitUnaryOperator(UnaryOperator* U) {
327 // Check for '&'. Any VarDecl whose value has its address-taken we
328 // treat as escaped.
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000329 Expr *E = U->getSubExpr()->IgnoreParenCasts();
John McCalle3027922010-08-25 11:45:40 +0000330 if (U->getOpcode() == UO_AddrOf)
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000331 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
332 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000333 Escaped.insert(VD);
334 return;
335 }
336 Visit(E);
337 }
338};
339} // end anonymous namespace
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000341
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000342//===----------------------------------------------------------------------===//
343// DeadStoresChecker
344//===----------------------------------------------------------------------===//
345
346namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000347class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000348public:
349 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
350 BugReporter &BR) const {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000351 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000352 CFG &cfg = *mgr.getCFG(D);
Anna Zaksc29bed32011-09-20 21:38:35 +0000353 AnalysisContext *AC = mgr.getAnalysisContext(D);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000354 ParentMap &pmap = mgr.getParentMap(D);
355 FindEscaped FS(&cfg);
356 FS.getCFG().VisitBlockStmts(FS);
Anna Zaksc29bed32011-09-20 21:38:35 +0000357 DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000358 L->runOnAllBlocks(A);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000359 }
360 }
361};
362}
363
364void ento::registerDeadStoresChecker(CheckerManager &mgr) {
365 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenek7e151302008-04-14 17:39:48 +0000366}