Skip to content

Commit f2cf76a

Browse files
authored
Fix fallthrough example macro to work with very old gcc versions
To deal with old gcc versions that do not have `__has_attribute`, the gcc manual suggest using this form. See https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html Signed-off-by: Lukas Backström (FKA Larsson) <[email protected]>
1 parent c2f296b commit f2cf76a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

docs/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,12 @@ This warning flag does not have a performance impact. However, sometimes a fallt
330330
The C17 standard[^C2017] does not provide a mechanism to mark intentional fallthroughs. Different tools support different mechanisms for marking one, including attributes and comments in various forms[^Shafik15]. A portable way to mark one is to define a function-like macro named `fallthrough()` to mark an intentional fallthrough that adjusts to the relevant tool (e.g., compiler) mechanism. We suggest using this construct below, inspired by the keyword-like construct used by the Linux kernel version 6.4 and later[^Howlett23]. We suggest using a function call syntax instead so more editors and other tools will deal with it correctly:
331331

332332
~~~c
333-
#if __has_attribute(__fallthrough__)
334-
# define fallthrough() __attribute__((__fallthrough__))
335-
#else
333+
#ifdef __has_attribute
334+
# if __has_attribute(__fallthrough__)
335+
# define fallthrough() __attribute__((__fallthrough__))
336+
# endif
337+
#endif
338+
#ifndef fallthrough
336339
# define fallthrough() do {} while (0) /* fallthrough */
337340
#endif
338341
~~~

0 commit comments

Comments
 (0)