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).
This commit is contained in:
Philip Chimento
2025-11-05 00:10:51 -08:00
parent 837a6eca8e
commit c247e1c20b

View File

@@ -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.
*
* |[<!-- language="C" -->
* 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:
*