blob: 7d912b046ebd4c54231eb698256946cacbc05706 [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;
167public:
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000168 DiagBugReport(BugType& D, FullSourceLoc l) :
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000169 RangedBugReport(D, NULL), L(l) {}
170
171 virtual ~DiagBugReport() {}
172 virtual FullSourceLoc getLocation(SourceManager&) { return L; }
173
174 void addString(const std::string& s) { Strs.push_back(s); }
175
176 typedef std::list<std::string>::const_iterator str_iterator;
177 str_iterator str_begin() const { return Strs.begin(); }
178 str_iterator str_end() const { return Strs.end(); }
179};
180
181class VISIBILITY_HIDDEN DiagCollector : public DiagnosticClient {
182 std::list<DiagBugReport> Reports;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000183 BugType& D;
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000184public:
185 DiagCollector(BugType& d) : D(d) {}
186
187 virtual ~DiagCollector() {}
188
189 virtual void HandleDiagnostic(Diagnostic &Diags,
190 Diagnostic::Level DiagLevel,
191 FullSourceLoc Pos,
192 diag::kind ID,
193 const std::string *Strs,
194 unsigned NumStrs,
195 const SourceRange *Ranges,
196 unsigned NumRanges) {
197
198 // FIXME: Use a map from diag::kind to BugType, instead of having just
199 // one BugType.
200
201 Reports.push_back(DiagBugReport(D, Pos));
202 DiagBugReport& R = Reports.back();
203
204 for ( ; NumRanges ; --NumRanges, ++Ranges)
205 R.addRange(*Ranges);
206
207 for ( ; NumStrs ; --NumStrs, ++Strs)
208 R.addString(*Strs);
209 }
210
211 // Iterators.
212
213 typedef std::list<DiagBugReport>::iterator iterator;
214 iterator begin() { return Reports.begin(); }
215 iterator end() { return Reports.end(); }
216};
217
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000218class VISIBILITY_HIDDEN DeadStoresChecker : public BugTypeCacheLocation {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000219public:
220 virtual const char* getName() const {
221 return "dead store";
222 }
223
224 virtual const char* getDescription() const {
Ted Kremenek1d5d1da2008-04-15 05:31:00 +0000225 return "Value stored to variable is never subsequently read.";
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000226 }
227
228 virtual void EmitWarnings(BugReporter& BR) {
229
230 // Run the dead store checker and collect the diagnostics.
231 DiagCollector C(*this);
Ted Kremenek1a654b62008-06-20 21:45:25 +0000232 DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C, BR.getParentMap());
233
234 GRExprEngine& Eng = BR.getEngine();
235 Eng.getLiveness().runOnAllBlocks(BR.getCFG(), &A);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000236
237 // Emit the bug reports.
238
239 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
240 BR.EmitWarning(*I);
241 }
242};
243} // end anonymous namespace
244
245BugType* clang::MakeDeadStoresChecker() {
246 return new DeadStoresChecker();
247}