From 91e5f2f36f486eac301e375f37e3b8bc5335378e Mon Sep 17 00:00:00 2001 From: Alex Langford Date: Wed, 3 Jul 2019 08:35:44 -0700 Subject: [PATCH] [Target] Centralize creation of scratch SwiftASTContexts I don't think it makes sense for ValueObject to know about SwiftASTContexts, so we should centralize the creation logic into Target. --- include/lldb/Core/ValueObject.h | 3 -- include/lldb/Target/Target.h | 5 +++ source/Core/ValueObject.cpp | 12 +----- source/Core/ValueObjectDynamicValue.cpp | 11 ++++-- .../Language/Swift/SwiftHashedContainer.cpp | 6 ++- source/Target/SwiftLanguageRuntime.cpp | 37 +++++++++++++------ source/Target/Target.cpp | 8 ++++ 7 files changed, 53 insertions(+), 29 deletions(-) diff --git a/include/lldb/Core/ValueObject.h b/include/lldb/Core/ValueObject.h index 4924d1bccce4..0d881eb99068 100644 --- a/include/lldb/Core/ValueObject.h +++ b/include/lldb/Core/ValueObject.h @@ -9,7 +9,6 @@ #ifndef liblldb_ValueObject_h_ #define liblldb_ValueObject_h_ -#include "lldb/Core/SwiftASTContextReader.h" #include "lldb/Core/Value.h" #include "lldb/Symbol/CompilerType.h" #include "lldb/Symbol/Type.h" @@ -614,8 +613,6 @@ class ValueObject : public UserID { virtual bool HasSyntheticValue(); - SwiftASTContextReader GetScratchSwiftASTContext(); - virtual bool IsSynthetic() { return false; } lldb::ValueObjectSP diff --git a/include/lldb/Target/Target.h b/include/lldb/Target/Target.h index e3771e6b01d9..01dbce672485 100644 --- a/include/lldb/Target/Target.h +++ b/include/lldb/Target/Target.h @@ -24,6 +24,7 @@ #include "lldb/Core/Architecture.h" #include "lldb/Core/Disassembler.h" #include "lldb/Core/ModuleList.h" +#include "lldb/Core/SwiftASTContextReader.h" #include "lldb/Core/UserSettingsController.h" #include "lldb/Expression/Expression.h" #include "lldb/Interpreter/OptionValueBoolean.h" @@ -1211,6 +1212,10 @@ class Target : public std::enable_shared_from_this, GetScratchSwiftASTContext(Status &error, ExecutionContextScope &exe_scope, bool create_on_demand = true); + SwiftASTContextReader GetScratchSwiftASTContext(Status &error, + ValueObject &valobj, + bool create_on_demand = true); + private: void DisplayFallbackSwiftContextErrors(SwiftASTContext *swift_ast_ctx); public: diff --git a/source/Core/ValueObject.cpp b/source/Core/ValueObject.cpp index 09551db125c5..c3c50c2b8908 100644 --- a/source/Core/ValueObject.cpp +++ b/source/Core/ValueObject.cpp @@ -10,6 +10,7 @@ #include "lldb/Core/Address.h" #include "lldb/Core/Module.h" +#include "lldb/Core/SwiftASTContextReader.h" #include "lldb/Core/ValueObjectCast.h" #include "lldb/Core/ValueObjectChild.h" #include "lldb/Core/ValueObjectConstResult.h" @@ -28,7 +29,6 @@ #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Symbol/CompileUnit.h" #include "lldb/Symbol/CompilerType.h" -#include "lldb/Symbol/SwiftASTContext.h" #include "lldb/Symbol/Declaration.h" #include "lldb/Symbol/SymbolContext.h" #include "lldb/Symbol/Type.h" @@ -1708,16 +1708,6 @@ LanguageType ValueObject::GetObjectRuntimeLanguage() { return lldb::eLanguageTypeUnknown; } -SwiftASTContextReader ValueObject::GetScratchSwiftASTContext() { - lldb::TargetSP target_sp(GetTargetSP()); - if (!target_sp) - return {}; - Status error; - ExecutionContext ctx = GetExecutionContextRef().Lock(false); - auto *exe_scope = ctx.GetBestExecutionContextScope(); - return target_sp->GetScratchSwiftASTContext(error, *exe_scope); -} - void ValueObject::AddSyntheticChild(ConstString key, ValueObject *valobj) { m_synthetic_children[key] = valobj; diff --git a/source/Core/ValueObjectDynamicValue.cpp b/source/Core/ValueObjectDynamicValue.cpp index c0e2308cc90e..9b593cd90f8b 100644 --- a/source/Core/ValueObjectDynamicValue.cpp +++ b/source/Core/ValueObjectDynamicValue.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "lldb/Core/SwiftASTContextReader.h" #include "lldb/Core/ValueObjectDynamicValue.h" #include "lldb/Core/Value.h" #include "lldb/Core/ValueObject.h" @@ -420,7 +421,11 @@ bool ValueObjectDynamicValue::DynamicValueTypeInfoNeedsUpdate() { if (!m_dynamic_type_info.HasType()) return false; - auto *cached_ctx = - static_cast(m_value.GetCompilerType().GetTypeSystem()); - return cached_ctx == GetScratchSwiftASTContext().get(); + Status error; + auto *scratch_ctx = GetTargetSP()->GetScratchSwiftASTContext(error, *this).get(); + if (!error.Success()) + return false; + + auto *cached_ctx = m_value.GetCompilerType().GetTypeSystem(); + return cached_ctx == scratch_ctx; } diff --git a/source/Plugins/Language/Swift/SwiftHashedContainer.cpp b/source/Plugins/Language/Swift/SwiftHashedContainer.cpp index 6e87e5266e4f..6132d39c1ec8 100644 --- a/source/Plugins/Language/Swift/SwiftHashedContainer.cpp +++ b/source/Plugins/Language/Swift/SwiftHashedContainer.cpp @@ -446,7 +446,11 @@ NativeHashedStorageHandler::NativeHashedStorageHandler( m_value_stride = value_type.GetByteStride(); if (SwiftASTContext *swift_ast = llvm::dyn_cast_or_null(key_type.GetTypeSystem())) { - auto scratch_ctx_reader = nativeStorage_sp->GetScratchSwiftASTContext(); + Status error; + auto scratch_ctx_reader = + m_process->GetTarget().GetScratchSwiftASTContext(error, *nativeStorage_sp.get()); + if (!error.Success()) + return; auto scratch_ctx = scratch_ctx_reader.get(); if (!scratch_ctx) return; diff --git a/source/Target/SwiftLanguageRuntime.cpp b/source/Target/SwiftLanguageRuntime.cpp index aca21f96ded8..d12612e6c4c0 100644 --- a/source/Target/SwiftLanguageRuntime.cpp +++ b/source/Target/SwiftLanguageRuntime.cpp @@ -1444,9 +1444,13 @@ SwiftLanguageRuntime::MetadataPromise::FulfillTypePromise(Status *error) { if (m_compiler_type.hasValue()) return m_compiler_type.getValue(); - auto swift_ast_ctx = m_for_object_sp->GetScratchSwiftASTContext(); + TargetSP target_sp = m_for_object_sp->GetTargetSP(); + Status type_system_error; + auto swift_ast_ctx = + target_sp->GetScratchSwiftASTContext(type_system_error, *m_for_object_sp); if (!swift_ast_ctx) { - error->SetErrorString("couldn't get Swift scratch context"); + error->SetErrorStringWithFormat("couldn't get Swift scratch context: %s", + type_system_error.AsCString()); return CompilerType(); } auto &remote_ast = m_swift_runtime.GetRemoteASTContext(*swift_ast_ctx); @@ -1486,9 +1490,13 @@ SwiftLanguageRuntime::MetadataPromise::FulfillKindPromise(Status *error) { if (m_metadata_kind.hasValue()) return m_metadata_kind; - auto swift_ast_ctx = m_for_object_sp->GetScratchSwiftASTContext(); + TargetSP target_sp = m_for_object_sp->GetTargetSP(); + Status type_system_error; + auto swift_ast_ctx = + target_sp->GetScratchSwiftASTContext(type_system_error, *m_for_object_sp); if (!swift_ast_ctx) { - error->SetErrorString("couldn't get Swift scratch context"); + error->SetErrorStringWithFormat("couldn't get Swift scratch context: %s", + type_system_error.AsCString()); return llvm::None; } auto &remote_ast = m_swift_runtime.GetRemoteASTContext(*swift_ast_ctx); @@ -1530,8 +1538,10 @@ bool SwiftLanguageRuntime::MetadataPromise::IsStaticallyDetermined() { SwiftLanguageRuntime::MetadataPromiseSP SwiftLanguageRuntime::GetMetadataPromise(lldb::addr_t addr, ValueObject &for_object) { - auto swift_ast_ctx = for_object.GetScratchSwiftASTContext(); - if (!swift_ast_ctx || swift_ast_ctx->HasFatalErrors()) + Status error; + auto swift_ast_ctx = + m_process->GetTarget().GetScratchSwiftASTContext(error, for_object); + if (!error.Success() || swift_ast_ctx->HasFatalErrors()) return nullptr; if (addr == 0 || addr == LLDB_INVALID_ADDRESS) @@ -1619,8 +1629,9 @@ SwiftLanguageRuntime::GetMemberVariableOffset(CompilerType instance_type, llvm::Optional scratch_ctx; if (instance) { - scratch_ctx = instance->GetScratchSwiftASTContext(); - if (!scratch_ctx) + Status error; + scratch_ctx = m_process->GetTarget().GetScratchSwiftASTContext(error, *instance); + if (!error.Success()) return llvm::None; } @@ -2350,8 +2361,10 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress( // use the scratch context where such operations are legal and safe. assert(IsScratchContextLocked(in_value.GetTargetSP()) && "Swift scratch context not locked ahead of dynamic type resolution"); - auto scratch_ctx = in_value.GetScratchSwiftASTContext(); - if (!scratch_ctx) + Status error; + auto scratch_ctx = + m_process->GetTarget().GetScratchSwiftASTContext(error, in_value); + if (!error.Success()) return false; auto retry_once = [&]() { @@ -3748,7 +3761,9 @@ SwiftLanguageRuntime::GetBridgedSyntheticChildProvider(ValueObject &valobj) { ProjectionSyntheticChildren::TypeProjectionUP type_projection( new ProjectionSyntheticChildren::TypeProjectionUP::element_type()); - if (auto swift_ast_ctx = valobj.GetScratchSwiftASTContext()) { + Status type_system_error; + auto swift_ast_ctx = m_process->GetTarget().GetScratchSwiftASTContext(type_system_error, valobj); + if (type_system_error.Success()) { Status error; CompilerType swift_type = swift_ast_ctx->GetTypeFromMangledTypename(type_name, error); diff --git a/source/Target/Target.cpp b/source/Target/Target.cpp index c47af0136788..d5e493a3779c 100644 --- a/source/Target/Target.cpp +++ b/source/Target/Target.cpp @@ -2534,6 +2534,14 @@ SwiftASTContextReader Target::GetScratchSwiftASTContext( return SwiftASTContextReader(GetSwiftScratchContextLock(), swift_ast_ctx); } +SwiftASTContextReader Target::GetScratchSwiftASTContext(Status &error, + ValueObject &valobj, + bool create_on_demand) { + ExecutionContext ctx = valobj.GetExecutionContextRef().Lock(false); + auto *exe_scope = ctx.GetBestExecutionContextScope(); + return GetScratchSwiftASTContext(error, *exe_scope); +} + static SharedMutex * GetSwiftScratchContextMutex(const ExecutionContext *exe_ctx) { if (!exe_ctx)