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


Thomas Pornin wrote:-

> On the following code:
> #define a(x) b
> #define b(x) a(x)
> a(a)(a)(a)
> 
> the cpp gives the following result:
> b(a)
> 
> which seems wrong to me (it performs one extra macro replacement, which
> should not have been done according to the nesting macro rule). It should
> have given: a(a)(a)
> 
> On the following code:
> #define a(x) b(
> #define b(x) a(
> a(a)x)x)
> 
> the cpp gives the following result:
> b
> 
> which seems equally wrong (it should have given: a(x)).

Indeed.

The current macro expander is an embarassing mess, which is entirely
my fault.  I'm redoing it to use the same algorithm as the old
text-based one, but on tokens instead, and in the process of tidying
up loose ends.  This is what code on my local drive gives:-

bash-2.04$ cat ~/bug.c
#define apply(...)   apply2 (__VA_ARGS__)  
#define half(x)      ((x) / 2)
#define apply2(f,x)  f (x)
apply (half, X)

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

#define c(x) d(
#define d(x) c(
c(c)x)x)
 
bash-2.04$ ./cpp0 ~/bug.c
# 1 "/home/neil/bug.c"



((X) / 2)



/home/neil/bug.c:14: unterminated argument list invoking macro "d"
b(x)



d
bash-2.04$

So it's getting the first two right (the first one is a bug Jamie
submitted a couple of weeks ago).  Interestingly, I don't think any
GCC version gets the last one right (hence the segfault in tradcpp,
and the result below).  I'm using the same algorithm as 2.95.2 which
is probably why it fails.  This is with gcc 2.95.2, which doesn't
understand __VA_ARGS__ (2.7.2.3 is similar):-

bash-2.04$ /usr/bin/cpp ~/bug.c
/home/neil/bug.c:12: unterminated macro call
/home/neil/bug.c:14: macro `c' used with too many (46) args
/home/neil/bug.c:14: macro `d' used with too many (5) args
/home/neil/bug.c:14: macro `c' used with too many (13) args
/home/neil/bug.c:14: macro `d' used with too many (337) args
/home/neil/bug.c:14: macro `c' used with too many (76) args
/home/neil/bug.c:14: macro `d' used with too many (86) args
/home/neil/bug.c:14: macro `c' used with too many (5) args
/home/neil/bug.c:14: macro `d' used with too many (37) args
/home/neil/bug.c:14: macro `c' used with too many (86) args
/home/neil/bug.c:14: macro `d' used with too many (90) args
/home/neil/bug.c:14: macro `c' used with too many (104) args
/home/neil/bug.c:14: macro `d' used with too many (86) args
/home/neil/bug.c:14: macro `c' used with too many (58) args
/home/neil/bug.c:14: macro `d' used with too many (112) args
/home/neil/bug.c:14: macro `c' used with too many (112) args
cpp: Internal compiler error: program cpp got fatal signal 11

Wow!

Neil.

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