blob: d5cb7ca7fdd30beded3d157a97eec8a0b40f483a [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 Kremenekc2b51d82008-01-08 18:19:08 +000025#include "llvm/Support/Compiler.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000026
27using namespace clang;
28
29namespace {
Ted Kremenek1a654b62008-06-20 21:45:25 +000030
Ted Kremenekc2b51d82008-01-08 18:19:08 +000031class VISIBILITY_HIDDEN 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
80 BR.EmitBasicReport(BugType, "Dead store", msg.c_str(), 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
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000088 if (VD->hasLocalStorage() && !Live(VD, AD) && !VD->getAttr<UnusedAttr>())
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 Kremenek3b587862009-01-09 22:15:01 +0000137 Expr* RHS = B->getRHS()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremeneke12691c2008-08-09 00:05:14 +0000139 // Special case: check for assigning null to a pointer.
Mike Stump1eb44332009-09-09 15:08:12 +0000140 // This is a common form of defensive programming.
Ted Kremeneke12691c2008-08-09 00:05:14 +0000141 if (VD->getType()->isPointerType()) {
Ted Kremenek3b587862009-01-09 22:15:01 +0000142 if (IntegerLiteral* L = dyn_cast<IntegerLiteral>(RHS))
Mike Stump1eb44332009-09-09 15:08:12 +0000143 // FIXME: Probably should have an Expr::isNullPointerConstant.
Ted Kremeneke12691c2008-08-09 00:05:14 +0000144 if (L->getValue() == 0)
145 return;
146 }
Ted Kremenek3b587862009-01-09 22:15:01 +0000147 // Special case: self-assignments. These are often used to shut up
148 // "unused variable" compiler warnings.
149 if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
150 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
151 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenek3b587862009-01-09 22:15:01 +0000153 // Otherwise, issue a warning.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000154 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump1eb44332009-09-09 15:08:12 +0000155 ? Enclosing
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000156 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Ted Kremeneke12691c2008-08-09 00:05:14 +0000158 CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000159 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000160 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000161 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
162 if (!U->isIncrementOp())
163 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek380277e2008-10-15 05:23:41 +0000165 // Handle: ++x within a subexpression. The solution is not warn
166 // about preincrements to dead variables when the preincrement occurs
167 // as a subexpression. This can lead to false negatives, e.g. "(++x);"
168 // A generalized dead code checker should find such issues.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000169 if (U->isPrefix() && Parents.isConsumedExpr(U))
Ted Kremenek380277e2008-10-15 05:23:41 +0000170 return;
Ted Kremenekb0f36322008-07-24 17:01:17 +0000171
Ted Kremeneka23157e2008-05-05 23:12:21 +0000172 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremeneka23157e2008-05-05 23:12:21 +0000174 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek2cfac222008-07-23 21:16:38 +0000175 CheckDeclRef(DR, U, DeadIncrement, AD, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000176 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000177 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000178 // Iterate through the decls. Warn if any initializers are complex
179 // expressions that are not live (never used).
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000180 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
181 DI != DE; ++DI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000183 VarDecl* V = dyn_cast<VarDecl>(*DI);
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000184
185 if (!V)
186 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000188 if (V->hasLocalStorage())
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000189 if (Expr* E = V->getInit()) {
190 // A dead initialization is a variable that is dead after it
191 // is initialized. We don't flag warnings for those variables
192 // marked 'unused'.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000193 if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000194 // Special case: check for initializations with constants.
195 //
196 // e.g. : int x = 0;
197 //
198 // If x is EVER assigned a new value later, don't issue
199 // a warning. This is because such initialization can be
200 // due to defensive programming.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000201 if (E->isConstantInitializer(Ctx))
202 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000204 // Special case: check for initializations from constant
205 // variables.
206 //
207 // e.g. extern const int MyConstant;
208 // int x = MyConstant;
209 //
210 if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
211 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
212 if (VD->hasGlobalStorage() &&
213 VD->getType().isConstQualified()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000215 Report(V, DeadInit, V->getLocation(), E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000216 }
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000217 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000218 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000219 }
220};
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000222} // end anonymous namespace
223
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000224//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000225// Driver function to invoke the Dead-Stores checker on a CFG.
226//===----------------------------------------------------------------------===//
227
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000228namespace {
229class VISIBILITY_HIDDEN FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
230 CFG *cfg;
231public:
232 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000234 CFG& getCFG() { return *cfg; }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000236 llvm::SmallPtrSet<VarDecl*, 20> Escaped;
237
238 void VisitUnaryOperator(UnaryOperator* U) {
239 // Check for '&'. Any VarDecl whose value has its address-taken we
240 // treat as escaped.
241 Expr* E = U->getSubExpr()->IgnoreParenCasts();
242 if (U->getOpcode() == UnaryOperator::AddrOf)
243 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
244 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
245 Escaped.insert(VD);
246 return;
247 }
248 Visit(E);
249 }
250};
251} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000253
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000254void clang::CheckDeadStores(CFG &cfg, LiveVariables &L, ParentMap &pmap,
255 BugReporter& BR) {
256 FindEscaped FS(&cfg);
Mike Stump1eb44332009-09-09 15:08:12 +0000257 FS.getCFG().VisitBlockStmts(FS);
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000258 DeadStoreObs A(BR.getContext(), BR, pmap, FS.Escaped);
259 L.runOnAllBlocks(cfg, &A);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000260}