new cpp preprocessor fails

Neil Booth neilb@earthling.net
Wed Nov 15 10:50:00 GMT 2000


Hi Helmut,

CPP is in fact correct, as I explain below.

First, let's simplify your example:

  #define M 8
  #define T2H_8(X) endian_t2h_8(X)
  #define CONCAT2(a,b)	 a##b
  #define XCONCAT2(a,b)     CONCAT2(a,b)
  #define unsigned_M XCONCAT2(unsigned_,M)
  #define T2H_M XCONCAT2(T2H_,M)
  T2H_M (*(unsigned_M*) sim_core_translate (mapping, addr));

CPP expands the last line to

  endian_t2h_8(*(XCONCAT2(unsigned_,8)*) sim_core_translate (mapping, addr));

as follows.  I use indentation to indicate successive levels of
macro nesting:-

1) T2H_M
2)   XCONCAT2 (T2H_, M)		// M -> 8 with argument pre-expansion
3)     CONCAT2 (T2H_, 8)
4)       T2H_8

Reading in the '(' and the parameters from the source file:-

5)         endian_t2h_8 (*(unsigned_M*) sim_core_translate (mapping, addr))

For argument pre-expansion, we are interested in unsigned_M

5a)        unsigned_M -> XCONCAT2(unsigned_,M)

And now you see that XCONCAT2 is a disabled macro, because it has been
encountered at depth 5 and was disabled deeper than level 2.

Older versions of GCC expand it differently, but this is a bug in
those versions that has been fixed in recent preprocessors.

Your example is a more complicated version of

#define foo(x) foo
foo(foo)(x)

which expands to "foo(x)" and not "foo" since foo is disabled for the
second nested expansion.

Neil.


More information about the Gcc-bugs mailing list