2007-09-06 23:01:46 +00:00
|
|
|
//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:25 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-06 23:01:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 10:40:09 +00:00
|
|
|
// This file defines a DeadStores, a flow-sensitive checker that looks for
|
2007-09-06 23:01:46 +00:00
|
|
|
// stores to variables that are no longer live.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2007-12-21 21:42:19 +00:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2007-09-25 04:31:27 +00:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
|
2008-04-14 17:39:48 +00:00
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2007-09-06 23:01:46 +00:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-09-11 17:24:14 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-06-20 21:45:25 +00:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2008-01-08 18:19:08 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-09-06 23:01:46 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2008-06-20 21:45:25 +00:00
|
|
|
|
2008-01-08 18:19:08 +00:00
|
|
|
class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
|
2007-09-15 23:21:08 +00:00
|
|
|
ASTContext &Ctx;
|
|
|
|
Diagnostic &Diags;
|
2008-04-14 17:39:48 +00:00
|
|
|
DiagnosticClient &Client;
|
2008-06-20 21:45:25 +00:00
|
|
|
ParentMap& Parents;
|
|
|
|
|
2007-09-06 23:01:46 +00:00
|
|
|
public:
|
2008-06-20 21:45:25 +00:00
|
|
|
DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client,
|
|
|
|
ParentMap& parents)
|
|
|
|
: Ctx(ctx), Diags(diags), Client(client), Parents(parents) {}
|
2008-04-14 17:39:48 +00:00
|
|
|
|
2007-09-25 04:31:27 +00:00
|
|
|
virtual ~DeadStoreObs() {}
|
|
|
|
|
2008-06-20 21:45:25 +00:00
|
|
|
unsigned GetDiag(VarDecl* VD, bool inEnclosing = false) {
|
|
|
|
std::string name(VD->getName());
|
|
|
|
|
|
|
|
std::string msg = inEnclosing
|
|
|
|
? "Although the value stored to '" + name +
|
|
|
|
"' is used in the enclosing expression, the value is never actually read"
|
|
|
|
" from '" + name + "'"
|
|
|
|
: "Value stored to '" + name + "' is never read";
|
2008-05-22 16:28:24 +00:00
|
|
|
|
2008-06-20 21:45:25 +00:00
|
|
|
return Diags.getCustomDiagID(Diagnostic::Warning, msg.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
|
|
|
|
bool hasEnclosing,
|
|
|
|
const LiveVariables::AnalysisDataTy& AD,
|
|
|
|
const LiveVariables::ValTy& Live) {
|
|
|
|
|
|
|
|
if (VD->hasLocalStorage() && !Live(VD, AD)) {
|
|
|
|
SourceRange R = Val->getSourceRange();
|
|
|
|
Diags.Report(&Client,
|
|
|
|
Ctx.getFullLoc(Ex->getSourceRange().getBegin()),
|
|
|
|
GetDiag(VD, hasEnclosing), 0, 0, &R, 1);
|
|
|
|
}
|
2008-05-21 22:59:16 +00:00
|
|
|
}
|
|
|
|
|
2008-05-05 23:12:21 +00:00
|
|
|
void CheckDeclRef(DeclRefExpr* DR, Expr* Val,
|
|
|
|
const LiveVariables::AnalysisDataTy& AD,
|
|
|
|
const LiveVariables::ValTy& Live) {
|
|
|
|
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
|
2008-06-20 21:45:25 +00:00
|
|
|
CheckVarDecl(VD, DR, Val, false, AD, Live);
|
2008-05-05 23:12:21 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 04:31:27 +00:00
|
|
|
virtual void ObserveStmt(Stmt* S,
|
|
|
|
const LiveVariables::AnalysisDataTy& AD,
|
|
|
|
const LiveVariables::ValTy& Live) {
|
2007-09-11 17:24:14 +00:00
|
|
|
|
2008-04-14 18:28:25 +00:00
|
|
|
// Skip statements in macros.
|
|
|
|
if (S->getLocStart().isMacroID())
|
|
|
|
return;
|
|
|
|
|
2007-09-06 23:01:46 +00:00
|
|
|
if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
|
2007-09-25 04:31:27 +00:00
|
|
|
if (!B->isAssignmentOp()) return; // Skip non-assignments.
|
2007-09-06 23:01:46 +00:00
|
|
|
|
2007-09-10 17:36:42 +00:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
|
2008-06-20 21:45:25 +00:00
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
|
|
|
|
|
|
|
// Special case: check for assigning null to a pointer. This
|
|
|
|
// is a common form of defensive programming.
|
|
|
|
// FIXME: Make this optional?
|
|
|
|
|
|
|
|
Expr* Val = B->getRHS();
|
|
|
|
llvm::APSInt Result(Ctx.getTypeSize(Val->getType()));
|
|
|
|
|
|
|
|
if (VD->getType()->isPointerType() &&
|
|
|
|
Val->IgnoreParenCasts()->isIntegerConstantExpr(Result, Ctx, 0))
|
|
|
|
if (Result == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckVarDecl(VD, DR, Val, Parents.isSubExpr(B), AD, Live);
|
|
|
|
}
|
2007-09-06 23:01:46 +00:00
|
|
|
}
|
2008-05-05 23:12:21 +00:00
|
|
|
else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
|
|
|
|
if (!U->isIncrementOp())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
|
|
|
|
|
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
|
|
|
|
CheckDeclRef(DR, U, AD, Live);
|
|
|
|
}
|
|
|
|
else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
|
2007-09-25 04:31:27 +00:00
|
|
|
// Iterate through the decls. Warn if any initializers are complex
|
|
|
|
// expressions that are not live (never used).
|
2008-04-14 17:52:13 +00:00
|
|
|
for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) {
|
|
|
|
|
|
|
|
VarDecl* V = dyn_cast<VarDecl>(SD);
|
|
|
|
if (!V) continue;
|
|
|
|
|
2007-09-28 20:48:41 +00:00
|
|
|
if (V->hasLocalStorage())
|
|
|
|
if (Expr* E = V->getInit()) {
|
2008-04-15 04:11:48 +00:00
|
|
|
if (!Live(V, AD)) {
|
2007-09-28 20:48:41 +00:00
|
|
|
// 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();
|
2008-04-14 17:39:48 +00:00
|
|
|
Diags.Report(&Client,
|
|
|
|
Ctx.getFullLoc(V->getLocation()),
|
2008-05-21 22:59:16 +00:00
|
|
|
GetDiag(V), 0, 0, &R, 1);
|
2007-09-28 20:48:41 +00:00
|
|
|
}
|
2007-09-11 17:24:14 +00:00
|
|
|
}
|
2007-09-25 04:31:27 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-06 23:01:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-04-14 17:39:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Driver function to invoke the Dead-Stores checker on a CFG.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 23:01:46 +00:00
|
|
|
|
2008-06-20 21:45:25 +00:00
|
|
|
void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx,
|
|
|
|
ParentMap& Parents, Diagnostic &Diags) {
|
2008-03-13 16:55:07 +00:00
|
|
|
LiveVariables L(cfg);
|
2007-09-06 23:01:46 +00:00
|
|
|
L.runOnCFG(cfg);
|
2008-07-02 18:39:20 +00:00
|
|
|
CheckDeadStores(cfg, Ctx, L, Parents, Diags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx, LiveVariables& L,
|
|
|
|
ParentMap& Parents, Diagnostic &Diags) {
|
|
|
|
|
2008-06-20 21:45:25 +00:00
|
|
|
DeadStoreObs A(Ctx, Diags, Diags.getClient(), Parents);
|
2008-03-13 16:55:07 +00:00
|
|
|
L.runOnAllBlocks(cfg, &A);
|
2007-09-06 23:01:46 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 17:39:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BugReporter-based invocation of the Dead-Stores checker.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN DiagBugReport : public RangedBugReport {
|
|
|
|
std::list<std::string> Strs;
|
|
|
|
FullSourceLoc L;
|
2008-06-20 23:13:39 +00:00
|
|
|
const char* description;
|
2008-04-14 17:39:48 +00:00
|
|
|
public:
|
2008-06-20 23:13:39 +00:00
|
|
|
DiagBugReport(const char* desc, BugType& D, FullSourceLoc l) :
|
|
|
|
RangedBugReport(D, NULL), L(l), description(desc) {}
|
2008-04-14 17:39:48 +00:00
|
|
|
|
|
|
|
virtual ~DiagBugReport() {}
|
|
|
|
virtual FullSourceLoc getLocation(SourceManager&) { return L; }
|
|
|
|
|
2008-06-20 23:13:39 +00:00
|
|
|
virtual const char* getDescription() const {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2008-04-14 17:39:48 +00:00
|
|
|
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;
|
2008-04-18 20:54:29 +00:00
|
|
|
BugType& D;
|
2008-04-14 17:39:48 +00:00
|
|
|
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.
|
|
|
|
|
2008-06-20 23:13:39 +00:00
|
|
|
Reports.push_back(DiagBugReport(Diags.getDescription(ID), D, Pos));
|
2008-04-14 17:39:48 +00:00
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
|
2008-04-18 20:54:29 +00:00
|
|
|
class VISIBILITY_HIDDEN DeadStoresChecker : public BugTypeCacheLocation {
|
2008-04-14 17:39:48 +00:00
|
|
|
public:
|
|
|
|
virtual const char* getName() const {
|
|
|
|
return "dead store";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* getDescription() const {
|
2008-04-15 05:31:00 +00:00
|
|
|
return "Value stored to variable is never subsequently read.";
|
2008-04-14 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR) {
|
|
|
|
|
|
|
|
// Run the dead store checker and collect the diagnostics.
|
|
|
|
DiagCollector C(*this);
|
2008-06-20 21:45:25 +00:00
|
|
|
DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C, BR.getParentMap());
|
|
|
|
|
2008-07-02 21:24:01 +00:00
|
|
|
|
|
|
|
BR.getLiveVariables().runOnAllBlocks(BR.getCFG(), &A);
|
2008-04-14 17:39:48 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|