blob: ec13328447a6acf8485cc5106e719a7c3e1822cd [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
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000015#include "clang/Analysis/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"
Ted Kremenekd2f642b2008-04-14 17:39:48 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
19#include "clang/Analysis/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;
27
28namespace {
Ted Kremenek1a654b62008-06-20 21:45:25 +000029
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000030class DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattnerc0508f92007-09-15 23:21:08 +000031 ASTContext &Ctx;
Ted Kremenek8f269862008-07-14 20:56:04 +000032 BugReporter& BR;
Ted Kremenek1a654b62008-06-20 21:45:25 +000033 ParentMap& Parents;
Ted Kremenekf96f16d2009-04-07 05:25:24 +000034 llvm::SmallPtrSet<VarDecl*, 20> Escaped;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Ted Kremenek2cfac222008-07-23 21:16:38 +000036 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump1eb44332009-09-09 15:08:12 +000037
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000038public:
Ted Kremenekf96f16d2009-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 Stump1eb44332009-09-09 15:08:12 +000042
Ted Kremenekfdd225e2007-09-25 04:31:27 +000043 virtual ~DeadStoreObs() {}
Ted Kremenekb930d7a2009-04-01 06:52:48 +000044
Ted Kremenek2cfac222008-07-23 21:16:38 +000045 void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +000046 if (Escaped.count(V))
47 return;
Ted Kremenek8f269862008-07-14 20:56:04 +000048
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000049 std::string name = V->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +000050
Ted Kremenek2cfac222008-07-23 21:16:38 +000051 const char* BugType = 0;
52 std::string msg;
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek2cfac222008-07-23 21:16:38 +000054 switch (dsk) {
55 default:
56 assert(false && "Impossible dead store type.");
Mike Stump1eb44332009-09-09 15:08:12 +000057
Ted Kremenek2cfac222008-07-23 21:16:38 +000058 case DeadInit:
Ted Kremenekefc620c2009-04-02 22:50:16 +000059 BugType = "Dead initialization";
Ted Kremenek2cfac222008-07-23 21:16:38 +000060 msg = "Value stored to '" + name +
61 "' during its initialization is never read";
62 break;
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenek2cfac222008-07-23 21:16:38 +000064 case DeadIncrement:
Ted Kremenekefc620c2009-04-02 22:50:16 +000065 BugType = "Dead increment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000066 case Standard:
Ted Kremenekefc620c2009-04-02 22:50:16 +000067 if (!BugType) BugType = "Dead assignment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000068 msg = "Value stored to '" + name + "' is never read";
69 break;
Mike Stump1eb44332009-09-09 15:08:12 +000070
Ted Kremenek2cfac222008-07-23 21:16:38 +000071 case Enclosing:
Ted Kremenekefc620c2009-04-02 22:50:16 +000072 BugType = "Dead nested assignment";
Ted Kremenek2cfac222008-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 Kremenekf9c2a5d2008-07-15 18:06:32 +000077 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Benjamin Kramerf0171732009-11-29 18:27:55 +000079 BR.EmitBasicReport(BugType, "Dead store", msg, L, R);
Ted Kremenek1a654b62008-06-20 21:45:25 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ted Kremenek1a654b62008-06-20 21:45:25 +000082 void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
Ted Kremenek2cfac222008-07-23 21:16:38 +000083 DeadStoreKind dsk,
Ted Kremenek1a654b62008-06-20 21:45:25 +000084 const LiveVariables::AnalysisDataTy& AD,
85 const LiveVariables::ValTy& Live) {
86
Ted Kremenek74635d82009-12-03 00:46:16 +000087 if (VD->hasLocalStorage() && !Live(VD, AD) &&
88 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>()))
Ted Kremenek2cfac222008-07-23 21:16:38 +000089 Report(VD, dsk, Ex->getSourceRange().getBegin(),
Mike Stump1eb44332009-09-09 15:08:12 +000090 Val->getSourceRange());
Ted Kremenek3eb817e2008-05-21 22:59:16 +000091 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenek2cfac222008-07-23 21:16:38 +000093 void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk,
Ted Kremeneka23157e2008-05-05 23:12:21 +000094 const LiveVariables::AnalysisDataTy& AD,
95 const LiveVariables::ValTy& Live) {
Mike Stump1eb44332009-09-09 15:08:12 +000096
Ted Kremeneka23157e2008-05-05 23:12:21 +000097 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek2cfac222008-07-23 21:16:38 +000098 CheckVarDecl(VD, DR, Val, dsk, AD, Live);
99 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenek2cfac222008-07-23 21:16:38 +0000101 bool isIncrement(VarDecl* VD, BinaryOperator* B) {
102 if (B->isCompoundAssignmentOp())
103 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek2cfac222008-07-23 21:16:38 +0000105 Expr* RHS = B->getRHS()->IgnoreParenCasts();
106 BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek2cfac222008-07-23 21:16:38 +0000108 if (!BRHS)
109 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek2cfac222008-07-23 21:16:38 +0000111 DeclRefExpr *DR;
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek2cfac222008-07-23 21:16:38 +0000113 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
114 if (DR->getDecl() == VD)
115 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremenek2cfac222008-07-23 21:16:38 +0000117 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
118 if (DR->getDecl() == VD)
119 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek2cfac222008-07-23 21:16:38 +0000121 return false;
Ted Kremeneka23157e2008-05-05 23:12:21 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000124 virtual void ObserveStmt(Stmt* S,
125 const LiveVariables::AnalysisDataTy& AD,
126 const LiveVariables::ValTy& Live) {
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek1c86b152008-04-14 18:28:25 +0000128 // Skip statements in macros.
129 if (S->getLocStart().isMacroID())
130 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
132 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000133 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000135 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +0000136 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremeneke12691c2008-08-09 00:05:14 +0000137 // Special case: check for assigning null to a pointer.
Mike Stump1eb44332009-09-09 15:08:12 +0000138 // This is a common form of defensive programming.
Ted Kremeneke12691c2008-08-09 00:05:14 +0000139 if (VD->getType()->isPointerType()) {
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000140 if (B->getRHS()->isNullPointerConstant(Ctx,
141 Expr::NPC_ValueDependentIsNull))
142 return;
Ted Kremeneke12691c2008-08-09 00:05:14 +0000143 }
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000144
145 Expr* RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek3b587862009-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 Stump1eb44332009-09-09 15:08:12 +0000151
Ted Kremenek3b587862009-01-09 22:15:01 +0000152 // Otherwise, issue a warning.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000153 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump1eb44332009-09-09 15:08:12 +0000154 ? Enclosing
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000155 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ted Kremeneke12691c2008-08-09 00:05:14 +0000157 CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000158 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000159 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000160 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
161 if (!U->isIncrementOp())
162 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek380277e2008-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 Kremenekb930d7a2009-04-01 06:52:48 +0000168 if (U->isPrefix() && Parents.isConsumedExpr(U))
Ted Kremenek380277e2008-10-15 05:23:41 +0000169 return;
Ted Kremenekb0f36322008-07-24 17:01:17 +0000170
Ted Kremeneka23157e2008-05-05 23:12:21 +0000171 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Ted Kremeneka23157e2008-05-05 23:12:21 +0000173 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek2cfac222008-07-23 21:16:38 +0000174 CheckDeclRef(DR, U, DeadIncrement, AD, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000175 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000176 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-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 Kremenek14f8b4f2008-08-05 20:46:55 +0000179 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
180 DI != DE; ++DI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000182 VarDecl* V = dyn_cast<VarDecl>(*DI);
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000183
184 if (!V)
185 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000187 if (V->hasLocalStorage())
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000188 if (Expr* E = V->getInit()) {
189 // A dead initialization is a variable that is dead after it
190 // is initialized. We don't flag warnings for those variables
191 // marked 'unused'.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000192 if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000193 // Special case: check for initializations with constants.
194 //
195 // e.g. : int x = 0;
196 //
197 // If x is EVER assigned a new value later, don't issue
198 // a warning. This is because such initialization can be
199 // due to defensive programming.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000200 if (E->isConstantInitializer(Ctx))
201 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000203 // Special case: check for initializations from constant
204 // variables.
205 //
206 // e.g. extern const int MyConstant;
207 // int x = MyConstant;
208 //
209 if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
210 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
211 if (VD->hasGlobalStorage() &&
212 VD->getType().isConstQualified()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000214 Report(V, DeadInit, V->getLocation(), E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000215 }
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000216 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000217 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000218 }
219};
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000221} // end anonymous namespace
222
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000223//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000224// Driver function to invoke the Dead-Stores checker on a CFG.
225//===----------------------------------------------------------------------===//
226
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000227namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000228class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000229 CFG *cfg;
230public:
231 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000233 CFG& getCFG() { return *cfg; }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000235 llvm::SmallPtrSet<VarDecl*, 20> Escaped;
236
237 void VisitUnaryOperator(UnaryOperator* U) {
238 // Check for '&'. Any VarDecl whose value has its address-taken we
239 // treat as escaped.
240 Expr* E = U->getSubExpr()->IgnoreParenCasts();
241 if (U->getOpcode() == UnaryOperator::AddrOf)
242 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
243 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
244 Escaped.insert(VD);
245 return;
246 }
247 Visit(E);
248 }
249};
250} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000252
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000253void clang::CheckDeadStores(CFG &cfg, LiveVariables &L, ParentMap &pmap,
254 BugReporter& BR) {
255 FindEscaped FS(&cfg);
Mike Stump1eb44332009-09-09 15:08:12 +0000256 FS.getCFG().VisitBlockStmts(FS);
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000257 DeadStoreObs A(BR.getContext(), BR, pmap, FS.Escaped);
258 L.runOnAllBlocks(cfg, &A);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000259}