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). 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index c00374d..15a6354 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -496,25 +496,13 @@ SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
if (dwords == 0) {
return;
}
-
- /* !!! FIXME: there are better ways to do this, but this is just to clean this up for now. */
- #ifdef __clang__
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wimplicit-fallthrough"
- #endif
-
switch (dwords % 4) {
- case 0: do { *_p++ = _val; /* fallthrough */
- case 3: *_p++ = _val; /* fallthrough */
- case 2: *_p++ = _val; /* fallthrough */
- case 1: *_p++ = _val; /* fallthrough */
+ case 0: do { *_p++ = _val; SDL_FALLTHROUGH;
+ case 3: *_p++ = _val; SDL_FALLTHROUGH;
+ case 2: *_p++ = _val; SDL_FALLTHROUGH;
+ case 1: *_p++ = _val;
} while ( --_n );
}
-
- #ifdef __clang__
- #pragma clang diagnostic pop
- #endif
-
#endif
}
diff --git a/include/begin_code.h b/include/begin_code.h
index 37bf975..b821195 100644
--- a/include/begin_code.h
+++ b/include/begin_code.h
@@ -164,3 +164,22 @@
#endif
#endif /* NULL */
#endif /* ! Mac OS X - breaks precompiled headers */
+
+#ifndef SDL_FALLTHROUGH
+#if (defined(__cplusplus) && __cplusplus >= 201703L) || \
+ (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L)
+#define SDL_FALLTHROUGH [[fallthrough]]
+#else
+#ifdef __has_attribute
+#define _HAS_FALLTHROUGH __has_attribute(__fallthrough__)
+#else
+#define _HAS_FALLTHROUGH 0
+#endif /* __has_attribute */
+#if _HAS_FALLTHROUGH
+#define SDL_FALLTHROUGH __attribute__((__fallthrough__))
+#else
+#define SDL_FALLTHROUGH do {} while (0) /* fallthrough */
+#endif /* _HAS_FALLTHROUGH */
+#undef _HAS_FALLTHROUGH
+#endif /* C++17 or C2x */
+#endif /* SDL_FALLTHROUGH not defined */
diff --git a/src/audio/SDL_wave.c b/src/audio/SDL_wave.c
index 88204fd..bf9fb54 100644
--- a/src/audio/SDL_wave.c
+++ b/src/audio/SDL_wave.c
@@ -1715,7 +1715,7 @@ WaveCheckFormat(WaveFile *file, size_t datalength)
if (file->facthint == FactStrict && file->fact.status <= 0) {
return SDL_SetError("Missing fact chunk in WAVE file");
}
- /* fallthrough */
+ SDL_FALLTHROUGH;
case PCM_CODE:
/* All supported formats require a non-zero bit depth. */
if (file->chunk.size < 16) {
@@ -1854,7 +1854,7 @@ WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 **audio_buf,
RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
break;
}
- /* fallthrough */
+ SDL_FALLTHROUGH;
case RiffSizeForce:
RIFFend = RIFFchunk.position + RIFFchunk.length;
RIFFlengthknown = SDL_TRUE;
diff --git a/src/render/software/SDL_draw.h b/src/render/software/SDL_draw.h
index d9abb94..5c63954 100644
--- a/src/render/software/SDL_draw.h
+++ b/src/render/software/SDL_draw.h
@@ -618,10 +618,10 @@ do { \
while (height--) { \
{ int n = (width+3)/4; \
switch (width & 3) { \
- case 0: do { op; pixel++; /* fallthrough */ \
- case 3: op; pixel++; /* fallthrough */ \
- case 2: op; pixel++; /* fallthrough */ \
- case 1: op; pixel++; /* fallthrough */ \
+ case 0: do { op; pixel++; SDL_FALLTHROUGH; \
+ case 3: op; pixel++; SDL_FALLTHROUGH; \
+ case 2: op; pixel++; SDL_FALLTHROUGH; \
+ case 1: op; pixel++; \
} while ( --n > 0 ); \
} \
} \
diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c
index e235541..34246bc 100644
--- a/src/stdlib/SDL_iconv.c
+++ b/src/stdlib/SDL_iconv.c
@@ -758,7 +758,7 @@ SDL_iconv(SDL_iconv_t cd,
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
- /* fallthrough */
+ SDL_FALLTHROUGH;
case ENCODING_UCS4BE:
if (ch > 0x7FFFFFFF) {
ch = UNKNOWN_UNICODE;
@@ -780,7 +780,7 @@ SDL_iconv(SDL_iconv_t cd,
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
- /* fallthrough */
+ SDL_FALLTHROUGH;
case ENCODING_UCS4LE:
if (ch > 0x7FFFFFFF) {
ch = UNKNOWN_UNICODE;
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 6922a24..13d6c54 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -1275,7 +1275,7 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
}
}
}
- /* Fall through to %d handling */
+ SDL_FALLTHROUGH;
case 'd':
if (inttype == DO_LONGLONG) {
Sint64 value;
@@ -1323,13 +1323,13 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
if (radix == 10) {
radix = 8;
}
- /* Fall through to unsigned handling */
+ SDL_FALLTHROUGH;
case 'x':
case 'X':
if (radix == 10) {
radix = 16;
}
- /* Fall through to unsigned handling */
+ SDL_FALLTHROUGH;
case 'u':
if (inttype == DO_LONGLONG) {
Uint64 value = 0;
@@ -1815,7 +1815,7 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
case 'p':
case 'x':
info.force_case = SDL_CASE_LOWER;
- /* Fall through to 'X' handling */
+ SDL_FALLTHROUGH;
case 'X':
if (info.force_case == SDL_CASE_NOCHANGE) {
info.force_case = SDL_CASE_UPPER;
@@ -1826,12 +1826,12 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
if (*fmt == 'p') {
inttype = DO_LONG;
}
- /* Fall through to unsigned handling */
+ SDL_FALLTHROUGH;
case 'o':
if (info.radix == 10) {
info.radix = 8;
}
- /* Fall through to unsigned handling */
+ SDL_FALLTHROUGH;
case 'u':
info.force_sign = SDL_FALSE;
if (info.precision >= 0) {
diff --git a/src/video/SDL_blit.h b/src/video/SDL_blit.h
index 4b50c7d..8b28138 100644
--- a/src/video/SDL_blit.h
+++ b/src/video/SDL_blit.h
@@ -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); \
} \
}
diff --git a/src/video/SDL_fillrect.c b/src/video/SDL_fillrect.c
index 6385e3e..59f6761 100644
--- a/src/video/SDL_fillrect.c
+++ b/src/video/SDL_fillrect.c
@@ -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;
diff --git a/src/video/SDL_pixels.c b/src/video/SDL_pixels.c
index 8015e02..1402167 100644
--- a/src/video/SDL_pixels.c
+++ b/src/video/SDL_pixels.c
@@ -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;
diff --git a/src/video/directfb/SDL_DirectFB_WM.c b/src/video/directfb/SDL_DirectFB_WM.c
index 8dd48a3..a90f98d 100644
--- a/src/video/directfb/SDL_DirectFB_WM.c
+++ b/src/video/directfb/SDL_DirectFB_WM.c
@@ -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)
diff --git a/src/video/directfb/SDL_DirectFB_events.c b/src/video/directfb/SDL_DirectFB_events.c
index 146bb99..c8c3368 100644
--- a/src/video/directfb/SDL_DirectFB_events.c
+++ b/src/video/directfb/SDL_DirectFB_events.c
@@ -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);
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index d5ffaec..2c74265 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -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:
diff --git a/test/controllermap.c b/test/controllermap.c
index 144c603..a70764a 100644
--- a/test/controllermap.c
+++ b/test/controllermap.c
@@ -562,7 +562,7 @@ WatchJoystick(SDL_Joystick * joystick)
if ((event.key.keysym.sym != SDLK_ESCAPE)) {
break;
}
- /* Fall through to signal quit */
+ SDL_FALLTHROUGH;
case SDL_QUIT:
done = SDL_TRUE;
break;
@@ -755,7 +755,7 @@ main(int argc, char *argv[])
if ((event.key.keysym.sym != SDLK_ESCAPE)) {
break;
}
- /* Fall through to signal quit */
+ SDL_FALLTHROUGH;
case SDL_QUIT:
done = SDL_TRUE;
break;
diff --git a/test/testgamecontroller.c b/test/testgamecontroller.c
index d0fb990..6aec600 100644
--- a/test/testgamecontroller.c
+++ b/test/testgamecontroller.c
@@ -386,7 +386,7 @@ loop(void *arg)
if (event.key.keysym.sym != SDLK_ESCAPE) {
break;
}
- /* Fall through to signal quit */
+ SDL_FALLTHROUGH;
case SDL_QUIT:
done = SDL_TRUE;
break;
diff --git a/test/testjoystick.c b/test/testjoystick.c
index e0ab270..b9e414b 100644
--- a/test/testjoystick.c
+++ b/test/testjoystick.c
@@ -183,7 +183,7 @@ loop(void *arg)
(event.key.keysym.sym != SDLK_AC_BACK)) {
break;
}
- /* Fall through to signal quit */
+ SDL_FALLTHROUGH;
case SDL_FINGERDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_QUIT: