blob: 73cb3f786b2c134db5378b3bbb9b62daf5786905 [file] [log] [blame]
Ted Kremeneke9646a02007-09-06 23:01:46 +00001//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Kremeneke9646a02007-09-06 23:01:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif2224fcb2008-03-06 10:40:09 +000010// This file defines a DeadStores, a flow-sensitive checker that looks for
Ted Kremeneke9646a02007-09-06 23:01:46 +000011// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremeneke9646a02007-09-06 23:01:46 +000015#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekcdf8e842007-12-21 21:42:19 +000016#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd7a2f812007-09-25 04:31:27 +000017#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremeneke3ef1c72008-04-14 17:39:48 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
19#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke9646a02007-09-06 23:01:46 +000020#include "clang/Basic/Diagnostic.h"
Ted Kremenek12789292007-09-11 17:24:14 +000021#include "clang/AST/ASTContext.h"
Ted Kremenek72f52c02008-06-20 21:45:25 +000022#include "clang/AST/ParentMap.h"
Ted Kremenekec818352008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Ted Kremeneke9646a02007-09-06 23:01:46 +000024
25using namespace clang;
26
27namespace {
Ted Kremenek72f52c02008-06-20 21:45:25 +000028
Ted Kremenekec818352008-01-08 18:19:08 +000029class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattner52332d02007-09-15 23:21:08 +000030 ASTContext &Ctx;
Ted Kremenek4bd03ef2008-07-14 20:56:04 +000031 BugReporter& BR;
Ted Kremenek72f52c02008-06-20 21:45:25 +000032 ParentMap& Parents;
Ted Kremenek8d481642008-07-23 21:16:38 +000033
34 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Ted Kremenek72f52c02008-06-20 21:45:25 +000035
Ted Kremeneke9646a02007-09-06 23:01:46 +000036public:
Ted Kremenek4bd03ef2008-07-14 20:56:04 +000037 DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents)
38 : Ctx(ctx), BR(br), Parents(parents) {}
Ted Kremeneke3ef1c72008-04-14 17:39:48 +000039
Ted Kremenekd7a2f812007-09-25 04:31:27 +000040 virtual ~DeadStoreObs() {}
41
Ted Kremenek8d481642008-07-23 21:16:38 +000042 void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) {
Ted Kremenek4bd03ef2008-07-14 20:56:04 +000043
Ted Kremenek63600042008-07-15 18:06:32 +000044 std::string name(V->getName());
Ted Kremenek72f52c02008-06-20 21:45:25 +000045
Ted Kremenek8d481642008-07-23 21:16:38 +000046 const char* BugType = 0;
47 std::string msg;
Ted Kremenek63600042008-07-15 18:06:32 +000048
Ted Kremenek8d481642008-07-23 21:16:38 +000049 switch (dsk) {
50 default:
51 assert(false && "Impossible dead store type.");
52
53 case DeadInit:
54 BugType = "dead initialization";
55 msg = "Value stored to '" + name +
56 "' during its initialization is never read";
57 break;
58
59 case DeadIncrement:
Ted Kremenek5e3cce52008-08-02 18:19:48 +000060 BugType = "dead increment";
Ted Kremenek8d481642008-07-23 21:16:38 +000061 case Standard:
62 if (!BugType) BugType = "dead store";
63 msg = "Value stored to '" + name + "' is never read";
64 break;
65
66 case Enclosing:
67 BugType = "dead store";
68 msg = "Although the value stored to '" + name +
69 "' is used in the enclosing expression, the value is never actually"
70 " read from '" + name + "'";
71 break;
Ted Kremenek63600042008-07-15 18:06:32 +000072 }
Ted Kremenek8d481642008-07-23 21:16:38 +000073
74 BR.EmitBasicReport(BugType, msg.c_str(), L, R);
Ted Kremenek72f52c02008-06-20 21:45:25 +000075 }
76
77 void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
Ted Kremenek8d481642008-07-23 21:16:38 +000078 DeadStoreKind dsk,
Ted Kremenek72f52c02008-06-20 21:45:25 +000079 const LiveVariables::AnalysisDataTy& AD,
80 const LiveVariables::ValTy& Live) {
81
Ted Kremenek14dbe322008-08-07 22:28:30 +000082 if (VD->hasLocalStorage() && !Live(VD, AD) && !VD->getAttr<UnusedAttr>())
Ted Kremenek8d481642008-07-23 21:16:38 +000083 Report(VD, dsk, Ex->getSourceRange().getBegin(),
Ted Kremenek4bd03ef2008-07-14 20:56:04 +000084 Val->getSourceRange());
Ted Kremenek40c8b9e2008-05-21 22:59:16 +000085 }
86
Ted Kremenek8d481642008-07-23 21:16:38 +000087 void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk,
Ted Kremenek401c3442008-05-05 23:12:21 +000088 const LiveVariables::AnalysisDataTy& AD,
89 const LiveVariables::ValTy& Live) {
90
91 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek8d481642008-07-23 21:16:38 +000092 CheckVarDecl(VD, DR, Val, dsk, AD, Live);
93 }
94
95 bool isIncrement(VarDecl* VD, BinaryOperator* B) {
96 if (B->isCompoundAssignmentOp())
97 return true;
98
99 Expr* RHS = B->getRHS()->IgnoreParenCasts();
100 BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
101
102 if (!BRHS)
103 return false;
104
105 DeclRefExpr *DR;
106
107 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
108 if (DR->getDecl() == VD)
109 return true;
110
111 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
112 if (DR->getDecl() == VD)
113 return true;
114
115 return false;
Ted Kremenek401c3442008-05-05 23:12:21 +0000116 }
117
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000118 virtual void ObserveStmt(Stmt* S,
119 const LiveVariables::AnalysisDataTy& AD,
120 const LiveVariables::ValTy& Live) {
Ted Kremenek12789292007-09-11 17:24:14 +0000121
Ted Kremenek0389e1d2008-04-14 18:28:25 +0000122 // Skip statements in macros.
123 if (S->getLocStart().isMacroID())
124 return;
125
Ted Kremeneke9646a02007-09-06 23:01:46 +0000126 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000127 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremeneke9646a02007-09-06 23:01:46 +0000128
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000129 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek72f52c02008-06-20 21:45:25 +0000130 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenekdfebae12008-08-09 00:05:14 +0000131 // Special case: check for assigning null to a pointer.
132 // This is a common form of defensive programming.
133 if (VD->getType()->isPointerType()) {
134 if (IntegerLiteral* L =
135 dyn_cast<IntegerLiteral>(B->getRHS()->IgnoreParenCasts()))
Ted Kremenekb515d132008-08-09 00:41:45 +0000136 // FIXME: Probably should have an Expr::isNullPointerConstant.
Ted Kremenekdfebae12008-08-09 00:05:14 +0000137 if (L->getValue() == 0)
138 return;
139 }
Ted Kremenek72f52c02008-06-20 21:45:25 +0000140
Ted Kremenek8d481642008-07-23 21:16:38 +0000141 DeadStoreKind dsk =
142 Parents.isSubExpr(B)
143 ? Enclosing
144 : (isIncrement(VD,B) ? DeadIncrement : Standard);
145
Ted Kremenekdfebae12008-08-09 00:05:14 +0000146 CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live);
Ted Kremenek72f52c02008-06-20 21:45:25 +0000147 }
Ted Kremeneke9646a02007-09-06 23:01:46 +0000148 }
Ted Kremenek401c3442008-05-05 23:12:21 +0000149 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
150 if (!U->isIncrementOp())
151 return;
Ted Kremenekd1a6c672008-07-24 17:01:17 +0000152
Ted Kremenek401c3442008-05-05 23:12:21 +0000153 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
154
155 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek8d481642008-07-23 21:16:38 +0000156 CheckDeclRef(DR, U, DeadIncrement, AD, Live);
Ted Kremenek401c3442008-05-05 23:12:21 +0000157 }
158 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000159 // Iterate through the decls. Warn if any initializers are complex
160 // expressions that are not live (never used).
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000161 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
162 DI != DE; ++DI) {
Ted Kremenek92770f22008-04-14 17:52:13 +0000163
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000164 VarDecl* V = dyn_cast<VarDecl>(*DI);
Ted Kremenek542adb12008-07-25 04:47:34 +0000165
166 if (!V)
167 continue;
Ted Kremenek92770f22008-04-14 17:52:13 +0000168
Ted Kremenek4f200542007-09-28 20:48:41 +0000169 if (V->hasLocalStorage())
Ted Kremenek542adb12008-07-25 04:47:34 +0000170 if (Expr* E = V->getInit()) {
171 // A dead initialization is a variable that is dead after it
172 // is initialized. We don't flag warnings for those variables
173 // marked 'unused'.
174 if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenek4f200542007-09-28 20:48:41 +0000175 // Special case: check for initializations with constants.
176 //
177 // e.g. : int x = 0;
178 //
179 // If x is EVER assigned a new value later, don't issue
180 // a warning. This is because such initialization can be
181 // due to defensive programming.
Ted Kremenek4bd03ef2008-07-14 20:56:04 +0000182 if (!E->isConstantExpr(Ctx,NULL))
Ted Kremenek8d481642008-07-23 21:16:38 +0000183 Report(V, DeadInit, V->getLocation(), E->getSourceRange());
Ted Kremenek12789292007-09-11 17:24:14 +0000184 }
Ted Kremenek542adb12008-07-25 04:47:34 +0000185 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000186 }
Ted Kremeneke9646a02007-09-06 23:01:46 +0000187 }
188};
189
190} // end anonymous namespace
191
Ted Kremeneke3ef1c72008-04-14 17:39:48 +0000192//===----------------------------------------------------------------------===//
Ted Kremenekeb9627b2008-07-02 23:16:33 +0000193// Driver function to invoke the Dead-Stores checker on a CFG.
194//===----------------------------------------------------------------------===//
195
196void clang::CheckDeadStores(LiveVariables& L, BugReporter& BR) {
Ted Kremenek4bd03ef2008-07-14 20:56:04 +0000197 DeadStoreObs A(BR.getContext(), BR, BR.getParentMap());
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000198 L.runOnAllBlocks(*BR.getCFG(), &A);
Ted Kremeneke3ef1c72008-04-14 17:39:48 +0000199}