Skip to content

Commit 9b6c27b

Browse files
committed
Implement a short-circuit case for Clang types in GetNumChildren().
This is avoids round-tripping the type through SwiftASTContext which is both faster and more reliable. rdar://86180838
1 parent 9d92e4f commit 9b6c27b

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,21 +2462,33 @@ TypeSystemSwiftTypeRef::GetNumChildren(opaque_compiler_type_t type,
24622462
LLDB_SCOPED_TIMER();
24632463
FALLBACK(GetNumChildren,
24642464
(ReconstructType(type), omit_empty_base_classes, exe_ctx));
2465-
if (exe_ctx)
2466-
if (auto *exe_scope = exe_ctx->GetBestExecutionContextScope())
2467-
if (auto *runtime =
2468-
SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
2469-
if (auto num_children =
2470-
runtime->GetNumChildren(GetCanonicalType(type), nullptr))
2471-
// Use a lambda to intercept and unwrap the `Optional` return value.
2472-
// Optional<uint32_t> uses more lax equivalency function.
2473-
return [&]() -> llvm::Optional<uint32_t> {
2474-
auto impl = [&]() { return num_children; };
2475-
VALIDATE_AND_RETURN(
2476-
impl, GetNumChildren, type,
2477-
(ReconstructType(type), omit_empty_base_classes, exe_ctx),
2478-
(ReconstructType(type), omit_empty_base_classes, exe_ctx));
2479-
}().getValueOr(0);
2465+
2466+
auto impl = [&]() -> llvm::Optional<uint32_t> {
2467+
if (exe_ctx)
2468+
if (auto *exe_scope = exe_ctx->GetBestExecutionContextScope())
2469+
if (auto *runtime =
2470+
SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
2471+
return runtime->GetNumChildren(GetCanonicalType(type), nullptr);
2472+
2473+
if (CompilerType clang_type = GetAsClangTypeOrNull(type)) {
2474+
bool is_signed;
2475+
// Clang-imported enum types always have one child in Swift.
2476+
if (clang_type.IsEnumerationType(is_signed))
2477+
return 1;
2478+
return clang_type.GetNumChildren(omit_empty_base_classes, exe_ctx);
2479+
}
2480+
return {};
2481+
};
2482+
if (llvm::Optional<uint32_t> num_children = impl())
2483+
// Use a lambda to intercept and unwrap the `Optional` return value.
2484+
// Optional<uint32_t> uses more lax equivalency function.
2485+
return [&]() -> llvm::Optional<uint32_t> {
2486+
auto impl = [&]() { return num_children; };
2487+
VALIDATE_AND_RETURN(
2488+
impl, GetNumChildren, type,
2489+
(ReconstructType(type), omit_empty_base_classes, exe_ctx),
2490+
(ReconstructType(type), omit_empty_base_classes, exe_ctx));
2491+
}().getValueOr(0);
24802492

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

0 commit comments

Comments
 (0)