Re: [PATCH 1/2] inline constant return from error() function

3 messages, 3 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH 1/2] inline constant return from error() function

From: Junio C Hamano <hidden>
Date: 2016-06-15 23:01:02

Jeff King [off-list ref] writes:
Commit e208f9c introduced a macro to turn error() calls
into:

  (error(), -1)

to make the constant return value more visible to the
calling code (and thus let the compiler make better
decisions about the code).

This works well for code like:

  return error(...);

but the "-1" is superfluous in code that just calls error()
without caring about the return value. In older versions of
gcc, that was fine, but gcc 4.9 complains with -Wunused-value.

We can work around this by encapsulating the constant return
value in a static inline function, as gcc specifically
avoids complaining about unused function returns unless the
function has been specifically marked with the
warn_unused_result attribute.
That's kind of W*A*T magic, and I generally try to avoid magic, as
long as it solves your "can we make both -O2 with new compilers and
-O3 happy?" I wouldn't complain ;-)
We also use the same trick for config_error_nonbool and
opterror, which learned the same error technique in a469a10.

Reported-by: Felipe Contreras <redacted>
Signed-off-by: Jeff King <redacted>
---
On Mon, May 05, 2014 at 05:29:38PM -0400, Jeff King wrote:
quoted
I cannot think of any other way to make the compiler aware of the
constant value, but perhaps somebody else is more clever than I am.
This came to me in a dream, and seems to work.

quoted hunk
 cache.h           | 2 +-
 git-compat-util.h | 6 +++++-
 parse-options.h   | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/cache.h b/cache.h
index 107ac61..e2f12b0 100644
--- a/cache.h
+++ b/cache.h
@@ -1272,7 +1272,7 @@ extern int git_env_bool(const char *, int);
 extern int git_config_system(void);
 extern int config_error_nonbool(const char *);
 #if defined(__GNUC__) && ! defined(__clang__)
-#define config_error_nonbool(s) (config_error_nonbool(s), -1)
+#define config_error_nonbool(s) (config_error_nonbool(s), const_error())
 #endif
 extern const char *get_log_output_encoding(void);
 extern const char *get_commit_output_encoding(void);
diff --git a/git-compat-util.h b/git-compat-util.h
index f6d3a46..b4c437e 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -331,7 +331,11 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
  * using the function as usual.
  */
 #if defined(__GNUC__) && ! defined(__clang__)
-#define error(...) (error(__VA_ARGS__), -1)
+static inline int const_error(void)
+{
+	return -1;
+}
+#define error(...) (error(__VA_ARGS__), const_error())
 #endif
 
 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
diff --git a/parse-options.h b/parse-options.h
index 3189676..2f9be96 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -177,7 +177,7 @@ extern NORETURN void usage_msg_opt(const char *msg,
 extern int optbug(const struct option *opt, const char *reason);
 extern int opterror(const struct option *opt, const char *reason, int flags);
 #if defined(__GNUC__) && ! defined(__clang__)
-#define opterror(o,r,f) (opterror((o),(r),(f)), -1)
+#define opterror(o,r,f) (opterror((o),(r),(f)), const_error())
 #endif
 
 /*----- incremental advanced APIs -----*/

Re: [PATCH 1/2] inline constant return from error() function

From: Jeff King <hidden>
Date: 2016-06-15 23:01:02

On Tue, May 06, 2014 at 03:29:37PM -0700, Junio C Hamano wrote:
quoted
We can work around this by encapsulating the constant return
value in a static inline function, as gcc specifically
avoids complaining about unused function returns unless the
function has been specifically marked with the
warn_unused_result attribute.
That's kind of W*A*T magic, and I generally try to avoid magic, as
long as it solves your "can we make both -O2 with new compilers and
-O3 happy?" I wouldn't complain ;-)
I agree it's rather magical, but I think it's something we can count on.
Certainly turning on warn_unused_result for every function would be a
catastrophe for most code bases, and I don't expect gcc to do it. It's
possible it would eventually grow smart to say "eh, I inlined this and
realized that you don't use the return value", but I think that would be
similarly a bad idea.

And it does work with -O2 and -O3 with both gcc-4.9 and clang in my
tests.

-Peff

Re: [PATCH 1/2] inline constant return from error() function

From: Felipe Contreras <hidden>
Date: 2016-06-15 23:01:06

Junio C Hamano wrote:
That's kind of W*A*T magic, and I generally try to avoid magic, as
long as it solves your "can we make both -O2 with new compilers and
-O3 happy?" I wouldn't complain ;-)
In case anybody is looking for a non-hacky way of doing this that
doesn't depend on the gcc version of the week, this is how I silenced
the warnings in git-fc:

https://github.com/felipec/git/commit/e14ca39ba0092f0305d5fb347e20b829f736b29b

-- 
Felipe Contreras
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help