tools/include/nolibc/compiler.h | 6 ++++++ tools/include/nolibc/stdio.h | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-)
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.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/include/nolibc/compiler.h | 6 ++++++
tools/include/nolibc/stdio.h | 3 ++-
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/tools/include/nolibc/compiler.h b/tools/include/nolibc/compiler.h
index 9bc6a706a332378e5af1f5baabf953f137f99749..d213b67e5f1f6101128ba8a7ddfbaa2daac20b9b 100644
--- a/tools/include/nolibc/compiler.h
+++ b/tools/include/nolibc/compiler.h
@@ -32,4 +32,10 @@
# define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector")))
#endif /* __nolibc_has_attribute(no_stack_protector) */
+#if __nolibc_has_attribute(fallthrough)
+# define __nolibc_fallthrough __attribute__((fallthrough))
+#else
+# define __nolibc_fallthrough
+#endif /* __nolibc_has_attribute(fallthrough) */
+
#endif /* _NOLIBC_COMPILER_H */
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index c968dbbc4ef8137e237b859bf18a6d2970230cbf..3892034198dd566d21a5cc0a9f67cf097d428393 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -15,6 +15,7 @@
#include "stdarg.h"
#include "stdlib.h"
#include "string.h"
+#include "compiler.h"
#ifndef EOF
#define EOF (-1)
@@ -264,7 +265,7 @@ int vfprintf(FILE *stream, const char *fmt, va_list args)
case 'p':
*(out++) = '0';
*(out++) = 'x';
- /* fall through */
+ __nolibc_fallthrough;
default: /* 'x' and 'p' above */
u64toh_r(v, out);
break;
---
base-commit: e7ed343658792771cf1b868df061661b7bcc5cef
change-id: 20240929-nolibc-fallthrough-ba822a4a9255
Best regards,
--
Thomas Weißschuh <linux@weissschuh.net>
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
© 2016 - 2024 Red Hat, Inc.