blob: f7523e508f08ddb3cd3243e82bad6adfc9e61e87 [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 Kremenekc2b51d82008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000023
24using namespace clang;
25
26namespace {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000027
Ted Kremenekc2b51d82008-01-08 18:19:08 +000028class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattnerc0508f92007-09-15 23:21:08 +000029 ASTContext &Ctx;
30 Diagnostic &Diags;
Ted Kremenekd2f642b2008-04-14 17:39:48 +000031 DiagnosticClient &Client;
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000032public:
Ted Kremenekd2f642b2008-04-14 17:39:48 +000033 DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client)
34 : Ctx(ctx), Diags(diags), Client(client) {}
35
Ted Kremenekfdd225e2007-09-25 04:31:27 +000036 virtual ~DeadStoreObs() {}
37
Ted Kremeneka23157e2008-05-05 23:12:21 +000038 void CheckDeclRef(DeclRefExpr* DR, Expr* Val,
39 const LiveVariables::AnalysisDataTy& AD,
40 const LiveVariables::ValTy& Live) {
41
42 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
43 if (VD->hasLocalStorage() && !Live(VD, AD)) {
44 SourceRange R = Val->getSourceRange();
45 Diags.Report(&Client,
46 Ctx.getFullLoc(DR->getSourceRange().getBegin()),
47 diag::warn_dead_store, 0, 0, &R, 1);
48 }
49 }
50
Ted Kremenekfdd225e2007-09-25 04:31:27 +000051 virtual void ObserveStmt(Stmt* S,
52 const LiveVariables::AnalysisDataTy& AD,
53 const LiveVariables::ValTy& Live) {
Ted Kremenekce1cab92007-09-11 17:24:14 +000054
Ted Kremenek1c86b152008-04-14 18:28:25 +000055 // Skip statements in macros.
56 if (S->getLocStart().isMacroID())
57 return;
58
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000059 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000060 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000061
Ted Kremenekc0576ca2007-09-10 17:36:42 +000062 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremeneka23157e2008-05-05 23:12:21 +000063 CheckDeclRef(DR, B->getRHS(), AD, Live);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000064 }
Ted Kremeneka23157e2008-05-05 23:12:21 +000065 else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
66 if (!U->isIncrementOp())
67 return;
68
69 Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
70
71 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
72 CheckDeclRef(DR, U, AD, Live);
73 }
74 else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +000075 // Iterate through the decls. Warn if any initializers are complex
76 // expressions that are not live (never used).
Ted Kremenekc967c9d2008-04-14 17:52:13 +000077 for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) {
78
79 VarDecl* V = dyn_cast<VarDecl>(SD);
80 if (!V) continue;
81
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000082 if (V->hasLocalStorage())
83 if (Expr* E = V->getInit()) {
Ted Kremenek9d7af512008-04-15 04:11:48 +000084 if (!Live(V, AD)) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000085 // Special case: check for initializations with constants.
86 //
87 // e.g. : int x = 0;
88 //
89 // If x is EVER assigned a new value later, don't issue
90 // a warning. This is because such initialization can be
91 // due to defensive programming.
92 if (!E->isConstantExpr(Ctx,NULL)) {
93 // Flag a warning.
94 SourceRange R = E->getSourceRange();
Ted Kremenekd2f642b2008-04-14 17:39:48 +000095 Diags.Report(&Client,
96 Ctx.getFullLoc(V->getLocation()),
Ted Kremenek9c728dc2007-12-12 22:39:36 +000097 diag::warn_dead_store, 0, 0, &R, 1);
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000098 }
Ted Kremenekce1cab92007-09-11 17:24:14 +000099 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000100 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000101 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000102 }
103};
104
105} // end anonymous namespace
106
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000107//===----------------------------------------------------------------------===//
108// Driver function to invoke the Dead-Stores checker on a CFG.
109//===----------------------------------------------------------------------===//
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000110
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000111void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000112 LiveVariables L(cfg);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000113 L.runOnCFG(cfg);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000114 DeadStoreObs A(Ctx, Diags, Diags.getClient());
Ted Kremenek7cb15932008-03-13 16:55:07 +0000115 L.runOnAllBlocks(cfg, &A);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000116}
117
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000118//===----------------------------------------------------------------------===//
119// BugReporter-based invocation of the Dead-Stores checker.
120//===----------------------------------------------------------------------===//
121
122namespace {
123
124class VISIBILITY_HIDDEN DiagBugReport : public RangedBugReport {
125 std::list<std::string> Strs;
126 FullSourceLoc L;
127public:
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000128 DiagBugReport(BugType& D, FullSourceLoc l) :
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000129 RangedBugReport(D, NULL), L(l) {}
130
131 virtual ~DiagBugReport() {}
132 virtual FullSourceLoc getLocation(SourceManager&) { return L; }
133
134 void addString(const std::string& s) { Strs.push_back(s); }
135
136 typedef std::list<std::string>::const_iterator str_iterator;
137 str_iterator str_begin() const { return Strs.begin(); }
138 str_iterator str_end() const { return Strs.end(); }
139};
140
141class VISIBILITY_HIDDEN DiagCollector : public DiagnosticClient {
142 std::list<DiagBugReport> Reports;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000143 BugType& D;
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000144public:
145 DiagCollector(BugType& d) : D(d) {}
146
147 virtual ~DiagCollector() {}
148
149 virtual void HandleDiagnostic(Diagnostic &Diags,
150 Diagnostic::Level DiagLevel,
151 FullSourceLoc Pos,
152 diag::kind ID,
153 const std::string *Strs,
154 unsigned NumStrs,
155 const SourceRange *Ranges,
156 unsigned NumRanges) {
157
158 // FIXME: Use a map from diag::kind to BugType, instead of having just
159 // one BugType.
160
161 Reports.push_back(DiagBugReport(D, Pos));
162 DiagBugReport& R = Reports.back();
163
164 for ( ; NumRanges ; --NumRanges, ++Ranges)
165 R.addRange(*Ranges);
166
167 for ( ; NumStrs ; --NumStrs, ++Strs)
168 R.addString(*Strs);
169 }
170
171 // Iterators.
172
173 typedef std::list<DiagBugReport>::iterator iterator;
174 iterator begin() { return Reports.begin(); }
175 iterator end() { return Reports.end(); }
176};
177
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000178class VISIBILITY_HIDDEN DeadStoresChecker : public BugTypeCacheLocation {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000179public:
180 virtual const char* getName() const {
181 return "dead store";
182 }
183
184 virtual const char* getDescription() const {
Ted Kremenek1d5d1da2008-04-15 05:31:00 +0000185 return "Value stored to variable is never subsequently read.";
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000186 }
187
188 virtual void EmitWarnings(BugReporter& BR) {
189
190 // Run the dead store checker and collect the diagnostics.
191 DiagCollector C(*this);
192 DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C);
193 GRExprEngine& Eng = BR.getEngine();
194 Eng.getLiveness().runOnAllBlocks(Eng.getCFG(), &A);
195
196 // Emit the bug reports.
197
198 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
199 BR.EmitWarning(*I);
200 }
201};
202} // end anonymous namespace
203
204BugType* clang::MakeDeadStoresChecker() {
205 return new DeadStoresChecker();
206}