From a802d6b20ab0d0f33806e1e33118a711ba1277a6 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Fri, 4 Jul 2025 00:12:21 -0400 Subject: [PATCH] src: avoid tentative definitions of static data Having multiple declarations of the same static data (where one is considered "tentative") is kind of an obscure C feature -- I didn't even know the name of it until today. It's also forbidden in C++. In the case of circular dependencies between static data <-> function, the more typical pattern is to forward-declare the function, then the data, then provide the function definition. Let's follow that pattern. --- src/buffer.c | 17 +++++++++-------- src/osd-field.c | 41 +++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index 1faa4620..8a2aa202 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -33,14 +33,8 @@ #include "common/box.h" #include "common/mem.h" -static const struct wlr_buffer_impl data_buffer_impl; - -static struct lab_data_buffer * -data_buffer_from_buffer(struct wlr_buffer *buffer) -{ - assert(buffer->impl == &data_buffer_impl); - return (struct lab_data_buffer *)buffer; -} +static struct lab_data_buffer *data_buffer_from_buffer( + struct wlr_buffer *buffer); static void data_buffer_destroy(struct wlr_buffer *wlr_buffer) @@ -80,6 +74,13 @@ static const struct wlr_buffer_impl data_buffer_impl = { .end_data_ptr_access = data_buffer_end_data_ptr_access, }; +static struct lab_data_buffer * +data_buffer_from_buffer(struct wlr_buffer *buffer) +{ + assert(buffer->impl == &data_buffer_impl); + return (struct lab_data_buffer *)buffer; +} + struct lab_data_buffer * buffer_adopt_cairo_surface(cairo_surface_t *surface) { diff --git a/src/osd-field.c b/src/osd-field.c index 9eec0837..2885febe 100644 --- a/src/osd-field.c +++ b/src/osd-field.c @@ -21,8 +21,6 @@ struct field_converter { field_conversion_type *fn; }; -static const struct field_converter field_converter[]; - /* Internal helpers */ static const char * @@ -200,6 +198,27 @@ field_set_title_short(struct buf *buf, struct view *view, const char *format) buf_add(buf, get_title_if_different(view)); } +static void field_set_custom(struct buf *buf, struct view *view, + const char *format); + +static const struct field_converter field_converter[LAB_FIELD_COUNT] = { + [LAB_FIELD_TYPE] = { 'B', field_set_type }, + [LAB_FIELD_TYPE_SHORT] = { 'b', field_set_type_short }, + [LAB_FIELD_WIN_STATE_ALL] = { 'S', field_set_win_state_all }, + [LAB_FIELD_WIN_STATE] = { 's', field_set_win_state }, + [LAB_FIELD_IDENTIFIER] = { 'I', field_set_identifier }, + [LAB_FIELD_TRIMMED_IDENTIFIER] = { 'i', field_set_identifier_trimmed }, + [LAB_FIELD_DESKTOP_ENTRY_NAME] = { 'n', field_set_desktop_entry_name}, + [LAB_FIELD_WORKSPACE] = { 'W', field_set_workspace }, + [LAB_FIELD_WORKSPACE_SHORT] = { 'w', field_set_workspace_short }, + [LAB_FIELD_OUTPUT] = { 'O', field_set_output }, + [LAB_FIELD_OUTPUT_SHORT] = { 'o', field_set_output_short }, + [LAB_FIELD_TITLE] = { 'T', field_set_title }, + [LAB_FIELD_TITLE_SHORT] = { 't', field_set_title_short }, + /* fmt_char can never be matched so prevents LAB_FIELD_CUSTOM recursion */ + [LAB_FIELD_CUSTOM] = { '\0', field_set_custom }, +}; + static void field_set_custom(struct buf *buf, struct view *view, const char *format) { @@ -278,24 +297,6 @@ reset_format: buf_reset(&field_result); } -static const struct field_converter field_converter[LAB_FIELD_COUNT] = { - [LAB_FIELD_TYPE] = { 'B', field_set_type }, - [LAB_FIELD_TYPE_SHORT] = { 'b', field_set_type_short }, - [LAB_FIELD_WIN_STATE_ALL] = { 'S', field_set_win_state_all }, - [LAB_FIELD_WIN_STATE] = { 's', field_set_win_state }, - [LAB_FIELD_IDENTIFIER] = { 'I', field_set_identifier }, - [LAB_FIELD_TRIMMED_IDENTIFIER] = { 'i', field_set_identifier_trimmed }, - [LAB_FIELD_DESKTOP_ENTRY_NAME] = { 'n', field_set_desktop_entry_name}, - [LAB_FIELD_WORKSPACE] = { 'W', field_set_workspace }, - [LAB_FIELD_WORKSPACE_SHORT] = { 'w', field_set_workspace_short }, - [LAB_FIELD_OUTPUT] = { 'O', field_set_output }, - [LAB_FIELD_OUTPUT_SHORT] = { 'o', field_set_output_short }, - [LAB_FIELD_TITLE] = { 'T', field_set_title }, - [LAB_FIELD_TITLE_SHORT] = { 't', field_set_title_short }, - /* fmt_char can never be matched so prevents LAB_FIELD_CUSTOM recursion */ - [LAB_FIELD_CUSTOM] = { '\0', field_set_custom }, -}; - struct window_switcher_field * osd_field_create(void) {