@@ -20043,10 +20043,6 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
2004320043 return;
2004420044 }
2004520045
20046- unsigned IntWidth = Context.getTargetInfo().getIntWidth();
20047- unsigned CharWidth = Context.getTargetInfo().getCharWidth();
20048- unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
20049-
2005020046 // Verify that all the values are okay, compute the size of the values, and
2005120047 // reverse the list.
2005220048 unsigned NumNegativeBits = 0;
@@ -20112,73 +20108,12 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
2011220108 BestPromotionType = BestType;
2011320109
2011420110 BestWidth = Context.getIntWidth(BestType);
20115- }
20116- else if (NumNegativeBits) {
20117- // If there is a negative value, figure out the smallest integer type (of
20118- // int/long/longlong) that fits.
20119- // If it's packed, check also if it fits a char or a short.
20120- if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) {
20121- BestType = Context.SignedCharTy;
20122- BestWidth = CharWidth;
20123- } else if (Packed && NumNegativeBits <= ShortWidth &&
20124- NumPositiveBits < ShortWidth) {
20125- BestType = Context.ShortTy;
20126- BestWidth = ShortWidth;
20127- } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
20128- BestType = Context.IntTy;
20129- BestWidth = IntWidth;
20130- } else {
20131- BestWidth = Context.getTargetInfo().getLongWidth();
20132-
20133- if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) {
20134- BestType = Context.LongTy;
20135- } else {
20136- BestWidth = Context.getTargetInfo().getLongLongWidth();
20137-
20138- if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
20139- Diag(Enum->getLocation(), diag::ext_enum_too_large);
20140- BestType = Context.LongLongTy;
20141- }
20142- }
20143- BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType);
2014420111 } else {
20145- // If there is no negative value, figure out the smallest type that fits
20146- // all of the enumerator values.
20147- // If it's packed, check also if it fits a char or a short.
20148- if (Packed && NumPositiveBits <= CharWidth) {
20149- BestType = Context.UnsignedCharTy;
20150- BestPromotionType = Context.IntTy;
20151- BestWidth = CharWidth;
20152- } else if (Packed && NumPositiveBits <= ShortWidth) {
20153- BestType = Context.UnsignedShortTy;
20154- BestPromotionType = Context.IntTy;
20155- BestWidth = ShortWidth;
20156- } else if (NumPositiveBits <= IntWidth) {
20157- BestType = Context.UnsignedIntTy;
20158- BestWidth = IntWidth;
20159- BestPromotionType
20160- = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus)
20161- ? Context.UnsignedIntTy : Context.IntTy;
20162- } else if (NumPositiveBits <=
20163- (BestWidth = Context.getTargetInfo().getLongWidth())) {
20164- BestType = Context.UnsignedLongTy;
20165- BestPromotionType
20166- = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus)
20167- ? Context.UnsignedLongTy : Context.LongTy;
20168- } else {
20169- BestWidth = Context.getTargetInfo().getLongLongWidth();
20170- if (NumPositiveBits > BestWidth) {
20171- // This can happen with bit-precise integer types, but those are not
20172- // allowed as the type for an enumerator per C23 6.7.2.2p4 and p12.
20173- // FIXME: GCC uses __int128_t and __uint128_t for cases that fit within
20174- // a 128-bit integer, we should consider doing the same.
20175- Diag(Enum->getLocation(), diag::ext_enum_too_large);
20176- }
20177- BestType = Context.UnsignedLongLongTy;
20178- BestPromotionType
20179- = (NumPositiveBits == BestWidth || !getLangOpts().CPlusPlus)
20180- ? Context.UnsignedLongLongTy : Context.LongLongTy;
20181- }
20112+ bool EnumTooLarge = Context.computeBestEnumTypes(
20113+ Packed, NumNegativeBits, NumPositiveBits, BestType, BestPromotionType);
20114+ BestWidth = Context.getIntWidth(BestType);
20115+ if (EnumTooLarge)
20116+ Diag(Enum->getLocation(), diag::ext_enum_too_large);
2018220117 }
2018320118
2018420119 // Loop over all of the enumerator constants, changing their types to match
@@ -20210,7 +20145,7 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
2021020145 // int; or,
2021120146 // - the enumerated type
2021220147 NewTy = Context.IntTy;
20213- NewWidth = IntWidth ;
20148+ NewWidth = Context.getTargetInfo().getIntWidth() ;
2021420149 NewSign = true;
2021520150 } else if (ECD->getType() == BestType) {
2021620151 // Already the right type!
0 commit comments