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/Analysis/LocalCheckers.h"
|
|
|
|
#include "clang/Analysis/LiveVariables.h"
|
2007-09-25 08:31:27 +04:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
|
2007-09-07 03:01:46 +04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-09-11 21:24:14 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2007-09-07 03:01:46 +04:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2007-09-25 08:31:27 +04:00
|
|
|
|
|
|
|
class DeadStoreObs : public LiveVariables::ObserverTy {
|
2007-09-16 03:21:08 +04:00
|
|
|
ASTContext &Ctx;
|
|
|
|
Diagnostic &Diags;
|
2007-09-07 03:01:46 +04:00
|
|
|
public:
|
2007-09-25 08:31:27 +04:00
|
|
|
DeadStoreObs(ASTContext &ctx,Diagnostic &diags) : Ctx(ctx), Diags(diags){}
|
|
|
|
virtual ~DeadStoreObs() {}
|
|
|
|
|
|
|
|
virtual void ObserveStmt(Stmt* S,
|
|
|
|
const LiveVariables::AnalysisDataTy& AD,
|
|
|
|
const LiveVariables::ValTy& Live) {
|
2007-09-11 21:24:14 +04:00
|
|
|
|
2007-09-07 03:01:46 +04:00
|
|
|
if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
|
2007-09-25 08:31:27 +04:00
|
|
|
if (!B->isAssignmentOp()) return; // Skip non-assignments.
|
2007-09-07 03:01:46 +04:00
|
|
|
|
2007-09-10 21:36:42 +04:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
|
2007-09-29 00:48:41 +04:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
|
|
|
|
if (VD->hasLocalStorage() && !Live(VD,AD)) {
|
|
|
|
SourceRange R = B->getRHS()->getSourceRange();
|
|
|
|
Diags.Report(DR->getSourceRange().Begin(), diag::warn_dead_store,
|
|
|
|
0, 0, &R, 1);
|
2007-09-07 03:01:46 +04:00
|
|
|
}
|
|
|
|
}
|
2007-09-25 08:31:27 +04:00
|
|
|
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).
|
2007-09-10 21:36:42 +04:00
|
|
|
for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ;
|
2007-09-25 08:31:27 +04:00
|
|
|
V = cast_or_null<VarDecl>(V->getNextDeclarator())) {
|
2007-09-29 00:48:41 +04:00
|
|
|
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();
|
|
|
|
Diags.Report(V->getLocation(), diag::warn_dead_store, 0, 0,
|
|
|
|
&R,1);
|
|
|
|
}
|
2007-09-11 21:24:14 +04:00
|
|
|
}
|
2007-09-25 08:31:27 +04:00
|
|
|
}
|
|
|
|
}
|
2007-09-07 03:01:46 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
2007-09-16 03:21:08 +04:00
|
|
|
void CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
|
2007-09-07 03:01:46 +04:00
|
|
|
LiveVariables L;
|
|
|
|
L.runOnCFG(cfg);
|
2007-09-25 08:31:27 +04:00
|
|
|
DeadStoreObs A(Ctx, Diags);
|
|
|
|
L.runOnAllBlocks(cfg,A);
|
2007-09-07 03:01:46 +04:00
|
|
|
}
|
|
|
|
|
2007-09-11 19:32:40 +04:00
|
|
|
} // end namespace clang
|