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

Marcel Schaible via cfe-dev cfe-dev at lists.llvm.org
Wed Aug 23 04:25:45 PDT 2017


Thanks for your help!

Marcel


Am 22.08.2017 um 20:37 schrieb Johannes Altmanninger:
> 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