@@ -20043,10 +20043,6 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
20043
20043
return;
20044
20044
}
20045
20045
20046
- unsigned IntWidth = Context.getTargetInfo().getIntWidth();
20047
- unsigned CharWidth = Context.getTargetInfo().getCharWidth();
20048
- unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
20049
-
20050
20046
// Verify that all the values are okay, compute the size of the values, and
20051
20047
// reverse the list.
20052
20048
unsigned NumNegativeBits = 0;
@@ -20112,73 +20108,12 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
20112
20108
BestPromotionType = BestType;
20113
20109
20114
20110
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);
20144
20111
} 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);
20182
20117
}
20183
20118
20184
20119
// Loop over all of the enumerator constants, changing their types to match
@@ -20210,7 +20145,7 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
20210
20145
// int; or,
20211
20146
// - the enumerated type
20212
20147
NewTy = Context.IntTy;
20213
- NewWidth = IntWidth ;
20148
+ NewWidth = Context.getTargetInfo().getIntWidth() ;
20214
20149
NewSign = true;
20215
20150
} else if (ECD->getType() == BestType) {
20216
20151
// Already the right type!
0 commit comments