blob: 4ad4e1c8c62727d1da1d20e535bdb313000fabb4 [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;
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000127 AnalysisDeclContext* AC;
Ted Kremenek34a69172008-06-20 21:45:25 +0000128 ParentMap& Parents;
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000129 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000130 OwningPtr<ReachableCode> reachableCode;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000131 const CFGBlock *currentBlock;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000132 OwningPtr<llvm::DenseSet<const VarDecl *> > InEH;
Mike Stump11289f42009-09-09 15:08:12 +0000133
Ted Kremenekecc851b2008-07-23 21:16:38 +0000134 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump11289f42009-09-09 15:08:12 +0000135
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000136public:
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000137 DeadStoreObs(const CFG &cfg, ASTContext &ctx,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000138 BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000139 llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
Anna Zaksc29bed32011-09-20 21:38:35 +0000140 : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000141 Escaped(escaped), currentBlock(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000142
Ted Kremenekad8bce02007-09-25 04:31:27 +0000143 virtual ~DeadStoreObs() {}
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000144
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000145 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
146 if (Live.isLive(D))
147 return true;
148 // Lazily construct the set that records which VarDecls are in
149 // EH code.
150 if (!InEH.get()) {
151 InEH.reset(new llvm::DenseSet<const VarDecl *>());
152 EHCodeVisitor V(*InEH.get());
153 V.TraverseStmt(AC->getBody());
154 }
155 // Treat all VarDecls that occur in EH code as being "always live"
156 // when considering to suppress dead stores. Frequently stores
157 // are followed by reads in EH code, but we don't have the ability
158 // to analyze that yet.
159 return InEH->count(D);
160 }
161
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000162 void Report(const VarDecl *V, DeadStoreKind dsk,
Anna Zaksc29bed32011-09-20 21:38:35 +0000163 PathDiagnosticLocation L, SourceRange R) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000164 if (Escaped.count(V))
165 return;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000166
167 // Compute reachable blocks within the CFG for trivial cases
168 // where a bogus dead store can be reported because itself is unreachable.
169 if (!reachableCode.get()) {
170 reachableCode.reset(new ReachableCode(cfg));
171 reachableCode->computeReachableBlocks();
172 }
173
174 if (!reachableCode->isReachable(currentBlock))
175 return;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000176
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000177 SmallString<64> buf;
Jordy Rose82c673d2011-08-21 05:25:15 +0000178 llvm::raw_svector_ostream os(buf);
179 const char *BugType = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Ted Kremenekecc851b2008-07-23 21:16:38 +0000181 switch (dsk) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000182 case DeadInit:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000183 BugType = "Dead initialization";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000184 os << "Value stored to '" << *V
Jordy Rose82c673d2011-08-21 05:25:15 +0000185 << "' during its initialization is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000186 break;
Mike Stump11289f42009-09-09 15:08:12 +0000187
Ted Kremenekecc851b2008-07-23 21:16:38 +0000188 case DeadIncrement:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000189 BugType = "Dead increment";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000190 case Standard:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000191 if (!BugType) BugType = "Dead assignment";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000192 os << "Value stored to '" << *V << "' is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000193 break;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenekecc851b2008-07-23 21:16:38 +0000195 case Enclosing:
Ted Kremenekf2248202011-01-13 20:58:56 +0000196 // Don't report issues in this case, e.g.: "if (x = foo())",
197 // where 'x' is unused later. We have yet to see a case where
198 // this is a real bug.
199 return;
Ted Kremenek81bfc072008-07-15 18:06:32 +0000200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Ted Kremenek5a10f082012-04-04 18:11:35 +0000202 BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R);
Ted Kremenek34a69172008-06-20 21:45:25 +0000203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000205 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenekecc851b2008-07-23 21:16:38 +0000206 DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000207 const LiveVariables::LivenessValues &Live) {
Ted Kremenek34a69172008-06-20 21:45:25 +0000208
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000209 if (!VD->hasLocalStorage())
210 return;
211 // Reference types confuse the dead stores checker. Skip them
212 // for now.
213 if (VD->getType()->getAs<ReferenceType>())
214 return;
215
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000216 if (!isLive(Live, VD) &&
Ted Kremenek0f833902014-01-15 00:59:23 +0000217 !(VD->hasAttr<UnusedAttr>() || VD->hasAttr<BlocksAttr>() ||
218 VD->hasAttr<ObjCPreciseLifetimeAttr>())) {
Anna Zaksc29bed32011-09-20 21:38:35 +0000219
220 PathDiagnosticLocation ExLoc =
221 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
222 Report(VD, dsk, ExLoc, Val->getSourceRange());
223 }
Ted Kremenek91f035c2008-05-21 22:59:16 +0000224 }
Mike Stump11289f42009-09-09 15:08:12 +0000225
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000226 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000227 const LiveVariables::LivenessValues& Live) {
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000228 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000229 CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenekecc851b2008-07-23 21:16:38 +0000230 }
Mike Stump11289f42009-09-09 15:08:12 +0000231
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000232 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000233 if (B->isCompoundAssignmentOp())
234 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000235
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000236 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000237 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenekecc851b2008-07-23 21:16:38 +0000239 if (!BRHS)
240 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000242 const DeclRefExpr *DR;
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenekecc851b2008-07-23 21:16:38 +0000244 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
245 if (DR->getDecl() == VD)
246 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenekecc851b2008-07-23 21:16:38 +0000248 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
249 if (DR->getDecl() == VD)
250 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000251
Ted Kremenekecc851b2008-07-23 21:16:38 +0000252 return false;
Ted Kremenekf15cd142008-05-05 23:12:21 +0000253 }
Mike Stump11289f42009-09-09 15:08:12 +0000254
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000255 virtual void observeStmt(const Stmt *S, const CFGBlock *block,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000256 const LiveVariables::LivenessValues &Live) {
Mike Stump11289f42009-09-09 15:08:12 +0000257
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000258 currentBlock = block;
259
Ted Kremenek87bfc032008-04-14 18:28:25 +0000260 // Skip statements in macros.
261 if (S->getLocStart().isMacroID())
262 return;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000264 // Only cover dead stores from regular assignments. ++/-- dead stores
265 // have never flagged a real bug.
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000266 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000267 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump11289f42009-09-09 15:08:12 +0000268
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000269 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek34a69172008-06-20 21:45:25 +0000270 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek0216b832008-08-09 00:05:14 +0000271 // Special case: check for assigning null to a pointer.
Mike Stump11289f42009-09-09 15:08:12 +0000272 // This is a common form of defensive programming.
Anna Zaks144579e2013-04-25 21:52:35 +0000273 const Expr *RHS =
274 LookThroughTransitiveAssignmentsAndCommaOperators(B->getRHS());
275 RHS = RHS->IgnoreParenCasts();
Ted Kremenekdc53f002012-04-04 19:58:03 +0000276
Ted Kremenekb4331a92010-02-23 21:19:33 +0000277 QualType T = VD->getType();
278 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenekdc53f002012-04-04 19:58:03 +0000279 if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
Ted Kremenek12b64952009-11-22 20:26:21 +0000280 return;
Ted Kremenek0216b832008-08-09 00:05:14 +0000281 }
Ted Kremenek12b64952009-11-22 20:26:21 +0000282
Ted Kremenek890d44e2009-01-09 22:15:01 +0000283 // Special case: self-assignments. These are often used to shut up
284 // "unused variable" compiler warnings.
Ted Kremenekdc53f002012-04-04 19:58:03 +0000285 if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
Ted Kremenek890d44e2009-01-09 22:15:01 +0000286 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
287 return;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek890d44e2009-01-09 22:15:01 +0000289 // Otherwise, issue a warning.
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000290 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump11289f42009-09-09 15:08:12 +0000291 ? Enclosing
Ted Kremeneke5fe6172009-01-20 00:47:45 +0000292 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump11289f42009-09-09 15:08:12 +0000293
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000294 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000295 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000296 }
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000297 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000298 if (!U->isIncrementOp() || U->isPrefix())
Ted Kremenekf15cd142008-05-05 23:12:21 +0000299 return;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000301 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000302 if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenekbb7818b2008-10-15 05:23:41 +0000303 return;
Ted Kremenek87b16f42008-07-24 17:01:17 +0000304
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000305 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000306
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000307 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000308 CheckDeclRef(DR, U, DeadIncrement, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000309 }
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000310 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
Ted Kremenekad8bce02007-09-25 04:31:27 +0000311 // Iterate through the decls. Warn if any initializers are complex
312 // expressions that are not live (never used).
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000313 for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000314 DI != DE; ++DI) {
Mike Stump11289f42009-09-09 15:08:12 +0000315
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000316 VarDecl *V = dyn_cast<VarDecl>(*DI);
Ted Kremenek092ec762008-07-25 04:47:34 +0000317
318 if (!V)
319 continue;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000320
321 if (V->hasLocalStorage()) {
322 // Reference types confuse the dead stores checker. Skip them
323 // for now.
324 if (V->getType()->getAs<ReferenceType>())
325 return;
326
Ted Kremenekdc53f002012-04-04 19:58:03 +0000327 if (const Expr *E = V->getInit()) {
328 while (const ExprWithCleanups *exprClean =
329 dyn_cast<ExprWithCleanups>(E))
John McCall31168b02011-06-15 23:02:42 +0000330 E = exprClean->getSubExpr();
331
Ted Kremenekdc53f002012-04-04 19:58:03 +0000332 // Look through transitive assignments, e.g.:
333 // int x = y = 0;
Anna Zaks144579e2013-04-25 21:52:35 +0000334 E = LookThroughTransitiveAssignmentsAndCommaOperators(E);
Ted Kremenekdc53f002012-04-04 19:58:03 +0000335
Ted Kremenek29f38082009-12-15 04:12:12 +0000336 // Don't warn on C++ objects (yet) until we can show that their
337 // constructors/destructors don't have side effects.
338 if (isa<CXXConstructExpr>(E))
339 return;
Ted Kremenek857f41c2009-12-23 04:11:44 +0000340
Ted Kremenek092ec762008-07-25 04:47:34 +0000341 // A dead initialization is a variable that is dead after it
342 // is initialized. We don't flag warnings for those variables
Ted Kremenek0f833902014-01-15 00:59:23 +0000343 // marked 'unused' or 'objc_precise_lifetime'.
344 if (!isLive(Live, V) &&
345 !V->hasAttr<UnusedAttr>() &&
346 !V->hasAttr<ObjCPreciseLifetimeAttr>()) {
Ted Kremeneka6ef56e2007-09-28 20:48:41 +0000347 // Special case: check for initializations with constants.
348 //
349 // e.g. : int x = 0;
350 //
351 // If x is EVER assigned a new value later, don't issue
352 // a warning. This is because such initialization can be
353 // due to defensive programming.
Richard Smith1e1f5ab2011-12-06 23:25:15 +0000354 if (E->isEvaluatable(Ctx))
Ted Kremenek0203db72009-02-09 18:01:00 +0000355 return;
Mike Stump11289f42009-09-09 15:08:12 +0000356
Ted Kremenekdc53f002012-04-04 19:58:03 +0000357 if (const DeclRefExpr *DRE =
358 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
359 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Ted Kremeneke174fda2010-03-18 01:22:39 +0000360 // Special case: check for initialization from constant
361 // variables.
362 //
363 // e.g. extern const int MyConstant;
364 // int x = MyConstant;
365 //
Ted Kremenek0203db72009-02-09 18:01:00 +0000366 if (VD->hasGlobalStorage() &&
Ted Kremeneke174fda2010-03-18 01:22:39 +0000367 VD->getType().isConstQualified())
368 return;
369 // Special case: check for initialization from scalar
370 // parameters. This is often a form of defensive
371 // programming. Non-scalars are still an error since
372 // because it more likely represents an actual algorithmic
373 // bug.
374 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
375 return;
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Anna Zaksc29bed32011-09-20 21:38:35 +0000378 PathDiagnosticLocation Loc =
379 PathDiagnosticLocation::create(V, BR.getSourceManager());
380 Report(V, DeadInit, Loc, E->getSourceRange());
Ted Kremenek2f1a79d2007-09-11 17:24:14 +0000381 }
Ted Kremenek092ec762008-07-25 04:47:34 +0000382 }
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000383 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000384 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000385 }
386};
Mike Stump11289f42009-09-09 15:08:12 +0000387
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000388} // end anonymous namespace
389
Ted Kremenek7e151302008-04-14 17:39:48 +0000390//===----------------------------------------------------------------------===//
Ted Kremenekc7efb532008-07-02 23:16:33 +0000391// Driver function to invoke the Dead-Stores checker on a CFG.
392//===----------------------------------------------------------------------===//
393
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000394namespace {
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000395class FindEscaped {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000396public:
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000397 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000398
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000399 void operator()(const Stmt *S) {
400 // Check for '&'. Any VarDecl whose address has been taken we treat as
401 // escaped.
402 // FIXME: What about references?
403 const UnaryOperator *U = dyn_cast<UnaryOperator>(S);
404 if (!U)
405 return;
406 if (U->getOpcode() != UO_AddrOf)
407 return;
408
409 const Expr *E = U->getSubExpr()->IgnoreParenCasts();
410 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
411 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
412 Escaped.insert(VD);
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000413 }
414};
415} // end anonymous namespace
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000417
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000418//===----------------------------------------------------------------------===//
419// DeadStoresChecker
420//===----------------------------------------------------------------------===//
421
422namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000423class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000424public:
425 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
426 BugReporter &BR) const {
Ted Kremenek3e05be92013-02-18 07:18:28 +0000427
428 // Don't do anything for template instantiations.
429 // Proving that code in a template instantiation is "dead"
430 // means proving that it is dead in all instantiations.
431 // This same problem exists with -Wunreachable-code.
432 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
433 if (FD->isTemplateInstantiation())
434 return;
435
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000436 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000437 CFG &cfg = *mgr.getCFG(D);
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000438 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000439 ParentMap &pmap = mgr.getParentMap(D);
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000440 FindEscaped FS;
441 cfg.VisitBlockStmts(FS);
Anna Zaksc29bed32011-09-20 21:38:35 +0000442 DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000443 L->runOnAllBlocks(A);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000444 }
445 }
446};
447}
448
449void ento::registerDeadStoresChecker(CheckerManager &mgr) {
450 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenek7e151302008-04-14 17:39:48 +0000451}