***SPAM*** Re: [ofa-general] [PATCH V2 1/3] Create a new library libibnetdisc

Ira Weiny weiny2 at llnl.gov
Tue Dec 23 16:41:41 PST 2008


On Tue, 23 Dec 2008 11:43:31 -0700
Jason Gunthorpe <jgunthorpe at obsidianresearch.com> wrote:

> On Tue, Dec 23, 2008 at 10:29:02AM -0800, Al Chu wrote:
> 
> > > +#define	IBND_DEBUG(str, args...) \
> > > +	if (ibdebug) printf("%s:%d; "str, __FILE__, __LINE__, ##args)
> > > +#define	IBND_ERROR(str, args...) \
> > > +	fprintf(stderr, "%s:%d; "str, __FILE__, __LINE__, ##args)
> > 
> > I believe the "args ..." and "##args" are only for gcc.  Not sure how
> > much this portability issue matters for OFED.  Personally, I always
> > do
> 
> Right that format is an obsolete gcc extension. Ira, it should be
> 
>      #define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)
> 
> Which is how C99 standardized varadic macros.
> 

Ok, I think this C99 compliant...  I had to break the call up into 2 printf's.

16:30:20 > git diff
diff --git a/infiniband-diags/libibnetdisc/include/infiniband/ibnetdisc.h b/infiniband-diags/libibnetdisc/in
index 737ffac..773c64b 100644
--- a/infiniband-diags/libibnetdisc/include/infiniband/ibnetdisc.h
+++ b/infiniband-diags/libibnetdisc/include/infiniband/ibnetdisc.h
@@ -43,10 +43,16 @@
 #define HASHGUID(guid) ((uint32_t)(((uint32_t)(guid) * 101) ^ ((uint32_t)((guid) >> 32) * 103)))
 #define HTSZ 137
 
-#define        IBND_DEBUG(str, args...) \
-       if (ibdebug) printf("%s:%d; "str, __FILE__, __LINE__, ##args)
-#define        IBND_ERROR(str, args...) \
-       fprintf(stderr, "%s:%d; "str, __FILE__, __LINE__, ##args)
+#define        IBND_DEBUG(...) \
+       if (ibdebug) { \
+               printf("%s:%d; ", __FILE__, __LINE__); \
+               printf(__VA_ARGS__); \
+       }
+#define        IBND_ERROR(...) \
+       { \
+               fprintf(stderr, "%s:%d; ", __FILE__, __LINE__); \
+               fprintf(stderr, __VA_ARGS__); \
+       }
 
 /** =========================================================================
  * ENUM definitions

Otherwise calls to the macro with only 1 parameter fail to compile.  It seems
that GCC has a couple of extensions [*] but the above should be C99 compliant
without GCC extensions.  Does that seem right?

Ira

[*] Ref: http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

   You can have named arguments as well as variable arguments in a variadic
   macro. We could define eprintf like this, instead:

     #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)

   This formulation looks more descriptive, but unfortunately it is less
   flexible: you must now supply at least one argument after the format string.
   In standard C, you cannot omit the comma separating the named argument from
   the variable arguments. Furthermore, if you leave the variable argument
   empty, you will get a syntax error, because there will be an extra comma
   after the format string.

     eprintf("success!\n", );
          ==> fprintf(stderr, "success!\n", );

   GNU CPP has a pair of extensions which deal with this problem. First, you
   are allowed to leave the variable argument out entirely:

     eprintf ("success!\n")
          ==> fprintf(stderr, "success!\n", );

   Second, the `##' token paste operator has a special meaning when placed
   between a comma and a variable argument. If you write

     #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)




More information about the general mailing list