20 #include "llvm/ADT/SmallPtrSet.h" 23 using namespace clang;
25 typedef llvm::SmallPtrSet<const VarDecl*, 32>
VarDeclSet;
28 DeclBody(DeclBody), Analyzed(
false) {
51 return !NonConstants->count(VD);
63 return UsedVars->count(VD);
67 const Decl *PseudoConstantAnalysis::getDecl(
const Expr *E) {
68 if (
const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
74 void PseudoConstantAnalysis::RunAnalysis() {
75 std::deque<const Stmt *> WorkList;
80 WorkList.push_back(DeclBody);
82 while (!WorkList.empty()) {
83 const Stmt *Head = WorkList.front();
86 if (
const Expr *Ex = dyn_cast<Expr>(Head))
87 Head = Ex->IgnoreParenCasts();
91 case Stmt::BinaryOperatorClass: {
108 if (LHSDecl == RHSDecl)
126 NonConstants->insert(VD);
137 case Stmt::UnaryOperatorClass: {
159 NonConstants->insert(VD);
170 case Stmt::DeclStmtClass: {
171 const DeclStmt *DS = cast<DeclStmt>(Head);
173 for (
const auto *I : DS->
decls()) {
190 if (
const VarDecl *RefVD = dyn_cast<VarDecl>(D)) {
191 NonConstants->insert(RefVD);
199 case Stmt::DeclRefExprClass: {
203 UsedVars->insert(VD);
210 case Stmt::BlockExprClass: {
211 const BlockExpr *B = cast<BlockExpr>(Head);
213 WorkList.push_back(B->
getBody());
224 WorkList.push_back(SubStmt);
Stmt - This represents one statement.
Decl - This represents one declaration (or definition), e.g.
Represents a variable declaration or definition.
~PseudoConstantAnalysis()
bool isReferenceType() const
A builtin binary operation expression such as "x + y" or "x <= y".
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
const Stmt * getBody() const
Expr - This represents one expression.
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
UnaryOperator - This represents the unary-expression's (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
PseudoConstantAnalysis(const Stmt *DeclBody)
bool wasReferenced(const VarDecl *VD)
Expr * getSubExpr() const
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
llvm::SmallPtrSet< const VarDecl *, 32 > VarDeclSet
Dataflow Directional Tag Classes.
const Expr * getInit() const
StmtClass getStmtClass() const
bool isPseudoConstant(const VarDecl *VD)
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
A reference to a declared variable, function, enum, etc.