Newer
Older
//==- DeadStoresChecker.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.
//
//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis
committed
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/Analysis/Analyses/LiveVariables.h"
#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenek
committed
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek
committed
#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/ASTContext.h"
Ted Kremenek
committed
#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer
committed
#include "llvm/ADT/SmallString.h"
using namespace clang;
namespace {
Ted Kremenek
committed
// FIXME: Eventually migrate into its own file, and have it managed by
// AnalysisManager.
class ReachableCode {
const CFG &cfg;
llvm::BitVector reachable;
public:
ReachableCode(const CFG &cfg)
: cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
void computeReachableBlocks();
bool isReachable(const CFGBlock *block) const {
return reachable[block->getBlockID()];
}
};
}
void ReachableCode::computeReachableBlocks() {
if (!cfg.getNumBlockIDs())
return;
Chris Lattner
committed
SmallVector<const CFGBlock*, 10> worklist;
Ted Kremenek
committed
worklist.push_back(&cfg.getEntry());
while (!worklist.empty()) {
const CFGBlock *block = worklist.back();
worklist.pop_back();
llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
if (isReachable)
continue;
isReachable = true;
for (CFGBlock::const_succ_iterator i = block->succ_begin(),
e = block->succ_end(); i != e; ++i)
if (const CFGBlock *succ = *i)
worklist.push_back(succ);
}
}
namespace {
Ted Kremenek
committed
class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek
committed
const CFG &cfg;
ASTContext &Ctx;
Ted Kremenek
committed
BugReporter& BR;
AnalysisDeclContext* AC;
Ted Kremenek
committed
llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
OwningPtr<ReachableCode> reachableCode;
Ted Kremenek
committed
const CFGBlock *currentBlock;
Ted Kremenek
committed
enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
public:
Ted Kremenek
committed
DeadStoreObs(const CFG &cfg, ASTContext &ctx,
BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents,
Ted Kremenek
committed
llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
: cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
Ted Kremenek
committed
Escaped(escaped), currentBlock(0) {}
virtual ~DeadStoreObs() {}
Ted Kremenek
committed
Ted Kremenek
committed
void Report(const VarDecl *V, DeadStoreKind dsk,
PathDiagnosticLocation L, SourceRange R) {
Ted Kremenek
committed
if (Escaped.count(V))
return;
Ted Kremenek
committed
// Compute reachable blocks within the CFG for trivial cases
// where a bogus dead store can be reported because itself is unreachable.
if (!reachableCode.get()) {
reachableCode.reset(new ReachableCode(cfg));
reachableCode->computeReachableBlocks();
}
if (!reachableCode->isReachable(currentBlock))
return;
Ted Kremenek
committed
SmallString<64> buf;
llvm::raw_svector_ostream os(buf);
const char *BugType = 0;
Ted Kremenek
committed
switch (dsk) {
case DeadInit:
Benjamin Kramer
committed
os << "Value stored to '" << *V
<< "' during its initialization is never read";
Ted Kremenek
committed
break;
Ted Kremenek
committed
case DeadIncrement:
Ted Kremenek
committed
case Standard:
if (!BugType) BugType = "Dead assignment";
Benjamin Kramer
committed
os << "Value stored to '" << *V << "' is never read";
Ted Kremenek
committed
break;
Ted Kremenek
committed
case Enclosing:
// Don't report issues in this case, e.g.: "if (x = foo())",
// where 'x' is unused later. We have yet to see a case where
// this is a real bug.
return;
BR.EmitBasicReport(BugType, "Dead store", os.str(), L, R);
Ted Kremenek
committed
void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenek
committed
DeadStoreKind dsk,
Ted Kremenek
committed
const LiveVariables::LivenessValues &Live) {
if (!VD->hasLocalStorage())
return;
// Reference types confuse the dead stores checker. Skip them
// for now.
if (VD->getType()->getAs<ReferenceType>())
return;
Ted Kremenek
committed
if (!Live.isLive(VD) &&
!(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) {
PathDiagnosticLocation ExLoc =
PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
Report(VD, dsk, ExLoc, Val->getSourceRange());
}
Ted Kremenek
committed
void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremenek
committed
const LiveVariables::LivenessValues& Live) {
Ted Kremenek
committed
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek
committed
CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenek
committed
}
Ted Kremenek
committed
bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenek
committed
if (B->isCompoundAssignmentOp())
return true;
Ted Kremenek
committed
const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek
committed
const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Ted Kremenek
committed
if (!BRHS)
return false;
Ted Kremenek
committed
const DeclRefExpr *DR;
Ted Kremenek
committed
if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
if (DR->getDecl() == VD)
return true;
Ted Kremenek
committed
if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
if (DR->getDecl() == VD)
return true;
Ted Kremenek
committed
return false;
Ted Kremenek
committed
virtual void observeStmt(const Stmt *S, const CFGBlock *block,
Ted Kremenek
committed
const LiveVariables::LivenessValues &Live) {
Ted Kremenek
committed
currentBlock = block;
// Skip statements in macros.
if (S->getLocStart().isMacroID())
return;
Ted Kremenek
committed
// Only cover dead stores from regular assignments. ++/-- dead stores
// have never flagged a real bug.
Ted Kremenek
committed
if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremenek
committed
if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek
committed
// Special case: check for assigning null to a pointer.
// This is a common form of defensive programming.
QualType T = VD->getType();
if (T->isPointerType() || T->isObjCObjectPointerType()) {
if (B->getRHS()->isNullPointerConstant(Ctx,
Expr::NPC_ValueDependentIsNull))
return;
Ted Kremenek
committed
}
Ted Kremenek
committed
Expr *RHS = B->getRHS()->IgnoreParenCasts();
// Special case: self-assignments. These are often used to shut up
// "unused variable" compiler warnings.
Ted Kremenek
committed
if (DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
return;
// Otherwise, issue a warning.
Ted Kremenek
committed
DeadStoreKind dsk = Parents.isConsumedExpr(B)
Ted Kremenek
committed
: (isIncrement(VD,B) ? DeadIncrement : Standard);
Ted Kremenek
committed
CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
}
Ted Kremenek
committed
else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenek
committed
if (!U->isIncrementOp() || U->isPrefix())
Ted Kremenek
committed
const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenek
committed
if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenek
committed
return;
Ted Kremenek
committed
const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Ted Kremenek
committed
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek
committed
CheckDeclRef(DR, U, DeadIncrement, Live);
Ted Kremenek
committed
else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
// Iterate through the decls. Warn if any initializers are complex
// expressions that are not live (never used).
Ted Kremenek
committed
for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
Ted Kremenek
committed
DI != DE; ++DI) {
Ted Kremenek
committed
VarDecl *V = dyn_cast<VarDecl>(*DI);
if (!V)
continue;
if (V->hasLocalStorage()) {
// Reference types confuse the dead stores checker. Skip them
// for now.
if (V->getType()->getAs<ReferenceType>())
return;
Ted Kremenek
committed
if (Expr *E = V->getInit()) {
while (ExprWithCleanups *exprClean = dyn_cast<ExprWithCleanups>(E))
E = exprClean->getSubExpr();
// Don't warn on C++ objects (yet) until we can show that their
// constructors/destructors don't have side effects.
if (isa<CXXConstructExpr>(E))
return;
Ted Kremenek
committed
// A dead initialization is a variable that is dead after it
// is initialized. We don't flag warnings for those variables
// marked 'unused'.
Ted Kremenek
committed
if (!Live.isLive(V) && V->getAttr<UnusedAttr>() == 0) {
// 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.
Richard Smith
committed
if (E->isEvaluatable(Ctx))
Ted Kremenek
committed
return;
Ted Kremenek
committed
if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
// Special case: check for initialization from constant
// variables.
//
// e.g. extern const int MyConstant;
// int x = MyConstant;
//
Ted Kremenek
committed
if (VD->hasGlobalStorage() &&
VD->getType().isConstQualified())
return;
// Special case: check for initialization from scalar
// parameters. This is often a form of defensive
// programming. Non-scalars are still an error since
// because it more likely represents an actual algorithmic
// bug.
if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
return;
}
PathDiagnosticLocation Loc =
PathDiagnosticLocation::create(V, BR.getSourceManager());
Report(V, DeadInit, Loc, E->getSourceRange());
}
}
}
};
} // end anonymous namespace
Ted Kremenek
committed
//===----------------------------------------------------------------------===//
// Driver function to invoke the Dead-Stores checker on a CFG.
//===----------------------------------------------------------------------===//
Ted Kremenek
committed
namespace {
Kovarththanan Rajaratnam
committed
class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenek
committed
CFG *cfg;
public:
FindEscaped(CFG *c) : cfg(c) {}
Ted Kremenek
committed
CFG& getCFG() { return *cfg; }
Ted Kremenek
committed
llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenek
committed
void VisitUnaryOperator(UnaryOperator* U) {
// Check for '&'. Any VarDecl whose value has its address-taken we
// treat as escaped.
Ted Kremenek
committed
Expr *E = U->getSubExpr()->IgnoreParenCasts();
John McCall
committed
if (U->getOpcode() == UO_AddrOf)
Ted Kremenek
committed
if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek
committed
Escaped.insert(VD);
return;
}
Visit(E);
}
};
} // end anonymous namespace
Ted Kremenek
committed
Argyrios Kyrtzidis
committed
//===----------------------------------------------------------------------===//
// DeadStoresChecker
//===----------------------------------------------------------------------===//
namespace {
class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis
committed
public:
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
BugReporter &BR) const {
if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidis
committed
CFG &cfg = *mgr.getCFG(D);
AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
Argyrios Kyrtzidis
committed
ParentMap &pmap = mgr.getParentMap(D);
FindEscaped FS(&cfg);
FS.getCFG().VisitBlockStmts(FS);
DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
Ted Kremenek
committed
L->runOnAllBlocks(A);
Argyrios Kyrtzidis
committed
}
}
};
}
void ento::registerDeadStoresChecker(CheckerManager &mgr) {
mgr.registerChecker<DeadStoresChecker>();