2011-01-14 04:58:56 +08:00
|
|
|
//==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==//
|
2007-09-07 07:01:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-07 07:01:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 18:40:09 +08:00
|
|
|
// This file defines a DeadStores, a flow-sensitive checker that looks for
|
2007-09-07 07:01:46 +08:00
|
|
|
// stores to variables that are no longer live.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-24 03:38:26 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
|
2007-12-22 05:42:19 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2007-09-25 12:31:27 +08:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2009-04-07 13:25:24 +08:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
|
2007-09-07 07:01:46 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-09-12 01:24:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-06-21 05:45:25 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2009-04-07 13:25:24 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2007-09-07 07:01:46 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2007-09-07 07:01:46 +08:00
|
|
|
|
|
|
|
namespace {
|
2008-06-21 05:45:25 +08:00
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class DeadStoreObs : public LiveVariables::ObserverTy {
|
2007-09-16 07:21:08 +08:00
|
|
|
ASTContext &Ctx;
|
2008-07-15 04:56:04 +08:00
|
|
|
BugReporter& BR;
|
2008-06-21 05:45:25 +08:00
|
|
|
ParentMap& Parents;
|
2009-04-07 13:25:24 +08:00
|
|
|
llvm::SmallPtrSet<VarDecl*, 20> Escaped;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-07 07:01:46 +08:00
|
|
|
public:
|
2009-04-07 13:25:24 +08:00
|
|
|
DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents,
|
|
|
|
llvm::SmallPtrSet<VarDecl*, 20> &escaped)
|
|
|
|
: Ctx(ctx), BR(br), Parents(parents), Escaped(escaped) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
virtual ~DeadStoreObs() {}
|
2009-04-01 14:52:48 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) {
|
2009-04-07 13:25:24 +08:00
|
|
|
if (Escaped.count(V))
|
|
|
|
return;
|
2008-07-15 04:56:04 +08:00
|
|
|
|
2008-11-24 13:29:24 +08:00
|
|
|
std::string name = V->getNameAsString();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
const char* BugType = 0;
|
|
|
|
std::string msg;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
switch (dsk) {
|
|
|
|
default:
|
|
|
|
assert(false && "Impossible dead store type.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
case DeadInit:
|
2009-04-03 06:50:16 +08:00
|
|
|
BugType = "Dead initialization";
|
2008-07-24 05:16:38 +08:00
|
|
|
msg = "Value stored to '" + name +
|
|
|
|
"' during its initialization is never read";
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
case DeadIncrement:
|
2009-04-03 06:50:16 +08:00
|
|
|
BugType = "Dead increment";
|
2008-07-24 05:16:38 +08:00
|
|
|
case Standard:
|
2009-04-03 06:50:16 +08:00
|
|
|
if (!BugType) BugType = "Dead assignment";
|
2008-07-24 05:16:38 +08:00
|
|
|
msg = "Value stored to '" + name + "' is never read";
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
case Enclosing:
|
2011-01-14 04:58:56 +08:00
|
|
|
// 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;
|
2008-07-16 02:06:32 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 02:27:55 +08:00
|
|
|
BR.EmitBasicReport(BugType, "Dead store", msg, L, R);
|
2008-06-21 05:45:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-21 05:45:25 +08:00
|
|
|
void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
|
2008-07-24 05:16:38 +08:00
|
|
|
DeadStoreKind dsk,
|
2008-06-21 05:45:25 +08:00
|
|
|
const LiveVariables::AnalysisDataTy& AD,
|
|
|
|
const LiveVariables::ValTy& Live) {
|
|
|
|
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
if (!VD->hasLocalStorage())
|
|
|
|
return;
|
|
|
|
// Reference types confuse the dead stores checker. Skip them
|
|
|
|
// for now.
|
|
|
|
if (VD->getType()->getAs<ReferenceType>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Live(VD, AD) &&
|
2009-12-03 08:46:16 +08:00
|
|
|
!(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>()))
|
2008-07-24 05:16:38 +08:00
|
|
|
Report(VD, dsk, Ex->getSourceRange().getBegin(),
|
2009-09-09 23:08:12 +08:00
|
|
|
Val->getSourceRange());
|
2008-05-22 06:59:16 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk,
|
2008-05-06 07:12:21 +08:00
|
|
|
const LiveVariables::AnalysisDataTy& AD,
|
|
|
|
const LiveVariables::ValTy& Live) {
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
|
2008-07-24 05:16:38 +08:00
|
|
|
CheckVarDecl(VD, DR, Val, dsk, AD, Live);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
bool isIncrement(VarDecl* VD, BinaryOperator* B) {
|
|
|
|
if (B->isCompoundAssignmentOp())
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
Expr* RHS = B->getRHS()->IgnoreParenCasts();
|
|
|
|
BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
if (!BRHS)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
DeclRefExpr *DR;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
|
|
|
|
if (DR->getDecl() == VD)
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
|
|
|
|
if (DR->getDecl() == VD)
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
return false;
|
2008-05-06 07:12:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
virtual void ObserveStmt(Stmt* S,
|
|
|
|
const LiveVariables::AnalysisDataTy& AD,
|
|
|
|
const LiveVariables::ValTy& Live) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-15 02:28:25 +08:00
|
|
|
// Skip statements in macros.
|
|
|
|
if (S->getLocStart().isMacroID())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
|
2007-09-25 12:31:27 +08:00
|
|
|
if (!B->isAssignmentOp()) return; // Skip non-assignments.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-11 01:36:42 +08:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
|
2008-06-21 05:45:25 +08:00
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
2008-08-09 08:05:14 +08:00
|
|
|
// Special case: check for assigning null to a pointer.
|
2009-09-09 23:08:12 +08:00
|
|
|
// This is a common form of defensive programming.
|
2010-02-24 05:19:33 +08:00
|
|
|
QualType T = VD->getType();
|
|
|
|
if (T->isPointerType() || T->isObjCObjectPointerType()) {
|
2009-11-23 04:26:21 +08:00
|
|
|
if (B->getRHS()->isNullPointerConstant(Ctx,
|
|
|
|
Expr::NPC_ValueDependentIsNull))
|
|
|
|
return;
|
2008-08-09 08:05:14 +08:00
|
|
|
}
|
2009-11-23 04:26:21 +08:00
|
|
|
|
|
|
|
Expr* RHS = B->getRHS()->IgnoreParenCasts();
|
2009-01-10 06:15:01 +08:00
|
|
|
// Special case: self-assignments. These are often used to shut up
|
|
|
|
// "unused variable" compiler warnings.
|
|
|
|
if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
|
|
|
|
if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-10 06:15:01 +08:00
|
|
|
// Otherwise, issue a warning.
|
2009-04-01 14:52:48 +08:00
|
|
|
DeadStoreKind dsk = Parents.isConsumedExpr(B)
|
2009-09-09 23:08:12 +08:00
|
|
|
? Enclosing
|
2009-01-20 08:47:45 +08:00
|
|
|
: (isIncrement(VD,B) ? DeadIncrement : Standard);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-09 08:05:14 +08:00
|
|
|
CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2007-09-07 07:01:46 +08:00
|
|
|
}
|
2008-05-06 07:12:21 +08:00
|
|
|
else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
|
|
|
|
if (!U->isIncrementOp())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-15 13:23:41 +08:00
|
|
|
// Handle: ++x within a subexpression. The solution is not warn
|
|
|
|
// about preincrements to dead variables when the preincrement occurs
|
|
|
|
// as a subexpression. This can lead to false negatives, e.g. "(++x);"
|
|
|
|
// A generalized dead code checker should find such issues.
|
2009-04-01 14:52:48 +08:00
|
|
|
if (U->isPrefix() && Parents.isConsumedExpr(U))
|
2008-10-15 13:23:41 +08:00
|
|
|
return;
|
2008-07-25 01:01:17 +08:00
|
|
|
|
2008-05-06 07:12:21 +08:00
|
|
|
Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-06 07:12:21 +08:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex))
|
2008-07-24 05:16:38 +08:00
|
|
|
CheckDeclRef(DR, U, DeadIncrement, AD, Live);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-05-06 07:12:21 +08:00
|
|
|
else if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
|
2007-09-25 12:31:27 +08:00
|
|
|
// Iterate through the decls. Warn if any initializers are complex
|
|
|
|
// expressions that are not live (never used).
|
2008-08-06 04:46:55 +08:00
|
|
|
for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
|
|
|
|
DI != DE; ++DI) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-06 04:46:55 +08:00
|
|
|
VarDecl* V = dyn_cast<VarDecl>(*DI);
|
2008-07-25 12:47:34 +08:00
|
|
|
|
|
|
|
if (!V)
|
|
|
|
continue;
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
|
|
|
|
if (V->hasLocalStorage()) {
|
|
|
|
// Reference types confuse the dead stores checker. Skip them
|
|
|
|
// for now.
|
|
|
|
if (V->getType()->getAs<ReferenceType>())
|
|
|
|
return;
|
|
|
|
|
2008-07-25 12:47:34 +08:00
|
|
|
if (Expr* E = V->getInit()) {
|
2009-12-15 12:12:12 +08:00
|
|
|
// 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;
|
2009-12-23 12:11:44 +08:00
|
|
|
|
2010-12-06 16:20:24 +08:00
|
|
|
if (isa<ExprWithCleanups>(E))
|
2009-12-23 12:11:44 +08:00
|
|
|
return;
|
|
|
|
|
2008-07-25 12:47:34 +08:00
|
|
|
// A dead initialization is a variable that is dead after it
|
|
|
|
// is initialized. We don't flag warnings for those variables
|
|
|
|
// marked 'unused'.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) {
|
2007-09-29 04:48:41 +08: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.
|
2010-08-03 05:13:48 +08:00
|
|
|
if (E->isConstantInitializer(Ctx, false))
|
2009-02-10 02:01:00 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-10 02:01:00 +08:00
|
|
|
if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
|
2010-03-18 09:22:39 +08:00
|
|
|
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;
|
|
|
|
//
|
2009-02-10 02:01:00 +08:00
|
|
|
if (VD->hasGlobalStorage() &&
|
2010-03-18 09:22:39 +08:00
|
|
|
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;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-10 02:01:00 +08:00
|
|
|
Report(V, DeadInit, V->getLocation(), E->getSourceRange());
|
2007-09-12 01:24:14 +08:00
|
|
|
}
|
2008-07-25 12:47:34 +08:00
|
|
|
}
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
}
|
2007-09-25 12:31:27 +08:00
|
|
|
}
|
2007-09-07 07:01:46 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-07 07:01:46 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-07-03 07:16:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Driver function to invoke the Dead-Stores checker on a CFG.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-04-07 13:25:24 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
|
2009-04-07 13:25:24 +08:00
|
|
|
CFG *cfg;
|
|
|
|
public:
|
|
|
|
FindEscaped(CFG *c) : cfg(c) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-07 13:25:24 +08:00
|
|
|
CFG& getCFG() { return *cfg; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-07 13:25:24 +08:00
|
|
|
llvm::SmallPtrSet<VarDecl*, 20> Escaped;
|
|
|
|
|
|
|
|
void VisitUnaryOperator(UnaryOperator* U) {
|
|
|
|
// Check for '&'. Any VarDecl whose value has its address-taken we
|
|
|
|
// treat as escaped.
|
|
|
|
Expr* E = U->getSubExpr()->IgnoreParenCasts();
|
2010-08-25 19:45:40 +08:00
|
|
|
if (U->getOpcode() == UO_AddrOf)
|
2009-04-07 13:25:24 +08:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
|
|
|
Escaped.insert(VD);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Visit(E);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-07 13:25:24 +08:00
|
|
|
|
2010-12-23 15:20:52 +08:00
|
|
|
void ento::CheckDeadStores(CFG &cfg, LiveVariables &L, ParentMap &pmap,
|
2009-09-10 13:44:00 +08:00
|
|
|
BugReporter& BR) {
|
|
|
|
FindEscaped FS(&cfg);
|
2009-09-09 23:08:12 +08:00
|
|
|
FS.getCFG().VisitBlockStmts(FS);
|
2009-09-10 13:44:00 +08:00
|
|
|
DeadStoreObs A(BR.getContext(), BR, pmap, FS.Escaped);
|
|
|
|
L.runOnAllBlocks(cfg, &A);
|
2008-04-15 01:39:48 +08:00
|
|
|
}
|