2007-09-07 03:01:46 +04:00
|
|
|
//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Ted Kremenek and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This files defines a DeadStores, a flow-sensitive checker that looks for
|
|
|
|
// stores to variables that are no longer live.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
|
|
|
#include "clang/Analysis/LiveVariables.h"
|
|
|
|
#include "clang/AST/CFG.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-09-10 19:56:38 +04:00
|
|
|
class DeadStoreObserver : public LiveVariablesObserver {
|
2007-09-07 03:01:46 +04:00
|
|
|
Preprocessor& PP;
|
|
|
|
public:
|
2007-09-10 19:56:38 +04:00
|
|
|
DeadStoreObserver(Preprocessor& pp) : PP(pp) {}
|
|
|
|
virtual ~DeadStoreObserver() {}
|
2007-09-07 03:01:46 +04:00
|
|
|
|
2007-09-10 19:56:38 +04:00
|
|
|
virtual void ObserveStmt(Stmt* S, LiveVariables& L, llvm::BitVector& Live) {
|
2007-09-07 03:01:46 +04:00
|
|
|
if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
|
|
|
|
// Is this an assignment?
|
|
|
|
if (!B->isAssignmentOp())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Is this an assignment to a variable?
|
2007-09-10 21:36:42 +04:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
|
2007-09-07 03:01:46 +04:00
|
|
|
// Is the variable live?
|
2007-09-10 21:36:42 +04:00
|
|
|
if (!L.isLive(Live,cast<VarDecl>(DR->getDecl()))) {
|
2007-09-07 03:01:46 +04:00
|
|
|
SourceRange R = B->getRHS()->getSourceRange();
|
|
|
|
PP.getDiagnostics().Report(DR->getSourceRange().Begin(),
|
|
|
|
diag::warn_dead_store, 0, 0,
|
|
|
|
&R,1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2007-09-07 03:39:53 +04:00
|
|
|
else if(DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
|
|
|
|
// Iterate through the decls. Warn if any of them (which have
|
|
|
|
// initializers) are not live.
|
2007-09-10 21:36:42 +04:00
|
|
|
for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ;
|
|
|
|
V = cast<VarDecl>(V->getNextDeclarator()))
|
|
|
|
if (Expr* E = V->getInit())
|
|
|
|
if (!L.isLive(Live,V)) {
|
|
|
|
SourceRange R = E->getSourceRange();
|
|
|
|
PP.getDiagnostics().Report(V->getLocation(),
|
|
|
|
diag::warn_dead_store, 0, 0,
|
|
|
|
&R,1);
|
|
|
|
}
|
2007-09-07 03:39:53 +04:00
|
|
|
}
|
2007-09-07 03:01:46 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
void CheckDeadStores(CFG& cfg, LiveVariables& L, Preprocessor& PP) {
|
2007-09-10 19:56:38 +04:00
|
|
|
DeadStoreObserver A(PP);
|
2007-09-07 03:01:46 +04:00
|
|
|
|
|
|
|
for (CFG::iterator I = cfg.begin(), E = cfg.end(); I != E; ++I)
|
|
|
|
L.runOnBlock(&(*I),&A);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckDeadStores(CFG& cfg, Preprocessor& PP) {
|
|
|
|
LiveVariables L;
|
|
|
|
L.runOnCFG(cfg);
|
|
|
|
CheckDeadStores(cfg,L,PP);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace clang
|