threads / patch / 32851

patchUse __VA_ARGS__ for all of error's arguments

Subject: [PATCH] Use __VA_ARGS__ for all of error's arguments

## tl;dr

9 messages between Feb 7, 2013 and Feb 8, 2013. Diffs are folded; open one to read it.

replies: 8people: 4as markdown or json

Matt Kraai· Feb 7, 2013, 20:00 UTC · lore
From: Matt Kraai <matt.kraai@amo.abbott.com>

QNX 6.3.2 uses GCC 2.95.3 by default, and GCC 2.95.3 doesn't remove the comma if the error macro's variable argument is left out.

Instead of testing for a sufficiently recent version of GCC, make __VA_ARGS__ match all of the arguments. Since this should work on any C99-compliant compiler, also remove the tests around the macro definition.

Signed-off-by: Matt Kraai <matt.kraai@amo.abbott.com>
---
 git-compat-util.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
Show changes to git-compat-util.h +2 −7
diff --git a/git-compat-util.h b/git-compat-util.h
index cc2abee..df1681f 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -305,14 +305,9 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
 
 /*
  * Let callers be aware of the constant return value; this can help
- * gcc with -Wuninitialized analysis. We have to restrict this trick to
- * gcc, though, because of the variadic macro and the magic ## comma pasting
- * behavior. But since we're only trying to help gcc, anyway, it's OK; other
- * compilers will fall back to using the function as usual.
+ * gcc with -Wuninitialized analysis.
  */
-#if defined(__GNUC__) && ! defined(__clang__)
-#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
-#endif
+#define error(...) (error(__VA_ARGS__), -1)
 
 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
 extern void set_error_routine(void (*routine)(const char *err, va_list params));
-- 
1.8.1.GIT
Junio C Hamano· Feb 7, 2013, 21:05 UTC · re: Matt Kraai · lore

Re: [PATCH] Use __VA_ARGS__ for all of error's arguments

Matt Kraai <kraai@ftbfs.org> writes:
> -#if defined(__GNUC__) && ! defined(__clang__)
> -#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
> -#endif
> +#define error(...) (error(__VA_ARGS__), -1)

Before your change, we only define error() macro for GCC variants, but with your patch that no longer is the case. Does every compiler that compiles Git correctly today support this style of varargs macros?

John Keeping· Feb 7, 2013, 21:14 UTC · re: Junio C Hamano · lore

Re: [PATCH] Use __VA_ARGS__ for all of error's arguments

On Thu, Feb 07, 2013 at 01:05:19PM -0800, Junio C Hamano wrote:
Show 11 quoted lines
> Matt Kraai <kraai@ftbfs.org> writes:
> 
> > -#if defined(__GNUC__) && ! defined(__clang__)
> > -#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
> > -#endif
> > +#define error(...) (error(__VA_ARGS__), -1)
> 
> Before your change, we only define error() macro for GCC variants,
> but with your patch that no longer is the case.  Does every compiler
> that compiles Git correctly today support this style of varargs
> macros?

At the very least the "! defined(__clang__)" was only recently added [1], although it shouldn't be needed once Clang 3.3 is released.

[1] http://article.gmane.org/gmane.comp.version-control.git/213787
John
Matt Kraai· Feb 7, 2013, 21:24 UTC · re: Junio C Hamano · lore

Re: [PATCH] Use __VA_ARGS__ for all of error's arguments

On Thu, Feb 07, 2013 at 01:05:19PM -0800, Junio C Hamano wrote:
Show 11 quoted lines
> Matt Kraai <kraai@ftbfs.org> writes:
> 
> > -#if defined(__GNUC__) && ! defined(__clang__)
> > -#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
> > -#endif
> > +#define error(...) (error(__VA_ARGS__), -1)
> 
> Before your change, we only define error() macro for GCC variants,
> but with your patch that no longer is the case.  Does every compiler
> that compiles Git correctly today support this style of varargs
> macros?

I don't know and I don't think it's likely I can confirm this. I'll submit a new patch that just changes the definition, since I don't know of any problems other than mine with the current situation.

-- 
Matt
Matt Kraai· Feb 7, 2013, 21:30 UTC · re: Matt Kraai · lore
From: Matt Kraai <matt.kraai@amo.abbott.com>

QNX 6.3.2 uses GCC 2.95.3 by default, and GCC 2.95.3 doesn't remove the comma if the error macro's variable argument is left out.

Instead of testing for a sufficiently recent version of GCC, make __VA_ARGS__ match all of the arguments.

Signed-off-by: Matt Kraai <matt.kraai@amo.abbott.com>
---
 git-compat-util.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
Show changes to git-compat-util.h +2 −5
diff --git a/git-compat-util.h b/git-compat-util.h
index cc2abee..2e960a9 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -305,13 +305,10 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
 
 /*
  * Let callers be aware of the constant return value; this can help
- * gcc with -Wuninitialized analysis. We have to restrict this trick to
- * gcc, though, because of the variadic macro and the magic ## comma pasting
- * behavior. But since we're only trying to help gcc, anyway, it's OK; other
- * compilers will fall back to using the function as usual.
+ * gcc with -Wuninitialized analysis.
  */
 #if defined(__GNUC__) && ! defined(__clang__)
-#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
+#define error(...) (error(__VA_ARGS__), -1)
 #endif
 
 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
