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 +
|
2008-07-02 23:16:33 +00:00
|
|
|
"' is used in the enclosing expression, the value is never actually"
|
|
|
|
" read from '" + name + "'"
|
2008-06-20 21:45:25 +00:00
|
|
|
: "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-07-02 23:16:33 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Driver function to invoke the Dead-Stores checker on a CFG.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void clang::CheckDeadStores(LiveVariables& L, BugReporter& BR) {
|
|
|
|
|
|
|
|
SimpleBugType BT("dead store");
|
|
|
|
DiagCollector C(BT);
|
|
|
|
|
|
|
|
DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C, BR.getParentMap());
|
2008-07-03 05:26:14 +00:00
|
|
|
L.runOnAllBlocks(*BR.getCFG(), &A);
|
2008-07-02 23:16:33 +00:00
|
|
|
|
|
|
|
// Emit the bug reports.
|
|
|
|
|
|
|
|
for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
|
|
|
|
BR.EmitWarning(*I);
|
2008-04-14 17:39:48 +00:00
|
|
|
}
|
2008-07-02 23:16:33 +00:00
|
|
|
|