[cfe-dev] Check if a location comes from begin/end of macro argument expansion

Argyrios Kyrtzidis kyrtzidis at apple.com
Thu Oct 11 11:35:14 PDT 2012


Hi Abramo,

On Oct 10, 2012, at 2:36 PM, Abramo Bagnara <abramo.bagnara at bugseng.com> wrote:

> 
> I'm struggling to find a way to detect if a given macro expanded
> location is at the begin (or the end) of a macro argument expansion.
> 
> e.g. in this program
> 
> #define M(z) 0 + z + 0
> #define N(x) M((x))
> void h() {
>  N(1);
> }
> 
> I'd need to detect that source location of integer literal 1 is at the
> begin (and the end) of expansion of argument x of macro N.
> 
> I think we should have enough info to do that (I've been able to reach a
> similar result concerning macro expansion begin/end), but I'm still
> unable to achieve always the correct final result.

This is not simple/easy to do currently.

I think you can take advantage of the fact that the spelling location of a macro function expansion will always contain the parens, e.g. you'll be able to lex "MACRONAME ( .... )" (similar to how Preprocessor::ReadFunctionLikeMacroArgs does it) and get the token locations between the parens.
In your example you'll get the token locations for:
'N' '(' '1' ')'   
and
'M' '(' '(' 'x' ')' ')'

you'll be able to lex the spelling locations and apply the offsets to the the actual locations in the expanded range of the macro contents.
After that you should be able to figure out if the location of '1' came from what token location inside the macro arguments. Also a bonus advantage will be being able to determine which exactly argument index it came from.

Did this make sense ?

-Argyrios

> 
> I'd be very happy if Argyrios or another SourceManager guru could shed
> some light on that.
> 
> -- 
> Abramo Bagnara
> 
> BUGSENG srl - http://bugseng.com
> mailto:abramo.bagnara at bugseng.com




More information about the cfe-dev mailing list