-- 
1.8.1.2.546.gfc9f004
Jeff King· Feb 8, 2013, 04:24 UTC · re: Matt Kraai · lore

Re: [PATCH] Use __VA_ARGS__ for all of error's arguments

On Thu, Feb 07, 2013 at 01:30:32PM -0800, Matt Kraai wrote:
Show 7 quoted lines
> From: Matt Kraai <matt.kraai@amo.abbott.com>
> 
> QNX 6.3.2 uses GCC 2.95.3 by default, and GCC 2.95.3 doesn't remove the
> comma if the error macro's variable argument is left out.
> 
> Instead of testing for a sufficiently recent version of GCC, make
> __VA_ARGS__ match all of the arguments.

Thanks, this looks better than the original (we do not assume a C99 compiler, so just doing this unconditionally would probably break some other older systems which do not use gcc).

Show 11 quoted lines
>  /*
>   * Let callers be aware of the constant return value; this can help
> - * gcc with -Wuninitialized analysis. We have to restrict this trick to
> - * gcc, though, because of the variadic macro and the magic ## comma pasting
> - * behavior. But since we're only trying to help gcc, anyway, it's OK; other
> - * compilers will fall back to using the function as usual.
> + * gcc with -Wuninitialized analysis.
>   */
>  #if defined(__GNUC__) && ! defined(__clang__)
> -#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
> +#define error(...) (error(__VA_ARGS__), -1)

Should you be dropping most of the comment like this? I would expect it to be more like:

  We have to restrict this trick to gcc, though, because we do not
  assume all compilers support variadic macros. But since...

Other than that, I think it is OK. The compiler will still catch "error()" with no arguments and generate the appropriate diagnostic (in fact, it is better, because the error is now passing too few args to a function, not to the macro).

-Peff
Matt Kraai· Feb 8, 2013, 04:39 UTC · re: Jeff King · lore

Re: [PATCH] Use __VA_ARGS__ for all of error's arguments

On Thu, Feb 07, 2013 at 11:24:28PM -0500, Jeff King wrote:
Show 5 quoted lines
> Should you be dropping most of the comment like this? I would expect it
> to be more like:
> 
>   We have to restrict this trick to gcc, though, because we do not
>   assume all compilers support variadic macros. But since...
I'll submit a new patch with this change tomorrow.
> Other than that, I think it is OK. The compiler will still catch
> "error()" with no arguments and generate the appropriate diagnostic (in
> fact, it is better, because the error is now passing too few args to a
> function, not to the macro).
Great, thanks for the review.
-- 
Matt
Matt Kraai· Feb 8, 2013, 15:09 UTC · re: Matt Kraai · lore
From: Matt Kraai <matt.kraai@amo.abbott.com>

QNX 6.3.2 uses GCC 2.95.3 by default, and GCC 2.95.3 doesn't remove the comma if the error macro's variable argument is left out.

Instead of testing for a sufficiently recent version of GCC, make __VA_ARGS__ match all of the arguments.

Signed-off-by: Matt Kraai <matt.kraai@amo.abbott.com>
---
 git-compat-util.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Show changes to git-compat-util.h +5 −5
diff --git a/git-compat-util.h b/git-compat-util.h
index cc2abee..b7eaaa9 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -305,13 +305,13 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
 
 /*
  * Let callers be aware of the constant return value; this can help
- * gcc with -Wuninitialized analysis. We have to restrict this trick to
- * gcc, though, because of the variadic macro and the magic ## comma pasting
- * behavior. But since we're only trying to help gcc, anyway, it's OK; other
- * compilers will fall back to using the function as usual.
+ * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
+ * because some compilers may not support variadic macros. Since we're only
+ * trying to help gcc, anyway, it's OK; other compilers will fall back to
+ * using the function as usual.
  */
 #if defined(__GNUC__) && ! defined(__clang__)
-#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
+#define error(...) (error(__VA_ARGS__), -1)
 #endif
 
 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
-- 
1.8.1.2.546.g90a97a4
Jeff King· Feb 8, 2013, 15:54 UTC · re: Matt Kraai · lore

Re: [PATCH] Use __VA_ARGS__ for all of error's arguments

On Fri, Feb 08, 2013 at 07:09:28AM -0800, Matt Kraai wrote:
Show 24 quoted lines
> From: Matt Kraai <matt.kraai@amo.abbott.com>
> 
> QNX 6.3.2 uses GCC 2.95.3 by default, and GCC 2.95.3 doesn't remove the
> comma if the error macro's variable argument is left out.
> 
> Instead of testing for a sufficiently recent version of GCC, make
> __VA_ARGS__ match all of the arguments.
>
> [...]
>
>  /*
>   * Let callers be aware of the constant return value; this can help
> - * gcc with -Wuninitialized analysis. We have to restrict this trick to
> - * gcc, though, because of the variadic macro and the magic ## comma pasting
> - * behavior. But since we're only trying to help gcc, anyway, it's OK; other
> - * compilers will fall back to using the function as usual.
> + * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
> + * because some compilers may not support variadic macros. Since we're only
> + * trying to help gcc, anyway, it's OK; other compilers will fall back to
> + * using the function as usual.
>   */
>  #if defined(__GNUC__) && ! defined(__clang__)
> -#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
> +#define error(...) (error(__VA_ARGS__), -1)
Acked-by: Jeff King <peff@peff.net>
Thanks.
-Peff

← back to recent threads