Add G_GNUC_FLAG_ENUM

I'm starting to use clang-tidy to do static analysis and I get lots of this type of false positive in my GObject boilerplate code:

warning: The value '10' provided to the cast expression is not in the valid range of values for the enum [clang-analyzer-optin.core.EnumCastOutOfRange]
  436 |         (GParamFlags)(G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
      |         ^

Of course I could switch that particular check off, but both GCC and Clang actually provide an annotation for this purpose, __attribute__((flag_enum)), that tells the compiler you intend to use an enum type as a bitflag. Besides doing the right thing in clang-tidy, this annotation also allows the compiler to generate more useful diagnostics. (E.g., to warn about the slightly weird definition of G_REGEX_NEWLINE_ANYCRLF.)

This merge request adds the macro G_GNUC_FLAG_ENUM which expands to __attribute__((flag_enum)) if it's available. It also teaches mkenums about this annotation, and adds it to all bitflag enums in the GLib codebase.

Merge request reports

Loading