New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add and use SDL_FALLTHROUGH
for fallthroughs
#4791
Conversation
ea45029
to
374d551
Compare
Case fallthrough warnings can be suppressed using the __fallthrough__ compiler attribute. Unfortunately, not all compilers have this attribute, or even have __has_attribute to check if they have the __fallthrough__ attribute. [[fallthrough]] is also available in C++17 and the next C2x, but not everyone uses C++17 or C2x. So define the SDL_FALLTHROUGH macro to deal with those problems - if we are using C++17 or C2x, it expands to [[fallthrough]]; else if the compiler has __has_attribute and has the __fallthrough__ attribute, then it expands to __attribute__((__fallthrough__)); else it expands to an empty statement, with a /* fallthrough */ comment (it's a do {} while (0) statement, because users of this macro need to use a semicolon, because [[fallthrough]] and __attribute__((__fallthrough__)) require a semicolon). Applications using SDL are also free to use this macro (because it is defined in begin_code.h). All existing /* fallthrough */ comments have been replaced with this macro. Some of them were unnecessary because they were the last case in a switch; using SDL_FALLTHROUGH in those cases would result in a compile error on compilers that support __fallthrough__, for having a __attribute__((__fallthrough__)) statement that didn't immediately precede a case label.
374d551
to
4282fb9
Compare
This causes multitudes of warning from old clang, e.g. clang-3.4.2:
|
Do you have a fix? |
unfortunately I don't |
This reverts commit 66a08aa. This causes problems with older compilers: #4791 (comment)
Okay, I reverted it for now. @InfoTeddy, can you look and see the best way to fix this? |
FWIW: clang documentation shows that the GNU syntax, i.e. |
Should be as simple as expanding to |
Do you want to create a new pull request for that? |
Sure, I can do that. |
Done: #4944 |
Case fallthrough warnings can be suppressed using the
__fallthrough__
compiler attribute. Unfortunately, not all compilers have this attribute, or even have__has_attribute
to check if they have the__fallthrough__
attribute.[[fallthrough]]
is also available in C++17 and the next C2x, but not everyone uses C++17 or C2x.So define the
SDL_FALLTHROUGH
macro to deal with those problems - if we are using C++17 or C2x, it expands to[[fallthrough]]
; else if the compiler has__has_attribute
and has the__fallthrough__
attribute, then it expands to__attribute__((__fallthrough__))
; else it expands to an empty statement, with a/* fallthrough */
comment (it's ado {} while (0)
statement, because users of this macro need to use a semicolon, because[[fallthrough]]
and__attribute__((__fallthrough__))
require a semicolon).Applications using SDL are also free to use this macro (because it is defined in
begin_code.h
).All existing
/* fallthrough */
comments have been replaced with this macro. Some of them were unnecessary because they were the last case in a switch; usingSDL_FALLTHROUGH
in those cases would result in a compile error on compilers that support__fallthrough__
, for having a__attribute__((__fallthrough__))
statement that didn't immediately precede a case label.