blob: db9016fa1e645e69a32027bb8cb7b3d86280edb2 [file] [log] [blame]
Ted Kremenek1bb9f252007-09-06 23:01:46 +00001//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
2//
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
Ted Kremenek1bb9f252007-09-06 23:01:46 +000015#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekbf593f82007-12-21 21:42:19 +000016#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekad8bce02007-09-25 04:31:27 +000017#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenek7e151302008-04-14 17:39:48 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
19#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek4d947fa2009-04-07 05:25:24 +000020#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1bb9f252007-09-06 23:01:46 +000021#include "clang/Basic/Diagnostic.h"
Ted Kremenek2f1a79d2007-09-11 17:24:14 +000022#include "clang/AST/ASTContext.h"
Ted Kremenek34a69172008-06-20 21:45:25 +000023#include "clang/AST/ParentMap.h"
Ted Kremenek4d947fa2009-04-07 05:25:24 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek1bb9f252007-09-06 23:01:46 +000025
26using namespace clang;
27
28namespace {
Ted Kremenek34a69172008-06-20 21:45:25 +000029
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000030class DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattner254987c2007-09-15 23:21:08 +000031 ASTContext &Ctx;
Ted Kremenekc18255d2008-07-14 20:56:04 +000032 BugReporter& BR;
Ted Kremenek34a69172008-06-20 21:45:25 +000033 ParentMap& Parents;
Ted Kremenek4d947fa2009-04-07 05:25:24 +000034 llvm::SmallPtrSet<VarDecl*, 20> Escaped;
Mike Stump11289f42009-09-09 15:08:12 +000035
Ted Kremenekecc851b2008-07-23 21:16:38 +000036 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump11289f42009-09-09 15:08:12 +000037
Ted Kremenek1bb9f252007-09-06 23:01:46 +000038public:
Ted Kremenek4d947fa2009-04-07 05:25:24 +000039 DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents,
40 llvm::SmallPtrSet<VarDecl*, 20> &escaped)
41 : Ctx(ctx), BR(br), Parents(parents), Escaped(escaped) {}
Mike Stump11289f42009-09-09 15:08:12 +000042
Ted Kremenekad8bce02007-09-25 04:31:27 +000043 virtual ~DeadStoreObs() {}
Ted Kremenek8b0dba32009-04-01 06:52:48 +000044
Ted Kremenekecc851b2008-07-23 21:16:38 +000045 void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) {
Ted Kremenek4d947fa2009-04-07 05:25:24 +000046 if (Escaped.count(V))
47 return;
Ted Kremenekc18255d2008-07-14 20:56:04 +000048
Chris Lattnerf3d3fae2008-11-24 05:29:24 +000049 std::string name = V->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +000050
Ted Kremenekecc851b2008-07-23 21:16:38 +000051 const char* BugType = 0;
52 std::string msg;
Mike Stump11289f42009-09-09 15:08:12 +000053
Ted Kremenekecc851b2008-07-23 21:16:38 +000054 switch (dsk) {
55 default:
56 assert(false && "Impossible dead store type.");
Mike Stump11289f42009-09-09 15:08:12 +000057
Ted Kremenekecc851b2008-07-23 21:16:38 +000058 case DeadInit:
Ted Kremenek6e4c2842009-04-02 22:50:16 +000059 BugType = "Dead initialization";
Ted Kremenekecc851b2008-07-23 21:16:38 +000060 msg = "Value stored to '" + name +
61 "' during its initialization is never read";
62 break;
Mike Stump11289f42009-09-09 15:08:12 +000063
Ted Kremenekecc851b2008-07-23 21:16:38 +000064 case DeadIncrement:
Ted Kremenek6e4c2842009-04-02 22:50:16 +000065 BugType = "Dead increment";
Ted Kremenekecc851b2008-07-23 21:16:38 +000066 case Standard:
Ted Kremenek6e4c2842009-04-02 22:50:16 +000067 if (!BugType) BugType = "Dead assignment";
Ted Kremenekecc851b2008-07-23 21:16:38 +000068 msg = "Value stored to '" + name + "' is never read";
69 break;
Mike Stump11289f42009-09-09 15:08:12 +000070
Ted Kremenekecc851b2008-07-23 21:16:38 +000071 case Enclosing:
Ted Kremenek6e4c2842009-04-02 22:50:16 +000072 BugType = "Dead nested assignment";
Ted Kremenekecc851b2008-07-23 21:16:38 +000073 msg = "Although the value stored to '" + name +
74 "' is used in the enclosing expression, the value is never actually"
75 " read from '" + name + "'";
76 break;
Ted Kremenek81bfc072008-07-15 18:06:32 +000077 }
Mike Stump11289f42009-09-09 15:08:12 +000078
Benjamin Kramer63415532009-11-29 18:27:55 +000079 BR.EmitBasicReport(BugType, "Dead store", msg, L, R);
Ted Kremenek34a69172008-06-20 21:45:25 +000080 }
Mike Stump11289f42009-09-09 15:08:12 +000081
Ted Kremenek34a69172008-06-20 21:45:25 +000082 void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
Ted Kremenekecc851b2008-07-23 21:16:38 +000083 DeadStoreKind dsk,
Ted Kremenek34a69172008-06-20 21:45:25 +000084 const LiveVariables::AnalysisDataTy& AD,
85 const LiveVariables::ValTy& Live) {
86
Ted Kremenekf66b7202009-12-03 00:46:16 +000087 if (VD->hasLocalStorage() && !Live(VD, AD) &&
88 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>()))
Ted Kremenekecc851b2008-07-23 21:16:38 +000089 Report(VD, dsk, Ex->getSourceRange().getBegin(),
Mike Stump11289f42009-09-09 15:08:12 +000090 Val->getSourceRange());
Ted Kremenek91f035c2008-05-21 22:59:16 +000091 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Ted Kremenekecc851b2008-07-23 21:16:38 +000093 void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk,
Ted Kremenekf15cd142008-05-05 23:12:21 +000094 const LiveVariables::AnalysisDataTy& AD,
95 const LiveVariables::ValTy& Live) {
Mike Stump11289f42009-09-09 15:08:12 +000096
Ted Kremenekf15cd142008-05-05 23:12:21 +000097 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekecc851b2008-07-23 21:16:38 +000098 CheckVarDecl(VD, DR, Val, dsk, AD, Live);
99 }
Mike Stump11289f42009-09-09 15:08:12 +0000100
Ted Kremenekecc851b2008-07-23 21:16:38 +0000101 bool isIncrement(VarDecl* VD, BinaryOperator* B) {
102 if (B->isCompoundAssignmentOp())
103 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000104
Ted Kremenekecc851b2008-07-23 21:16:38 +0000105 Expr* RHS = B->getRHS()->IgnoreParenCasts();
106 BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Ted Kremenekecc851b2008-07-23 21:16:38 +0000108 if (!BRHS)
109 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000110
Ted Kremenekecc851b2008-07-23 21:16:38 +0000111 DeclRefExpr *DR;
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenekecc851b2008-07-23 21:16:38 +0000113 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
114 if (DR->getDecl() == VD)
115 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000116
Ted Kremenekecc851b2008-07-23 21:16:38 +0000117 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
118 if (DR->getDecl() == VD)
119 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000120
Ted Kremenekecc851b2008-07-23 21:16:38 +0000121 return false;
Ted Kremenekf15cd142008-05-05 23:12:21 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Ted Kremenekad8bce02007-09-25 04:31:27 +0000124 virtual void ObserveStmt(Stmt* S,
125 const LiveVariables::AnalysisDataTy& AD,
126 const LiveVariables::ValTy& Live) {
Mike Stump11289f42009-09-09 15:08:12 +0000127
Ted Kremenek87bfc032008-04-14 18:28:25 +0000128 // Skip statements in macros.
129 if (S->getLocStart().isMacroID())
130 return;
Mike Stump11289f42009-09-09 15:08:12 +0000131
132 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000133 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump11289f42009-09-09 15:08:12 +0000134
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000135 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek34a69172008-06-20 21:45:25 +0000136 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek0216b832008-08-09 00:05:14 +0000137 // Special case: check for assigning null to a pointer.
Mike Stump11289f42009-09-09 15:08:12 +0000138 // This is a common form of defensive programming.
Ted Kremenek0216b832008-08-09 00:05:14 +0000139 if (VD->getType()->isPointerType()) {
Ted Kremenek12b64952009-11-22 20:26:21 +0000140 if (B->getRHS()->isNullPointerConstant(Ctx,
141 Expr::NPC_ValueDependentIsNull))
142 return;
Ted Kremenek0216b832008-08-09 00:05:14 +0000143 }
Ted Kremenek12b64952009-11-22 20:26:21 +0000144
145 Expr* RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek890d44e2009-01-09 22:15:01 +0000146 // Special case: self-assignments. These are often used to shut up
147 // "unused variable" compiler warnings.
148 if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
149 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
150 return;
Mike Stump11289f42009-09-09 15:08:12 +0000151
Ted Kremenek890d44e2009-01-09 22:15:01 +0000152 // Otherwise, issue a warning.
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000153 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump11289f42009-09-09 15:08:12 +0000154 ? Enclosing
Ted Kremeneke5fe6172009-01-20 00:47:45 +0000155 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump11289f42009-09-09 15:08:12 +0000156
Ted Kremenek0216b832008-08-09 00:05:14 +0000157 CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000158 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000159 }
Ted Kremenekf15cd142008-05-05 23:12:21 +0000160 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
161 if (!U->isIncrementOp())
162 return;
Mike Stump11289f42009-09-09 15:08:12 +0000163
Ted Kremenekbb7818b2008-10-15 05:23:41 +0000164 // Handle: ++x within a subexpression. The solution is not warn
165 // about preincrements to dead variables when the preincrement occurs
166 // as a subexpression. This can lead to false negatives, e.g. "(++x);"
167 // A generalized dead code checker should find such issues.
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000168 if (U->isPrefix() && Parents.isConsumedExpr(U))
Ted Kremenekbb7818b2008-10-15 05:23:41 +0000169 return;
Ted Kremenek87b16f42008-07-24 17:01:17 +0000170
Ted Kremenekf15cd142008-05-05 23:12:21 +0000171 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +0000172
Ted Kremenekf15cd142008-05-05 23:12:21 +0000173 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenekecc851b2008-07-23 21:16:38 +0000174 CheckDeclRef(DR, U, DeadIncrement, AD, Live);
Mike Stump11289f42009-09-09 15:08:12 +0000175 }
Ted Kremenekf15cd142008-05-05 23:12:21 +0000176 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekad8bce02007-09-25 04:31:27 +0000177 // Iterate through the decls. Warn if any initializers are complex
178 // expressions that are not live (never used).
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000179 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
180 DI != DE; ++DI) {
Mike Stump11289f42009-09-09 15:08:12 +0000181
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000182 VarDecl* V = dyn_cast<VarDecl>(*DI);
Ted Kremenek092ec762008-07-25 04:47:34 +0000183
184 if (!V)
185 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremeneka6ef56e2007-09-28 20:48:41 +0000187 if (V->hasLocalStorage())
Ted Kremenek092ec762008-07-25 04:47:34 +0000188 if (Expr* E = V->getInit()) {
Ted Kremenek29f38082009-12-15 04:12:12 +0000189 // Don't warn on C++ objects (yet) until we can show that their
190 // constructors/destructors don't have side effects.
191 if (isa<CXXConstructExpr>(E))
192 return;
Ted Kremenek092ec762008-07-25 04:47:34 +0000193 // A dead initialization is a variable that is dead after it
194 // is initialized. We don't flag warnings for those variables
195 // marked 'unused'.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000196 if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremeneka6ef56e2007-09-28 20:48:41 +0000197 // Special case: check for initializations with constants.
198 //
199 // e.g. : int x = 0;
200 //
201 // If x is EVER assigned a new value later, don't issue
202 // a warning. This is because such initialization can be
203 // due to defensive programming.
Ted Kremenek0203db72009-02-09 18:01:00 +0000204 if (E->isConstantInitializer(Ctx))
205 return;
Mike Stump11289f42009-09-09 15:08:12 +0000206
Ted Kremenek0203db72009-02-09 18:01:00 +0000207 // Special case: check for initializations from constant
208 // variables.
209 //
210 // e.g. extern const int MyConstant;
211 // int x = MyConstant;
212 //
213 if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
214 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
215 if (VD->hasGlobalStorage() &&
216 VD->getType().isConstQualified()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek0203db72009-02-09 18:01:00 +0000218 Report(V, DeadInit, V->getLocation(), E->getSourceRange());
Ted Kremenek2f1a79d2007-09-11 17:24:14 +0000219 }
Ted Kremenek092ec762008-07-25 04:47:34 +0000220 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000221 }
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000222 }
223};
Mike Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenek1bb9f252007-09-06 23:01:46 +0000225} // end anonymous namespace
226
Ted Kremenek7e151302008-04-14 17:39:48 +0000227//===----------------------------------------------------------------------===//
Ted Kremenekc7efb532008-07-02 23:16:33 +0000228// Driver function to invoke the Dead-Stores checker on a CFG.
229//===----------------------------------------------------------------------===//
230
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000231namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000232class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000233 CFG *cfg;
234public:
235 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump11289f42009-09-09 15:08:12 +0000236
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000237 CFG& getCFG() { return *cfg; }
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000239 llvm::SmallPtrSet<VarDecl*, 20> Escaped;
240
241 void VisitUnaryOperator(UnaryOperator* U) {
242 // Check for '&'. Any VarDecl whose value has its address-taken we
243 // treat as escaped.
244 Expr* E = U->getSubExpr()->IgnoreParenCasts();
245 if (U->getOpcode() == UnaryOperator::AddrOf)
246 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
247 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
248 Escaped.insert(VD);
249 return;
250 }
251 Visit(E);
252 }
253};
254} // end anonymous namespace
Mike Stump11289f42009-09-09 15:08:12 +0000255
Ted Kremenek4d947fa2009-04-07 05:25:24 +0000256
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000257void clang::CheckDeadStores(CFG &cfg, LiveVariables &L, ParentMap &pmap,
258 BugReporter& BR) {
259 FindEscaped FS(&cfg);
Mike Stump11289f42009-09-09 15:08:12 +0000260 FS.getCFG().VisitBlockStmts(FS);
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000261 DeadStoreObs A(BR.getContext(), BR, pmap, FS.Escaped);
262 L.runOnAllBlocks(cfg, &A);
Ted Kremenek7e151302008-04-14 17:39:48 +0000263}