Skip to content

Commit 16e015b

Browse files
authored
[NFC][lldb] Use ASTContext::getPromotedIntegerType for integral promotion in DIL (llvm#191448)
1 parent f5eb99a commit 16e015b

7 files changed

Lines changed: 33 additions & 119 deletions

File tree

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ class CompilerType {
226226

227227
bool IsScalarOrUnscopedEnumerationType() const;
228228

229+
/// Checks if the type is eligible for integral promotion.
230+
/// \see GetPromotedIntegerType
229231
bool IsPromotableIntegerType() const;
230232

231233
bool IsPointerToVoid() const;
@@ -508,6 +510,11 @@ class CompilerType {
508510

509511
CompilerType GetTypeForFormatters() const;
510512

513+
/// If the type is promotable, returns the type promoted to a larger
514+
/// integer type according to the type system rules.
515+
/// \see IsPromotableIntegerType
516+
CompilerType GetPromotedIntegerType() const;
517+
511518
LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
512519

513520
bool IsMeaninglessWithoutDynamicResolution() const;

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,10 @@ class TypeSystem : public PluginInterface,
418418
GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx,
419419
bool expand_pack);
420420

421-
// DIL
422-
423-
/// Checks if the type is eligible for integral promotion.
424421
virtual bool IsPromotableIntegerType(lldb::opaque_compiler_type_t type);
425422

426-
/// Perform integral promotion on a given type.
427-
/// This promotes eligible types (boolean, integers, unscoped enumerations)
428-
/// to a larger integer type according to type system rules.
429-
/// \returns Promoted type.
430-
virtual llvm::Expected<CompilerType>
431-
DoIntegralPromotion(CompilerType from, ExecutionContextScope *exe_scope);
423+
virtual CompilerType
424+
GetPromotedIntegerType(lldb::opaque_compiler_type_t type);
432425

433426
// Dumping types
434427

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 8 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -7269,98 +7269,16 @@ CompilerType TypeSystemClang::GetTypeForFormatters(void *type) {
72697269

72707270
bool TypeSystemClang::IsPromotableIntegerType(
72717271
lldb::opaque_compiler_type_t type) {
7272-
// Unscoped enums are always considered as promotable, even if their
7273-
// underlying type does not need to be promoted (e.g. "int").
7274-
bool is_signed = false;
7275-
bool isUnscopedEnumerationType =
7276-
IsEnumerationType(type, is_signed) && !IsScopedEnumerationType(type);
7277-
if (isUnscopedEnumerationType)
7278-
return true;
7279-
7280-
switch (GetBasicTypeEnumeration(type)) {
7281-
case lldb::eBasicTypeBool:
7282-
case lldb::eBasicTypeChar:
7283-
case lldb::eBasicTypeSignedChar:
7284-
case lldb::eBasicTypeUnsignedChar:
7285-
case lldb::eBasicTypeShort:
7286-
case lldb::eBasicTypeUnsignedShort:
7287-
case lldb::eBasicTypeWChar:
7288-
case lldb::eBasicTypeSignedWChar:
7289-
case lldb::eBasicTypeUnsignedWChar:
7290-
case lldb::eBasicTypeChar16:
7291-
case lldb::eBasicTypeChar32:
7292-
return true;
7293-
7294-
default:
7295-
return false;
7296-
}
7297-
7298-
llvm_unreachable("All cases handled above.");
7272+
clang::QualType qual_type(GetCanonicalQualType(type));
7273+
return getASTContext().isPromotableIntegerType(qual_type);
72997274
}
73007275

7301-
llvm::Expected<CompilerType>
7302-
TypeSystemClang::DoIntegralPromotion(CompilerType from,
7303-
ExecutionContextScope *exe_scope) {
7304-
if (!from.IsInteger() && !from.IsUnscopedEnumerationType())
7305-
return from;
7306-
7307-
if (!from.IsPromotableIntegerType())
7308-
return from;
7309-
7310-
if (from.IsUnscopedEnumerationType()) {
7311-
EnumDecl *enum_decl = GetAsEnumDecl(from);
7312-
CompilerType promotion_type = GetType(enum_decl->getPromotionType());
7313-
return DoIntegralPromotion(promotion_type, exe_scope);
7314-
}
7315-
7316-
lldb::BasicType builtin_type =
7317-
from.GetCanonicalType().GetBasicTypeEnumeration();
7318-
uint64_t from_size = 0;
7319-
if (builtin_type == lldb::eBasicTypeWChar ||
7320-
builtin_type == lldb::eBasicTypeSignedWChar ||
7321-
builtin_type == lldb::eBasicTypeUnsignedWChar ||
7322-
builtin_type == lldb::eBasicTypeChar16 ||
7323-
builtin_type == lldb::eBasicTypeChar32) {
7324-
// Find the type that can hold the entire range of values for our type.
7325-
bool is_signed = from.IsSigned();
7326-
llvm::Expected<uint64_t> from_size = from.GetByteSize(exe_scope);
7327-
if (!from_size)
7328-
return from_size.takeError();
7329-
CompilerType promote_types[] = {
7330-
GetBasicTypeFromAST(lldb::eBasicTypeInt),
7331-
GetBasicTypeFromAST(lldb::eBasicTypeUnsignedInt),
7332-
GetBasicTypeFromAST(lldb::eBasicTypeLong),
7333-
GetBasicTypeFromAST(lldb::eBasicTypeUnsignedLong),
7334-
GetBasicTypeFromAST(lldb::eBasicTypeLongLong),
7335-
GetBasicTypeFromAST(lldb::eBasicTypeUnsignedLongLong),
7336-
};
7337-
for (CompilerType &type : promote_types) {
7338-
llvm::Expected<uint64_t> byte_size = type.GetByteSize(exe_scope);
7339-
if (!byte_size)
7340-
return byte_size.takeError();
7341-
if (*from_size < *byte_size ||
7342-
(*from_size == *byte_size && is_signed == type.IsSigned())) {
7343-
return type;
7344-
}
7345-
}
7346-
llvm_unreachable("char type should fit into long long");
7347-
}
7348-
7349-
// Here we can promote only to "int" or "unsigned int".
7350-
CompilerType int_type = GetBasicTypeFromAST(lldb::eBasicTypeInt);
7351-
llvm::Expected<uint64_t> int_byte_size = int_type.GetByteSize(exe_scope);
7352-
if (!int_byte_size)
7353-
return int_byte_size.takeError();
7354-
7355-
// Signed integer types can be safely promoted to "int".
7356-
if (from.IsSigned()) {
7357-
return int_type;
7358-
}
7359-
// Unsigned integer types are promoted to "unsigned int" if "int" cannot hold
7360-
// their entire value range.
7361-
return (from_size == *int_byte_size)
7362-
? GetBasicTypeFromAST(lldb::eBasicTypeUnsignedInt)
7363-
: int_type;
7276+
CompilerType
7277+
TypeSystemClang::GetPromotedIntegerType(lldb::opaque_compiler_type_t type) {
7278+
if (!IsPromotableIntegerType(type))
7279+
return CompilerType();
7280+
clang::QualType qual_type(GetCanonicalQualType(type));
7281+
return GetType(getASTContext().getPromotedIntegerType(qual_type));
73647282
}
73657283

73667284
clang::EnumDecl *TypeSystemClang::GetAsEnumDecl(const CompilerType &type) {

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -941,13 +941,10 @@ class TypeSystemClang : public TypeSystem {
941941

942942
CompilerType GetTypeForFormatters(void *type) override;
943943

944-
// DIL
945-
946944
bool IsPromotableIntegerType(lldb::opaque_compiler_type_t type) override;
947945

948-
llvm::Expected<CompilerType>
949-
DoIntegralPromotion(CompilerType from,
950-
ExecutionContextScope *exe_scope) override;
946+
CompilerType
947+
GetPromotedIntegerType(lldb::opaque_compiler_type_t type) override;
951948

952949
#define LLDB_INVALID_DECL_LEVEL UINT32_MAX
953950
// LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if child_decl_ctx

lldb/source/Symbol/CompilerType.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,13 @@ CompilerType CompilerType::GetTypeForFormatters() const {
10101010
return CompilerType();
10111011
}
10121012

1013+
CompilerType CompilerType::GetPromotedIntegerType() const {
1014+
if (IsValid())
1015+
if (auto type_system_sp = GetTypeSystem())
1016+
return type_system_sp->GetPromotedIntegerType(m_type);
1017+
return CompilerType();
1018+
}
1019+
10131020
LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
10141021
if (IsValid())
10151022
if (auto type_system_sp = GetTypeSystem())

lldb/source/Symbol/TypeSystem.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,9 @@ bool TypeSystem::IsPromotableIntegerType(lldb::opaque_compiler_type_t type) {
123123
return false;
124124
}
125125

126-
llvm::Expected<CompilerType>
127-
TypeSystem::DoIntegralPromotion(CompilerType from,
128-
ExecutionContextScope *exe_scope) {
129-
return llvm::createStringError(
130-
"Integral promotion is not implemented for this TypeSystem");
126+
CompilerType
127+
TypeSystem::GetPromotedIntegerType(lldb::opaque_compiler_type_t type) {
128+
return CompilerType();
131129
}
132130

133131
bool TypeSystem::IsTemplateType(lldb::opaque_compiler_type_t type) {

lldb/source/ValueObject/DILEval.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,10 @@ Interpreter::UnaryConversion(lldb::ValueObjectSP valobj, uint32_t location) {
113113
if (in_type.IsArrayType())
114114
valobj = ArrayToPointerConversion(*valobj, *m_exe_ctx_scope, "result");
115115

116-
if (valobj->GetCompilerType().IsInteger() ||
117-
valobj->GetCompilerType().IsUnscopedEnumerationType()) {
118-
llvm::Expected<CompilerType> promoted_type =
119-
type_system.get()->DoIntegralPromotion(valobj->GetCompilerType(),
120-
m_exe_ctx_scope.get());
121-
if (!promoted_type)
122-
return promoted_type.takeError();
123-
if (!promoted_type->CompareTypes(valobj->GetCompilerType()))
124-
return valobj->CastToBasicType(*promoted_type);
125-
}
116+
CompilerType promoted_type =
117+
valobj->GetCompilerType().GetPromotedIntegerType();
118+
if (promoted_type)
119+
return valobj->CastToBasicType(promoted_type);
126120

127121
return valobj;
128122
}

0 commit comments

Comments
 (0)