[cfe-dev] Traverse AST

Kai Wang via cfe-dev cfe-dev at lists.llvm.org
Wed Feb 17 10:00:35 PST 2016


Hi all,

I'm using clang AST to do some static analysis.
In my case, I want to find all assignment statements for pointer variables.
For example, here is the source code snippet:
*int *a, int **b;*
***b = *a;*
I want to know there is an assignment from "*a" to "**b".

I use RecursiveASTVistor to traverse AST. I rewrite VistitStmt() to check
if AST Node is an assignment statement.

bool VisitStmt(Stmt *s) {

 if(BinaryOperator *binaryOperator = dyn_cast<BinaryOperator>(s)) {

      if(binaryOperator->isAssignmentOp()) { do something}

}

Here is the AST for the source code snippet above:

*BinaryOperator* 0x9532d48 <line:66:2, col:14> 'int' '='

    | |-UnaryOperator 0x9532cb0 <col:2, col:4> 'int' lvalue prefix '*'

    | | `-ImplicitCastExpr 0x9532c98 <col:3, col:4> 'int *' <LValueToRValue>

    | |   `-UnaryOperator 0x9532c78 <col:3, col:4> 'int *' lvalue prefix '*'

    | |     `-ImplicitCastExpr 0x9532c60 <col:4> 'int **' <LValueToRValue>

    | |       `-DeclRefExpr 0x9532c38 <col:4> 'int **' lvalue *Var*
0x9532b00* 'b'* 'int **'

    | `-ImplicitCastExpr 0x9532d30 <col:13, col:14> 'int' <LValueToRValue>

    |   `-UnaryOperator 0x9532d10 <col:13, col:14> 'int' lvalue prefix '*'

    |     `-ImplicitCastExpr 0x9532cf8 <col:14> 'int *' <LValueToRValue>

    |       `-DeclRefExpr 0x9532cd0 <col:14> 'int *' lvalue *Var* 0x9532a88*
'a'* 'int *'

In VisitStmt() function, at the point when this *BinaryOperator* is
visited, I only have the information of the current AST Node.
How can I traverse the *subtree* of this *BinaryOperator *so that I can get
all the info, such as variable name *a*, *b*, prefix ** ,** *?

Thank you.
-- 
Regards,
Kai
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160217/a195eac7/attachment.html>


More information about the cfe-dev mailing list