Re: [PATCH] tools/nolibc: compiler: add macro __nolibc_fallthrough
From: Willy Tarreau
Date: Sun Sep 29 2024 - 23:34:13 EST
Hi Thomas,
On Sun, Sep 29, 2024 at 11:49:57PM +0200, Thomas Weißschuh wrote:
> Recent version of GCC and clang gained -Wimplicit-fallthrough,
> warning about implicit fall-through between switch labels.
> As nolibc does not control the compilation flags, this can trigger
> warnings for when built by the user.
> Make use of the "fallthrough" attribute to explicitly annotate the
> expected fall-throughs and silence the warning.
Good idea!
> +#if __nolibc_has_attribute(fallthrough)
> +# define __nolibc_fallthrough __attribute__((fallthrough))
> +#else
> +# define __nolibc_fallthrough
> +#endif /* __nolibc_has_attribute(fallthrough) */
In haproxy we're prepending a "do { } while (0)" statement at the
beginning of the macro, first to make sure to have a statement for
the case where it's not supported, and because gcc-7 will emit a
warning if placed after a label (and clang will even error in this
case). I'm covering all known cases with the following:
#if __has_attribute(fallthrough)
# define __fallthrough do { } while (0); __attribute__((fallthrough))
#else
# define __fallthrough do { } while (0)
#endif
I think you should do the same here since, as you mentioned, we have
no control over the user's toolchain.
Thanks,
Willy