Skip to content

Commit c9bf5ff

Browse files
committed
Make SwiftOptionSetSummaryProvider reject other types early (NFC)
Prior to this patch SwiftOptionSetSummaryProvider would bregister itself for any Clang-imported type, but only provide a non-empty summary for enums. By moving the enum check into WouldEvenConsiderFormatting, this is avoided. rdar://problem/55026321
1 parent a1a45d3 commit c9bf5ff

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

lldb/source/Plugins/Language/Swift/SwiftOptionSet.cpp

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,39 @@ using namespace lldb_private;
2828
using namespace lldb_private::formatters;
2929
using namespace lldb_private::formatters::swift;
3030

31-
bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
32-
WouldEvenConsiderFormatting(CompilerType clang_type) {
31+
/// If this is a Clang enum wrapped in a Swift type, return the clang::EnumDecl.
32+
static clang::EnumDecl *GetAsEnumDecl(CompilerType swift_type) {
33+
if (!swift_type)
34+
return nullptr;
35+
3336
SwiftASTContext *swift_ast_ctx =
34-
llvm::dyn_cast_or_null<SwiftASTContext>(clang_type.GetTypeSystem());
37+
llvm::dyn_cast_or_null<SwiftASTContext>(swift_type.GetTypeSystem());
3538
if (!swift_ast_ctx)
36-
return false;
39+
return nullptr;
40+
41+
CompilerType clang_type;
42+
if (!swift_ast_ctx->IsImportedType(swift_type, &clang_type))
43+
return nullptr;
44+
45+
if (!clang_type.IsValid())
46+
return nullptr;
47+
48+
if (!llvm::isa<ClangASTContext>(clang_type.GetTypeSystem()))
49+
return nullptr;
50+
51+
auto qual_type =
52+
clang::QualType::getFromOpaquePtr(clang_type.GetOpaqueQualType());
53+
if (qual_type->getTypeClass() != clang::Type::TypeClass::Enum)
54+
return nullptr;
55+
56+
if (const clang::EnumType *enum_type = qual_type->getAs<clang::EnumType>())
57+
return enum_type->getDecl();
58+
return nullptr;
59+
}
3760

38-
return clang_type.IsValid() &&
39-
swift_ast_ctx->IsImportedType(clang_type, nullptr);
61+
bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
62+
WouldEvenConsiderFormatting(CompilerType swift_type) {
63+
return GetAsEnumDecl(swift_type);
4064
}
4165

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

59-
static clang::EnumDecl *GetAsEnumDecl(const CompilerType &compiler_type) {
60-
if (compiler_type.IsValid() &&
61-
llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem())) {
62-
opaque_compiler_type_t clang_type = compiler_type.GetOpaqueQualType();
63-
clang::QualType qual_type = clang::QualType::getFromOpaquePtr(clang_type);
64-
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
65-
switch (type_class) {
66-
case clang::Type::TypeClass::Enum: {
67-
if (const clang::EnumType *enum_type =
68-
qual_type->getAs<clang::EnumType>())
69-
return enum_type->getDecl();
70-
break;
71-
}
72-
default:
73-
break;
74-
}
75-
}
76-
77-
return nullptr;
78-
}
79-
8083
void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
8184
FillCasesIfNeeded() {
8285
if (m_cases.hasValue())
8386
return;
8487

8588
m_cases = CasesVector();
86-
SwiftASTContext *swift_ast_ctx =
87-
llvm::dyn_cast_or_null<SwiftASTContext>(m_type.GetTypeSystem());
88-
89-
if (!swift_ast_ctx)
90-
return;
91-
CompilerType original_type;
92-
if (!swift_ast_ctx->IsImportedType(m_type, &original_type))
93-
return;
94-
clang::EnumDecl *enum_decl = GetAsEnumDecl(original_type);
89+
clang::EnumDecl *enum_decl = GetAsEnumDecl(m_type);
9590
if (!enum_decl)
9691
return;
9792

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

0 commit comments

Comments
 (0)