GCC CPP not expanding macro correctly?

Segher Boessenkool segher@koffie.nl
Thu Apr 24 03:45:00 GMT 2003


AWLaFramboise@aol.com wrote:
> #define FIRST() FUNC
> #define SECOND() ()
> #define FUNC() 0
> int main() {
>   return FIRST()SECOND();
> }
> -----
> After preprocessing (translation stage 4), this code looks like (according to gcc 3.2.2 with gcc -E):
> int main() {
>   return FUNC();
> }
> 
> Question is: Why does this code not expand to this?
> int main() {
>   return 0;
> }

Because the preprocessor never sees  FUNC()  ; it sees
FUNC SECOND()  and decides (correctly) this isn't an
instance of the function-like macro FUNC() (because
there isn't a left parenthesis).  After it has expanded
SECOND() it doesn't go back to trying FUNC().

> Adding another layer of indirection or re-arranging #define's
  > do not seem to help.

Try this:

-- 8< --
#define FIRST() FUNC
#define SECOND() ()
#define FUNC() 0
#define CLUNK(x, y) x y
int main() {
    return CLUNK(FIRST(), SECOND());
}
-- 8< --

Yes it's ugly, and it fails on some compilers.  Not on GCC, though ;)

> Is this a bug?

Nope.


Segher




More information about the Gcc mailing list