Newer
Older
//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines a DeadStores, a flow-sensitive checker that looks for
// stores to variables that are no longer live.
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/LocalCheckers.h"
#include "clang/Analysis/Analyses/LiveVariables.h"
#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenek
committed
#include "clang/Analysis/PathSensitive/BugReporter.h"
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/ASTContext.h"
#include "llvm/Support/Compiler.h"
using namespace clang;
namespace {
class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
ASTContext &Ctx;
Diagnostic &Diags;
Ted Kremenek
committed
DiagnosticClient &Client;
public:
Ted Kremenek
committed
DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client)
: Ctx(ctx), Diags(diags), Client(client) {}
virtual ~DeadStoreObs() {}
virtual void ObserveStmt(Stmt* S,
const LiveVariables::AnalysisDataTy& AD,
const LiveVariables::ValTy& Live) {
if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
if (!B->isAssignmentOp()) return; // Skip non-assignments.
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
if (VD->hasLocalStorage() && !Live(VD,AD)) {
SourceRange R = B->getRHS()->getSourceRange();
Ted Kremenek
committed
Diags.Report(&Client,
Ctx.getFullLoc(DR->getSourceRange().getBegin()),
diag::warn_dead_store, 0, 0, &R, 1);
}
}
else if(DeclStmt* DS = dyn_cast<DeclStmt>(S))
// Iterate through the decls. Warn if any initializers are complex
// expressions that are not live (never used).
for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ;
V = cast_or_null<VarDecl>(V->getNextDeclarator())) {
if (V->hasLocalStorage())
if (Expr* E = V->getInit()) {
if (!Live(DS->getDecl(),AD)) {
// Special case: check for initializations with constants.
//
// e.g. : int x = 0;
//
// If x is EVER assigned a new value later, don't issue
// a warning. This is because such initialization can be
// due to defensive programming.
if (!E->isConstantExpr(Ctx,NULL)) {
// Flag a warning.
SourceRange R = E->getSourceRange();
Ted Kremenek
committed
Diags.Report(&Client,
Ctx.getFullLoc(V->getLocation()),
diag::warn_dead_store, 0, 0, &R, 1);
}
}
}
}
};
} // end anonymous namespace
Ted Kremenek
committed
//===----------------------------------------------------------------------===//
// Driver function to invoke the Dead-Stores checker on a CFG.
//===----------------------------------------------------------------------===//
Ted Kremenek
committed
void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek
committed
LiveVariables L(cfg);
L.runOnCFG(cfg);
Ted Kremenek
committed
DeadStoreObs A(Ctx, Diags, Diags.getClient());
Ted Kremenek
committed
L.runOnAllBlocks(cfg, &A);
}
Ted Kremenek
committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//===----------------------------------------------------------------------===//
// BugReporter-based invocation of the Dead-Stores checker.
//===----------------------------------------------------------------------===//
namespace {
class VISIBILITY_HIDDEN DiagBugReport : public RangedBugReport {
std::list<std::string> Strs;
FullSourceLoc L;
public:
DiagBugReport(const BugType& D, FullSourceLoc l) :
RangedBugReport(D, NULL), L(l) {}
virtual ~DiagBugReport() {}
virtual FullSourceLoc getLocation(SourceManager&) { return L; }
void addString(const std::string& s) { Strs.push_back(s); }
typedef std::list<std::string>::const_iterator str_iterator;
str_iterator str_begin() const { return Strs.begin(); }
str_iterator str_end() const { return Strs.end(); }
};
class VISIBILITY_HIDDEN DiagCollector : public DiagnosticClient {
std::list<DiagBugReport> Reports;
const BugType& D;
public:
DiagCollector(BugType& d) : D(d) {}
virtual ~DiagCollector() {}
virtual void HandleDiagnostic(Diagnostic &Diags,
Diagnostic::Level DiagLevel,
FullSourceLoc Pos,
diag::kind ID,
const std::string *Strs,
unsigned NumStrs,
const SourceRange *Ranges,
unsigned NumRanges) {
// FIXME: Use a map from diag::kind to BugType, instead of having just
// one BugType.
Reports.push_back(DiagBugReport(D, Pos));
DiagBugReport& R = Reports.back();
for ( ; NumRanges ; --NumRanges, ++Ranges)
R.addRange(*Ranges);
for ( ; NumStrs ; --NumStrs, ++Strs)
R.addString(*Strs);
}
// Iterators.
typedef std::list<DiagBugReport>::iterator iterator;
iterator begin() { return Reports.begin(); }
iterator end() { return Reports.end(); }
};
class VISIBILITY_HIDDEN DeadStoresChecker : public BugType {
public:
virtual const char* getName() const {
return "dead store";
}
virtual const char* getDescription() const {
return "Value stored to variable is never used.";
}
virtual void EmitWarnings(BugReporter& BR) {
// Run the dead store checker and collect the diagnostics.
DiagCollector C(*this);
DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C);
GRExprEngine& Eng = BR.getEngine();
Eng.getLiveness().runOnAllBlocks(Eng.getCFG(), &A);
// Emit the bug reports.
for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
BR.EmitWarning(*I);
}
};
} // end anonymous namespace
BugType* clang::MakeDeadStoresChecker() {
return new DeadStoresChecker();
}