[clang] [clang][sema] Add support and documentation for `__has_extension(c_fixed_enum)` (PR #117507)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 27 10:30:50 PST 2024


================
@@ -121,6 +121,16 @@ int NegativeShortTest[NegativeShort == -1 ? 1 : -1];
 enum Color { Red, Green, Blue }; // expected-note{{previous use is here}}
 typedef struct Color NewColor; // expected-error {{use of 'Color' with tag type that does not match previous declaration}}
 
+// Enumerations with a fixed underlying type. 
+// https://github.com/llvm/llvm-project/issues/116880
+#if __STDC_VERSION__ >= 202311L && !__has_feature(c_fixed_enum)
+    #error c_fixed_enum should be set a feature in C23 mode
+#elif __STDC_VERSION__ < 202311L && !__has_extension(c_fixed_enum)
+    #error c_fixed_enum should be a language extension in <C23 mode
+#else
+    typedef enum : unsigned char { Pink, Black, Cyan } Color; // pre-c23-warning {{enumeration types with a fixed underlying type are a C23 extension}}
+#endif
----------------
AaronBallman wrote:

How about something along the lines of:
```
#if __STDC_VERSION__ >= 202311L
static_assert(__has_feature(c_fixed_enum));
static_assert(__has_extension(c_fixed_enum)); // Matches behavior for c_alignas, etc
#else
_Static_assert(__has_extension(c_fixed_enum), "");
_Static_assert(!__has_feature(c_fixed_enum), "");
#endif
```

https://github.com/llvm/llvm-project/pull/117507


More information about the cfe-commits mailing list