blob: 44fdb3afbba59d4c897f41c7aca5df8aaeeac435 [file] [log] [blame]
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00001//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenek1ed6d2e2007-09-06 23:01:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines a DeadStores, a flow-sensitive checker that looks for
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000011// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000015#include "clang/GR/Checkers/LocalCheckers.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000016#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000017#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000018#include "clang/GR/BugReporter/BugReporter.h"
19#include "clang/GR/PathSensitive/GRExprEngine.h"
Ted Kremenekf96f16d2009-04-07 05:25:24 +000020#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000021#include "clang/Basic/Diagnostic.h"
Ted Kremenekce1cab92007-09-11 17:24:14 +000022#include "clang/AST/ASTContext.h"
Ted Kremenek1a654b62008-06-20 21:45:25 +000023#include "clang/AST/ParentMap.h"
Ted Kremenekf96f16d2009-04-07 05:25:24 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000025
26using namespace clang;
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000027using namespace GR;
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000028
29namespace {
Ted Kremenek1a654b62008-06-20 21:45:25 +000030
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000031class DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattnerc0508f92007-09-15 23:21:08 +000032 ASTContext &Ctx;
Ted Kremenek8f269862008-07-14 20:56:04 +000033 BugReporter& BR;
Ted Kremenek1a654b62008-06-20 21:45:25 +000034 ParentMap& Parents;
Ted Kremenekf96f16d2009-04-07 05:25:24 +000035 llvm::SmallPtrSet<VarDecl*, 20> Escaped;
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenek2cfac222008-07-23 21:16:38 +000037 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump1eb44332009-09-09 15:08:12 +000038
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000039public:
Ted Kremenekf96f16d2009-04-07 05:25:24 +000040 DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents,
41 llvm::SmallPtrSet<VarDecl*, 20> &escaped)
42 : Ctx(ctx), BR(br), Parents(parents), Escaped(escaped) {}
Mike Stump1eb44332009-09-09 15:08:12 +000043
Ted Kremenekfdd225e2007-09-25 04:31:27 +000044 virtual ~DeadStoreObs() {}
Ted Kremenekb930d7a2009-04-01 06:52:48 +000045
Ted Kremenek2cfac222008-07-23 21:16:38 +000046 void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +000047 if (Escaped.count(V))
48 return;
Ted Kremenek8f269862008-07-14 20:56:04 +000049
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000050 std::string name = V->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek2cfac222008-07-23 21:16:38 +000052 const char* BugType = 0;
53 std::string msg;
Mike Stump1eb44332009-09-09 15:08:12 +000054
Ted Kremenek2cfac222008-07-23 21:16:38 +000055 switch (dsk) {
56 default:
57 assert(false && "Impossible dead store type.");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenek2cfac222008-07-23 21:16:38 +000059 case DeadInit:
Ted Kremenekefc620c2009-04-02 22:50:16 +000060 BugType = "Dead initialization";
Ted Kremenek2cfac222008-07-23 21:16:38 +000061 msg = "Value stored to '" + name +
62 "' during its initialization is never read";
63 break;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Ted Kremenek2cfac222008-07-23 21:16:38 +000065 case DeadIncrement:
Ted Kremenekefc620c2009-04-02 22:50:16 +000066 BugType = "Dead increment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000067 case Standard:
Ted Kremenekefc620c2009-04-02 22:50:16 +000068 if (!BugType) BugType = "Dead assignment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000069 msg = "Value stored to '" + name + "' is never read";
70 break;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Ted Kremenek2cfac222008-07-23 21:16:38 +000072 case Enclosing:
Ted Kremenekefc620c2009-04-02 22:50:16 +000073 BugType = "Dead nested assignment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000074 msg = "Although the value stored to '" + name +
75 "' is used in the enclosing expression, the value is never actually"
76 " read from '" + name + "'";
77 break;
Ted Kremenekf9c2a5d2008-07-15 18:06:32 +000078 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Benjamin Kramerf0171732009-11-29 18:27:55 +000080 BR.EmitBasicReport(BugType, "Dead store", msg, L, R);
Ted Kremenek1a654b62008-06-20 21:45:25 +000081 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Ted Kremenek1a654b62008-06-20 21:45:25 +000083 void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
Ted Kremenek2cfac222008-07-23 21:16:38 +000084 DeadStoreKind dsk,
Ted Kremenek1a654b62008-06-20 21:45:25 +000085 const LiveVariables::AnalysisDataTy& AD,
86 const LiveVariables::ValTy& Live) {
87
Ted Kremenek852274d2009-12-16 03:18:58 +000088 if (!VD->hasLocalStorage())
89 return;
90 // Reference types confuse the dead stores checker. Skip them
91 // for now.
92 if (VD->getType()->getAs<ReferenceType>())
93 return;
94
95 if (!Live(VD, AD) &&
Ted Kremenek74635d82009-12-03 00:46:16 +000096 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>()))
Ted Kremenek2cfac222008-07-23 21:16:38 +000097 Report(VD, dsk, Ex->getSourceRange().getBegin(),
Mike Stump1eb44332009-09-09 15:08:12 +000098 Val->getSourceRange());
Ted Kremenek3eb817e2008-05-21 22:59:16 +000099 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenek2cfac222008-07-23 21:16:38 +0000101 void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk,
Ted Kremeneka23157e2008-05-05 23:12:21 +0000102 const LiveVariables::AnalysisDataTy& AD,
103 const LiveVariables::ValTy& Live) {
Ted Kremeneka23157e2008-05-05 23:12:21 +0000104 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek2cfac222008-07-23 21:16:38 +0000105 CheckVarDecl(VD, DR, Val, dsk, AD, Live);
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek2cfac222008-07-23 21:16:38 +0000108 bool isIncrement(VarDecl* VD, BinaryOperator* B) {
109 if (B->isCompoundAssignmentOp())
110 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek2cfac222008-07-23 21:16:38 +0000112 Expr* RHS = B->getRHS()->IgnoreParenCasts();
113 BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek2cfac222008-07-23 21:16:38 +0000115 if (!BRHS)
116 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Ted Kremenek2cfac222008-07-23 21:16:38 +0000118 DeclRefExpr *DR;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenek2cfac222008-07-23 21:16:38 +0000120 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
121 if (DR->getDecl() == VD)
122 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Ted Kremenek2cfac222008-07-23 21:16:38 +0000124 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
125 if (DR->getDecl() == VD)
126 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek2cfac222008-07-23 21:16:38 +0000128 return false;
Ted Kremeneka23157e2008-05-05 23:12:21 +0000129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000131 virtual void ObserveStmt(Stmt* S,
132 const LiveVariables::AnalysisDataTy& AD,
133 const LiveVariables::ValTy& Live) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Ted Kremenek1c86b152008-04-14 18:28:25 +0000135 // Skip statements in macros.
136 if (S->getLocStart().isMacroID())
137 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
139 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000140 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000142 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +0000143 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremeneke12691c2008-08-09 00:05:14 +0000144 // Special case: check for assigning null to a pointer.
Mike Stump1eb44332009-09-09 15:08:12 +0000145 // This is a common form of defensive programming.
Ted Kremenek89132202010-02-23 21:19:33 +0000146 QualType T = VD->getType();
147 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000148 if (B->getRHS()->isNullPointerConstant(Ctx,
149 Expr::NPC_ValueDependentIsNull))
150 return;
Ted Kremeneke12691c2008-08-09 00:05:14 +0000151 }
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000152
153 Expr* RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek3b587862009-01-09 22:15:01 +0000154 // Special case: self-assignments. These are often used to shut up
155 // "unused variable" compiler warnings.
156 if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
157 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
158 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremenek3b587862009-01-09 22:15:01 +0000160 // Otherwise, issue a warning.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000161 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump1eb44332009-09-09 15:08:12 +0000162 ? Enclosing
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000163 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremeneke12691c2008-08-09 00:05:14 +0000165 CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000166 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000167 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000168 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
169 if (!U->isIncrementOp())
170 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek380277e2008-10-15 05:23:41 +0000172 // Handle: ++x within a subexpression. The solution is not warn
173 // about preincrements to dead variables when the preincrement occurs
174 // as a subexpression. This can lead to false negatives, e.g. "(++x);"
175 // A generalized dead code checker should find such issues.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000176 if (U->isPrefix() && Parents.isConsumedExpr(U))
Ted Kremenek380277e2008-10-15 05:23:41 +0000177 return;
Ted Kremenekb0f36322008-07-24 17:01:17 +0000178
Ted Kremeneka23157e2008-05-05 23:12:21 +0000179 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremeneka23157e2008-05-05 23:12:21 +0000181 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek2cfac222008-07-23 21:16:38 +0000182 CheckDeclRef(DR, U, DeadIncrement, AD, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000183 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000184 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000185 // Iterate through the decls. Warn if any initializers are complex
186 // expressions that are not live (never used).
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000187 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
188 DI != DE; ++DI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000190 VarDecl* V = dyn_cast<VarDecl>(*DI);
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000191
192 if (!V)
193 continue;
Ted Kremenek852274d2009-12-16 03:18:58 +0000194
195 if (V->hasLocalStorage()) {
196 // Reference types confuse the dead stores checker. Skip them
197 // for now.
198 if (V->getType()->getAs<ReferenceType>())
199 return;
200
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000201 if (Expr* E = V->getInit()) {
Ted Kremenek43f19e32009-12-15 04:12:12 +0000202 // Don't warn on C++ objects (yet) until we can show that their
203 // constructors/destructors don't have side effects.
204 if (isa<CXXConstructExpr>(E))
205 return;
Ted Kremenek604d9392009-12-23 04:11:44 +0000206
John McCall4765fa02010-12-06 08:20:24 +0000207 if (isa<ExprWithCleanups>(E))
Ted Kremenek604d9392009-12-23 04:11:44 +0000208 return;
209
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000210 // A dead initialization is a variable that is dead after it
211 // is initialized. We don't flag warnings for those variables
212 // marked 'unused'.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000213 if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000214 // Special case: check for initializations with constants.
215 //
216 // e.g. : int x = 0;
217 //
218 // If x is EVER assigned a new value later, don't issue
219 // a warning. This is because such initialization can be
220 // due to defensive programming.
John McCall4204f072010-08-02 21:13:48 +0000221 if (E->isConstantInitializer(Ctx, false))
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000222 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000224 if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
Ted Kremenekebd42f42010-03-18 01:22:39 +0000225 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
226 // Special case: check for initialization from constant
227 // variables.
228 //
229 // e.g. extern const int MyConstant;
230 // int x = MyConstant;
231 //
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000232 if (VD->hasGlobalStorage() &&
Ted Kremenekebd42f42010-03-18 01:22:39 +0000233 VD->getType().isConstQualified())
234 return;
235 // Special case: check for initialization from scalar
236 // parameters. This is often a form of defensive
237 // programming. Non-scalars are still an error since
238 // because it more likely represents an actual algorithmic
239 // bug.
240 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
241 return;
242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000244 Report(V, DeadInit, V->getLocation(), E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000245 }
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000246 }
Ted Kremenek852274d2009-12-16 03:18:58 +0000247 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000248 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000249 }
250};
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000252} // end anonymous namespace
253
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000254//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000255// Driver function to invoke the Dead-Stores checker on a CFG.
256//===----------------------------------------------------------------------===//
257
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000258namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000259class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000260 CFG *cfg;
261public:
262 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000264 CFG& getCFG() { return *cfg; }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000266 llvm::SmallPtrSet<VarDecl*, 20> Escaped;
267
268 void VisitUnaryOperator(UnaryOperator* U) {
269 // Check for '&'. Any VarDecl whose value has its address-taken we
270 // treat as escaped.
271 Expr* E = U->getSubExpr()->IgnoreParenCasts();
John McCall2de56d12010-08-25 11:45:40 +0000272 if (U->getOpcode() == UO_AddrOf)
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000273 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
274 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
275 Escaped.insert(VD);
276 return;
277 }
278 Visit(E);
279 }
280};
281} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000283
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000284void GR::CheckDeadStores(CFG &cfg, LiveVariables &L, ParentMap &pmap,
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000285 BugReporter& BR) {
286 FindEscaped FS(&cfg);
Mike Stump1eb44332009-09-09 15:08:12 +0000287 FS.getCFG().VisitBlockStmts(FS);
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000288 DeadStoreObs A(BR.getContext(), BR, pmap, FS.Escaped);
289 L.runOnAllBlocks(cfg, &A);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000290}