Removed "ObserverTy" from core DataflowValues types. The observer

mechanism can be implemented simply by affixing the Observer to an
analysis meta data, so it doesn't need to be a required type.  This
also permits analyses not to implement an Observer if it doesn't make
sense.

Changed "DataflowValues::MetaDataTy" to
"DataflowValues::AnalysisDataTy" to reflect that the type
enscapsulated the data associated with analyzing a given CFG.

Changed CFGStmtVisitor::BlockStmt_VisitImplicitControlFlowStmt(Stmt*)
to ...VisitImplicitControlFlowExpr(Expr*).  The type narrowing is more
precise and more useful to clients.

Added CFGStmtVisitor::BlockStmt_VisitExpr to reflect the visitation of
expressions at the block statement level.  This captures all implicit
control-flow statements as well as other expressions that are hoisted
to the block level (such as conditions for terminators and function
calls).  This is especially useful for dataflow analysis.

llvm-svn: 42034
This commit is contained in:
Ted Kremenek 2007-09-17 17:14:52 +00:00
parent c3fddac8e7
commit 789ea076a8
6 changed files with 66 additions and 57 deletions

View File

@ -62,14 +62,13 @@ public:
typedef typename _DFValuesTy::AnalysisDirTag AnalysisDirTag;
typedef typename _DFValuesTy::ValTy ValTy;
typedef typename _DFValuesTy::BlockDataMapTy BlockDataMapTy;
typedef typename _DFValuesTy::ObserverTy ObserverTy;
//===--------------------------------------------------------------------===//
// External interface: constructing and running the solver.
//===--------------------------------------------------------------------===//
public:
DataflowSolver(DFValuesTy& d, ObserverTy* o = NULL) : D(d), O(o) {}
DataflowSolver(DFValuesTy& d) : D(d) {}
~DataflowSolver() {}
/// runOnCFG - Computes dataflow values for all blocks in a CFG.
@ -86,7 +85,7 @@ public:
/// only be used for querying the dataflow values within a block with
/// and Observer object.
void runOnBlock(const CFGBlock* B) {
TransferFuncsTy TF (D.getMetaData(),O);
TransferFuncsTy TF (D.getAnalysisData());
ProcessBlock(B,TF,AnalysisDirTag());
}
@ -106,7 +105,7 @@ private:
WorkList.enqueue(&cfg.getEntry());
// Create the state for transfer functions.
TransferFuncsTy TF(D.getMetaData(),O);
TransferFuncsTy TF(D.getAnalysisData());
// Process the worklist until it is empty.
while (!WorkList.isEmpty()) {
@ -131,7 +130,7 @@ private:
WorkList.enqueue(&cfg.getExit());
// Create the state for transfer functions.
TransferFuncsTy TF(D.getMetaData(),O);
TransferFuncsTy TF(D.getAnalysisData());
// Process the worklist until it is empty.
while (!WorkList.isEmpty()) {
@ -213,7 +212,6 @@ private:
private:
DFValuesTy& D;
ObserverTy* O;
};

View File

@ -24,26 +24,30 @@ using namespace clang;
namespace {
class RegisterDecls : public CFGVarDeclVisitor<RegisterDecls> {
UninitializedValues::MetaDataTy& M;
class RegisterDeclsAndExprs : public CFGVarDeclVisitor<RegisterDeclsAndExprs> {
UninitializedValues::AnalysisDataTy& AD;
public:
RegisterDecls(const CFG& cfg, UninitializedValues::MetaDataTy& m) :
CFGVarDeclVisitor<RegisterDecls>(cfg), M(m) {}
RegisterDeclsAndExprs(const CFG& cfg, UninitializedValues::AnalysisDataTy& ad)
: CFGVarDeclVisitor<RegisterDeclsAndExprs>(cfg), AD(ad)
{}
void VisitVarDecl(VarDecl* D) {
if (M.Map.find(D) == M.Map.end()) {
M.Map[D] = M.NumDecls++;
}
}
if (AD.VMap.find(D) == AD.VMap.end())
AD.VMap[D] = AD.Counter++;
}
void BlockStmt_VisitExpr(Expr* E) {
if (AD.EMap.find(E) == AD.EMap.end())
AD.EMap[E] = AD.Counter++;
}
};
} // end anonymous namespace
void UninitializedValues::InitializeValues(const CFG& cfg) {
RegisterDecls R(cfg,this->getMetaData());
R.VisitAllDecls();
getBlockDataMap()[ &cfg.getEntry() ].resize( getMetaData().NumDecls );
RegisterDeclsAndExprs R(cfg,this->getAnalysisData());
R.VisitAllDecls();
getBlockDataMap()[ &cfg.getEntry() ].resize( getAnalysisData().Counter );
}
//===--------------------------------------------------------------------===//
@ -53,15 +57,17 @@ void UninitializedValues::InitializeValues(const CFG& cfg) {
namespace {
class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
UninitializedValues::ValTy V;
UninitializedValues::MetaDataTy& M;
UninitializedValues::ObserverTy* O;
UninitializedValues::AnalysisDataTy& AD;
public:
TransferFuncs(UninitializedValues::MetaDataTy& m,
UninitializedValues::ObserverTy* o) : M(m), O(o) {
V.resize(M.NumDecls);
TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {
V.resize(AD.Counter);
}
UninitializedValues::ValTy& getVal() { return V; }
// bool VisitDeclRefExpr(DeclRefExpr* DR);
// bool VisitBinaryOperator(BinaryOperator* B);
// bool VisitUnaryOperator(UnaryOperator* U);
};
} // end anonymous namespace

View File

@ -28,7 +28,8 @@ static_cast<ImplClass*>(this)->BlockStmt_Visit ## CLASS(static_cast<CLASS*>(S));
#define DEFAULT_BLOCKSTMT_VISIT(CLASS) RetTy BlockStmt_Visit ## CLASS(CLASS *S)\
{ return\
static_cast<ImplClass*>(this)->BlockStmt_VisitImplicitControlFlowStmt(S); }
static_cast<ImplClass*>(this)->BlockStmt_VisitImplicitControlFlowExpr(\
cast<Expr>(S)); }
template <typename ImplClass, typename RetTy=void>
class CFGStmtVisitor : public StmtVisitor<ImplClass,RetTy> {
@ -54,7 +55,11 @@ public:
}
default:
return static_cast<ImplClass*>(this)->BlockStmt_VisitStmt(S);
if (isa<Expr>(S))
return
static_cast<ImplClass*>(this)->BlockStmt_VisitExpr(cast<Expr>(S));
else
return static_cast<ImplClass*>(this)->BlockStmt_VisitStmt(S);
}
}
@ -62,8 +67,12 @@ public:
DEFAULT_BLOCKSTMT_VISIT(StmtExpr)
DEFAULT_BLOCKSTMT_VISIT(ConditionalOperator)
RetTy BlockStmt_VisitImplicitControlFlowStmt(Stmt* S) {
return static_cast<ImplClass*>(this)->BlockStmt_VisitStmt(S);
RetTy BlockStmt_VisitImplicitControlFlowExpr(Expr* E) {
return static_cast<ImplClass*>(this)->BlockStmt_VisitExpr(E);
}
RetTy BlockStmt_VisitExpr(Expr* E) {
return static_cast<ImplClass*>(this)->BlockStmt_VisitStmt(E);
}
RetTy BlockStmt_VisitStmt(Stmt* S) {
@ -72,12 +81,12 @@ public:
RetTy BlockStmt_VisitLogicalOp(BinaryOperator* B) {
return
static_cast<ImplClass*>(this)->BlockStmt_VisitImplicitControlFlowStmt(B);
static_cast<ImplClass*>(this)->BlockStmt_VisitImplicitControlFlowExpr(B);
}
RetTy BlockStmt_VisitComma(BinaryOperator* B) {
return
static_cast<ImplClass*>(this)->BlockStmt_VisitImplicitControlFlowStmt(B);
static_cast<ImplClass*>(this)->BlockStmt_VisitImplicitControlFlowExpr(B);
}
//===--------------------------------------------------------------------===//

View File

@ -15,7 +15,7 @@
#ifndef LLVM_CLANG_ANALYSIS_CFG_VARDECL_VISITOR_H
#define LLVM_CLANG_ANALYSIS_CFG_VARDECL_VISITOR_H
#include "clang/AST/StmtVisitor.h"
#include "clang/Analysis/CFGStmtVisitor.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/CFG.h"
@ -23,7 +23,7 @@
namespace clang {
template <typename ImplClass>
class CFGVarDeclVisitor : public StmtVisitor<ImplClass> {
class CFGVarDeclVisitor : public CFGStmtVisitor<ImplClass> {
const CFG& cfg;
public:
CFGVarDeclVisitor(const CFG& c) : cfg(c) {}

View File

@ -35,7 +35,7 @@ namespace dataflow {
} // end namespace dataflow
template <typename TypeClass,
template <typename ValueTypes,
typename _AnalysisDirTag = dataflow::forward_analysis_tag >
class DataflowValues {
@ -44,10 +44,8 @@ class DataflowValues {
//===--------------------------------------------------------------------===//
public:
typedef typename TypeClass::ValTy ValTy;
typedef typename TypeClass::MetaDataTy MetaDataTy;
typedef typename TypeClass::ObserverTy ObserverTy;
typedef typename ValueTypes::ValTy ValTy;
typedef typename ValueTypes::AnalysisDataTy AnalysisDataTy;
typedef _AnalysisDirTag AnalysisDirTag;
typedef llvm::DenseMap<const CFGBlock*, ValTy> BlockDataMapTy;
@ -101,19 +99,20 @@ public:
BlockDataMapTy& getBlockDataMap() { return BlockDataMap; }
const BlockDataMapTy& getBlockDataMap() const { return BlockDataMap; }
/// getMetaData - Retrieves the meta data associated with a dataflow analysis.
/// getAnalysisData - Retrieves the meta data associated with a
/// dataflow analysis for analyzing a particular CFG.
/// This is typically consumed by transfer function code (via the solver).
/// This can also be used by subclasses to interpret the dataflow values.
MetaDataTy& getMetaData() { return Meta; }
const MetaDataTy& getMetaData() const { return Meta; }
AnalysisDataTy& getAnalysisData() { return AnalysisData; }
const AnalysisDataTy& getAnalysisData() const { return AnalysisData; }
//===--------------------------------------------------------------------===//
// Internal data.
//===--------------------------------------------------------------------===//
protected:
BlockDataMapTy BlockDataMap;
MetaDataTy Meta;
BlockDataMapTy BlockDataMap;
AnalysisDataTy AnalysisData;
};
} // end namespace clang

View File

@ -21,10 +21,12 @@
namespace clang {
class VarDecl;
class Expr;
/// UninitializedValuesTypes - Utility class to wrap type declarations
/// used for defining the UninitializedValues class.
class UninitializedValuesTypes {
/// UninitializedValues_ValueTypes - Utility class to wrap type declarations
/// for dataflow values and dataflow analysis state for the
/// Unitialized Values analysis.
class UninitializedValues_ValueTypes {
public:
class ValTy {
llvm::BitVector BV;
@ -41,26 +43,21 @@ public:
void copyValues(ValTy& RHS) { BV = RHS.BV; }
};
struct MetaDataTy {
llvm::DenseMap<const VarDecl*, unsigned > Map;
unsigned NumDecls;
struct AnalysisDataTy {
llvm::DenseMap<const VarDecl*, unsigned > VMap;
llvm::DenseMap<const Expr*, unsigned > EMap;
unsigned Counter;
MetaDataTy() : NumDecls(0) {}
};
class ObserverTy {
virtual ~ObserverTy();
virtual void ObserveStmt(Stmt* S, MetaDataTy& M, ValTy& V) {}
virtual void ObserveBlockExit(const CFGBlock* B, MetaDataTy& M, ValTy& V) {}
AnalysisDataTy() : Counter(0) {}
};
};
/// UninitializedValues - Objects of this class encapsulate dataflow analysis
/// information regarding what variable declarations in a function are
/// potentially unintialized.
class UninitializedValues : public DataflowValues<UninitializedValuesTypes> {
class UninitializedValues :
public DataflowValues<UninitializedValues_ValueTypes> {
//===--------------------------------------------------------------------===//
// Public interface.
//===--------------------------------------------------------------------===//