Skip to content

Implement a short-circuit case for Clang types in GetNumChildren(). #3651

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
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
42 changes: 27 additions & 15 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2462,21 +2462,33 @@ TypeSystemSwiftTypeRef::GetNumChildren(opaque_compiler_type_t type,
LLDB_SCOPED_TIMER();
FALLBACK(GetNumChildren,
(ReconstructType(type), omit_empty_base_classes, exe_ctx));
if (exe_ctx)
if (auto *exe_scope = exe_ctx->GetBestExecutionContextScope())
if (auto *runtime =
SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
if (auto num_children =
runtime->GetNumChildren(GetCanonicalType(type), nullptr))
// Use a lambda to intercept and unwrap the `Optional` return value.
// Optional<uint32_t> uses more lax equivalency function.
return [&]() -> llvm::Optional<uint32_t> {
auto impl = [&]() { return num_children; };
VALIDATE_AND_RETURN(
impl, GetNumChildren, type,
(ReconstructType(type), omit_empty_base_classes, exe_ctx),
(ReconstructType(type), omit_empty_base_classes, exe_ctx));
}().getValueOr(0);

auto impl = [&]() -> llvm::Optional<uint32_t> {
if (exe_ctx)
if (auto *exe_scope = exe_ctx->GetBestExecutionContextScope())
if (auto *runtime =
SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
return runtime->GetNumChildren(GetCanonicalType(type), nullptr);

if (CompilerType clang_type = GetAsClangTypeOrNull(type)) {
bool is_signed;
// Clang-imported enum types always have one child in Swift.
if (clang_type.IsEnumerationType(is_signed))
return 1;
return clang_type.GetNumChildren(omit_empty_base_classes, exe_ctx);
}
return {};
};
if (llvm::Optional<uint32_t> num_children = impl())
// Use a lambda to intercept and unwrap the `Optional` return value.
// Optional<uint32_t> uses more lax equivalency function.
return [&]() -> llvm::Optional<uint32_t> {
auto impl = [&]() { return num_children; };
VALIDATE_AND_RETURN(
impl, GetNumChildren, type,
(ReconstructType(type), omit_empty_base_classes, exe_ctx),
(ReconstructType(type), omit_empty_base_classes, exe_ctx));
}().getValueOr(0);

LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
"Using SwiftASTContext::GetNumChildren fallback for type %s",
Expand Down