[cfe-dev] AST UnaryOperator subexpression

Petr Kostka packyr at atlas.cz
Tue Apr 22 15:40:36 PDT 2008


Hi,

I am trying to walk the AST tree and wrap all the dereferenced expressions
"*expr" with macro, e.g. "*DEREF_EXPR(expr)", but I get strange behavior with
following code snippet that should do the wrapping:

const UnaryOperator *pUnaryOperator = dyn_cast<const UnaryOperator>(pExpr);
assert(pUnaryOperator);
if (UnaryOperator::Deref == pUnaryOperator->getOpcode())
{
    const Expr *pSubExpr = pUnaryOperator->getSubExpr();
    mRewriter.InsertCStrBefore(pSubExpr->getSourceRange().getBegin(),
"DEREF_EXPR(");
    mRewriter.InsertCStrAfter(pSubExpr->getSourceRange().getEnd(), ")");
}

When the subexpression of the dereference operator (*) is another compound
expression, e.g. "(bar+1)" in following code sample

void foo(void) {
    unsigned *bar = 0;
    *(bar+1) = 1;
}

I get the expected result

void foo(void) {
    unsigned *bar = 0;
    *DEREF_EXPR((bar+1)) = 1;
}

But when the subexpression is leaf token

void foo(void) {
    unsigned *bar = 0;
    *bar = 1;
}

I get

void foo(void) {
    unsigned *bar = 0;
    *DEREF_EXPR()bar = 1;
}

instead of expected

void foo(void) {
    unsigned *bar = 0;
    *DEREF_EXPR(bar) = 1;
}

I would assume that pSubExpr->getSourceRange().getBegin() and
pSubExpr->getSourceRange().getEnd() source locations should point BEFORE and
AFTER the subexpression, but it does not work as expected. In case of leaf token
subexpression both point to the same source location (before the token). What am
I doing wrong?

Thanks,

Petr










More information about the cfe-dev mailing list