blob: 208e66f3e969d5f50c8ad5df79804e587bb6c2f0 [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;
31 Diagnostic &Diags;
Ted Kremenekd2f642b2008-04-14 17:39:48 +000032 DiagnosticClient &Client;
Ted Kremenek1a654b62008-06-20 21:45:25 +000033 ParentMap& Parents;
34
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000035public:
Ted Kremenek1a654b62008-06-20 21:45:25 +000036 DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client,
37 ParentMap& parents)
38 : Ctx(ctx), Diags(diags), Client(client), Parents(parents) {}
Ted Kremenekd2f642b2008-04-14 17:39:48 +000039
Ted Kremenekfdd225e2007-09-25 04:31:27 +000040 virtual ~DeadStoreObs() {}
41
Ted Kremenek1a654b62008-06-20 21:45:25 +000042 unsigned GetDiag(VarDecl* VD, bool inEnclosing = false) {
43 std::string name(VD->getName());
Ted Kremenekc07ba352008-05-22 16:28:24 +000044
Ted Kremenek1a654b62008-06-20 21:45:25 +000045 std::string msg = inEnclosing
46 ? "Although the value stored to '" + name +
47 "' is used in the enclosing expression, the value is never actually read"
48 " from '" + name + "'"
49 : "Value stored to '" + name + "' is never read";
50
51 return Diags.getCustomDiagID(Diagnostic::Warning, msg.c_str());
52 }
53
54 void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
55 bool hasEnclosing,
56 const LiveVariables::AnalysisDataTy& AD,
57 const LiveVariables::ValTy& Live) {
58
59 if (VD->hasLocalStorage() && !Live(VD, AD)) {
60 SourceRange R = Val->getSourceRange();
61 Diags.Report(&Client,
62 Ctx.getFullLoc(Ex->getSourceRange().getBegin()),
63 GetDiag(VD, hasEnclosing), 0, 0, &R, 1);
64 }
Ted Kremenek3eb817e2008-05-21 22:59:16 +000065 }
66
Ted Kremeneka23157e2008-05-05 23:12:21 +000067 void CheckDeclRef(DeclRefExpr* DR, Expr* Val,
68 const LiveVariables::AnalysisDataTy& AD,
69 const LiveVariables::ValTy& Live) {
70
71 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek1a654b62008-06-20 21:45:25 +000072 CheckVarDecl(VD, DR, Val, false, AD, Live);
Ted Kremeneka23157e2008-05-05 23:12:21 +000073 }
74
Ted Kremenekfdd225e2007-09-25 04:31:27 +000075 virtual void ObserveStmt(Stmt* S,
76 const LiveVariables::AnalysisDataTy& AD,
77 const LiveVariables::ValTy& Live) {
Ted Kremenekce1cab92007-09-11 17:24:14 +000078
Ted Kremenek1c86b152008-04-14 18:28:25 +000079 // Skip statements in macros.
80 if (S->getLocStart().isMacroID())
81 return;
82
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000083 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000084 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000085
Ted Kremenekc0576ca2007-09-10 17:36:42 +000086 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +000087 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
88
89 // Special case: check for assigning null to a pointer. This
90 // is a common form of defensive programming.
91 // FIXME: Make this optional?
92
93 Expr* Val = B->getRHS();
94 llvm::APSInt Result(Ctx.getTypeSize(Val->getType()));
95
96 if (VD->getType()->isPointerType() &&
97 Val->IgnoreParenCasts()->isIntegerConstantExpr(Result, Ctx, 0))
98 if (Result == 0)
99 return;
100
101 CheckVarDecl(VD, DR, Val, Parents.isSubExpr(B), AD, Live);
102 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000103 }
Ted Kremeneka23157e2008-05-05 23:12:21 +0000104 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
105 if (!U->isIncrementOp())
106 return;
107
108 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
109
110 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
111 CheckDeclRef(DR, U, AD, Live);
112 }
113 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000114 // Iterate through the decls. Warn if any initializers are complex
115 // expressions that are not live (never used).
Ted Kremenekc967c9d2008-04-14 17:52:13 +0000116 for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) {
117
118 VarDecl* V = dyn_cast<VarDecl>(SD);
119 if (!V) continue;
120
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000121 if (V->hasLocalStorage())
122 if (Expr* E = V->getInit()) {
Ted Kremenek9d7af512008-04-15 04:11:48 +0000123 if (!Live(V, AD)) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000124 // Special case: check for initializations with constants.
125 //
126 // e.g. : int x = 0;
127 //
128 // If x is EVER assigned a new value later, don't issue
129 // a warning. This is because such initialization can be
130 // due to defensive programming.
131 if (!E->isConstantExpr(Ctx,NULL)) {
132 // Flag a warning.
133 SourceRange R = E->getSourceRange();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000134 Diags.Report(&Client,
135 Ctx.getFullLoc(V->getLocation()),
Ted Kremenek3eb817e2008-05-21 22:59:16 +0000136 GetDiag(V), 0, 0, &R, 1);
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000137 }
Ted Kremenekce1cab92007-09-11 17:24:14 +0000138 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000139 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000140 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000141 }
142};
143
144} // end anonymous namespace
145
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000146//===----------------------------------------------------------------------===//
147// Driver function to invoke the Dead-Stores checker on a CFG.
148//===----------------------------------------------------------------------===//
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000149
Ted Kremenek1a654b62008-06-20 21:45:25 +0000150void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx,
151 ParentMap& Parents, Diagnostic &Diags) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000152 LiveVariables L(cfg);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000153 L.runOnCFG(cfg);
Ted Kremenek1a654b62008-06-20 21:45:25 +0000154 DeadStoreObs A(Ctx, Diags, Diags.getClient(), Parents);
Ted Kremenek7cb15932008-03-13 16:55:07 +0000155 L.runOnAllBlocks(cfg, &A);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000156}
157
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000158//===----------------------------------------------------------------------===//
159// BugReporter-based invocation of the Dead-Stores checker.
160//===----------------------------------------------------------------------===//
161
162namespace {
163
164class VISIBILITY_HIDDEN DiagBugReport : public RangedBugReport {
165 std::list<std::string> Strs;
166 FullSourceLoc L;
Ted Kremenekbd527ef2008-06-20 23:13:39 +0000167 const char* description;
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000168public:
Ted Kremenekbd527ef2008-06-20 23:13:39 +0000169 DiagBugReport(const char* desc, BugType& D, FullSourceLoc l) :
170 RangedBugReport(D, NULL), L(l), description(desc) {}
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000171
172 virtual ~DiagBugReport() {}
173 virtual FullSourceLoc getLocation(SourceManager&) { return L; }
174
Ted Kremenekbd527ef2008-06-20 23:13:39 +0000175 virtual const char* getDescription() const {
176 return description;
177 }
178
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000179 void addString(const std::string& s) { Strs.push_back(s); }
180
181 typedef std::list<std::string>::const_iterator str_iterator;
182 str_iterator str_begin() const { return Strs.begin(); }
183 str_iterator str_end() const { return Strs.end(); }
184};
185
186class VISIBILITY_HIDDEN DiagCollector : public DiagnosticClient {
187 std::list<DiagBugReport> Reports;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000188 BugType& D;
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000189public:
190 DiagCollector(BugType& d) : D(d) {}
191
192 virtual ~DiagCollector() {}
193
194 virtual void HandleDiagnostic(Diagnostic &Diags,
195 Diagnostic::Level DiagLevel,
196 FullSourceLoc Pos,
197 diag::kind ID,
198 const std::string *Strs,
199 unsigned NumStrs,
200 const SourceRange *Ranges,
201 unsigned NumRanges) {
202
203 // FIXME: Use a map from diag::kind to BugType, instead of having just
204 // one BugType.
205
Ted Kremenekbd527ef2008-06-20 23:13:39 +0000206 Reports.push_back(DiagBugReport(Diags.getDescription(ID), D, Pos));
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000207 DiagBugReport& R = Reports.back();
208
209 for ( ; NumRanges ; --NumRanges, ++Ranges)
210 R.addRange(*Ranges);
211
212 for ( ; NumStrs ; --NumStrs, ++Strs)
213 R.addString(*Strs);
214 }
215
216 // Iterators.
217
218 typedef std::list<DiagBugReport>::iterator iterator;
219 iterator begin() { return Reports.begin(); }
220 iterator end() { return Reports.end(); }
221};
222
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000223class VISIBILITY_HIDDEN DeadStoresChecker : public BugTypeCacheLocation {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000224public:
225 virtual const char* getName() const {
226 return "dead store";
227 }
228
229 virtual const char* getDescription() const {
Ted Kremenek1d5d1da2008-04-15 05:31:00 +0000230 return "Value stored to variable is never subsequently read.";
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000231 }
232
233 virtual void EmitWarnings(BugReporter& BR) {
234
235 // Run the dead store checker and collect the diagnostics.
236 DiagCollector C(*this);
Ted Kremenek1a654b62008-06-20 21:45:25 +0000237 DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C, BR.getParentMap());
238
239 GRExprEngine& Eng = BR.getEngine();
240 Eng.getLiveness().runOnAllBlocks(BR.getCFG(), &A);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000241
242 // Emit the bug reports.
243
244 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
245 BR.EmitWarning(*I);
246 }
247};
248} // end anonymous namespace
249
250BugType* clang::MakeDeadStoresChecker() {
251 return new DeadStoresChecker();
252}