This is the mail archive of the gcc-bugs@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]

Re: cpp and compliance to C99


Hi Thomas,

I've looked at this again, and now I believe the behaviour is
unspecified according to the standard (thanks to Zack for pointing
this out in a different context in a separate mail).

Appendix J to the C99 standard states (under "unspecified
behaviour"):-

 - When a fully expanded macro replacement list contains a
   function-like macro name as its last preprocessing token and the
   next preprocessing token from the source file is a (, and the fully
   expanded replacement of that macro ends with the name of the first
   macro and the next preprocessing token from the source file is
   again a (, whether that is considered a nested replacement
   (6.10.3).

Your examples fall under this clause.  Here are four examples that I
think are affected by this.  Two are yours, and the other two are in
the testsuite.  The text above doesn't cover the glue case, but I
think it is intended to and the author just hadn't thought of it.

#define glue(x, y) x ## y
#define xglue(x,y) glue (x,y)
glue (xgl, ue) (1, 2)

#define c(x) d
#define d(x) c(x)
c(c)(c)(c)

#define a(x) b(
#define b(x) a(
a(a)x)x)

#define f h
#define h(a) a+f
f(1)(2)

The question is, in the words of the standard, are these expansions a
single "nested" macro expansion, or are they not nested?  Treating
them as not nested gives (modulo newlines):-

12
/home/neil/test1.c:12: unterminated argument list invoking macro "b"
d(c)
b
1+h(2)

Treating them as nested, and thus disabling the nested expansions,
gives:-

glue (1,2)
c(c)(c)
a(x)
1+f(2)

Both of these outputs are from a new macro expander I have on my hard
drive.  Getting one behaviour or the other is as simple as moving one
line of code.

The question is, which behaviour do we want for GCC?  Older GCCs treat
them as not nested, giving the first set of results above (well, aprt
from a segfault).  I think Zack prefers to maintain old GCC behaviour.
However, I believe the latter is the more logical behaviour, and what
I expected before Zack pointed out Appendix J to me.  Otherwise I
think we have the problem of worrying when something is a nested
expansion or not.  If we just say anything that could possibly be
regarded as nested will be, it seems less ambiguous (and avoids the
problems with the "glue" example being overlooked in the standard).

What do others think?  Jeff?  Jamie?  Dave?  Input greatly
appreciated.

Neil.

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