blob: 7c44c217061f275cf2202b52c3eea704cfbee576 [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());
88
89 while (!worklist.empty()) {
90 const CFGBlock *block = worklist.back();
91 worklist.pop_back();
92 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
93 if (isReachable)
94 continue;
95 isReachable = true;
96 for (CFGBlock::const_succ_iterator i = block->succ_begin(),
97 e = block->succ_end(); i != e; ++i)
98 if (const CFGBlock *succ = *i)
99 worklist.push_back(succ);
100 }
101}
102
Anna Zaks144579e2013-04-25 21:52:35 +0000103static const Expr *
104LookThroughTransitiveAssignmentsAndCommaOperators(const Expr *Ex) {
Ted Kremenekdc53f002012-04-04 19:58:03 +0000105 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 }
Anna Zaks144579e2013-04-25 21:52:35 +0000114 if (BO->getOpcode() == BO_Comma) {
115 Ex = BO->getRHS();
116 continue;
117 }
Ted Kremenekdc53f002012-04-04 19:58:03 +0000118 break;
119 }
120 return Ex;
121}
122
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000123namespace {
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000124class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000125 const CFG &cfg;
Chris Lattner254987c2007-09-15 23:21:08 +0000126 ASTContext &Ctx;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000127 BugReporter& BR;
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;
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000131 OwningPtr<ReachableCode> reachableCode;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000132 const CFGBlock *currentBlock;
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000133 OwningPtr<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:
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000138 DeadStoreObs(const CFG &cfg, ASTContext &ctx,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000139 BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000140 llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
Anna Zaksc29bed32011-09-20 21:38:35 +0000141 : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000142 Escaped(escaped), currentBlock(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenekad8bce02007-09-25 04:31:27 +0000144 virtual ~DeadStoreObs() {}
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000145
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000146 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
147 if (Live.isLive(D))
148 return true;
149 // Lazily construct the set that records which VarDecls are in
150 // EH code.
151 if (!InEH.get()) {
152 InEH.reset(new llvm::DenseSet<const VarDecl *>());
153 EHCodeVisitor V(*InEH.get());
154 V.TraverseStmt(AC->getBody());
155 }
156 // Treat all VarDecls that occur in EH code as being "always live"
157 // when considering to suppress dead stores. Frequently stores
158 // are followed by reads in EH code, but we don't have the ability
159 // to analyze that yet.
160 return InEH->count(D);
161 }
162
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000163 void Report(const VarDecl *V, DeadStoreKind dsk,
Anna Zaksc29bed32011-09-20 21:38:35 +0000164 PathDiagnosticLocation L, SourceRange R) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000165 if (Escaped.count(V))
166 return;
Ted Kremenek9865d7f2011-02-11 23:24:26 +0000167
168 // Compute reachable blocks within the CFG for trivial cases
169 // where a bogus dead store can be reported because itself is unreachable.
170 if (!reachableCode.get()) {
171 reachableCode.reset(new ReachableCode(cfg));
172 reachableCode->computeReachableBlocks();
173 }
174
175 if (!reachableCode->isReachable(currentBlock))
176 return;
Ted Kremenekc18255d2008-07-14 20:56:04 +0000177
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000178 SmallString<64> buf;
Jordy Rose82c673d2011-08-21 05:25:15 +0000179 llvm::raw_svector_ostream os(buf);
180 const char *BugType = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000181
Ted Kremenekecc851b2008-07-23 21:16:38 +0000182 switch (dsk) {
Ted Kremenekecc851b2008-07-23 21:16:38 +0000183 case DeadInit:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000184 BugType = "Dead initialization";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000185 os << "Value stored to '" << *V
Jordy Rose82c673d2011-08-21 05:25:15 +0000186 << "' during its initialization is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000187 break;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Ted Kremenekecc851b2008-07-23 21:16:38 +0000189 case DeadIncrement:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000190 BugType = "Dead increment";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000191 case Standard:
Ted Kremenek6e4c2842009-04-02 22:50:16 +0000192 if (!BugType) BugType = "Dead assignment";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000193 os << "Value stored to '" << *V << "' is never read";
Ted Kremenekecc851b2008-07-23 21:16:38 +0000194 break;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Ted Kremenekecc851b2008-07-23 21:16:38 +0000196 case Enclosing:
Ted Kremenekf2248202011-01-13 20:58:56 +0000197 // Don't report issues in this case, e.g.: "if (x = foo())",
198 // where 'x' is unused later. We have yet to see a case where
199 // this is a real bug.
200 return;
Ted Kremenek81bfc072008-07-15 18:06:32 +0000201 }
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek5a10f082012-04-04 18:11:35 +0000203 BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R);
Ted Kremenek34a69172008-06-20 21:45:25 +0000204 }
Mike Stump11289f42009-09-09 15:08:12 +0000205
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000206 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenekecc851b2008-07-23 21:16:38 +0000207 DeadStoreKind dsk,
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000208 const LiveVariables::LivenessValues &Live) {
Ted Kremenek34a69172008-06-20 21:45:25 +0000209
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000210 if (!VD->hasLocalStorage())
211 return;
212 // Reference types confuse the dead stores checker. Skip them
213 // for now.
214 if (VD->getType()->getAs<ReferenceType>())
215 return;
216
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000217 if (!isLive(Live, VD) &&
Anna Zaksc29bed32011-09-20 21:38:35 +0000218 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) {
219
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
343 // marked 'unused'.
Ted Kremenekcadd9f12012-09-06 22:32:48 +0000344 if (!isLive(Live, V) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremeneka6ef56e2007-09-28 20:48:41 +0000345 // Special case: check for initializations with constants.
346 //
347 // e.g. : int x = 0;
348 //
349 // If x is EVER assigned a new value later, don't issue
350 // a warning. This is because such initialization can be
351 // due to defensive programming.
Richard Smith1e1f5ab2011-12-06 23:25:15 +0000352 if (E->isEvaluatable(Ctx))
Ted Kremenek0203db72009-02-09 18:01:00 +0000353 return;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenekdc53f002012-04-04 19:58:03 +0000355 if (const DeclRefExpr *DRE =
356 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
357 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Ted Kremeneke174fda2010-03-18 01:22:39 +0000358 // Special case: check for initialization from constant
359 // variables.
360 //
361 // e.g. extern const int MyConstant;
362 // int x = MyConstant;
363 //
Ted Kremenek0203db72009-02-09 18:01:00 +0000364 if (VD->hasGlobalStorage() &&
Ted Kremeneke174fda2010-03-18 01:22:39 +0000365 VD->getType().isConstQualified())
366 return;
367 // Special case: check for initialization from scalar
368 // parameters. This is often a form of defensive
369 // programming. Non-scalars are still an error since
370 // because it more likely represents an actual algorithmic
371 // bug.
372 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
373 return;
374 }
Mike Stump11289f42009-09-09 15:08:12 +0000375
Anna Zaksc29bed32011-09-20 21:38:35 +0000376 PathDiagnosticLocation Loc =
377 PathDiagnosticLocation::create(V, BR.getSourceManager());
378 Report(V, DeadInit, Loc, E->getSourceRange());
Ted Kremenek2f1a79d2007-09-11 17:24:14 +0000379 }
Ted Kremenek092ec762008-07-25 04:47:34 +0000380 }
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000381 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000382 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000383 }
384};
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000386} // end anonymous namespace
387
Ted Kremenek7e151302008-04-14 17:39:48 +0000388//===----------------------------------------------------------------------===//
Ted Kremenekc7efb532008-07-02 23:16:33 +0000389// Driver function to invoke the Dead-Stores checker on a CFG.
390//===----------------------------------------------------------------------===//
391
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000392namespace {
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000393class FindEscaped {
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000394public:
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000395 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000396
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000397 void operator()(const Stmt *S) {
398 // Check for '&'. Any VarDecl whose address has been taken we treat as
399 // escaped.
400 // FIXME: What about references?
401 const UnaryOperator *U = dyn_cast<UnaryOperator>(S);
402 if (!U)
403 return;
404 if (U->getOpcode() != UO_AddrOf)
405 return;
406
407 const Expr *E = U->getSubExpr()->IgnoreParenCasts();
408 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
409 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
410 Escaped.insert(VD);
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000411 }
412};
413} // end anonymous namespace
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000415
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000416//===----------------------------------------------------------------------===//
417// DeadStoresChecker
418//===----------------------------------------------------------------------===//
419
420namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000421class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000422public:
423 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
424 BugReporter &BR) const {
Ted Kremenek3e05be92013-02-18 07:18:28 +0000425
426 // Don't do anything for template instantiations.
427 // Proving that code in a template instantiation is "dead"
428 // means proving that it is dead in all instantiations.
429 // This same problem exists with -Wunreachable-code.
430 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
431 if (FD->isTemplateInstantiation())
432 return;
433
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000434 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000435 CFG &cfg = *mgr.getCFG(D);
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000436 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000437 ParentMap &pmap = mgr.getParentMap(D);
Jordan Rosea7f94ce2013-05-15 23:22:55 +0000438 FindEscaped FS;
439 cfg.VisitBlockStmts(FS);
Anna Zaksc29bed32011-09-20 21:38:35 +0000440 DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
Ted Kremeneke9fda1e2011-07-28 23:07:59 +0000441 L->runOnAllBlocks(A);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000442 }
443 }
444};
445}
446
447void ento::registerDeadStoresChecker(CheckerManager &mgr) {
448 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenek7e151302008-04-14 17:39:48 +0000449}