Preprocessor bug
Martin Sebor
sebor@roguewave.com
Tue Nov 21 10:59:00 GMT 2000
Neil Booth wrote:
>
> 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".
Yes, although I don't agree that the two examples exploit the same
feature. You're talking about 6.10.3.4, p2, which is not relevant in
John's example. Here's a simplified testcase which I'm pretty sure is
well-formed and should produce, (what else?), foobar:
#define __paste(a,b) a##b
#define paste(a,b) __paste(a, b)
#define declfoo(a) paste(foo, a)
#define declare(a, b) paste(decl, a)(b)
declare(foo, bar)
Regards
Martin
More information about the Gcc-bugs
mailing list