Skip to content

Commit 8ffea27

Browse files
committed
[lldb] Refactor and test TypeSystemClang::GetEnumerationIntegerType
1 parent 9063022 commit 8ffea27

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,7 @@ TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
790790
attrs.name.GetCString(), GetClangDeclContextContainingDIE(die, nullptr),
791791
attrs.decl, enumerator_clang_type, attrs.is_scoped_enum);
792792
} else {
793-
enumerator_clang_type =
794-
m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType());
793+
enumerator_clang_type = m_ast.GetEnumerationIntegerType(clang_type);
795794
}
796795

797796
LinkDeclContextToDIE(TypeSystemClang::GetDeclContextForType(clang_type), die);

lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,7 @@ bool PDBASTParser::AddEnumValue(CompilerType enum_type,
11541154
default:
11551155
return false;
11561156
}
1157-
CompilerType underlying_type =
1158-
m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
1157+
CompilerType underlying_type = m_ast.GetEnumerationIntegerType(enum_type);
11591158
uint32_t byte_size = m_ast.getASTContext().getTypeSize(
11601159
ClangUtil::GetQualType(underlying_type));
11611160
auto enum_constant_decl = m_ast.AddEnumerationValueToEnumerationType(

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7899,8 +7899,7 @@ clang::EnumConstantDecl *TypeSystemClang::AddEnumerationValueToEnumerationType(
78997899
clang::EnumConstantDecl *TypeSystemClang::AddEnumerationValueToEnumerationType(
79007900
const CompilerType &enum_type, const Declaration &decl, const char *name,
79017901
int64_t enum_value, uint32_t enum_value_bit_size) {
7902-
CompilerType underlying_type =
7903-
GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
7902+
CompilerType underlying_type = GetEnumerationIntegerType(enum_type);
79047903
bool is_signed = false;
79057904
underlying_type.IsIntegerType(is_signed);
79067905

@@ -7910,20 +7909,14 @@ clang::EnumConstantDecl *TypeSystemClang::AddEnumerationValueToEnumerationType(
79107909
return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
79117910
}
79127911

7913-
CompilerType
7914-
TypeSystemClang::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
7915-
clang::QualType enum_qual_type(GetCanonicalQualType(type));
7916-
const clang::Type *clang_type = enum_qual_type.getTypePtr();
7917-
if (clang_type) {
7918-
const clang::EnumType *enutype =
7919-
llvm::dyn_cast<clang::EnumType>(clang_type);
7920-
if (enutype) {
7921-
clang::EnumDecl *enum_decl = enutype->getDecl();
7922-
if (enum_decl)
7923-
return GetType(enum_decl->getIntegerType());
7924-
}
7925-
}
7926-
return CompilerType();
7912+
CompilerType TypeSystemClang::GetEnumerationIntegerType(CompilerType type) {
7913+
clang::QualType qt(ClangUtil::GetQualType(type));
7914+
const clang::Type *clang_type = qt.getTypePtrOrNull();
7915+
const auto *enum_type = llvm::dyn_cast_or_null<clang::EnumType>(clang_type);
7916+
if (!enum_type)
7917+
return CompilerType();
7918+
7919+
return GetType(enum_type->getDecl()->getIntegerType());
79277920
}
79287921

79297922
CompilerType

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,10 @@ class TypeSystemClang : public TypeSystem {
857857
const CompilerType &enum_type, const Declaration &decl, const char *name,
858858
const llvm::APSInt &value);
859859

860-
CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type);
860+
/// Returns the underlying integer type for an enum type. If the given type
861+
/// is invalid or not an enum-type, the function returns an invalid
862+
/// CompilerType.
863+
CompilerType GetEnumerationIntegerType(CompilerType type);
861864

862865
// Pointers & References
863866

lldb/unittests/Symbol/TestTypeSystemClang.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,42 @@ TEST_F(TestTypeSystemClang, TestDisplayNameEmpty) {
231231
EXPECT_EQ("", ast.getDisplayName());
232232
}
233233

234+
TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeInvalid) {
235+
EXPECT_FALSE(m_ast->GetEnumerationIntegerType(CompilerType()).IsValid());
236+
}
237+
238+
TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeUnexpectedType) {
239+
CompilerType int_type = m_ast->GetBasicType(lldb::eBasicTypeInt);
240+
CompilerType t = m_ast->GetEnumerationIntegerType(int_type);
241+
EXPECT_FALSE(t.IsValid());
242+
}
243+
244+
TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeBasicTypes) {
245+
// All possible underlying integer types of enums.
246+
const std::vector<lldb::BasicType> types_to_test = {
247+
eBasicTypeInt, eBasicTypeUnsignedInt, eBasicTypeLong,
248+
eBasicTypeUnsignedLong, eBasicTypeLongLong, eBasicTypeUnsignedLongLong,
249+
};
250+
251+
for (bool scoped : {true, false}) {
252+
SCOPED_TRACE("scoped: " + std::to_string(scoped));
253+
for (lldb::BasicType basic_type : types_to_test) {
254+
SCOPED_TRACE(std::to_string(basic_type));
255+
256+
TypeSystemClang ast("enum_ast", HostInfo::GetTargetTriple());
257+
CompilerType basic_compiler_type = ast.GetBasicType(basic_type);
258+
EXPECT_TRUE(basic_compiler_type.IsValid());
259+
260+
CompilerType enum_type =
261+
ast.CreateEnumerationType("my_enum", ast.GetTranslationUnitDecl(),
262+
Declaration(), basic_compiler_type, scoped);
263+
CompilerType t = ast.GetEnumerationIntegerType(enum_type);
264+
// Check that the type we put in at the start is found again.
265+
EXPECT_EQ(basic_compiler_type.GetTypeName(), t.GetTypeName());
266+
}
267+
}
268+
}
269+
234270
TEST_F(TestTypeSystemClang, TestIsClangType) {
235271
clang::ASTContext &context = m_ast->getASTContext();
236272
lldb::opaque_compiler_type_t bool_ctype =

0 commit comments

Comments
 (0)