From c247e1c20b8e2c965dd38060ab66ec352b40e2d1 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Wed, 5 Nov 2025 00:10:51 -0800 Subject: [PATCH] gmacros: Add G_GNUC_FLAG_ENUM This expands to __attribute__((flag_enum)) if available. It is useful when using clang-tidy to do static analysis, which will warn about code like (GParamFlags)(G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE). --- glib/gmacros.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/glib/gmacros.h b/glib/gmacros.h index 9c36e9925..a047d0ea2 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -779,6 +779,38 @@ #define GLIB_CANNOT_IGNORE_DEPRECATIONS #endif +/** + * G_GNUC_FLAG_ENUM: + * + * Expands to the GNU C `flag_enum` attribute if the compiler is gcc or clang. + * This attribute indicates that an enumerated type is used in bitwise + * operations. + * It is sometimes used in static analysis. + * + * See the + * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-flag_005fenum-type-attribute) + * for details. + * + * |[ + * typedef enum { + * G_KEY_FILE_NONE = 0, + * G_KEY_FILE_KEEP_COMMENTS = 1 << 0, + * G_KEY_FILE_KEEP_TRANSLATIONS = 1 << 1 + * } G_GNUC_FLAG_ENUM GKeyFileFlags; + * ]| + * + * (The attribute can also be placed after `enum` and before the opening brace, + * but that may cause it to be misinterpreted as the name of the enum if the + * macro is not defined.) + * + * Since: 2.88 + */ +#if g_macro__has_attribute(flag_enum) +#define G_GNUC_FLAG_ENUM __attribute__((flag_enum)) +#else +#define G_GNUC_FLAG_ENUM +#endif + /** * G_GNUC_MAY_ALIAS: *