[clang] [clang][C23] Support N3029 Improved Normal Enumerations (PR #103917)
Mariya Podchishchaeva via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 14 06:06:59 PDT 2024
https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/103917
Basically clang already implemented 90% of the feature as an extension. This commit disables warnings for C23 and aligns types of enumerators according to the recent wording.
>From eee57bcc211a8a045c0102ebb2f4410d52d657f6 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Wed, 14 Aug 2024 05:56:18 -0700
Subject: [PATCH] [clang][C23] Support N3029 Improved Normal Enumerations
Basically clang already implemented 90% of the feature as an extension.
This commit disables warnings for C23 and aligns types of enumerators
according to the recent wording.
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaDecl.cpp | 44 +++++++++++++++------------
clang/test/C/C23/n3029.c | 59 +++++++++++++++++++++++++++++++++++++
clang/test/Sema/enum.c | 52 +++++++++++++++++++++++++-------
clang/www/c_status.html | 2 +-
5 files changed, 129 insertions(+), 30 deletions(-)
create mode 100644 clang/test/C/C23/n3029.c
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6796a619ba97f8..376ed0facc8504 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -125,6 +125,8 @@ C2y Feature Support
C23 Feature Support
^^^^^^^^^^^^^^^^^^^
+- Clang now supports `N3029 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm>`_ Improved Normal Enumerations.
+
Non-comprehensive list of changes in this release
-------------------------------------------------
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 641b180527da55..03f14487d0b80e 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19474,11 +19474,12 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
// representable as an int.
// Complain if the value is not representable in an int.
- if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy))
- Diag(IdLoc, diag::ext_enum_value_not_int)
- << toString(EnumVal, 10) << Val->getSourceRange()
- << (EnumVal.isUnsigned() || EnumVal.isNonNegative());
- else if (!Context.hasSameType(Val->getType(), Context.IntTy)) {
+ if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) {
+ if (!getLangOpts().C23)
+ Diag(IdLoc, diag::ext_enum_value_not_int)
+ << toString(EnumVal, 10) << Val->getSourceRange()
+ << (EnumVal.isUnsigned() || EnumVal.isNonNegative());
+ } else if (!Context.hasSameType(Val->getType(), Context.IntTy)) {
// Force the type of the expression to 'int'.
Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).get();
}
@@ -19553,12 +19554,12 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
// If we're not in C++, diagnose the overflow of enumerator values,
// which in C99 means that the enumerator value is not representable in
- // an int (C99 6.7.2.2p2). However, we support GCC's extension that
- // permits enumerator values that are representable in some larger
- // integral type.
- if (!getLangOpts().CPlusPlus && !T.isNull())
+ // an int (C99 6.7.2.2p2). However C23 permits enumerator values that
+ // are representable in some larger integral type and we allow it in
+ // older language modes as an extension.
+ if (!getLangOpts().CPlusPlus && !getLangOpts().C23 && !T.isNull())
Diag(IdLoc, diag::warn_enum_value_overflow);
- } else if (!getLangOpts().CPlusPlus &&
+ } else if (!getLangOpts().CPlusPlus && !getLangOpts().C23 &&
!EltTy->isDependentType() &&
!isRepresentableIntegerValue(Context, EnumVal, EltTy)) {
// Enforce C99 6.7.2.2p2 even when we compute the next value.
@@ -19882,9 +19883,6 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
return;
}
- // TODO: If the result value doesn't fit in an int, it must be a long or long
- // long value. ISO C does not support this, but GCC does as an extension,
- // emit a warning.
unsigned IntWidth = Context.getTargetInfo().getIntWidth();
unsigned CharWidth = Context.getTargetInfo().getCharWidth();
unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
@@ -19893,13 +19891,14 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
// reverse the list.
unsigned NumNegativeBits = 0;
unsigned NumPositiveBits = 0;
+ bool MembersRepresentableByInt = true;
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
EnumConstantDecl *ECD =
cast_or_null<EnumConstantDecl>(Elements[i]);
if (!ECD) continue; // Already issued a diagnostic.
- const llvm::APSInt &InitVal = ECD->getInitVal();
+ llvm::APSInt InitVal = ECD->getInitVal();
// Keep track of the size of positive and negative values.
if (InitVal.isUnsigned() || InitVal.isNonNegative()) {
@@ -19911,6 +19910,8 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
NumNegativeBits =
std::max(NumNegativeBits, (unsigned)InitVal.getSignificantBits());
}
+ MembersRepresentableByInt &=
+ isRepresentableIntegerValue(Context, InitVal, Context.IntTy);
}
// If we have an empty set of enumerators we still need one bit.
@@ -19932,7 +19933,7 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
// int, long long int, or unsigned long long int.
// C99 6.4.4.3p2:
// An identifier declared as an enumeration constant has type int.
- // The C99 rule is modified by a gcc extension
+ // The C99 rule is modified by C23.
QualType BestPromotionType;
bool Packed = Enum->hasAttr<PackedAttr>();
@@ -20026,7 +20027,7 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
auto *ECD = cast_or_null<EnumConstantDecl>(D);
if (!ECD) continue; // Already issued a diagnostic.
- // Standard C says the enumerators have int type, but we allow, as an
+ // C99 says the enumerators have int type, but we allow, as an
// extension, the enumerators to be larger than int size. If each
// enumerator value fits in an int, type it as an int, otherwise type it the
// same as the enumerator decl itself. This means that in "enum { X = 1U }"
@@ -20040,9 +20041,14 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
QualType NewTy;
unsigned NewWidth;
bool NewSign;
- if (!getLangOpts().CPlusPlus &&
- !Enum->isFixed() &&
- isRepresentableIntegerValue(Context, InitVal, Context.IntTy)) {
+ if (!getLangOpts().CPlusPlus && !Enum->isFixed() &&
+ MembersRepresentableByInt) {
+ // C23 6.7.3.3.3p15:
+ // The enumeration member type for an enumerated type without fixed
+ // underlying type upon completion is:
+ // - int if all the values of the enumeration are representable as an
+ // int; or,
+ // - the enumerated type
NewTy = Context.IntTy;
NewWidth = IntWidth;
NewSign = true;
diff --git a/clang/test/C/C23/n3029.c b/clang/test/C/C23/n3029.c
new file mode 100644
index 00000000000000..d0dccb02501133
--- /dev/null
+++ b/clang/test/C/C23/n3029.c
@@ -0,0 +1,59 @@
+// RUN: %clang_cc1 -verify -fsyntax-only --embed-dir=%S/Inputs -std=c23 %s -pedantic
+
+#include <limits.h>
+
+#define GET_TYPE_INT(x) _Generic(x, \
+ char: 1,\
+ unsigned char: 2,\
+ signed char: 3,\
+ short: 4,\
+ unsigned short: 5,\
+ int: 6,\
+ unsigned int: 7,\
+ long: 8,\
+ unsigned long: 9,\
+ long long: 10,\
+ unsigned long long: 11,\
+ default: 0xFF\
+ )\
+
+enum x {
+a = INT_MAX,
+b = ULLONG_MAX,
+a_type = GET_TYPE_INT(a),
+b_type = GET_TYPE_INT(b)
+};
+
+static_assert(GET_TYPE_INT(a) == GET_TYPE_INT(b));
+
+extern enum x e_a;
+extern __typeof(b) e_a;
+extern __typeof(a) e_a;
+
+enum a {
+ a0 = 0xFFFFFFFFFFFFFFFFULL
+};
+
+_Bool e () {
+ return a0;
+}
+
+int f () {
+ return a0; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 18446744073709551615 to -1}}
+}
+
+unsigned long g () {
+ return a0;
+}
+
+unsigned long long h () {
+ return a0;
+}
+
+enum big_enum {
+ big_enum_a = LONG_MAX,
+ big_enum_b = a + 1,
+ big_enum_c = ULLONG_MAX
+};
+
+static_assert(GET_TYPE_INT(big_enum_a) == GET_TYPE_INT(big_enum_b));
diff --git a/clang/test/Sema/enum.c b/clang/test/Sema/enum.c
index b0707914c0d852..d2cd27a0c02299 100644
--- a/clang/test/Sema/enum.c
+++ b/clang/test/Sema/enum.c
@@ -1,23 +1,23 @@
-// RUN: %clang_cc1 -triple %itanium_abi_triple %s -fsyntax-only -verify -pedantic
+// RUN: %clang_cc1 -triple %itanium_abi_triple %s -fsyntax-only -verify=expected,c99 -pedantic
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -fsyntax-only -std=c23 -verify -pedantic
enum e {A,
- B = 42LL << 32, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
+ B = 42LL << 32, // c99-warning {{ISO C restricts enumerator values to range of 'int'}}
C = -4, D = 12456 };
enum f { a = -2147483648, b = 2147483647 }; // ok.
enum g { // too negative
- c = -2147483649, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
+ c = -2147483649, // c99-warning {{ISO C restricts enumerator values to range of 'int'}}
d = 2147483647 };
enum h { e = -2147483648, // too pos
- f = 2147483648, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
- i = 0xFFFF0000 // expected-warning {{too large}}
+ f = 2147483648, // c99-warning {{ISO C restricts enumerator values to range of 'int'}}
+ i = 0xFFFF0000 // c99-warning {{too large}}
};
// minll maxull
enum x // expected-warning {{enumeration values exceed range of largest integer}}
-{ y = -9223372036854775807LL-1, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
-z = 9223372036854775808ULL }; // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
+{ y = -9223372036854775807LL-1, // c99-warning {{ISO C restricts enumerator values to range of 'int'}}
+z = 9223372036854775808ULL }; // c99-warning {{ISO C restricts enumerator values to range of 'int'}}
int test(void) {
return sizeof(enum e) ;
@@ -172,10 +172,8 @@ enum class GH42372_2 {
#if __STDC_VERSION__ >= 202311L
// FIXME: GCC picks __uint128_t as the underlying type for the enumeration
// value and Clang picks unsigned long long.
-// FIXME: Clang does not yet implement WG14 N3029, so the warning about
-// restricting enumerator values to 'int' is not correct.
enum GH59352 { // expected-warning {{enumeration values exceed range of largest integer}}
- BigVal = 66666666666666666666wb // expected-warning {{ISO C restricts enumerator values to range of 'int' (66666666666666666666 is too large)}}
+ BigVal = 66666666666666666666wb // c99-warning {{ISO C restricts enumerator values to range of 'int' (66666666666666666666 is too large)}}
};
_Static_assert(BigVal == 66666666666666666666wb); /* expected-error {{static assertion failed due to requirement 'BigVal == 66666666666666666666wb'}}
expected-note {{expression evaluates to '11326434445538011818 == 66666666666666666666'}}
@@ -191,4 +189,38 @@ _Static_assert(
__uint128_t : 1
)
);
+
+#include <limits.h>
+
+void fooinc23() {
+ enum E1 {
+ V1 = INT_MAX
+ } e1;
+
+ enum E2 {
+ V2 = INT_MAX,
+ V3
+ } e2;
+
+ enum E3 {
+ V4 = INT_MAX,
+ V5 = LONG_MIN
+ } e3;
+
+ enum E4 {
+ V6 = 1u,
+ V7 = 2wb
+ } e4;
+
+ _Static_assert(_Generic(V1, int : 1));
+ _Static_assert(_Generic(V2, int : 0, unsigned int : 1));
+ _Static_assert(_Generic(V3, int : 0, unsigned int : 1));
+ _Static_assert(_Generic(V4, int : 0, signed long : 1));
+ _Static_assert(_Generic(V5, int : 0, signed long : 1));
+ _Static_assert(_Generic(V6, int : 1));
+ _Static_assert(_Generic(V7, int : 1));
+ _Static_assert(_Generic((enum E4){}, unsigned int : 1));
+
+}
+
#endif // __STDC_VERSION__ >= 202311L
diff --git a/clang/www/c_status.html b/clang/www/c_status.html
index a5d04506b642bc..cc2c83d5225e98 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -1160,7 +1160,7 @@ <h2 id="c2x">C23 implementation status</h2>
<tr>
<td>Improved normal enumerations</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3029.htm">N3029</a></td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="unreleased" align="center">Clang 20</td>
</tr>
<tr>
<td>Relax requirements for va_start</td>
More information about the cfe-commits
mailing list