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

cpp(gcc) and recursive macros


Hi,

I have a problem with understanding how gcc (cpp) handles recursive
macros. The old-style handling is not to care about them (if recursive,
there will be endless loops); the new style is once a macro is expanded,
its recursive expansion is suppressed in the resulting text. Both is
clear. gcc works in the later style by default; all this so far is
documented nicely in info pages. But sometimes it chooses to fall back
to old-style mode, expanding recursively. Can you help me what
conditions must be fulfilled to this happen? I peeked into gcc sources,
but I failed to find the *reason*.

Here is an example; I am concerned about why name2 is expanded
recursively in the second example.

// Example 1
#define cat(a,b) a##b
#define CAT(a,b) cat(a,b)

CAT(C,AT(x,y))
// Recursive expansion of CAT is not done.
// Result is "CAT(x,y)", as expected.

// Example 2
#define name2(a, b) _rwname2(a,b)
#define _rwname2(a, b) a##b
#define RWGQueue(type) name2(type,RWGQueue)
#define declare(Class, type) name2(Class,declare)(type)
#define RWGQueuedeclare(type) class RWGQueue(type);

declare(RWGQueue, RWCollectable)
// name2 is recursive, but expanded - why?
// Result is "class RWCollectableRWGQueue;"


I'd expect the following to happen:

declare(RWGQueue, RWCollectable)
name2(RWGQueue, declare)(RWCollectable)
	// From this point, I enclose the token sequence that is a
	// result of expanding name2 betwen stars.
*_rwname2(RWGQueue, declare)*(RWCollectable)
*RWGQueuedeclare(RWCollectable)*
*class RWGQueue(RWCollectable)*
*class name2(RWCollectable, RWGQueue);*
	// Now why name2 isn't suppressed?


(I tried to make a less complicated example, but everything I tried
was working as expected, except this magic one).

I tried to figure out who wrote cpp, to ask him; but I did not succeeded
with that. If you know a competent preson in this topic, who is willing
to give his e-mail, please let me know.

Thank you,
Gabor Varga


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