[cfe-dev] AST UnaryOperator subexpression

Petr Kostka packyr at atlas.cz
Wed Apr 23 11:49:34 PDT 2008


Chris Lattner <clattner at ...> writes:

> > 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?
> 
> Actually they don't.  The trick here is that "end" points to the  
> *start* of the last token in the range.  This makes construction of  
> the source ranges much more clean and simple, but pushes some logic  
> into the clients.  Basically, to insert text after the end of the  
> range, you have to add the length of the last token.  Luckily, this is  
> really easy to get :)

OK, makes sense. I was confused because my sample inserted the same token as was
already present at the end (")") so I did not notice something the difference.
 
>    // If E is a macro expansion, we want the instantiation location.   
> You should determine how you want to handle this.  There are many  
> possible strategies.
>    E = SM.getLogicalLoc(E);
> 
>    // Add the size of the end token.
>    E = E.getFileLocWithOffset(Lexer::MeasureTokenLength(E, SM));
>    mRewriter.InsertCStrAfter(E, ")");
> 
> Please let me know if that doesn't work.

Simple fix and works as expected. I guess I was looking in wrong places when
looking for relevant code samples before. 

> 
> -Chris
> 

Thanks for your help!

-Petr







More information about the cfe-dev mailing list