On 05/10/2011 09:29 AM, Eric Blake wrote:
> ##__VA_ARGS__ is a GNU extension; it does not work on all C99
compilers.
>
> I'm worried that this is not portable.
But, we CAN do:
#ifdef __GNUC__
# define high_level(fmt, ...) low_level(fmt, ##__VA_ARGS__)
#else
suitably expanded (and we can also place a cap on the maximum number of
arguments we ever expect to see in VIR_DEBUG, rather than going all the
way to the C99 limit of 63).
Even simpler, using ideas from
https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/
(and actually tested this time):
#define ARG_8(_0, _1, _2, _3, _4, _5, _6, _7, ...) _7
#define HAS_COMMA(...) ARG_8(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 0)
# define f(...) f_(f, HAS_COMMA(__VA_ARGS__), __VA_ARGS__)
# define f_(a, b, ...) f__(a, b, __VA_ARGS__)
# define f__(a, b, ...) a##b(__VA_ARGS__)
# define f0(x) printf("%s", x)
# define f1(x, ...) printf(x, __VA_ARGS__)
I'll work up a followup patch along these lines; it doesn't seem too
impossible to guarantee C99 portability for the four macros.
Yeah, it's gross, but we limit the grossness to one header, and
the rest
of the code is more maintainable as a result.
--
Eric Blake eblake(a)redhat.com +1-801-349-2682
Libvirt virtualization library
http://libvirt.org