blob: c1ea76735c4181fa5207c9f2a41a7155b84d0156 [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"
Ted Kremenek2f1a79d2007-09-11 17:24:14 +000016#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
Ted Kremenek34a69172008-06-20 21:45:25 +000018#include "clang/AST/ParentMap.h"
Ted Kremenekcadd9f12012-09-06 22:32:48 +000019#include "clang/AST/RecursiveASTVisitor.h"
Ted Kremenek8de92c02012-10-30 04:43:51 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek8de92c02012-10-30 04:43:51 +000021#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
22#include "clang/StaticAnalyzer/Core/Checker.h"
23#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
24#include "llvm/ADT/BitVector.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenekcadd9f12012-09-06 22:32:48 +000026#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek1bb9f252007-09-06 23:01:46 +000027
28using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000029using namespace ento;
Ted Kremenek1bb9f252007-09-06 23:01:46 +000030
Ted Kremenekcadd9f12012-09-06 22:32:48 +000031namespace {
32
33/// A simple visitor to record what VarDecls occur in EH-handling code.
34class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> {
35public:
36 bool inEH;
37 llvm::DenseSet<const VarDecl *> &S;
38
39 bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
40 SaveAndRestore<bool> inFinally(inEH, true);
41 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S);
42 }
43
44 bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) {
45 SaveAndRestore<bool> inCatch(inEH, true);
46 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S);
47 }
48
49 bool TraverseCXXCatchStmt(CXXCatchStmt *S) {
50 SaveAndRestore<bool> inCatch(inEH, true);
51 return TraverseStmt(S->getHandlerBlock());
52 }
53
54 bool VisitDeclRefExpr(DeclRefExpr *DR) {
55 if (inEH)
56 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
57 S.insert(D);
58 return true;
59 }
60
61 EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) :
62 inEH(false), S(S) {}
63};
Ted Kremenek34a69172008-06-20 21:45:25 +000064
Ted Kremenek9865d7f2011-02-11 23:24:26 +000065// FIXME: Eventually migrate into its own file, and have it managed by
66// AnalysisManager.
67class ReachableCode {
68 const CFG &cfg;
69 llvm::BitVector reachable;
70public:
71 ReachableCode(const CFG &cfg)
72 : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
73
74 void computeReachableBlocks();
75
76 bool isReachable(const CFGBlock *block) const {
77 return reachable[block->getBlockID()];
78 }
79};
80}
81
82void ReachableCode::computeReachableBlocks() {
83 if (!cfg.getNumBlockIDs())
84 return;
85
Chris Lattner0e62c1c2011-07-23 10:55:15 +000086 SmallVector<const CFGBlock*, 10> worklist;
Ted Kremenek9865d7f2011-02-11 23:24:26 +000087 worklist.push_back(&cfg.getEntry());
Robert Wilhelm25284cc2013-08-23 16:11:15 +000088
Ted Kremenek9865d7f2011-02-11 23:24:26 +000089 while (!worklist.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +000090 const CFGBlock *block = worklist.pop_back_val();
Ted Kremenek9865d7f2011-02-11 23:24:26 +000091 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
92 if (isReachable)
93 continue;
94 isReachable = true;
95 for (CFGBlock::const_succ_iterator i = block->succ_begin(),
96 e = block->succ_end(); i != e; ++i)
97 if (const CFGBlock *succ = *i)
98 worklist.push_back(succ);
99 }
100}
101
Anna Zaks144579e2013-04-25 21:52:35 +0000102static const Expr *
103LookThroughTransitiveAssignmentsAndCommaOperators(const Expr *Ex) {
Ted Kremenekdc53f002012-04-04 19:58:03 +0000104 while (Ex) {
105 const BinaryOperator *BO =
106 dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
107 if (!BO)
108 break;
109 if (BO->getOpcode() == BO_Assign) {
110 Ex = BO->getRHS();
111 continue;
112 }
Anna Zaks144579e2013-04-25 21:52:35 +0000113 if (BO->getOpcode() == BO_Comma) {
114 Ex = BO->getRHS();
115 continue;
116 }
Ted Kremenekdc53f002012-04-04 19:58:03 +0000117 break;
118 }
119 return Ex;
120}
121
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000122namespace {
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000123class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000124 const CFG &cfg;
Chris Lattner254987c2007-09-15 23:21:08 +0000125 ASTContext &Ctx;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000126 BugReporter& BR;
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000127 const CheckerBase *Checker;
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000128 AnalysisDeclContext* AC;
Ted Kremenek34a69172008-06-20 21:45:25 +0000129 ParentMap& Parents;
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000130 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000131 std::unique_ptr<ReachableCode> reachableCode;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000132 const CFGBlock *currentBlock;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000133 std::unique_ptr<llvm::DenseSet<const VarDecl *>> InEH;
Mike Stump11289f42009-09-09 15:08:12 +0000134
Ted Kremenekecc851b2008-07-23 21:16:38 +0000135 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump11289f42009-09-09 15:08:12 +0000136
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000137public:
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000138 DeadStoreObs(const CFG &cfg, ASTContext &ctx, BugReporter &br,
139 const CheckerBase *checker, AnalysisDeclContext *ac,
140 ParentMap &parents,
141 llvm::SmallPtrSet<const VarDecl *, 20> &escaped)
142 : cfg(cfg), Ctx(ctx), BR(br), Checker(checker), AC(ac), Parents(parents),
Craig Topper0dbb7832014-05-27 02:45:47 +0000143 Escaped(escaped), currentBlock(nullptr) {}
Mike Stump11289f42009-09-09 15:08:12 +0000144
Ted Kremenekad8bce02007-09-25 04:31:27 +0000145 virtual ~DeadStoreObs() {}
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000146
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000147 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
148 if (Live.isLive(D))
149 return true;
150 // Lazily construct the set that records which VarDecls are in
151 // EH code.
152 if (!InEH.get()) {
153 InEH.reset(new llvm::DenseSet<const VarDecl *>());
154 EHCodeVisitor V(*InEH.get());
155 V.TraverseStmt(AC->getBody());
156 }
157 // Treat all VarDecls that occur in EH code as being "always live"
158 // when considering to suppress dead stores. Frequently stores
159 // are followed by reads in EH code, but we don't have the ability
160 // to analyze that yet.
161 return InEH->count(D);
162 }
163
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000164 void Report(const VarDecl *V, DeadStoreKind dsk,
Anna Zaksc29bed32011-09-20 21:38:35 +0000165 PathDiagnosticLocation L, SourceRange R) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000166 if (Escaped.count(V))
167 return;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000168
169 // Compute reachable blocks within the CFG for trivial cases
170 // where a bogus dead store can be reported because itself is unreachable.
171 if (!reachableCode.get()) {
172 reachableCode.reset(new ReachableCode(cfg));
173 reachableCode->computeReachableBlocks();
174 }
175
176 if (!reachableCode->isReachable(currentBlock))
177 return;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000178
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000179 SmallString<64> buf;
Jordy Rose82c673d2011-08-21 05:25:15 +0000180 llvm::raw_svector_ostream os(buf);
Craig Topper0dbb7832014-05-27 02:45:47 +0000181 const char *BugType = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenekecc851b2008-07-23 21:16:38 +0000183 switch (dsk) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000184 case DeadInit:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000185 BugType = "Dead initialization";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000186 os << "Value stored to '" << *V
Jordy Rose82c673d2011-08-21 05:25:15 +0000187 << "' during its initialization is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000188 break;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Ted Kremenekecc851b2008-07-23 21:16:38 +0000190 case DeadIncrement:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000191 BugType = "Dead increment";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000192 case Standard:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000193 if (!BugType) BugType = "Dead assignment";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000194 os << "Value stored to '" << *V << "' is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000195 break;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenekecc851b2008-07-23 21:16:38 +0000197 case Enclosing:
Ted Kremenekf2248202011-01-13 20:58:56 +0000198 // Don't report issues in this case, e.g.: "if (x = foo())",
199 // where 'x' is unused later. We have yet to see a case where
200 // this is a real bug.
201 return;
Ted Kremenek81bfc072008-07-15 18:06:32 +0000202 }
Mike Stump11289f42009-09-09 15:08:12 +0000203
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000204 BR.EmitBasicReport(AC->getDecl(), Checker, BugType, "Dead store", os.str(),
205 L, R);
Ted Kremenek34a69172008-06-20 21:45:25 +0000206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000208 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenekecc851b2008-07-23 21:16:38 +0000209 DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000210 const LiveVariables::LivenessValues &Live) {
Ted Kremenek34a69172008-06-20 21:45:25 +0000211
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000212 if (!VD->hasLocalStorage())
213 return;
214 // Reference types confuse the dead stores checker. Skip them
215 // for now.
216 if (VD->getType()->getAs<ReferenceType>())
217 return;
218
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000219 if (!isLive(Live, VD) &&
Ted Kremenek0f833902014-01-15 00:59:23 +0000220 !(VD->hasAttr<UnusedAttr>() || VD->hasAttr<BlocksAttr>() ||
221 VD->hasAttr<ObjCPreciseLifetimeAttr>())) {
Anna Zaksc29bed32011-09-20 21:38:35 +0000222
223 PathDiagnosticLocation ExLoc =
224 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
225 Report(VD, dsk, ExLoc, Val->getSourceRange());
226 }
Ted Kremenek91f035c2008-05-21 22:59:16 +0000227 }
Mike Stump11289f42009-09-09 15:08:12 +0000228
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000229 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000230 const LiveVariables::LivenessValues& Live) {
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000231 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000232 CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenekecc851b2008-07-23 21:16:38 +0000233 }
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000235 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000236 if (B->isCompoundAssignmentOp())
237 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000239 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000240 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenekecc851b2008-07-23 21:16:38 +0000242 if (!BRHS)
243 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000244
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000245 const DeclRefExpr *DR;
Mike Stump11289f42009-09-09 15:08:12 +0000246
Ted Kremenekecc851b2008-07-23 21:16:38 +0000247 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
248 if (DR->getDecl() == VD)
249 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000250
Ted Kremenekecc851b2008-07-23 21:16:38 +0000251 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
252 if (DR->getDecl() == VD)
253 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000254
Ted Kremenekecc851b2008-07-23 21:16:38 +0000255 return false;
Ted Kremenekf15cd142008-05-05 23:12:21 +0000256 }
Mike Stump11289f42009-09-09 15:08:12 +0000257
Craig Topperfb6b25b2014-03-15 04:29:04 +0000258 void observeStmt(const Stmt *S, const CFGBlock *block,
259 const LiveVariables::LivenessValues &Live) override {
Mike Stump11289f42009-09-09 15:08:12 +0000260
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000261 currentBlock = block;
262
Ted Kremenek87bfc032008-04-14 18:28:25 +0000263 // Skip statements in macros.
264 if (S->getLocStart().isMacroID())
265 return;
Mike Stump11289f42009-09-09 15:08:12 +0000266
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000267 // Only cover dead stores from regular assignments. ++/-- dead stores
268 // have never flagged a real bug.
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000269 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000270 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump11289f42009-09-09 15:08:12 +0000271
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000272 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek34a69172008-06-20 21:45:25 +0000273 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek0216b832008-08-09 00:05:14 +0000274 // Special case: check for assigning null to a pointer.
Mike Stump11289f42009-09-09 15:08:12 +0000275 // This is a common form of defensive programming.
Anna Zaks144579e2013-04-25 21:52:35 +0000276 const Expr *RHS =
277 LookThroughTransitiveAssignmentsAndCommaOperators(B->getRHS());
278 RHS = RHS->IgnoreParenCasts();
Ted Kremenekdc53f002012-04-04 19:58:03 +0000279
Ted Kremenekb4331a92010-02-23 21:19:33 +0000280 QualType T = VD->getType();
281 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenekdc53f002012-04-04 19:58:03 +0000282 if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
Ted Kremenek12b64952009-11-22 20:26:21 +0000283 return;
Ted Kremenek0216b832008-08-09 00:05:14 +0000284 }
Ted Kremenek12b64952009-11-22 20:26:21 +0000285
Ted Kremenek890d44e2009-01-09 22:15:01 +0000286 // Special case: self-assignments. These are often used to shut up
287 // "unused variable" compiler warnings.
Ted Kremenekdc53f002012-04-04 19:58:03 +0000288 if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
Ted Kremenek890d44e2009-01-09 22:15:01 +0000289 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
290 return;
Mike Stump11289f42009-09-09 15:08:12 +0000291
Ted Kremenek890d44e2009-01-09 22:15:01 +0000292 // Otherwise, issue a warning.
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000293 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump11289f42009-09-09 15:08:12 +0000294 ? Enclosing
Ted Kremeneke5fe6172009-01-20 00:47:45 +0000295 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000297 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000298 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000299 }
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000300 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000301 if (!U->isIncrementOp() || U->isPrefix())
Ted Kremenekf15cd142008-05-05 23:12:21 +0000302 return;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000304 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000305 if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenekbb7818b2008-10-15 05:23:41 +0000306 return;
Ted Kremenek87b16f42008-07-24 17:01:17 +0000307
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000308 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000309
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000310 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000311 CheckDeclRef(DR, U, DeadIncrement, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000312 }
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000313 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
Ted Kremenekad8bce02007-09-25 04:31:27 +0000314 // Iterate through the decls. Warn if any initializers are complex
315 // expressions that are not live (never used).
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000316 for (const auto *DI : DS->decls()) {
317 const auto *V = dyn_cast<VarDecl>(DI);
Ted Kremenek092ec762008-07-25 04:47:34 +0000318
319 if (!V)
320 continue;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000321
322 if (V->hasLocalStorage()) {
323 // Reference types confuse the dead stores checker. Skip them
324 // for now.
325 if (V->getType()->getAs<ReferenceType>())
326 return;
327
Ted Kremenekdc53f002012-04-04 19:58:03 +0000328 if (const Expr *E = V->getInit()) {
329 while (const ExprWithCleanups *exprClean =
330 dyn_cast<ExprWithCleanups>(E))
John McCall31168b02011-06-15 23:02:42 +0000331 E = exprClean->getSubExpr();
332
Ted Kremenekdc53f002012-04-04 19:58:03 +0000333 // Look through transitive assignments, e.g.:
334 // int x = y = 0;
Anna Zaks144579e2013-04-25 21:52:35 +0000335 E = LookThroughTransitiveAssignmentsAndCommaOperators(E);
Ted Kremenekdc53f002012-04-04 19:58:03 +0000336
Ted Kremenek29f38082009-12-15 04:12:12 +0000337 // Don't warn on C++ objects (yet) until we can show that their
338 // constructors/destructors don't have side effects.
339 if (isa<CXXConstructExpr>(E))
340 return;
Ted Kremenek857f41c2009-12-23 04:11:44 +0000341
Ted Kremenek092ec762008-07-25 04:47:34 +0000342 // A dead initialization is a variable that is dead after it
343 // is initialized. We don't flag warnings for those variables
Ted Kremenek0f833902014-01-15 00:59:23 +0000344 // marked 'unused' or 'objc_precise_lifetime'.
345 if (!isLive(Live, V) &&
346 !V->hasAttr<UnusedAttr>() &&
347 !V->hasAttr<ObjCPreciseLifetimeAttr>()) {
Ted Kremeneka6ef56e2007-09-28 20:48:41 +0000348 // Special case: check for initializations with constants.
349 //
350 // e.g. : int x = 0;
351 //
352 // If x is EVER assigned a new value later, don't issue
353 // a warning. This is because such initialization can be
354 // due to defensive programming.
Richard Smith1e1f5ab2011-12-06 23:25:15 +0000355 if (E->isEvaluatable(Ctx))
Ted Kremenek0203db72009-02-09 18:01:00 +0000356 return;
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremenekdc53f002012-04-04 19:58:03 +0000358 if (const DeclRefExpr *DRE =
359 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
360 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Ted Kremeneke174fda2010-03-18 01:22:39 +0000361 // Special case: check for initialization from constant
362 // variables.
363 //
364 // e.g. extern const int MyConstant;
365 // int x = MyConstant;
366 //
Ted Kremenek0203db72009-02-09 18:01:00 +0000367 if (VD->hasGlobalStorage() &&
Ted Kremeneke174fda2010-03-18 01:22:39 +0000368 VD->getType().isConstQualified())
369 return;
370 // Special case: check for initialization from scalar
371 // parameters. This is often a form of defensive
372 // programming. Non-scalars are still an error since
373 // because it more likely represents an actual algorithmic
374 // bug.
375 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
376 return;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anna Zaksc29bed32011-09-20 21:38:35 +0000379 PathDiagnosticLocation Loc =
380 PathDiagnosticLocation::create(V, BR.getSourceManager());
381 Report(V, DeadInit, Loc, E->getSourceRange());
Ted Kremenek2f1a79d2007-09-11 17:24:14 +0000382 }
Ted Kremenek092ec762008-07-25 04:47:34 +0000383 }
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000384 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000385 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000386 }
387};
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000389} // end anonymous namespace
390
Ted Kremenek7e151302008-04-14 17:39:48 +0000391//===----------------------------------------------------------------------===//
Ted Kremenekc7efb532008-07-02 23:16:33 +0000392// Driver function to invoke the Dead-Stores checker on a CFG.
393//===----------------------------------------------------------------------===//
394
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000395namespace {
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000396class FindEscaped {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000397public:
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000398 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000399
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000400 void operator()(const Stmt *S) {
401 // Check for '&'. Any VarDecl whose address has been taken we treat as
402 // escaped.
403 // FIXME: What about references?
404 const UnaryOperator *U = dyn_cast<UnaryOperator>(S);
405 if (!U)
406 return;
407 if (U->getOpcode() != UO_AddrOf)
408 return;
409
410 const Expr *E = U->getSubExpr()->IgnoreParenCasts();
411 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
412 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
413 Escaped.insert(VD);
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000414 }
415};
416} // end anonymous namespace
Mike Stump11289f42009-09-09 15:08:12 +0000417
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000418
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000419//===----------------------------------------------------------------------===//
420// DeadStoresChecker
421//===----------------------------------------------------------------------===//
422
423namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000424class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000425public:
426 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
427 BugReporter &BR) const {
Ted Kremenek3e05be92013-02-18 07:18:28 +0000428
429 // Don't do anything for template instantiations.
430 // Proving that code in a template instantiation is "dead"
431 // means proving that it is dead in all instantiations.
432 // This same problem exists with -Wunreachable-code.
433 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
434 if (FD->isTemplateInstantiation())
435 return;
436
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000437 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000438 CFG &cfg = *mgr.getCFG(D);
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000439 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000440 ParentMap &pmap = mgr.getParentMap(D);
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000441 FindEscaped FS;
442 cfg.VisitBlockStmts(FS);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000443 DeadStoreObs A(cfg, BR.getContext(), BR, this, AC, pmap, FS.Escaped);
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000444 L->runOnAllBlocks(A);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000445 }
446 }
447};
448}
449
450void ento::registerDeadStoresChecker(CheckerManager &mgr) {
451 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenek7e151302008-04-14 17:39:48 +0000452}