how VisitBin## methods for binary operators are called

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how VisitBin## methods for binary operators are called

Rajendra
Hi,

I am working on a static analysis tool for C. I am finding it strange -
traversal of AST using VisitBin## calls. For example x = x + y + z; AST
would be like:
   =
  /  \
x    +2
     / \
    +1   z
   / \
  x   y

Using pre-order DFS traversal, I want to know in what order
VisitBinAdd() are called?

For RHS of assignment operator =, first VisitBinAdd() should be called
for +2 (add). Then again VisitBinAdd() would be called for +1 (add) with
LHS and RHS as x, y. Further it should walk up and see z as RHS of +2
(add).

But when printed LHS and RHS inside VisitBinAdd(), I see something
strange:
it prints RHS of +2 (add)
it prints LHS and RHS of +1 (add)

     Found BinaryOperator
         Additive Op +
         ImplicitCastExpr:
         LValueToRValue
         RHS identifier = z
         type: int
     Found BinaryOperator
         Additive Op +
         ImplicitCastExpr:
         LValueToRValue
         LHS identifier = x
         type: int
         ImplicitCastExpr:
         LValueToRValue
         RHS identifier = y
         type: int

Why it is so? Why it doesn't print LHS and RHS for +1 and then RHS for
+2 ?
Please do correct me if something is wrong here.

- Rajendra
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: how VisitBin## methods for binary operators are called

Rajendra
Hi,

Can someone help on how VisitBin## methods are called and how to walk up in Visit methods?

I want to understand in case of pre-order DFS traversal by clang, how to walk up in the tree?
e.g. for expression x + y + z, I am creating an abstract expression whenever I see an arithmetic operator, how to walk up in tree to get (x+y) as lhs and z as rhs operand?
I need this for x = x + y + z; to get abstract expression for x+y+z  as rhs of assignment operator.