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);
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(
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);
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
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
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 =