@@ -19633,23 +19633,6 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
19633
19633
ProcessAPINotes(Record);
19634
19634
}
19635
19635
19636
- /// Determine whether the given integral value is representable within
19637
- /// the given type T.
19638
- static bool isRepresentableIntegerValue(ASTContext &Context,
19639
- llvm::APSInt &Value,
19640
- QualType T) {
19641
- assert((T->isIntegralType(Context) || T->isEnumeralType()) &&
19642
- "Integral type required!");
19643
- unsigned BitWidth = Context.getIntWidth(T);
19644
-
19645
- if (Value.isUnsigned() || Value.isNonNegative()) {
19646
- if (T->isSignedIntegerOrEnumerationType())
19647
- --BitWidth;
19648
- return Value.getActiveBits() <= BitWidth;
19649
- }
19650
- return Value.getSignificantBits() <= BitWidth;
19651
- }
19652
-
19653
19636
// Given an integral type, return the next larger integral type
19654
19637
// (or a NULL type of no such type exists).
19655
19638
static QualType getNextLargerIntegralType(ASTContext &Context, QualType T) {
@@ -19723,7 +19706,7 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
19723
19706
// representable in the underlying type of the enumeration. In C++11,
19724
19707
// we perform a non-narrowing conversion as part of converted constant
19725
19708
// expression checking.
19726
- if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) {
19709
+ if (!Context. isRepresentableIntegerValue(EnumVal, EltTy)) {
19727
19710
if (Context.getTargetInfo()
19728
19711
.getTriple()
19729
19712
.isWindowsMSVCEnvironment()) {
@@ -19752,7 +19735,7 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
19752
19735
// representable as an int.
19753
19736
19754
19737
// Complain if the value is not representable in an int.
19755
- if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) {
19738
+ if (!Context. isRepresentableIntegerValue(EnumVal, Context.IntTy)) {
19756
19739
Diag(IdLoc, getLangOpts().C23
19757
19740
? diag::warn_c17_compat_enum_value_not_int
19758
19741
: diag::ext_c23_enum_value_not_int)
@@ -19844,7 +19827,7 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
19844
19827
: diag::ext_c23_enum_value_not_int)
19845
19828
<< 1 << toString(EnumVal, 10) << 1;
19846
19829
} else if (!getLangOpts().CPlusPlus && !EltTy->isDependentType() &&
19847
- !isRepresentableIntegerValue(Context, EnumVal, EltTy)) {
19830
+ !Context. isRepresentableIntegerValue(EnumVal, EltTy)) {
19848
19831
// Enforce C99 6.7.2.2p2 even when we compute the next value.
19849
19832
Diag(IdLoc, getLangOpts().C23 ? diag::warn_c17_compat_enum_value_not_int
19850
19833
: diag::ext_c23_enum_value_not_int)
@@ -20171,35 +20154,8 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
20171
20154
// reverse the list.
20172
20155
unsigned NumNegativeBits = 0;
20173
20156
unsigned NumPositiveBits = 0;
20174
- bool MembersRepresentableByInt = true;
20175
-
20176
- for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
20177
- EnumConstantDecl *ECD =
20178
- cast_or_null<EnumConstantDecl>(Elements[i]);
20179
- if (!ECD) continue; // Already issued a diagnostic.
20180
-
20181
- llvm::APSInt InitVal = ECD->getInitVal();
20182
-
20183
- // Keep track of the size of positive and negative values.
20184
- if (InitVal.isUnsigned() || InitVal.isNonNegative()) {
20185
- // If the enumerator is zero that should still be counted as a positive
20186
- // bit since we need a bit to store the value zero.
20187
- unsigned ActiveBits = InitVal.getActiveBits();
20188
- NumPositiveBits = std::max({NumPositiveBits, ActiveBits, 1u});
20189
- } else {
20190
- NumNegativeBits =
20191
- std::max(NumNegativeBits, (unsigned)InitVal.getSignificantBits());
20192
- }
20193
- MembersRepresentableByInt &=
20194
- isRepresentableIntegerValue(Context, InitVal, Context.IntTy);
20195
- }
20196
-
20197
- // If we have an empty set of enumerators we still need one bit.
20198
- // From [dcl.enum]p8
20199
- // If the enumerator-list is empty, the values of the enumeration are as if
20200
- // the enumeration had a single enumerator with value 0
20201
- if (!NumPositiveBits && !NumNegativeBits)
20202
- NumPositiveBits = 1;
20157
+ bool MembersRepresentableByInt =
20158
+ Context.computeEnumBits(Elements, NumNegativeBits, NumPositiveBits);
20203
20159
20204
20160
// Figure out the type that should be used for this enum.
20205
20161
QualType BestType;
0 commit comments