[cfe-dev] RecursiveAstVisitor: How to get the LHS and its type of an assigment inside a VistCallExpr?

Johannes Altmanninger via cfe-dev cfe-dev at lists.llvm.org
Tue Aug 22 11:37:15 PDT 2017


Marcel Schaible via cfe-dev <cfe-dev at lists.llvm.org> writes:

> Hi,
>
> in my source-to-source translation tool I try to get the lefthandside of 
> assignment in my VisitCallExpr:
>
> Example:
>
> lhs = foo(42);
>
> I need to retrieve "lhs" and its associated type.
>
> Any hint is welcome!
>
> Marcel

Hi,

So there are multiple ways to do this

You could move to using ASTMatchers, they make it easy to perform
structural queries. You can try this with clang-query:

match binaryOperator(hasDescendant(callExpr()), hasOperatorName("="))

hasDescendant() matches any descendant in a subexpression, has() only
direct children
Note that this does not match overloaded operator= nor initializations.


>
> My code snippet:
>
>      bool VisitCallExpr(CallExpr *CallExpression) {
>          QualType q = CallExpression->getType();
>          const Type *t = q.getTypePtrOrNull();
>          const Stmt* callExpression = CallExpression;
>
>          FunctionDecl *func = CallExpression->getDirectCallee();
>
>          ...
>
>          // How to retrieve "LHS" here and its type?
>
>          ...
>
>          return true;
>      }
>

To keep using RecursiveASTVisitor you could just record the assignment
operators / declarations as you traverse the tree.

bool VisitBinaryOperator(BinaryOperator *BinOp) {
     if (BinOp->getOpCodeStr() == " ") {
        // store BinOp->getLHS()
     }
}

bool VisitVarDecl(VarDecl *VD) {
     // store VD->getInit()
}

use Expr::getType() and ValueDecl::getType() to get the Type

I hope it is clear how to do it

Johannes

>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list