This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC CPP not expanding macro correctly?


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




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]