Skip to content

Commit 9791f25

Browse files
[clang][sema] Add support and documentation for __has_extension(c_fixed_enum) (#117507)
This PR addresses #116880 Updated [LanguageExtensions.rst](https://github.com/llvm/llvm-project/blob/main/clang/docs/LanguageExtensions.rst) to include support for C++11 enumerations with a fixed underlying type in C. Included a note that this is only a language extension prior to C23. Updated [Features.def](https://github.com/llvm-mirror/clang/blob/master/include/clang/Basic/Features.def) to support for `__has_extension(c_fixed_enum)` by added it as a feature (for C23) and an extension (for <C23). Updated [enum.c](https://github.com/llvm/llvm-project/blob/main/clang/test/Sema/enum.c) to ensure support of C++11 enumerations with a fixed underlying type in both <C23 and C23, as well as the functionality of `__has_extension(c_fixed_enum)`. --- In enum.c, I encountered a warning when testing enumerations with a fixed underlying type in pre-C23 modes. Specifically, the test produces the warning: `enumeration types with a fixed underlying type are a C23 extension`. I am unsure if this warning is expected behavior, as enumerations with a fixed underlying type should behave identically in pre-C23 and C23 modes. I expected that adding `c_fixed_enum` as an extension would fix this warning. Feedback on whether this is correct or requires adjustment would be appreciated. I was also unsure of the best location for the additions in `Features.def`, I would appreciate advice on this as well Note that this is my first PR to LLVM, so please liberally critique it!
1 parent deed1b0 commit 9791f25

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ Enumerations with a fixed underlying type
19891989
-----------------------------------------
19901990
19911991
Clang provides support for C++11 enumerations with a fixed underlying type
1992-
within Objective-C. For example, one can write an enumeration type as:
1992+
within Objective-C and C `prior to C23 <https://open-std.org/JTC1/SC22/WG14/www/docs/n3030.htm>`_. For example, one can write an enumeration type as:
19931993
19941994
.. code-block:: c++
19951995
@@ -2001,6 +2001,14 @@ value, is ``unsigned char``.
20012001
Use ``__has_feature(objc_fixed_enum)`` to determine whether support for fixed
20022002
underlying types is available in Objective-C.
20032003
2004+
Use ``__has_extension(c_fixed_enum)`` to determine whether support for fixed
2005+
underlying types is available in C prior to C23. This will also report ``true`` in C23
2006+
and later modes as the functionality is available even if it's not an extension in
2007+
those modes.
2008+
2009+
Use ``__has_feature(c_fixed_enum)`` to determine whether support for fixed
2010+
underlying types is available in C23 and later.
2011+
20042012
Interoperability with C++11 lambdas
20052013
-----------------------------------
20062014

clang/include/clang/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ FEATURE(c_atomic, LangOpts.C11)
163163
FEATURE(c_generic_selections, LangOpts.C11)
164164
FEATURE(c_static_assert, LangOpts.C11)
165165
FEATURE(c_thread_local, LangOpts.C11 &&PP.getTargetInfo().isTLSSupported())
166+
// C23 features
167+
FEATURE(c_fixed_enum, LangOpts.C23)
166168
// C++11 features
167169
FEATURE(cxx_access_control_sfinae, LangOpts.CPlusPlus11)
168170
FEATURE(cxx_alias_templates, LangOpts.CPlusPlus11)
@@ -269,6 +271,7 @@ EXTENSION(c_static_assert, true)
269271
EXTENSION(c_thread_local, PP.getTargetInfo().isTLSSupported())
270272
// C23 features supported by other languages as extensions
271273
EXTENSION(c_attributes, true)
274+
EXTENSION(c_fixed_enum, true)
272275
// C++11 features supported by other languages as extensions.
273276
EXTENSION(cxx_atomic, LangOpts.CPlusPlus)
274277
EXTENSION(cxx_default_function_template_args, LangOpts.CPlusPlus)

clang/test/Sema/enum.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ int NegativeShortTest[NegativeShort == -1 ? 1 : -1];
121121
enum Color { Red, Green, Blue }; // expected-note{{previous use is here}}
122122
typedef struct Color NewColor; // expected-error {{use of 'Color' with tag type that does not match previous declaration}}
123123

124+
// Enumerations with a fixed underlying type.
125+
// https://github.com/llvm/llvm-project/issues/116880
126+
#if __STDC_VERSION__ >= 202311L
127+
static_assert(__has_feature(c_fixed_enum));
128+
static_assert(__has_extension(c_fixed_enum)); // Matches behavior for c_alignas, etc
129+
#else
130+
_Static_assert(__has_extension(c_fixed_enum), "");
131+
_Static_assert(!__has_feature(c_fixed_enum), "");
132+
#endif
133+
typedef enum : unsigned char { Pink, Black, Cyan } Color; // pre-c23-warning {{enumeration types with a fixed underlying type are a C23 extension}}
134+
124135
// PR28903
125136
// In C it is valid to define tags inside enums.
126137
struct PR28903 {

0 commit comments

Comments
 (0)