[cfe-dev] how VisitBin## methods for binary operators are called

Rajendra rks at cse.iitb.ac.in
Sat Jan 5 09:14:01 PST 2013


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



More information about the cfe-dev mailing list