Skip to content
5 changes: 4 additions & 1 deletion clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,7 @@ Enumerations with a fixed underlying type
-----------------------------------------

Clang provides support for C++11 enumerations with a fixed underlying type
within Objective-C. For example, one can write an enumeration type as:
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:

.. code-block:: c++

Expand All @@ -1998,6 +1998,9 @@ value, is ``unsigned char``.
Use ``__has_feature(objc_fixed_enum)`` to determine whether support for fixed
underlying types is available in Objective-C.

Use ``__has_extension(c_fixed_enum)`` to determine whether support for fixed
underlying types is available in C.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably update the wording here to mention __has_feature and language version behavior.


Interoperability with C++11 lambdas
-----------------------------------

Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ EXTENSION(datasizeof, LangOpts.CPlusPlus)

FEATURE(cxx_abi_relative_vtable, LangOpts.CPlusPlus && LangOpts.RelativeCXXABIVTables)

//Fixed enum feature and extension, to be relocated in this file
FEATURE(c_fixed_enum, true)
EXTENSION(c_fixed_enum, true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven’t looked at the features code in a bit, but does this need to be both a FEATURE and an EXTENSION?

CC @AaronBallman

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should be both, but I'd move the code around a bit, similar to:

FEATURE(c_alignas, LangOpts.C11)
(we can add a new section for C23 features)

EXTENSION(c_alignas, true)
(we can put under the C23 section)


// CUDA/HIP Features
FEATURE(cuda_noinline_keyword, LangOpts.CUDA)
EXTENSION(cuda_implicit_host_device_templates, LangOpts.CUDA && LangOpts.OffloadImplicitHostDeviceTemplates)
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Sema/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ 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
typedef enum : unsigned char { Pink, Black, Cyan } Color;
#else
_Static_assert(__has_extension(c_fixed_enum), "Ensure language extension support for enumerations with a fixed underlying type in <C23");
typedef enum : unsigned char { Pink, Black, Cyan } Color; // expected-warning {{enumeration types with a fixed underlying type are a C23 extension}}
#endif

// PR28903
// In C it is valid to define tags inside enums.
struct PR28903 {
Expand Down
Loading