[Lldb-commits] [lldb] 8ffea27 - [lldb] Refactor and test TypeSystemClang::GetEnumerationIntegerType
Raphael Isemann via lldb-commits
lldb-commits at lists.llvm.org
Tue Feb 18 00:53:59 PST 2020
Author: Raphael Isemann
Date: 2020-02-18T09:52:49+01:00
New Revision: 8ffea27ae42ad210b9162c3afc91c61dfb82acf7
URL: https://github.com/llvm/llvm-project/commit/8ffea27ae42ad210b9162c3afc91c61dfb82acf7
DIFF: https://github.com/llvm/llvm-project/commit/8ffea27ae42ad210b9162c3afc91c61dfb82acf7.diff
LOG: [lldb] Refactor and test TypeSystemClang::GetEnumerationIntegerType
Added:
Modified:
lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
lldb/unittests/Symbol/TestTypeSystemClang.cpp
Removed:
################################################################################
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 5b3ea8838786..02f481684cd7 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -790,8 +790,7 @@ TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
attrs.name.GetCString(), GetClangDeclContextContainingDIE(die, nullptr),
attrs.decl, enumerator_clang_type, attrs.is_scoped_enum);
} else {
- enumerator_clang_type =
- m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType());
+ enumerator_clang_type = m_ast.GetEnumerationIntegerType(clang_type);
}
LinkDeclContextToDIE(TypeSystemClang::GetDeclContextForType(clang_type), die);
diff --git a/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp b/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
index 48bc57272c2a..a03878890d5c 100644
--- a/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
+++ b/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
@@ -1154,8 +1154,7 @@ bool PDBASTParser::AddEnumValue(CompilerType enum_type,
default:
return false;
}
- CompilerType underlying_type =
- m_ast.GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
+ CompilerType underlying_type = m_ast.GetEnumerationIntegerType(enum_type);
uint32_t byte_size = m_ast.getASTContext().getTypeSize(
ClangUtil::GetQualType(underlying_type));
auto enum_constant_decl = m_ast.AddEnumerationValueToEnumerationType(
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 2fa5dc38eb8e..475c927f6114 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -7899,8 +7899,7 @@ clang::EnumConstantDecl *TypeSystemClang::AddEnumerationValueToEnumerationType(
clang::EnumConstantDecl *TypeSystemClang::AddEnumerationValueToEnumerationType(
const CompilerType &enum_type, const Declaration &decl, const char *name,
int64_t enum_value, uint32_t enum_value_bit_size) {
- CompilerType underlying_type =
- GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
+ CompilerType underlying_type = GetEnumerationIntegerType(enum_type);
bool is_signed = false;
underlying_type.IsIntegerType(is_signed);
@@ -7910,20 +7909,14 @@ clang::EnumConstantDecl *TypeSystemClang::AddEnumerationValueToEnumerationType(
return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
}
-CompilerType
-TypeSystemClang::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
- clang::QualType enum_qual_type(GetCanonicalQualType(type));
- const clang::Type *clang_type = enum_qual_type.getTypePtr();
- if (clang_type) {
- const clang::EnumType *enutype =
- llvm::dyn_cast<clang::EnumType>(clang_type);
- if (enutype) {
- clang::EnumDecl *enum_decl = enutype->getDecl();
- if (enum_decl)
- return GetType(enum_decl->getIntegerType());
- }
- }
- return CompilerType();
+CompilerType TypeSystemClang::GetEnumerationIntegerType(CompilerType type) {
+ clang::QualType qt(ClangUtil::GetQualType(type));
+ const clang::Type *clang_type = qt.getTypePtrOrNull();
+ const auto *enum_type = llvm::dyn_cast_or_null<clang::EnumType>(clang_type);
+ if (!enum_type)
+ return CompilerType();
+
+ return GetType(enum_type->getDecl()->getIntegerType());
}
CompilerType
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
index dc9a10bd61a0..2fd12c81c133 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
@@ -857,7 +857,10 @@ class TypeSystemClang : public TypeSystem {
const CompilerType &enum_type, const Declaration &decl, const char *name,
const llvm::APSInt &value);
- CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type);
+ /// Returns the underlying integer type for an enum type. If the given type
+ /// is invalid or not an enum-type, the function returns an invalid
+ /// CompilerType.
+ CompilerType GetEnumerationIntegerType(CompilerType type);
// Pointers & References
diff --git a/lldb/unittests/Symbol/TestTypeSystemClang.cpp b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
index b0a42a26fd6c..a3345e815ee9 100644
--- a/lldb/unittests/Symbol/TestTypeSystemClang.cpp
+++ b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
@@ -231,6 +231,42 @@ TEST_F(TestTypeSystemClang, TestDisplayNameEmpty) {
EXPECT_EQ("", ast.getDisplayName());
}
+TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeInvalid) {
+ EXPECT_FALSE(m_ast->GetEnumerationIntegerType(CompilerType()).IsValid());
+}
+
+TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeUnexpectedType) {
+ CompilerType int_type = m_ast->GetBasicType(lldb::eBasicTypeInt);
+ CompilerType t = m_ast->GetEnumerationIntegerType(int_type);
+ EXPECT_FALSE(t.IsValid());
+}
+
+TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeBasicTypes) {
+ // All possible underlying integer types of enums.
+ const std::vector<lldb::BasicType> types_to_test = {
+ eBasicTypeInt, eBasicTypeUnsignedInt, eBasicTypeLong,
+ eBasicTypeUnsignedLong, eBasicTypeLongLong, eBasicTypeUnsignedLongLong,
+ };
+
+ for (bool scoped : {true, false}) {
+ SCOPED_TRACE("scoped: " + std::to_string(scoped));
+ for (lldb::BasicType basic_type : types_to_test) {
+ SCOPED_TRACE(std::to_string(basic_type));
+
+ TypeSystemClang ast("enum_ast", HostInfo::GetTargetTriple());
+ CompilerType basic_compiler_type = ast.GetBasicType(basic_type);
+ EXPECT_TRUE(basic_compiler_type.IsValid());
+
+ CompilerType enum_type =
+ ast.CreateEnumerationType("my_enum", ast.GetTranslationUnitDecl(),
+ Declaration(), basic_compiler_type, scoped);
+ CompilerType t = ast.GetEnumerationIntegerType(enum_type);
+ // Check that the type we put in at the start is found again.
+ EXPECT_EQ(basic_compiler_type.GetTypeName(), t.GetTypeName());
+ }
+ }
+}
+
TEST_F(TestTypeSystemClang, TestIsClangType) {
clang::ASTContext &context = m_ast->getASTContext();
lldb::opaque_compiler_type_t bool_ctype =
More information about the lldb-commits
mailing list