Skip to content

[C23] No longer assert on huge enumerator values #81760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ Improvements to Clang's diagnostics

- Clang now diagnoses friend declarations with an ``enum`` elaborated-type-specifier in language modes after C++98.

- Now diagnoses an enumeration constant whose value is larger than can be
represented by ``unsigned long long``, which can happen with a large constant
using the ``wb`` or ``uwb`` suffix. The maximal underlying type is currently
``unsigned long long``, but this behavior may change in the future when Clang
implements
`WG14 N3029 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3029.htm>`_.
Fixes `#69352 <https://github.com/llvm/llvm-project/issues/69352>`_.

Improvements to Clang's time-trace
----------------------------------

Expand Down
9 changes: 7 additions & 2 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20317,8 +20317,13 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
? Context.UnsignedLongTy : Context.LongTy;
} else {
BestWidth = Context.getTargetInfo().getLongLongWidth();
assert(NumPositiveBits <= BestWidth &&
"How could an initializer get larger than ULL?");
if (NumPositiveBits > BestWidth) {
// This can happen with bit-precise integer types, but those are not
// allowed as the type for an enumerator per C23 6.7.2.2p4 and p12.
// FIXME: GCC uses __int128_t and __uint128_t for cases that fit within
// a 128-bit integer, we should consider doing the same.
Diag(Enum->getLocation(), diag::ext_enum_too_large);
}
BestType = Context.UnsignedLongLongTy;
BestPromotionType
= (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus)
Expand Down
25 changes: 25 additions & 0 deletions clang/test/Sema/enum.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple %s -fsyntax-only -verify -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'}}
C = -4, D = 12456 };
Expand Down Expand Up @@ -167,3 +168,27 @@ enum struct GH42372_1 { // expected-error {{expected identifier or '{'}}
enum class GH42372_2 {
One
};

#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)}}
};
_Static_assert(BigVal == 66666666666666666666wb); /* expected-error {{static assertion failed due to requirement 'BigVal == 66666666666666666666wb'}}
expected-note {{expression evaluates to '11326434445538011818 == 66666666666666666666'}}
*/
_Static_assert(
_Generic(BigVal, // expected-error {{static assertion failed}}
_BitInt(67) : 0,
__INTMAX_TYPE__ : 0,
__UINTMAX_TYPE__ : 0,
long long : 0,
unsigned long long : 0,
__int128_t : 0,
__uint128_t : 1
)
);
#endif // __STDC_VERSION__ >= 202311L