Add and use SDL_FALLTHROUGH for fallthroughs

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).

Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.

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.
This commit is contained in:
Misa
2021-09-27 14:38:12 -07:00
committed by Ozkan Sezer
parent c877dd93c5
commit 3bf7994fe2
15 changed files with 65 additions and 55 deletions

View File

@@ -479,14 +479,14 @@ do { \
#define DUFFS_LOOP8(pixel_copy_increment, width) \
{ int n = (width+7)/8; \
switch (width & 7) { \
case 0: do { pixel_copy_increment; /* fallthrough */ \
case 7: pixel_copy_increment; /* fallthrough */ \
case 6: pixel_copy_increment; /* fallthrough */ \
case 5: pixel_copy_increment; /* fallthrough */ \
case 4: pixel_copy_increment; /* fallthrough */ \
case 3: pixel_copy_increment; /* fallthrough */ \
case 2: pixel_copy_increment; /* fallthrough */ \
case 1: pixel_copy_increment; /* fallthrough */ \
case 0: do { pixel_copy_increment; SDL_FALLTHROUGH; \
case 7: pixel_copy_increment; SDL_FALLTHROUGH; \
case 6: pixel_copy_increment; SDL_FALLTHROUGH; \
case 5: pixel_copy_increment; SDL_FALLTHROUGH; \
case 4: pixel_copy_increment; SDL_FALLTHROUGH; \
case 3: pixel_copy_increment; SDL_FALLTHROUGH; \
case 2: pixel_copy_increment; SDL_FALLTHROUGH; \
case 1: pixel_copy_increment; \
} while ( --n > 0 ); \
} \
}
@@ -495,10 +495,10 @@ do { \
#define DUFFS_LOOP4(pixel_copy_increment, width) \
{ int n = (width+3)/4; \
switch (width & 3) { \
case 0: do { pixel_copy_increment; /* fallthrough */ \
case 3: pixel_copy_increment; /* fallthrough */ \
case 2: pixel_copy_increment; /* fallthrough */ \
case 1: pixel_copy_increment; /* fallthrough */ \
case 0: do { pixel_copy_increment; SDL_FALLTHROUGH; \
case 3: pixel_copy_increment; SDL_FALLTHROUGH; \
case 2: pixel_copy_increment; SDL_FALLTHROUGH; \
case 1: pixel_copy_increment; \
} while (--n > 0); \
} \
}

View File

@@ -145,13 +145,13 @@ SDL_FillRect1(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
switch ((uintptr_t) p & 3) {
case 1:
*p++ = (Uint8) color;
--n; /* fallthrough */
--n; SDL_FALLTHROUGH;
case 2:
*p++ = (Uint8) color;
--n; /* fallthrough */
--n; SDL_FALLTHROUGH;
case 3:
*p++ = (Uint8) color;
--n; /* fallthrough */
--n;
}
SDL_memset4(p, color, (n >> 2));
}
@@ -159,11 +159,11 @@ SDL_FillRect1(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
p += (n & ~3);
switch (n & 3) {
case 3:
*p++ = (Uint8) color; /* fallthrough */
*p++ = (Uint8) color; SDL_FALLTHROUGH;
case 2:
*p++ = (Uint8) color; /* fallthrough */
*p++ = (Uint8) color; SDL_FALLTHROUGH;
case 1:
*p++ = (Uint8) color; /* fallthrough */
*p++ = (Uint8) color;
}
}
pixels += pitch;

View File

@@ -333,7 +333,7 @@ SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
if (Rmask == 0) {
return SDL_PIXELFORMAT_RGB555;
}
/* fallthrough */
SDL_FALLTHROUGH;
case 16:
if (Rmask == 0) {
return SDL_PIXELFORMAT_RGB565;

View File

@@ -323,7 +323,7 @@ DirectFB_WM_ProcessEvent(_THIS, SDL_Window * window, DFBWindowEvent * evt)
}
if (window->flags & SDL_WINDOW_MAXIMIZED)
return 1;
/* fall through */
SDL_FALLTHROUGH;
default:
windata->wm_grab = pos;
if (grabbed_window != NULL)

View File

@@ -261,7 +261,7 @@ ProcessWindowEvent(_THIS, SDL_Window *sdlwin, DFBWindowEvent * evt)
SDL_SendWindowEvent(sdlwin, SDL_WINDOWEVENT_MOVED,
evt->x, evt->y);
}
/* fall throught */
SDL_FALLTHROUGH;
case DWET_SIZE:
/* FIXME: what about < 0 */
evt->w -= (windata->theme.right_size + windata->theme.left_size);

View File

@@ -729,6 +729,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
}
}
/* don't break here, fall through to check the wParam like the button presses */
SDL_FALLTHROUGH;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP: