Skip to content

Commit 4abfc5a

Browse files
committed
Use computeBestEnumTypes() from ASTContext & cleanup code
1 parent 77c262d commit 4abfc5a

File tree

3 files changed

+2
-106
lines changed

3 files changed

+2
-106
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3994,13 +3994,6 @@ class Sema final : public SemaBase {
39943994
SourceLocation IdLoc, IdentifierInfo *Id,
39953995
const ParsedAttributesView &Attrs,
39963996
SourceLocation EqualLoc, Expr *Val);
3997-
3998-
bool ComputeBestEnumProperties(ASTContext &Context, EnumDecl *Enum,
3999-
bool isCpp, bool isPacked,
4000-
unsigned NumNegativeBits,
4001-
unsigned NumPositiveBits, unsigned &BestWidth,
4002-
QualType &BestType,
4003-
QualType &BestPromotionType);
40043997
void ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
40053998
Decl *EnumDecl, ArrayRef<Decl *> Elements, Scope *S,
40063999
const ParsedAttributesView &Attr);

clang/lib/Sema/SemaDecl.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -20036,87 +20036,6 @@ bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const llvm::APInt &Val,
2003620036
return !(FlagMask & Val) || (AllowMask && !(FlagMask & ~Val));
2003720037
}
2003820038

20039-
bool Sema::ComputeBestEnumProperties(ASTContext &Context, EnumDecl *Enum,
20040-
bool is_cpp, bool isPacked,
20041-
unsigned NumNegativeBits,
20042-
unsigned NumPositiveBits,
20043-
unsigned &BestWidth, QualType &BestType,
20044-
QualType &BestPromotionType) {
20045-
unsigned IntWidth = Context.getTargetInfo().getIntWidth();
20046-
unsigned CharWidth = Context.getTargetInfo().getCharWidth();
20047-
unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
20048-
bool enum_too_large = false;
20049-
if (NumNegativeBits) {
20050-
// If there is a negative value, figure out the smallest integer type (of
20051-
// int/long/longlong) that fits.
20052-
// If it's packed, check also if it fits a char or a short.
20053-
if (isPacked && NumNegativeBits <= CharWidth &&
20054-
NumPositiveBits < CharWidth) {
20055-
BestType = Context.SignedCharTy;
20056-
BestWidth = CharWidth;
20057-
} else if (isPacked && NumNegativeBits <= ShortWidth &&
20058-
NumPositiveBits < ShortWidth) {
20059-
BestType = Context.ShortTy;
20060-
BestWidth = ShortWidth;
20061-
} else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
20062-
BestType = Context.IntTy;
20063-
BestWidth = IntWidth;
20064-
} else {
20065-
BestWidth = Context.getTargetInfo().getLongWidth();
20066-
20067-
if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) {
20068-
BestType = Context.LongTy;
20069-
} else {
20070-
BestWidth = Context.getTargetInfo().getLongLongWidth();
20071-
20072-
if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
20073-
enum_too_large = true;
20074-
BestType = Context.LongLongTy;
20075-
}
20076-
}
20077-
BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType);
20078-
} else {
20079-
// If there is no negative value, figure out the smallest type that fits
20080-
// all of the enumerator values.
20081-
// If it's packed, check also if it fits a char or a short.
20082-
if (isPacked && NumPositiveBits <= CharWidth) {
20083-
BestType = Context.UnsignedCharTy;
20084-
BestPromotionType = Context.IntTy;
20085-
BestWidth = CharWidth;
20086-
} else if (isPacked && NumPositiveBits <= ShortWidth) {
20087-
BestType = Context.UnsignedShortTy;
20088-
BestPromotionType = Context.IntTy;
20089-
BestWidth = ShortWidth;
20090-
} else if (NumPositiveBits <= IntWidth) {
20091-
BestType = Context.UnsignedIntTy;
20092-
BestWidth = IntWidth;
20093-
BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
20094-
? Context.UnsignedIntTy
20095-
: Context.IntTy;
20096-
} else if (NumPositiveBits <=
20097-
(BestWidth = Context.getTargetInfo().getLongWidth())) {
20098-
BestType = Context.UnsignedLongTy;
20099-
BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
20100-
? Context.UnsignedLongTy
20101-
: Context.LongTy;
20102-
} else {
20103-
BestWidth = Context.getTargetInfo().getLongLongWidth();
20104-
if (NumPositiveBits > BestWidth) {
20105-
// This can happen with bit-precise integer types, but those are not
20106-
// allowed as the type for an enumerator per C23 6.7.2.2p4 and p12.
20107-
// FIXME: GCC uses __int128_t and __uint128_t for cases that fit within
20108-
// a 128-bit integer, we should consider doing the same.
20109-
enum_too_large = true;
20110-
}
20111-
BestType = Context.UnsignedLongLongTy;
20112-
BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
20113-
? Context.UnsignedLongLongTy
20114-
: Context.LongLongTy;
20115-
}
20116-
}
20117-
return enum_too_large;
20118-
}
20119-
2012020039
void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
2012120040
Decl *EnumDeclX, ArrayRef<Decl *> Elements, Scope *S,
2012220041
const ParsedAttributesView &Attrs) {

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,12 +2387,6 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
23872387
}
23882388
}
23892389

2390-
/// The following code follows the same logic as in Sema::ActOnEnumBody
2391-
/// clang/lib/Sema/SemaDecl.cpp
2392-
// If we have an empty set of enumerators we still need one bit.
2393-
// From [dcl.enum]p8
2394-
// If the enumerator-list is empty, the values of the enumeration are as if
2395-
// the enumeration had a single enumerator with value 0
23962390
if (!NumPositiveBits && !NumNegativeBits)
23972391
NumPositiveBits = 1;
23982392

@@ -2401,20 +2395,10 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
24012395
enum_decl->setNumPositiveBits(NumPositiveBits);
24022396
enum_decl->setNumNegativeBits(NumNegativeBits);
24032397

2404-
auto ts_ptr = clang_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
2405-
if (!ts_ptr)
2406-
return enumerators_added;
2407-
24082398
clang::QualType BestPromotionType;
24092399
clang::QualType BestType;
2410-
unsigned BestWidth;
2411-
2412-
auto &Context = m_ast.getASTContext();
2413-
bool is_cpp = Language::LanguageIsCPlusPlus(
2414-
SymbolFileDWARF::GetLanguage(*parent_die.GetCU()));
2415-
ts_ptr->getSema()->ComputeBestEnumProperties(
2416-
Context, enum_decl, is_cpp, false, NumNegativeBits, NumPositiveBits,
2417-
BestWidth, BestType, BestPromotionType);
2400+
m_ast.getASTContext().computeBestEnumTypes(
2401+
false, NumNegativeBits, NumPositiveBits, BestType, BestPromotionType);
24182402
enum_decl->setPromotionType(BestPromotionType);
24192403

24202404
return enumerators_added;

0 commit comments

Comments
 (0)