[cfe-dev] Macro expansion weirdness (or bug?)

Abramo Bagnara abramo.bagnara at gmail.com
Thu Mar 25 12:08:18 PDT 2010


Il 25/03/2010 19:34, Chris Lattner ha scritto:
> 
> On Mar 25, 2010, at 10:51 AM, Abramo Bagnara wrote:
> 
>>
>> Expanding this snippet:
>>
>> #define n(v) v
>> #define l X m
>> #define m l
>> n(m)
>>
>> clang preprocessor generate three MacroExpands callbacks in this order:
>>
>> n, m, l
>>
>> As it can be seen no other expansion are attempted (so to avoid
>> recursivity) and everything is fine.
>>
>> If I remove the X as in:
>>
>> #define n(v) v
>> #define l m
>> #define m l
>> n(m)
>>
>> the generated MacroExpands callbacks are in order:
>>
>> n, m, l, m, l
>>
>> This seems very strange to me and I guess it a symptom of a bug
>> somewhere in macro expansion machinery.
> 
> This is expected and is how macro expansion (which is a horrible
> part of the standard works). However, it could be a problem where the
> callback isn't invoked as you'd expect it to *shrug*.

At a first moment reading c99 6.10.3.4p2 I was under the impression that
the second sample was right and the first one was wrong (i.e. a first
time m and l are expanded due to macro "n" argument expansions and a
second time they are expanded due to body re-expansion), but looking at
gcc -E, icc -E and clang -E output for first example I've concluded that
my interpretation was wrong (otherwise the X would appear twice in the
output).

This implies that probably, according to standard (or at least to all
these implementations) the macros m and l are expanded only during
argument expansion and then the resulting token m is marked "expansion
disabled" and then it's not furtherly expanded during body re-scanning.

But generated MacroExpands callback for the second sample seems to
contradict all this.

>From your message it seems you're expecting *more* callback invocations
in the first sample, while I'd expect *less* callback invocations in the
second sample.

Could you explain better your position?

-- 
Abramo Bagnara

Opera Unica                          Phone: +39.0546.656023
Via Borghesi, 16
48014 Castel Bolognese (RA) - Italy



More information about the cfe-dev mailing list