From d1ee47361c0b67f9bb5956172e1e6aa91d11a3a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Molinari?= Date: Sat, 18 May 2024 18:07:50 +0200 Subject: [PATCH] helpers: Add FALLTHROUGH macro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use gcc and clang's 'fallthrough' attribute instead of a comment to fall through switch statements. This allows to request fall through inside a block and prevents issues with preprocessed files. Signed-off-by: Loïc Molinari --- clients/terminal.c | 4 ++-- desktop-shell/shell.c | 4 ++-- libweston/backend-wayland/wayland.c | 2 +- shared/helpers.h | 7 +++++++ tests/subsurface-test.c | 18 +++++++++--------- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/clients/terminal.c b/clients/terminal.c index c0f222dd..871391ac 100644 --- a/clients/terminal.c +++ b/clients/terminal.c @@ -1711,7 +1711,7 @@ handle_non_csi_escape(struct terminal *terminal, char code) break; case 'E': /* NEL - Newline */ terminal->column = 0; - // fallthrough + FALLTHROUGH; case 'D': /* IND - Linefeed */ terminal->row += 1; if (terminal->row > terminal->margin_bottom) { @@ -1893,7 +1893,7 @@ handle_special_char(struct terminal *terminal, char c) if (terminal->mode & MODE_LF_NEWLINE) { terminal->column = 0; } - /* fallthrough */ + FALLTHROUGH; case '\v': case '\f': terminal->row++; diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c index 8ba3d817..430660f6 100644 --- a/desktop-shell/shell.c +++ b/desktop-shell/shell.c @@ -398,13 +398,13 @@ get_output_work_area(struct desktop_shell *shell, switch (shell->panel_position) { case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP: area->y += sh_output->panel_surface->height; - /* fallthrough */ + FALLTHROUGH; case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM: area->height -= sh_output->panel_surface->height; break; case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT: area->x += sh_output->panel_surface->width; - /* fallthrough */ + FALLTHROUGH; case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT: area->width -= sh_output->panel_surface->width; break; diff --git a/libweston/backend-wayland/wayland.c b/libweston/backend-wayland/wayland.c index 0f57c9e4..d79bad21 100644 --- a/libweston/backend-wayland/wayland.c +++ b/libweston/backend-wayland/wayland.c @@ -2941,8 +2941,8 @@ wayland_backend_create(struct weston_compositor *compositor, if (renderer != WESTON_RENDERER_PIXMAN) break; weston_log_continue("; falling back to Pixman.\n"); + FALLTHROUGH; } - /* fallthrough */ case WESTON_RENDERER_PIXMAN: if (weston_compositor_init_renderer(compositor, WESTON_RENDERER_PIXMAN, diff --git a/shared/helpers.h b/shared/helpers.h index dc203d74..10a9de0e 100644 --- a/shared/helpers.h +++ b/shared/helpers.h @@ -235,6 +235,13 @@ do { \ #define unreachable(str) assert(!str) #endif +#if __has_attribute(fallthrough) +/* Supported at least by gcc and clang. */ +#define FALLTHROUGH __attribute__((fallthrough)) +#else +#define FALLTHROUGH do {} while(0) +#endif + /** * Returns number of bits set in 32-bit value x. * diff --git a/tests/subsurface-test.c b/tests/subsurface-test.c index 34b95082..9f790ffc 100644 --- a/tests/subsurface-test.c +++ b/tests/subsurface-test.c @@ -728,31 +728,31 @@ create_subsurface_tree(struct client *client, struct wl_surface **surfs, case 11: SUB_LINK(10, 2); - /* fallthrough */ + FALLTHROUGH; case 10: SUB_LINK(9, 2); - /* fallthrough */ + FALLTHROUGH; case 9: SUB_LINK(8, 6); - /* fallthrough */ + FALLTHROUGH; case 8: SUB_LINK(7, 6); - /* fallthrough */ + FALLTHROUGH; case 7: SUB_LINK(6, 2); - /* fallthrough */ + FALLTHROUGH; case 6: SUB_LINK(5, 1); - /* fallthrough */ + FALLTHROUGH; case 5: SUB_LINK(4, 3); - /* fallthrough */ + FALLTHROUGH; case 4: SUB_LINK(3, 1); - /* fallthrough */ + FALLTHROUGH; case 3: SUB_LINK(2, 0); - /* fallthrough */ + FALLTHROUGH; case 2: SUB_LINK(1, 0);