Skip to content

Commit 294f69e

Browse files
JoePerchestorvalds
authored andcommitted
compiler_attributes.h: Add 'fallthrough' pseudo keyword for switch/case use
Reserve the pseudo keyword 'fallthrough' for the ability to convert the various case block /* fallthrough */ style comments to appear to be an actual reserved word with the same gcc case block missing fallthrough warning capability. All switch/case blocks now should end in one of: break; fallthrough; goto <label>; return [expression]; continue; In C mode, GCC supports the __fallthrough__ attribute since 7.1, the same time the warning and the comment parsing were introduced. fallthrough devolves to an empty "do {} while (0)" if the compiler version (any version less than gcc 7) does not support the attribute. Signed-off-by: Joe Perches <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Suggested-by: Dan Carpenter <[email protected]> Cc: Miguel Ojeda <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 48f9bcf commit 294f69e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

include/linux/compiler_attributes.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# define __GCC4_has_attribute___noclone__ 1
4141
# define __GCC4_has_attribute___nonstring__ 0
4242
# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
43+
# define __GCC4_has_attribute___fallthrough__ 0
4344
#endif
4445

4546
/*
@@ -185,6 +186,22 @@
185186
# define __noclone
186187
#endif
187188

189+
/*
190+
* Add the pseudo keyword 'fallthrough' so case statement blocks
191+
* must end with any of these keywords:
192+
* break;
193+
* fallthrough;
194+
* goto <label>;
195+
* return [expression];
196+
*
197+
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#Statement-Attributes
198+
*/
199+
#if __has_attribute(__fallthrough__)
200+
# define fallthrough __attribute__((__fallthrough__))
201+
#else
202+
# define fallthrough do {} while (0) /* fallthrough */
203+
#endif
204+
188205
/*
189206
* Note the missing underscores.
190207
*

0 commit comments

Comments
 (0)