From 83bafded23da634f924bce76093ccf64b4da1802 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Wed, 6 Aug 2025 08:30:57 -0700 Subject: [PATCH] [lldb] Compute reflection context in TaskSyntheticFrontEnd --- .../Language/Swift/SwiftFormatters.cpp | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp b/lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp index 2eaf88d0c7738..4d9362e8a55c1 100644 --- a/lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp +++ b/lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp @@ -917,14 +917,15 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd { // Remove any bogus child tasks. // Very rarely, the child tasks include a bogus task which has an // invalid task id of 0. - llvm::erase_if(tasks, [&](auto task_ptr) { - if (auto task_info = - expectedToOptional(m_reflection_ctx->asyncTaskInfo(task_ptr))) - return task_info->id == 0; - // Don't filter children with errors here. Let these tasks reach the - // formatter's existing error handling. - return false; - }); + if (auto reflection_ctx = GetReflectionContext()) + llvm::erase_if(tasks, [&](auto task_ptr) { + if (auto task_info = + expectedToOptional(reflection_ctx->asyncTaskInfo(task_ptr))) + return task_info->id == 0; + // Don't filter children with errors here. Let these tasks reach the + // formatter's existing error handling. + return false; + }); std::string mangled_typename = mangledTypenameForTasksTuple(tasks.size()); @@ -970,15 +971,14 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd { } lldb::ChildCacheState Update() override { - if (auto *runtime = SwiftLanguageRuntime::Get(m_backend.GetProcessSP())) { - m_reflection_ctx = runtime->GetReflectionContext(); + if (auto reflection_ctx = GetReflectionContext()) { ValueObjectSP task_obj_sp = m_backend.GetChildMemberWithName("_task"); if (!task_obj_sp) return ChildCacheState::eRefetch; m_task_ptr = task_obj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); if (m_task_ptr != LLDB_INVALID_ADDRESS) { llvm::Expected task_info = - m_reflection_ctx->asyncTaskInfo(m_task_ptr); + reflection_ctx->asyncTaskInfo(m_task_ptr); if (auto err = task_info.takeError()) { LLDB_LOG_ERROR( GetLog(LLDBLog::DataFormatters | LLDBLog::Types), std::move(err), @@ -1010,7 +1010,12 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd { } private: - ThreadSafeReflectionContext m_reflection_ctx; + ThreadSafeReflectionContext GetReflectionContext() { + if (auto *runtime = SwiftLanguageRuntime::Get(m_backend.GetProcessSP())) + return runtime->GetReflectionContext(); + return {}; + } + TypeSystemSwiftTypeRef *m_ts = nullptr; addr_t m_task_ptr = LLDB_INVALID_ADDRESS; ReflectionContextInterface::AsyncTaskInfo m_task_info;