This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC CPP not expanding macro correctly?
- From: Segher Boessenkool <segher at koffie dot nl>
- To: AWLaFramboise at aol dot com
- Cc: gcc at gcc dot gnu dot org
- Date: Wed, 23 Apr 2003 11:31:58 +0200
- Subject: Re: GCC CPP not expanding macro correctly?
- References: <2B34188D.09008BF3.E8E247C1@aol.com>
AWLaFramboise at aol dot 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