I can't get the defined size

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

I can't get the defined size

Rambo
Hello,

This is some Program fragments for test:
 section 1:
bool conver(int a[],int b[])
{
   for(i=0;i<5;i++)
     a[i]=b[i];
}
section 2:
int main()
{
   int a[10];
   int num[5]={1,2,3,4,5};
  conver(a,num);
}
Now my problem is that I traverse them by some code as follows,i can't get the defined size of array a and b.the reason is obvious,the section 1 is traversed before section 2.I wonder someone can give me a better method to break this trouble,thank you!

bool MyASTConsumer::HandleTopLevelDecl(DeclGroupRef d)
{
  typedef DeclGroupRef::iterator iter;
  for (iter b = d.begin(), e = d.end(); b != e; ++b)
  {
    rv.TraverseDecl(*b);
  }
  return true; // keep going
}
Reply | Threaded
Open this post in threaded view
|

Re: I can't get the defined size

Rajendra
I am also looking to get defined size of array.

Using VisitDeclStmt(Stmt *s), I can get following info:
   Found DeclStmt
        decl statement: int dummy[5]
        identifier name = dummy
        type = int [5]

But how to get defined size?

Also using, VisitBinAssign(BinaryOperator *E) and VisitArraySubscriptExpr(ArraySubscriptExpr* S), I can get following info:
    Found BinaryOperator
        Assignment Op =  in B1
        IntegerLiteral:
        RHS value: 10

        found array subscript expr
        ImplicitCastExpr:
        ArrayToPointerDecay
        identifier = arr
        IntegerLiteral:
        index value: 9

But why I am getting RHS first for arr[9] = 10;? I should get first LHS arr[9] and then RHS value 10, since Visit methods are called in pre-order DFS manner as AST is traverse. Correct me if it is otherwise?

I am having same trouble when I have unary minus on LHS of a binary operation (e.g. x = -1 + x;), then VisitUnaryMinus() is getting visited after RHS? why it is so?

- Rajendra