Preprocessor bug

Neil Booth neilb@earthling.net
Tue Nov 21 04:00:00 GMT 2000


John Hinke wrote:-

> #define name2(a,b)  tmpname2(a,b)
> #define tmpname2(a,b) a##b
> #define declare(Class,type)  name2(Class,declare)(type)
> #define MyClassdeclare(type) name2(type,MyClass)
> declare(MyClass,int)

Yes, this is a longstanding bug fix in the current preprocessor.

> Any comments?  If I'm doing something wrong, please let me know.  This code
> has been working for many years now.  Also, other compilers that I tested
> produce the correct (2.95.2) output.

That doesn't make the other compilers, including 2.95.2, correct <g>.
It's easy to get this wrong with the stack-based expansion approach that
I imagine most macro expanders use.  However, if you can show me what's
wrong with my reasoning, I'll revert the behaviour.

Here is the expansion stack as I and GNU CPP see it, where an extra
level of indentation indicates an extra level of nestedness:-

declare(MyClass, int)
  name2(MyClass,declare)(int)
    tmpname2(MyClass,declare)(int)
      MyClassDeclare(int)
        name2(int,MyClass)

As you can see, name2 is nested 3 levels below a previous expansion of
itself, so it shouldn't expand.  I don't think this is at all
ambiguous in the standard.

This is a more complicated example of

#define foo(x) foo
foo(foo)(x)

which I believe most would agree expands to "foo(x)" and not "foo".

Neil.


More information about the Gcc-bugs mailing list