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: preprocessing question


One explanation could be:

----- Original Message ----- From: "Jan Beulich" <jbeulich@novell.com>
To: <gcc@gcc.gnu.org>
Sent: Monday, September 25, 2006 11:23 AM
Subject: preprocessing question



Can anyone set me strait on why, in the following code fragment

int x(unsigned);

struct alt_x {
unsigned val;
};

#define x        alt_x
#define alt_x(p) x(p+1)

since "x" is already defined as a object-like macro, "x" is replaced by "alt_x", so this essentially is: #define alt_x(p) alt_x(p+1)


Since C99 (6.10.3.9) describes:
... defines an object-like macro that causes each subsequent instance of the macro name to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive.



Since the "x" in the replacement list of "alt_x" macro definition is a "subsequent instance of the macro name", so it is replaced by "x"'s replaement list, which is "alt_x".




int test(struct x *p) { return x(p->val); }

the function invoked in test() is alt_x (rather than x)? I would have
expected that the preprocessor
- finds that x is an object like macro, and replaces it with alt_x
- finds that alt_x is a function-like macro and replaces it with x(...)
- finds that again x is an object like macro, but recognizes that it
already participated in expansion, so doesn't replace x by alt_x a
second time.

Our compiler team also considers this misbehavior, but since I
tested three other compilers, and they all behave the same, I
continue to wonder if I'm mis-reading something in the standard.

Thanks a lot, Jan



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