]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD: compiler: define a __fallthrough statement for switch/case
authorWilly Tarreau <w@1wt.eu>
Sun, 13 Nov 2022 11:21:22 +0000 (12:21 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 14 Nov 2022 10:14:02 +0000 (11:14 +0100)
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.

include/haproxy/compiler.h

index 82b6ae4c5e2ac29e2a559f75bdb5e343ea4ec521..26d1d22dc74c07b73e7069d89cb4df47f3184204 100644 (file)
 #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