20 #include "llvm/ADT/SmallPtrSet.h"
23 using namespace clang;
26 #define VARDECL_SET_SIZE 256
27 typedef llvm::SmallPtrSet<const VarDecl*, VARDECL_SET_SIZE>
VarDeclSet;
30 DeclBody(DeclBody), Analyzed(
false) {
53 return !NonConstants->count(VD);
65 return UsedVars->count(VD);
69 const Decl *PseudoConstantAnalysis::getDecl(
const Expr *
E) {
70 if (
const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
77 std::deque<const Stmt *> WorkList;
82 WorkList.push_back(DeclBody);
84 while (!WorkList.empty()) {
85 const Stmt *Head = WorkList.front();
88 if (
const Expr *Ex = dyn_cast<Expr>(Head))
89 Head = Ex->IgnoreParenCasts();
91 switch (Head->getStmtClass()) {
93 case Stmt::BinaryOperatorClass: {
110 if (LHSDecl == RHSDecl)
127 NonConstants->insert(VD);
138 case Stmt::UnaryOperatorClass: {
160 NonConstants->insert(VD);
171 case Stmt::DeclStmtClass: {
172 const DeclStmt *DS = cast<DeclStmt>(Head);
174 for (
const auto *
I : DS->
decls()) {
191 if (
const VarDecl *RefVD = dyn_cast<VarDecl>(D)) {
192 NonConstants->insert(RefVD);
200 case Stmt::DeclRefExprClass: {
204 UsedVars->insert(VD);
211 case Stmt::BlockExprClass: {
212 const BlockExpr *B = cast<BlockExpr>(Head);
214 WorkList.push_back(B->
getBody());
223 for (
const Stmt *SubStmt : Head->children())
225 WorkList.push_back(SubStmt);
Decl - This represents one declaration (or definition), e.g.
llvm::SmallPtrSet< const VarDecl *, VARDECL_SET_SIZE > VarDeclSet
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. ...
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...