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

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 24 13:45:17 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Aidan Goldfarb (AidanGoldfarb)

<details>
<summary>Changes</summary>

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.

Note that this is my first PR to LLVM, so please liberally critique it! 

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


3 Files Affected:

- (modified) clang/docs/LanguageExtensions.rst (+4-1) 
- (modified) clang/include/clang/Basic/Features.def (+4) 
- (modified) clang/test/Sema/enum.c (+9) 


``````````diff
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 3c9078bcdf8118..1c400d87c4948b 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -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++
 
@@ -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.
+
 Interoperability with C++11 lambdas
 -----------------------------------
 
diff --git a/clang/include/clang/Basic/Features.def b/clang/include/clang/Basic/Features.def
index 9088c867d53ce4..ab963a876db342 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -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)  
+
 // CUDA/HIP Features
 FEATURE(cuda_noinline_keyword, LangOpts.CUDA)
 EXTENSION(cuda_implicit_host_device_templates, LangOpts.CUDA && LangOpts.OffloadImplicitHostDeviceTemplates)
diff --git a/clang/test/Sema/enum.c b/clang/test/Sema/enum.c
index 4f6d04ba7f9182..7b3f7d30e91d82 100644
--- a/clang/test/Sema/enum.c
+++ b/clang/test/Sema/enum.c
@@ -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 {

``````````

</details>


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


More information about the cfe-commits mailing list