[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: error: Support the compiler's control flow analysis better (was: cop
From: |
Bruno Haible |
Subject: |
Re: error: Support the compiler's control flow analysis better (was: copy-file: Silence gcc warnings) |
Date: |
Sat, 03 Jun 2023 12:00:32 +0200 |
Paul Eggert wrote:
> > How about changing this to use __builtin_constant_p instead of
> > __OPTIMIZE__? I.e., use the comma expression if __builtin_constant_p
> > (status), and use the statement expression otherwise.
>
> I gave that a shot by installing the attached.
Oh, I see now what you meant. That's clever. So clever that it
deserves comments :-)
2023-06-03 Bruno Haible <bruno@clisp.org>
error: Improve comments.
* lib/error.in.h (__gl_error_call): Add more comments.
diff --git a/lib/error.in.h b/lib/error.in.h
index 279258f63e..ef4b3c3815 100644
--- a/lib/error.in.h
+++ b/lib/error.in.h
@@ -50,13 +50,21 @@
#endif
/* Helper macro for supporting the compiler's control flow analysis better.
- It evaluates its arguments only once. It uses __builtin_constant_p
- and comma expressions to work around GCC false positives.
+ It evaluates its arguments only once.
Test case: Compile copy-file.c with "gcc -Wimplicit-fallthrough". */
#ifdef __GNUC__
+/* Use 'unreachable' to tell the compiler when the function call does not
+ return. */
# define __gl_error_call1(function, status, ...) \
((function) (status, __VA_ARGS__), \
(status) != 0 ? unreachable () : (void) 0)
+/* If STATUS is a not a constant, the function call may or may not return;
+ therefore -Wimplicit-fallthrough will produce a warning. Use a compound
+ statement in order to evaluate STATUS only once.
+ If STATUS is a constant, we don't use a compound statement, because that
+ would trigger a -Wimplicit-fallthrough warning even when STATUS is != 0,
+ when not optimizing. This causes STATUS to be evaluated twice, but
+ that's OK since it does not have side effects. */
# define __gl_error_call(function, status, ...) \
(__builtin_constant_p (status) \
? __gl_error_call1 (function, status, __VA_ARGS__) \