[cfe-dev] AST problems with multiple declaration in the same line!

Simone Pellegrini spellegrini at dps.uibk.ac.at
Fri Feb 6 06:06:49 PST 2009


Dear all,
I am experiencing a weird behavior when in a C program I write something 
like:
{
    ...
    int j,k;
    ...
}

For what I see the declaration line should be represented using a 
DeclStmt node in the tree. And that works fine as my visitor is able to 
read both the declaration of j and k using the following code:

void TransferFuncs::VisitDeclStmt(DeclStmt* DS){
    for(DeclStmt::decl_iterator it = DS->decl_begin(); it != 
DS->decl_end(); ++it){
        cout << "VAR DECL: " << (*it)->getNameAsString() << endl;
        if( (VarDecl *VD = dyn_cast<VarDecl>(*it)) )
            ...
            SAVE_DECL_STMT = DS;
    }
}

On, during my analysis I suppose all the statements are included into a 
code block (which afaik is represented by a CompoundStmt node). What I 
need to know is the position of the Decl statement in the block of code 
I am analyzing! In order to find the node... I use the std::find 
function in the following way:

std::find(block->body_begin(), block->body_end(), SAVE_DECL_STMT);

and guess what? The statement is not found! :( If I split the 
declaration into 2 distinct lines... like:

{
    ...
    int j;
    int k;
    ...
}

everything works fine, but when declarations are in the same line... my 
code stop working! Why?
Furthermore, I have also tryied to get the parent of that DeclStmt using 
the ParentMap class... and it seems that DeclStmt has no parent! How 
it's possible?

Even when I dump the code, the statement is there:
(CompoundStmt 0x8c5630
  ...
  (BinaryOperator 0x8c3de0 'int' '='
    (DeclRefExpr 0x8c3d60 'int' Var='n' 0x8c3960)
    (IntegerLiteral 0x8c3da0 'int' 20))
  (DeclStmt 0x8c3f60
    0x8c3ea0 "int j"    0x8c3ef0 "int k"
  (CallExpr 0x8c40b0 'int'
    (ImplicitCastExpr 0x8c4070 'int (*)(char const *restrict, ...)'
      (DeclRefExpr 0x8c3f90 'int (char const *restrict, ...)' 
FunctionDecl='printf' 0x858060))
    (ImplicitCastExpr 0x8c4110 'char const *restrict'
      (StringLiteral 0x8c3fd0 'char [15]' "My rank is: %d"))
    (DeclRefExpr 0x8c4030 'int' Var='rank' 0x8c3450))
    ...
)

regards, S. Pellegrini



More information about the cfe-dev mailing list