Skip to content

Make SwiftOptionSetSummaryProvider reject other types early (NFC) #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 35 additions & 39 deletions lldb/source/Plugins/Language/Swift/SwiftOptionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,39 @@ using namespace lldb_private;
using namespace lldb_private::formatters;
using namespace lldb_private::formatters::swift;

bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
WouldEvenConsiderFormatting(CompilerType clang_type) {
/// If this is a Clang enum wrapped in a Swift type, return the clang::EnumDecl.
static clang::EnumDecl *GetAsEnumDecl(CompilerType swift_type) {
if (!swift_type)
return nullptr;

SwiftASTContext *swift_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContext>(clang_type.GetTypeSystem());
llvm::dyn_cast_or_null<SwiftASTContext>(swift_type.GetTypeSystem());
if (!swift_ast_ctx)
return false;
return nullptr;

CompilerType clang_type;
if (!swift_ast_ctx->IsImportedType(swift_type, &clang_type))
return nullptr;

if (!clang_type.IsValid())
return nullptr;

if (!llvm::isa<ClangASTContext>(clang_type.GetTypeSystem()))
return nullptr;

auto qual_type =
clang::QualType::getFromOpaquePtr(clang_type.GetOpaqueQualType());
if (qual_type->getTypeClass() != clang::Type::TypeClass::Enum)
return nullptr;

if (const clang::EnumType *enum_type = qual_type->getAs<clang::EnumType>())
return enum_type->getDecl();
return nullptr;
}

return clang_type.IsValid() &&
swift_ast_ctx->IsImportedType(clang_type, nullptr);
bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
WouldEvenConsiderFormatting(CompilerType swift_type) {
return GetAsEnumDecl(swift_type);
}

lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
Expand All @@ -56,55 +80,27 @@ static ConstString GetDisplayCaseName(::swift::ClangImporter *clang_importer,
return ConstString(case_decl->getName());
}

static clang::EnumDecl *GetAsEnumDecl(const CompilerType &compiler_type) {
if (compiler_type.IsValid() &&
llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem())) {
opaque_compiler_type_t clang_type = compiler_type.GetOpaqueQualType();
clang::QualType qual_type = clang::QualType::getFromOpaquePtr(clang_type);
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
switch (type_class) {
case clang::Type::TypeClass::Enum: {
if (const clang::EnumType *enum_type =
qual_type->getAs<clang::EnumType>())
return enum_type->getDecl();
break;
}
default:
break;
}
}

return nullptr;
}

void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
FillCasesIfNeeded() {
if (m_cases.hasValue())
return;

m_cases = CasesVector();
SwiftASTContext *swift_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContext>(m_type.GetTypeSystem());

if (!swift_ast_ctx)
return;
CompilerType original_type;
if (!swift_ast_ctx->IsImportedType(m_type, &original_type))
return;
clang::EnumDecl *enum_decl = GetAsEnumDecl(original_type);
clang::EnumDecl *enum_decl = GetAsEnumDecl(m_type);
if (!enum_decl)
return;

SwiftASTContext *swift_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContext>(m_type.GetTypeSystem());
::swift::ClangImporter *clang_importer = swift_ast_ctx->GetClangImporter();
auto iter = enum_decl->enumerator_begin(), end = enum_decl->enumerator_end();
for (; iter != end; ++iter) {
clang::EnumConstantDecl *case_decl = *iter;
if (case_decl) {
llvm::APInt case_init_val(case_decl->getInitVal());
// extend all cases to 64 bits so that equality check is fast
// Extend all cases to 64 bits so that equality check is fast
// but if they are larger than 64, I am going to get out of that
// case
// and then pick it up again as unmatched data at the end
// case and then pick it up again as unmatched data at the end.
if (case_init_val.getBitWidth() < 64)
case_init_val = case_init_val.zext(64);
if (case_init_val.getBitWidth() > 64)
Expand Down