From: Willy Tarreau Date: Sun, 13 Nov 2022 11:21:22 +0000 (+0100) Subject: BUILD: compiler: define a __fallthrough statement for switch/case X-Git-Tag: v2.7-dev9~99 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1f344c0f30078daf178b4b17dc642540de4ed6ca;p=thirdparty%2Fhaproxy.git BUILD: compiler: define a __fallthrough statement for switch/case When the code is preprocessed first and compiled later, such as when built under distcc, a lot of fallthrough warnings are emitted because the preprocessor has already stripped the comments. As an alternative, a "fallthrough" attribute was added with the same compilers as those which started to emit those warnings. However it's not portable to older compilers. Let's just define a __fallthrough statement that corresponds to this attribute on supported compilers and only switches to the classical empty do {} while (0) on other ones. This way the code will support being cleaned up using __fallthrough. --- diff --git a/include/haproxy/compiler.h b/include/haproxy/compiler.h index 82b6ae4c5e..26d1d22dc7 100644 --- a/include/haproxy/compiler.h +++ b/include/haproxy/compiler.h @@ -60,6 +60,20 @@ #define __has_attribute(x) __equals_1(__has_attribute_ ## x) #endif +/* The fallthrough attribute arrived with gcc 7, the same version that started + * to emit the fallthrough warnings and to parse the comments. Comments do not + * manage to stop the warning when preprocessing is split from compiling (e.g. + * when building under distcc). Better encourage the use of a __fallthrough + * statement instead. There are still limitations in that clang doesn't accept + * it after a label; this is the reason why we're always preceding it with an + * empty do-while. + */ +#if __has_attribute(fallthrough) +# define __fallthrough do { } while (0); __attribute__((fallthrough)) +#else +# define __fallthrough do { } while (0) +#endif + #if !defined(__GNUC__) /* Some versions of glibc irresponsibly redefine __attribute__() to empty for * non-gcc compilers, and as such, silently break all constructors with other