blob: f2e3e6d7815e113a24f0667fd7af8c73f8c58541 [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"
21#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
22#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
23#include "clang/StaticAnalyzer/Core/Checker.h"
24#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
25#include "llvm/ADT/BitVector.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Ted Kremenekcadd9f12012-09-06 22:32:48 +000027#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek1bb9f252007-09-06 23:01:46 +000028
29using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000030using namespace ento;
Ted Kremenek1bb9f252007-09-06 23:01:46 +000031
Ted Kremenekcadd9f12012-09-06 22:32:48 +000032namespace {
33
34/// A simple visitor to record what VarDecls occur in EH-handling code.
35class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> {
36public:
37 bool inEH;
38 llvm::DenseSet<const VarDecl *> &S;
39
40 bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
41 SaveAndRestore<bool> inFinally(inEH, true);
42 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S);
43 }
44
45 bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) {
46 SaveAndRestore<bool> inCatch(inEH, true);
47 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S);
48 }
49
50 bool TraverseCXXCatchStmt(CXXCatchStmt *S) {
51 SaveAndRestore<bool> inCatch(inEH, true);
52 return TraverseStmt(S->getHandlerBlock());
53 }
54
55 bool VisitDeclRefExpr(DeclRefExpr *DR) {
56 if (inEH)
57 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
58 S.insert(D);
59 return true;
60 }
61
62 EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) :
63 inEH(false), S(S) {}
64};
Ted Kremenek34a69172008-06-20 21:45:25 +000065
Ted Kremenek9865d7f2011-02-11 23:24:26 +000066// FIXME: Eventually migrate into its own file, and have it managed by
67// AnalysisManager.
68class ReachableCode {
69 const CFG &cfg;
70 llvm::BitVector reachable;
71public:
72 ReachableCode(const CFG &cfg)
73 : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
74
75 void computeReachableBlocks();
76
77 bool isReachable(const CFGBlock *block) const {
78 return reachable[block->getBlockID()];
79 }
80};
81}
82
83void ReachableCode::computeReachableBlocks() {
84 if (!cfg.getNumBlockIDs())
85 return;
86
Chris Lattner0e62c1c2011-07-23 10:55:15 +000087 SmallVector<const CFGBlock*, 10> worklist;
Ted Kremenek9865d7f2011-02-11 23:24:26 +000088 worklist.push_back(&cfg.getEntry());
89
90 while (!worklist.empty()) {
91 const CFGBlock *block = worklist.back();
92 worklist.pop_back();
93 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
94 if (isReachable)
95 continue;
96 isReachable = true;
97 for (CFGBlock::const_succ_iterator i = block->succ_begin(),
98 e = block->succ_end(); i != e; ++i)
99 if (const CFGBlock *succ = *i)
100 worklist.push_back(succ);
101 }
102}
103
Ted Kremenekdc53f002012-04-04 19:58:03 +0000104static const Expr *LookThroughTransitiveAssignments(const Expr *Ex) {
105 while (Ex) {
106 const BinaryOperator *BO =
107 dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
108 if (!BO)
109 break;
110 if (BO->getOpcode() == BO_Assign) {
111 Ex = BO->getRHS();
112 continue;
113 }
114 break;
115 }
116 return Ex;
117}
118
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000119namespace {
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000120class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000121 const CFG &cfg;
Chris Lattner254987c2007-09-15 23:21:08 +0000122 ASTContext &Ctx;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000123 BugReporter& BR;
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000124 AnalysisDeclContext* AC;
Ted Kremenek34a69172008-06-20 21:45:25 +0000125 ParentMap& Parents;
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000126 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000127 OwningPtr<ReachableCode> reachableCode;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000128 const CFGBlock *currentBlock;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000129 OwningPtr<llvm::DenseSet<const VarDecl *> > InEH;
Mike Stump11289f42009-09-09 15:08:12 +0000130
Ted Kremenekecc851b2008-07-23 21:16:38 +0000131 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump11289f42009-09-09 15:08:12 +0000132
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000133public:
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000134 DeadStoreObs(const CFG &cfg, ASTContext &ctx,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000135 BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000136 llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
Anna Zaksc29bed32011-09-20 21:38:35 +0000137 : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000138 Escaped(escaped), currentBlock(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000139
Ted Kremenekad8bce02007-09-25 04:31:27 +0000140 virtual ~DeadStoreObs() {}
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000141
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000142 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
143 if (Live.isLive(D))
144 return true;
145 // Lazily construct the set that records which VarDecls are in
146 // EH code.
147 if (!InEH.get()) {
148 InEH.reset(new llvm::DenseSet<const VarDecl *>());
149 EHCodeVisitor V(*InEH.get());
150 V.TraverseStmt(AC->getBody());
151 }
152 // Treat all VarDecls that occur in EH code as being "always live"
153 // when considering to suppress dead stores. Frequently stores
154 // are followed by reads in EH code, but we don't have the ability
155 // to analyze that yet.
156 return InEH->count(D);
157 }
158
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000159 void Report(const VarDecl *V, DeadStoreKind dsk,
Anna Zaksc29bed32011-09-20 21:38:35 +0000160 PathDiagnosticLocation L, SourceRange R) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000161 if (Escaped.count(V))
162 return;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000163
164 // Compute reachable blocks within the CFG for trivial cases
165 // where a bogus dead store can be reported because itself is unreachable.
166 if (!reachableCode.get()) {
167 reachableCode.reset(new ReachableCode(cfg));
168 reachableCode->computeReachableBlocks();
169 }
170
171 if (!reachableCode->isReachable(currentBlock))
172 return;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000173
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000174 SmallString<64> buf;
Jordy Rose82c673d2011-08-21 05:25:15 +0000175 llvm::raw_svector_ostream os(buf);
176 const char *BugType = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000177
Ted Kremenekecc851b2008-07-23 21:16:38 +0000178 switch (dsk) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000179 case DeadInit:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000180 BugType = "Dead initialization";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000181 os << "Value stored to '" << *V
Jordy Rose82c673d2011-08-21 05:25:15 +0000182 << "' during its initialization is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000183 break;
Mike Stump11289f42009-09-09 15:08:12 +0000184
Ted Kremenekecc851b2008-07-23 21:16:38 +0000185 case DeadIncrement:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000186 BugType = "Dead increment";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000187 case Standard:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000188 if (!BugType) BugType = "Dead assignment";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000189 os << "Value stored to '" << *V << "' is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000190 break;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Ted Kremenekecc851b2008-07-23 21:16:38 +0000192 case Enclosing:
Ted Kremenekf2248202011-01-13 20:58:56 +0000193 // Don't report issues in this case, e.g.: "if (x = foo())",
194 // where 'x' is unused later. We have yet to see a case where
195 // this is a real bug.
196 return;
Ted Kremenek81bfc072008-07-15 18:06:32 +0000197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenek5a10f082012-04-04 18:11:35 +0000199 BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R);
Ted Kremenek34a69172008-06-20 21:45:25 +0000200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000202 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenekecc851b2008-07-23 21:16:38 +0000203 DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000204 const LiveVariables::LivenessValues &Live) {
Ted Kremenek34a69172008-06-20 21:45:25 +0000205
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000206 if (!VD->hasLocalStorage())
207 return;
208 // Reference types confuse the dead stores checker. Skip them
209 // for now.
210 if (VD->getType()->getAs<ReferenceType>())
211 return;
212
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000213 if (!isLive(Live, VD) &&
Anna Zaksc29bed32011-09-20 21:38:35 +0000214 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) {
215
216 PathDiagnosticLocation ExLoc =
217 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
218 Report(VD, dsk, ExLoc, Val->getSourceRange());
219 }
Ted Kremenek91f035c2008-05-21 22:59:16 +0000220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000222 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000223 const LiveVariables::LivenessValues& Live) {
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000224 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000225 CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenekecc851b2008-07-23 21:16:38 +0000226 }
Mike Stump11289f42009-09-09 15:08:12 +0000227
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000228 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000229 if (B->isCompoundAssignmentOp())
230 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000231
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000232 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000233 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenekecc851b2008-07-23 21:16:38 +0000235 if (!BRHS)
236 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000238 const DeclRefExpr *DR;
Mike Stump11289f42009-09-09 15:08:12 +0000239
Ted Kremenekecc851b2008-07-23 21:16:38 +0000240 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
241 if (DR->getDecl() == VD)
242 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenekecc851b2008-07-23 21:16:38 +0000244 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->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 return false;
Ted Kremenekf15cd142008-05-05 23:12:21 +0000249 }
Mike Stump11289f42009-09-09 15:08:12 +0000250
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000251 virtual void observeStmt(const Stmt *S, const CFGBlock *block,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000252 const LiveVariables::LivenessValues &Live) {
Mike Stump11289f42009-09-09 15:08:12 +0000253
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000254 currentBlock = block;
255
Ted Kremenek87bfc032008-04-14 18:28:25 +0000256 // Skip statements in macros.
257 if (S->getLocStart().isMacroID())
258 return;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000260 // Only cover dead stores from regular assignments. ++/-- dead stores
261 // have never flagged a real bug.
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000262 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000263 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump11289f42009-09-09 15:08:12 +0000264
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000265 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek34a69172008-06-20 21:45:25 +0000266 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek0216b832008-08-09 00:05:14 +0000267 // Special case: check for assigning null to a pointer.
Mike Stump11289f42009-09-09 15:08:12 +0000268 // This is a common form of defensive programming.
Ted Kremenekdc53f002012-04-04 19:58:03 +0000269 const Expr *RHS = LookThroughTransitiveAssignments(B->getRHS());
270
Ted Kremenekb4331a92010-02-23 21:19:33 +0000271 QualType T = VD->getType();
272 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenekdc53f002012-04-04 19:58:03 +0000273 if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
Ted Kremenek12b64952009-11-22 20:26:21 +0000274 return;
Ted Kremenek0216b832008-08-09 00:05:14 +0000275 }
Ted Kremenek12b64952009-11-22 20:26:21 +0000276
Ted Kremenekdc53f002012-04-04 19:58:03 +0000277 RHS = RHS->IgnoreParenCasts();
Ted Kremenek890d44e2009-01-09 22:15:01 +0000278 // Special case: self-assignments. These are often used to shut up
279 // "unused variable" compiler warnings.
Ted Kremenekdc53f002012-04-04 19:58:03 +0000280 if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
Ted Kremenek890d44e2009-01-09 22:15:01 +0000281 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
282 return;
Mike Stump11289f42009-09-09 15:08:12 +0000283
Ted Kremenek890d44e2009-01-09 22:15:01 +0000284 // Otherwise, issue a warning.
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000285 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump11289f42009-09-09 15:08:12 +0000286 ? Enclosing
Ted Kremeneke5fe6172009-01-20 00:47:45 +0000287 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000289 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000290 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000291 }
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000292 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000293 if (!U->isIncrementOp() || U->isPrefix())
Ted Kremenekf15cd142008-05-05 23:12:21 +0000294 return;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000296 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenekb1c392a2011-02-12 00:17:19 +0000297 if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenekbb7818b2008-10-15 05:23:41 +0000298 return;
Ted Kremenek87b16f42008-07-24 17:01:17 +0000299
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000300 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000301
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000302 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000303 CheckDeclRef(DR, U, DeadIncrement, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000304 }
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000305 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
Ted Kremenekad8bce02007-09-25 04:31:27 +0000306 // Iterate through the decls. Warn if any initializers are complex
307 // expressions that are not live (never used).
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000308 for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000309 DI != DE; ++DI) {
Mike Stump11289f42009-09-09 15:08:12 +0000310
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000311 VarDecl *V = dyn_cast<VarDecl>(*DI);
Ted Kremenek092ec762008-07-25 04:47:34 +0000312
313 if (!V)
314 continue;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000315
316 if (V->hasLocalStorage()) {
317 // Reference types confuse the dead stores checker. Skip them
318 // for now.
319 if (V->getType()->getAs<ReferenceType>())
320 return;
321
Ted Kremenekdc53f002012-04-04 19:58:03 +0000322 if (const Expr *E = V->getInit()) {
323 while (const ExprWithCleanups *exprClean =
324 dyn_cast<ExprWithCleanups>(E))
John McCall31168b02011-06-15 23:02:42 +0000325 E = exprClean->getSubExpr();
326
Ted Kremenekdc53f002012-04-04 19:58:03 +0000327 // Look through transitive assignments, e.g.:
328 // int x = y = 0;
329 E = LookThroughTransitiveAssignments(E);
330
Ted Kremenek29f38082009-12-15 04:12:12 +0000331 // Don't warn on C++ objects (yet) until we can show that their
332 // constructors/destructors don't have side effects.
333 if (isa<CXXConstructExpr>(E))
334 return;
Ted Kremenek857f41c2009-12-23 04:11:44 +0000335
Ted Kremenek092ec762008-07-25 04:47:34 +0000336 // A dead initialization is a variable that is dead after it
337 // is initialized. We don't flag warnings for those variables
338 // marked 'unused'.
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000339 if (!isLive(Live, V) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremeneka6ef56e2007-09-28 20:48:41 +0000340 // Special case: check for initializations with constants.
341 //
342 // e.g. : int x = 0;
343 //
344 // If x is EVER assigned a new value later, don't issue
345 // a warning. This is because such initialization can be
346 // due to defensive programming.
Richard Smith1e1f5ab2011-12-06 23:25:15 +0000347 if (E->isEvaluatable(Ctx))
Ted Kremenek0203db72009-02-09 18:01:00 +0000348 return;
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenekdc53f002012-04-04 19:58:03 +0000350 if (const DeclRefExpr *DRE =
351 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
352 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Ted Kremeneke174fda2010-03-18 01:22:39 +0000353 // Special case: check for initialization from constant
354 // variables.
355 //
356 // e.g. extern const int MyConstant;
357 // int x = MyConstant;
358 //
Ted Kremenek0203db72009-02-09 18:01:00 +0000359 if (VD->hasGlobalStorage() &&
Ted Kremeneke174fda2010-03-18 01:22:39 +0000360 VD->getType().isConstQualified())
361 return;
362 // Special case: check for initialization from scalar
363 // parameters. This is often a form of defensive
364 // programming. Non-scalars are still an error since
365 // because it more likely represents an actual algorithmic
366 // bug.
367 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
368 return;
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Anna Zaksc29bed32011-09-20 21:38:35 +0000371 PathDiagnosticLocation Loc =
372 PathDiagnosticLocation::create(V, BR.getSourceManager());
373 Report(V, DeadInit, Loc, E->getSourceRange());
Ted Kremenek2f1a79d2007-09-11 17:24:14 +0000374 }
Ted Kremenek092ec762008-07-25 04:47:34 +0000375 }
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000376 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000377 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000378 }
379};
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000381} // end anonymous namespace
382
Ted Kremenek7e151302008-04-14 17:39:48 +0000383//===----------------------------------------------------------------------===//
Ted Kremenekc7efb532008-07-02 23:16:33 +0000384// Driver function to invoke the Dead-Stores checker on a CFG.
385//===----------------------------------------------------------------------===//
386
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000387namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000388class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000389 CFG *cfg;
390public:
391 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump11289f42009-09-09 15:08:12 +0000392
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000393 CFG& getCFG() { return *cfg; }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000395 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000396
397 void VisitUnaryOperator(UnaryOperator* U) {
398 // Check for '&'. Any VarDecl whose value has its address-taken we
399 // treat as escaped.
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000400 Expr *E = U->getSubExpr()->IgnoreParenCasts();
John McCalle3027922010-08-25 11:45:40 +0000401 if (U->getOpcode() == UO_AddrOf)
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000402 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
403 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000404 Escaped.insert(VD);
405 return;
406 }
407 Visit(E);
408 }
409};
410} // end anonymous namespace
Mike Stump11289f42009-09-09 15:08:12 +0000411
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000412
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000413//===----------------------------------------------------------------------===//
414// DeadStoresChecker
415//===----------------------------------------------------------------------===//
416
417namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000418class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000419public:
420 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
421 BugReporter &BR) const {
Ted Kremenek3e05be92013-02-18 07:18:28 +0000422
423 // Don't do anything for template instantiations.
424 // Proving that code in a template instantiation is "dead"
425 // means proving that it is dead in all instantiations.
426 // This same problem exists with -Wunreachable-code.
427 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
428 if (FD->isTemplateInstantiation())
429 return;
430
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000431 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000432 CFG &cfg = *mgr.getCFG(D);
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000433 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000434 ParentMap &pmap = mgr.getParentMap(D);
435 FindEscaped FS(&cfg);
436 FS.getCFG().VisitBlockStmts(FS);
Anna Zaksc29bed32011-09-20 21:38:35 +0000437 DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000438 L->runOnAllBlocks(A);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000439 }
440 }
441};
442}
443
444void ento::registerDeadStoresChecker(CheckerManager &mgr) {
445 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenek7e151302008-04-14 17:39:48 +0000446}