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))
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();
89 switch (Head->getStmtClass()) {
91 case Stmt::BinaryOperatorClass: {
108 if (LHSDecl == RHSDecl)
125 NonConstants->insert(VD);
136 case Stmt::UnaryOperatorClass: {
158 NonConstants->insert(VD);
169 case Stmt::DeclStmtClass: {
170 const DeclStmt *DS = cast<DeclStmt>(Head);
172 for (
const auto *
I : DS->
decls()) {
189 if (
const VarDecl *RefVD = dyn_cast<VarDecl>(D)) {
190 NonConstants->insert(RefVD);
198 case Stmt::DeclRefExprClass: {
202 UsedVars->insert(VD);
209 case Stmt::BlockExprClass: {
210 const BlockExpr *B = cast<BlockExpr>(Head);
212 WorkList.push_back(B->
getBody());
221 for (
const Stmt *SubStmt : Head->children())
223 WorkList.push_back(SubStmt);
const Expr * getInit() const
VarDecl - An instance of this class is created to represent a variable declaration or definition...
~PseudoConstantAnalysis()
bool isReferenceType() const
const Stmt * getBody() const
A builtin binary operation expression such as "x + y" or "x <= y".
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
bool isStaticLocal() const
isStaticLocal - Returns true if a variable with function scope is a static local variable.
detail::InMemoryDirectory::const_iterator I
Expr - This represents one expression.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Expr * getSubExpr() const
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)
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
llvm::SmallPtrSet< const VarDecl *, 32 > VarDeclSet
detail::InMemoryDirectory::const_iterator E
bool isPseudoConstant(const VarDecl *VD)
Run one or more source code analyses.
A reference to a declared variable, function, enum, etc.
bool hasLocalStorage() const
hasLocalStorage - Returns true if a variable with function scope is a non-static local variable...