#826 variadic macros won't accept zero arguments

open
nobody
5
2012-12-21
2007-05-26
Jeremy
No

It is possible to define a variadic macro that has no fixed arguments, but impossible to call such a macro with zero arguments. Here is a simple test case set up for GCC:

---------------------------------------
#define annotate(...) %feature("java:methodmodifiers","@Annotation("#__VA_ARGS__") public")

annotate(one=1,two=2) methodTwo;
annotate(one=1) methodOne;
annotate() methodZero;
---------------------------------------

The stringification of __VA_ARGS__ is there to make GCC happy, and GCC turns the above into the expected:

---------------------------------------
%feature("java:methodmodifiers","@Annotation(""one=1,two=2"") public") methodTwo;
%feature("java:methodmodifiers","@Annotation(""one=1"") public") methodOne;
%feature("java:methodmodifiers","@Annotation("""") public") methodZero;
---------------------------------------

Not that you should just take my word for it, but I did check the C99 standard, and I believe that behavior is conformant. Contrast that with what the SWIG preprocessor does... First get rid of the unnecessary stringification so the output is a little more pretty:

---------------------------------------
#define annotate(...) %feature("java:methodmodifiers","@Annotation(__VA_ARGS__) public")

annotate(one=1,two=2) methodTwo;
annotate(one=1) methodOne;
annotate() methodZero;
---------------------------------------

And we see that it fails for the last test case:

---------------------------------------
%feature("java:methodmodifiers","@Annotation(one=1,two=2) public") methodTwo;
%feature("java:methodmodifiers","@Annotation(one=1) public") methodOne;
annotate methodZero;
---------------------------------------

Sorry I don't have time to chase this down, but I believe I found a likely culprit at line 771 of cpp.c (Revision 9639):

---------------------------------------
770 /* If the macro expects arguments, but none were supplied, we leave it in place */
771 if (!args && (margs) && Len(margs) > 0) {
772 return NewString(name);
773 }
---------------------------------------

From my test case code above you can see that I am trying to write a helper macro to decorate the generated Java methods with some of those Java 1.5 Annotation things, which can accept any number (including zero) of arguments. But this bug is making it impossible to use the obvious syntax.

Discussion

  • William Fulton

    William Fulton - 2010-09-01

    I've attached a first attempt at fixing this, but am moving onto more pressing problems for now. The patch fixes the problem but it breaks the python test-suite and so it isn't really any good. More precisely, I think the preprocessor needs modifying to internally differentiate between a macro with zero arguments and a macro that does not take any arguments, so that the following errors out:

    #define METHODNAME method1
    #define MYMACRO() void METHODNAME() {}
    MYMACRO

    If this is fixed first, I think the varargs macro and zero arguments will be easier to fix.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks