blob: 504de3c97dce0c75fcaa20c5ecfd91c4b0ef2557 [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 Kremenek1ed6d2e2007-09-06 23:01:46 +000020#include "clang/Basic/Diagnostic.h"
Ted Kremenekce1cab92007-09-11 17:24:14 +000021#include "clang/AST/ASTContext.h"
Ted Kremenek1a654b62008-06-20 21:45:25 +000022#include "clang/AST/ParentMap.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000024
25using namespace clang;
26
27namespace {
Ted Kremenek1a654b62008-06-20 21:45:25 +000028
Ted Kremenekc2b51d82008-01-08 18:19:08 +000029class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattnerc0508f92007-09-15 23:21:08 +000030 ASTContext &Ctx;
Ted Kremenek8f269862008-07-14 20:56:04 +000031 BugReporter& BR;
Ted Kremenek1a654b62008-06-20 21:45:25 +000032 ParentMap& Parents;
Ted Kremenek2cfac222008-07-23 21:16:38 +000033
34 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Ted Kremenek1a654b62008-06-20 21:45:25 +000035
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000036public:
Ted Kremenek8f269862008-07-14 20:56:04 +000037 DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents)
38 : Ctx(ctx), BR(br), Parents(parents) {}
Ted Kremenekd2f642b2008-04-14 17:39:48 +000039
Ted Kremenekfdd225e2007-09-25 04:31:27 +000040 virtual ~DeadStoreObs() {}
41
Ted Kremenek7f5fce72009-01-20 00:47:45 +000042 bool isConsumedExpr(Expr* E) const;
43
44
Ted Kremenek2cfac222008-07-23 21:16:38 +000045 void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) {
Ted Kremenek8f269862008-07-14 20:56:04 +000046
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000047 std::string name = V->getNameAsString();
Ted Kremenek1a654b62008-06-20 21:45:25 +000048
Ted Kremenek2cfac222008-07-23 21:16:38 +000049 const char* BugType = 0;
50 std::string msg;
Ted Kremenekf9c2a5d2008-07-15 18:06:32 +000051
Ted Kremenek2cfac222008-07-23 21:16:38 +000052 switch (dsk) {
53 default:
54 assert(false && "Impossible dead store type.");
55
56 case DeadInit:
57 BugType = "dead initialization";
58 msg = "Value stored to '" + name +
59 "' during its initialization is never read";
60 break;
61
62 case DeadIncrement:
Ted Kremenek48d5faf2008-08-02 18:19:48 +000063 BugType = "dead increment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000064 case Standard:
Ted Kremenek8c036c72008-09-20 04:23:38 +000065 if (!BugType) BugType = "dead assignment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000066 msg = "Value stored to '" + name + "' is never read";
67 break;
68
69 case Enclosing:
Ted Kremenek8c036c72008-09-20 04:23:38 +000070 BugType = "dead nested assignment";
Ted Kremenek2cfac222008-07-23 21:16:38 +000071 msg = "Although the value stored to '" + name +
72 "' is used in the enclosing expression, the value is never actually"
73 " read from '" + name + "'";
74 break;
Ted Kremenekf9c2a5d2008-07-15 18:06:32 +000075 }
Ted Kremenek2cfac222008-07-23 21:16:38 +000076
Ted Kremenek8c036c72008-09-20 04:23:38 +000077 BR.EmitBasicReport(BugType, "Dead Store", msg.c_str(), L, R);
Ted Kremenek1a654b62008-06-20 21:45:25 +000078 }
79
80 void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
Ted Kremenek2cfac222008-07-23 21:16:38 +000081 DeadStoreKind dsk,
Ted Kremenek1a654b62008-06-20 21:45:25 +000082 const LiveVariables::AnalysisDataTy& AD,
83 const LiveVariables::ValTy& Live) {
84
Ted Kremenekb238c3e2008-08-07 22:28:30 +000085 if (VD->hasLocalStorage() && !Live(VD, AD) && !VD->getAttr<UnusedAttr>())
Ted Kremenek2cfac222008-07-23 21:16:38 +000086 Report(VD, dsk, Ex->getSourceRange().getBegin(),
Ted Kremenek8f269862008-07-14 20:56:04 +000087 Val->getSourceRange());
Ted Kremenek3eb817e2008-05-21 22:59:16 +000088 }
89
Ted Kremenek2cfac222008-07-23 21:16:38 +000090 void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk,
Ted Kremeneka23157e2008-05-05 23:12:21 +000091 const LiveVariables::AnalysisDataTy& AD,
92 const LiveVariables::ValTy& Live) {
93
94 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek2cfac222008-07-23 21:16:38 +000095 CheckVarDecl(VD, DR, Val, dsk, AD, Live);
96 }
97
98 bool isIncrement(VarDecl* VD, BinaryOperator* B) {
99 if (B->isCompoundAssignmentOp())
100 return true;
101
102 Expr* RHS = B->getRHS()->IgnoreParenCasts();
103 BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
104
105 if (!BRHS)
106 return false;
107
108 DeclRefExpr *DR;
109
110 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
111 if (DR->getDecl() == VD)
112 return true;
113
114 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
115 if (DR->getDecl() == VD)
116 return true;
117
118 return false;
Ted Kremeneka23157e2008-05-05 23:12:21 +0000119 }
120
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000121 virtual void ObserveStmt(Stmt* S,
122 const LiveVariables::AnalysisDataTy& AD,
123 const LiveVariables::ValTy& Live) {
Ted Kremenekce1cab92007-09-11 17:24:14 +0000124
Ted Kremenek1c86b152008-04-14 18:28:25 +0000125 // Skip statements in macros.
126 if (S->getLocStart().isMacroID())
127 return;
128
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000129 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000130 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000131
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000132 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +0000133 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek3b587862009-01-09 22:15:01 +0000134 Expr* RHS = B->getRHS()->IgnoreParenCasts();
135
Ted Kremeneke12691c2008-08-09 00:05:14 +0000136 // Special case: check for assigning null to a pointer.
137 // This is a common form of defensive programming.
138 if (VD->getType()->isPointerType()) {
Ted Kremenek3b587862009-01-09 22:15:01 +0000139 if (IntegerLiteral* L = dyn_cast<IntegerLiteral>(RHS))
Ted Kremenek23f78232008-08-09 00:41:45 +0000140 // FIXME: Probably should have an Expr::isNullPointerConstant.
Ted Kremeneke12691c2008-08-09 00:05:14 +0000141 if (L->getValue() == 0)
142 return;
143 }
Ted Kremenek3b587862009-01-09 22:15:01 +0000144 // Special case: self-assignments. These are often used to shut up
145 // "unused variable" compiler warnings.
146 if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
147 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
148 return;
149
150 // Otherwise, issue a warning.
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000151 DeadStoreKind dsk = isConsumedExpr(B)
152 ? Enclosing
153 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Ted Kremenek2cfac222008-07-23 21:16:38 +0000154
Ted Kremeneke12691c2008-08-09 00:05:14 +0000155 CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live);
Ted Kremenek1a654b62008-06-20 21:45:25 +0000156 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000157 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000158 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
159 if (!U->isIncrementOp())
160 return;
Ted Kremenek380277e2008-10-15 05:23:41 +0000161
162 // Handle: ++x within a subexpression. The solution is not warn
163 // about preincrements to dead variables when the preincrement occurs
164 // as a subexpression. This can lead to false negatives, e.g. "(++x);"
165 // A generalized dead code checker should find such issues.
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000166 if (U->isPrefix() && isConsumedExpr(U))
Ted Kremenek380277e2008-10-15 05:23:41 +0000167 return;
Ted Kremenekb0f36322008-07-24 17:01:17 +0000168
Ted Kremeneka23157e2008-05-05 23:12:21 +0000169 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
170
171 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek2cfac222008-07-23 21:16:38 +0000172 CheckDeclRef(DR, U, DeadIncrement, AD, Live);
Ted Kremeneka23157e2008-05-05 23:12:21 +0000173 }
174 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000175 // Iterate through the decls. Warn if any initializers are complex
176 // expressions that are not live (never used).
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000177 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
178 DI != DE; ++DI) {
Ted Kremenekc967c9d2008-04-14 17:52:13 +0000179
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000180 VarDecl* V = dyn_cast<VarDecl>(*DI);
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000181
182 if (!V)
183 continue;
Ted Kremenekc967c9d2008-04-14 17:52:13 +0000184
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000185 if (V->hasLocalStorage())
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000186 if (Expr* E = V->getInit()) {
187 // A dead initialization is a variable that is dead after it
188 // is initialized. We don't flag warnings for those variables
189 // marked 'unused'.
190 if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000191 // Special case: check for initializations with constants.
192 //
193 // e.g. : int x = 0;
194 //
195 // If x is EVER assigned a new value later, don't issue
196 // a warning. This is because such initialization can be
197 // due to defensive programming.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000198 if (E->isConstantInitializer(Ctx))
199 return;
200
201 // Special case: check for initializations from constant
202 // variables.
203 //
204 // e.g. extern const int MyConstant;
205 // int x = MyConstant;
206 //
207 if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
208 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
209 if (VD->hasGlobalStorage() &&
210 VD->getType().isConstQualified()) return;
211
212 Report(V, DeadInit, V->getLocation(), E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000213 }
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000214 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000215 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000216 }
217};
218
219} // end anonymous namespace
220
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000221bool DeadStoreObs::isConsumedExpr(Expr* E) const {
222 Stmt *P = Parents.getParent(E);
223 Stmt *DirectChild = E;
224
225 // Ignore parents that are parentheses or casts.
226 while (P && (isa<ParenExpr>(E) || isa<CastExpr>(E))) {
227 DirectChild = P;
228 P = Parents.getParent(P);
229 }
230
231 if (!P)
232 return false;
233
234 switch (P->getStmtClass()) {
235 default:
236 return isa<Expr>(P);
237 case Stmt::BinaryOperatorClass: {
238 BinaryOperator *BE = cast<BinaryOperator>(P);
239 return BE->getOpcode()==BinaryOperator::Comma && DirectChild==BE->getLHS();
240 }
241 case Stmt::ForStmtClass:
242 return DirectChild == cast<ForStmt>(P)->getCond();
243 case Stmt::WhileStmtClass:
244 return DirectChild == cast<WhileStmt>(P)->getCond();
245 case Stmt::DoStmtClass:
246 return DirectChild == cast<DoStmt>(P)->getCond();
247 case Stmt::IfStmtClass:
248 return DirectChild == cast<IfStmt>(P)->getCond();
249 case Stmt::IndirectGotoStmtClass:
250 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
251 case Stmt::SwitchStmtClass:
252 return DirectChild == cast<SwitchStmt>(P)->getCond();
253 case Stmt::ReturnStmtClass:
254 return true;
255 }
256}
257
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000258//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000259// Driver function to invoke the Dead-Stores checker on a CFG.
260//===----------------------------------------------------------------------===//
261
262void clang::CheckDeadStores(LiveVariables& L, BugReporter& BR) {
Ted Kremenek8f269862008-07-14 20:56:04 +0000263 DeadStoreObs A(BR.getContext(), BR, BR.getParentMap());
Ted Kremenek7032f462008-07-03 05:26:14 +0000264 L.runOnAllBlocks(*BR.getCFG(), &A);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000265}