[cfe-dev] Read/Write Accesses of global variables in a Function body...

Peter clang_llvm_123-4 at yahoo.de
Mon Dec 19 06:56:57 PST 2011


Hi everybody,

I am trying to write a recursive AST visitor that detects all read/write accesses of global
variables within the body of a given function declaration.

My Questions are:

1) How to detect read/write accesses (for a given function) in a simple way?
2) How to detect if a variable is global?


Currently I am experimenting with the following code...is it going into the right direction?

I hope someone can help...?

Thanks in advance!!!
Peter


bool VisitFunctionDecl (clang::FunctionDecl* decl)
{   
    if (decl->isThisDeclarationADefinition() && decl->hasBody())
    {

    clang::Stmt *stmt = decl->getBody();
    
        for (clang::Stmt::child_iterator I = stmt->child_begin(), E = stmt->child_end(); I!=E; ++I)
        {
            if (clang::Stmt *child = *I) 
            {
                if (child->getStmtClass()==clang::Stmt::BinaryOperatorClass)
                {
                    clang::BinaryOperator* B = llvm::cast<clang::BinaryOperator>(child);
                    if (B->isAssignmentOp())
                    {
                        clang::CompoundAssignOperator *assign_opterator = llvm::cast<clang::CompoundAssignOperator>(B);

                        clang::Expr *lhs = assign_opterator->getLHS();
                        clang::Expr *rhs = assign_opterator->getRHS();


                        //So far so good

                        //For the left hand side I can probably do the following.

                        if(lhs->getStmtClass() == clang::Expr::DeclRefExprClass)
                        {
                            clang::DeclRefExpr *lExpr = llvm::cast<clang::DeclRefExpr>(lhs);
                            clang::ValueDecl *lValue = lExpr->getDecl();

                             //How to find out if the lvalue is global ??                   

                            ...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111219/949fb484/attachment.html>


More information about the cfe-dev mailing list