Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.

Commit 91e5f2f

Browse files
committed
[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.
1 parent 035895c commit 91e5f2f

File tree

7 files changed

+53
-29
lines changed

7 files changed

+53
-29
lines changed

include/lldb/Core/ValueObject.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef liblldb_ValueObject_h_
1010
#define liblldb_ValueObject_h_
1111

12-
#include "lldb/Core/SwiftASTContextReader.h"
1312
#include "lldb/Core/Value.h"
1413
#include "lldb/Symbol/CompilerType.h"
1514
#include "lldb/Symbol/Type.h"
@@ -614,8 +613,6 @@ class ValueObject : public UserID {
614613

615614
virtual bool HasSyntheticValue();
616615

617-
SwiftASTContextReader GetScratchSwiftASTContext();
618-
619616
virtual bool IsSynthetic() { return false; }
620617

621618
lldb::ValueObjectSP

include/lldb/Target/Target.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "lldb/Core/Architecture.h"
2525
#include "lldb/Core/Disassembler.h"
2626
#include "lldb/Core/ModuleList.h"
27+
#include "lldb/Core/SwiftASTContextReader.h"
2728
#include "lldb/Core/UserSettingsController.h"
2829
#include "lldb/Expression/Expression.h"
2930
#include "lldb/Interpreter/OptionValueBoolean.h"
@@ -1211,6 +1212,10 @@ class Target : public std::enable_shared_from_this<Target>,
12111212
GetScratchSwiftASTContext(Status &error, ExecutionContextScope &exe_scope,
12121213
bool create_on_demand = true);
12131214

1215+
SwiftASTContextReader GetScratchSwiftASTContext(Status &error,
1216+
ValueObject &valobj,
1217+
bool create_on_demand = true);
1218+
12141219
private:
12151220
void DisplayFallbackSwiftContextErrors(SwiftASTContext *swift_ast_ctx);
12161221
public:

source/Core/ValueObject.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "lldb/Core/Address.h"
1212
#include "lldb/Core/Module.h"
13+
#include "lldb/Core/SwiftASTContextReader.h"
1314
#include "lldb/Core/ValueObjectCast.h"
1415
#include "lldb/Core/ValueObjectChild.h"
1516
#include "lldb/Core/ValueObjectConstResult.h"
@@ -28,7 +29,6 @@
2829
#include "lldb/Symbol/ClangASTContext.h"
2930
#include "lldb/Symbol/CompileUnit.h"
3031
#include "lldb/Symbol/CompilerType.h"
31-
#include "lldb/Symbol/SwiftASTContext.h"
3232
#include "lldb/Symbol/Declaration.h"
3333
#include "lldb/Symbol/SymbolContext.h"
3434
#include "lldb/Symbol/Type.h"
@@ -1708,16 +1708,6 @@ LanguageType ValueObject::GetObjectRuntimeLanguage() {
17081708
return lldb::eLanguageTypeUnknown;
17091709
}
17101710

1711-
SwiftASTContextReader ValueObject::GetScratchSwiftASTContext() {
1712-
lldb::TargetSP target_sp(GetTargetSP());
1713-
if (!target_sp)
1714-
return {};
1715-
Status error;
1716-
ExecutionContext ctx = GetExecutionContextRef().Lock(false);
1717-
auto *exe_scope = ctx.GetBestExecutionContextScope();
1718-
return target_sp->GetScratchSwiftASTContext(error, *exe_scope);
1719-
}
1720-
17211711
void ValueObject::AddSyntheticChild(ConstString key,
17221712
ValueObject *valobj) {
17231713
m_synthetic_children[key] = valobj;

source/Core/ValueObjectDynamicValue.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "lldb/Core/SwiftASTContextReader.h"
910
#include "lldb/Core/ValueObjectDynamicValue.h"
1011
#include "lldb/Core/Value.h"
1112
#include "lldb/Core/ValueObject.h"
@@ -420,7 +421,11 @@ bool ValueObjectDynamicValue::DynamicValueTypeInfoNeedsUpdate() {
420421
if (!m_dynamic_type_info.HasType())
421422
return false;
422423

423-
auto *cached_ctx =
424-
static_cast<SwiftASTContext *>(m_value.GetCompilerType().GetTypeSystem());
425-
return cached_ctx == GetScratchSwiftASTContext().get();
424+
Status error;
425+
auto *scratch_ctx = GetTargetSP()->GetScratchSwiftASTContext(error, *this).get();
426+
if (!error.Success())
427+
return false;
428+
429+
auto *cached_ctx = m_value.GetCompilerType().GetTypeSystem();
430+
return cached_ctx == scratch_ctx;
426431
}

source/Plugins/Language/Swift/SwiftHashedContainer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,11 @@ NativeHashedStorageHandler::NativeHashedStorageHandler(
446446
m_value_stride = value_type.GetByteStride();
447447
if (SwiftASTContext *swift_ast =
448448
llvm::dyn_cast_or_null<SwiftASTContext>(key_type.GetTypeSystem())) {
449-
auto scratch_ctx_reader = nativeStorage_sp->GetScratchSwiftASTContext();
449+
Status error;
450+
auto scratch_ctx_reader =
451+
m_process->GetTarget().GetScratchSwiftASTContext(error, *nativeStorage_sp.get());
452+
if (!error.Success())
453+
return;
450454
auto scratch_ctx = scratch_ctx_reader.get();
451455
if (!scratch_ctx)
452456
return;

source/Target/SwiftLanguageRuntime.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,9 +1444,13 @@ SwiftLanguageRuntime::MetadataPromise::FulfillTypePromise(Status *error) {
14441444
if (m_compiler_type.hasValue())
14451445
return m_compiler_type.getValue();
14461446

1447-
auto swift_ast_ctx = m_for_object_sp->GetScratchSwiftASTContext();
1447+
TargetSP target_sp = m_for_object_sp->GetTargetSP();
1448+
Status type_system_error;
1449+
auto swift_ast_ctx =
1450+
target_sp->GetScratchSwiftASTContext(type_system_error, *m_for_object_sp);
14481451
if (!swift_ast_ctx) {
1449-
error->SetErrorString("couldn't get Swift scratch context");
1452+
error->SetErrorStringWithFormat("couldn't get Swift scratch context: %s",
1453+
type_system_error.AsCString());
14501454
return CompilerType();
14511455
}
14521456
auto &remote_ast = m_swift_runtime.GetRemoteASTContext(*swift_ast_ctx);
@@ -1486,9 +1490,13 @@ SwiftLanguageRuntime::MetadataPromise::FulfillKindPromise(Status *error) {
14861490
if (m_metadata_kind.hasValue())
14871491
return m_metadata_kind;
14881492

1489-
auto swift_ast_ctx = m_for_object_sp->GetScratchSwiftASTContext();
1493+
TargetSP target_sp = m_for_object_sp->GetTargetSP();
1494+
Status type_system_error;
1495+
auto swift_ast_ctx =
1496+
target_sp->GetScratchSwiftASTContext(type_system_error, *m_for_object_sp);
14901497
if (!swift_ast_ctx) {
1491-
error->SetErrorString("couldn't get Swift scratch context");
1498+
error->SetErrorStringWithFormat("couldn't get Swift scratch context: %s",
1499+
type_system_error.AsCString());
14921500
return llvm::None;
14931501
}
14941502
auto &remote_ast = m_swift_runtime.GetRemoteASTContext(*swift_ast_ctx);
@@ -1530,8 +1538,10 @@ bool SwiftLanguageRuntime::MetadataPromise::IsStaticallyDetermined() {
15301538
SwiftLanguageRuntime::MetadataPromiseSP
15311539
SwiftLanguageRuntime::GetMetadataPromise(lldb::addr_t addr,
15321540
ValueObject &for_object) {
1533-
auto swift_ast_ctx = for_object.GetScratchSwiftASTContext();
1534-
if (!swift_ast_ctx || swift_ast_ctx->HasFatalErrors())
1541+
Status error;
1542+
auto swift_ast_ctx =
1543+
m_process->GetTarget().GetScratchSwiftASTContext(error, for_object);
1544+
if (!error.Success() || swift_ast_ctx->HasFatalErrors())
15351545
return nullptr;
15361546

15371547
if (addr == 0 || addr == LLDB_INVALID_ADDRESS)
@@ -1619,8 +1629,9 @@ SwiftLanguageRuntime::GetMemberVariableOffset(CompilerType instance_type,
16191629

16201630
llvm::Optional<SwiftASTContextReader> scratch_ctx;
16211631
if (instance) {
1622-
scratch_ctx = instance->GetScratchSwiftASTContext();
1623-
if (!scratch_ctx)
1632+
Status error;
1633+
scratch_ctx = m_process->GetTarget().GetScratchSwiftASTContext(error, *instance);
1634+
if (!error.Success())
16241635
return llvm::None;
16251636
}
16261637

@@ -2350,8 +2361,10 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress(
23502361
// use the scratch context where such operations are legal and safe.
23512362
assert(IsScratchContextLocked(in_value.GetTargetSP()) &&
23522363
"Swift scratch context not locked ahead of dynamic type resolution");
2353-
auto scratch_ctx = in_value.GetScratchSwiftASTContext();
2354-
if (!scratch_ctx)
2364+
Status error;
2365+
auto scratch_ctx =
2366+
m_process->GetTarget().GetScratchSwiftASTContext(error, in_value);
2367+
if (!error.Success())
23552368
return false;
23562369

23572370
auto retry_once = [&]() {
@@ -3748,7 +3761,9 @@ SwiftLanguageRuntime::GetBridgedSyntheticChildProvider(ValueObject &valobj) {
37483761
ProjectionSyntheticChildren::TypeProjectionUP type_projection(
37493762
new ProjectionSyntheticChildren::TypeProjectionUP::element_type());
37503763

3751-
if (auto swift_ast_ctx = valobj.GetScratchSwiftASTContext()) {
3764+
Status type_system_error;
3765+
auto swift_ast_ctx = m_process->GetTarget().GetScratchSwiftASTContext(type_system_error, valobj);
3766+
if (type_system_error.Success()) {
37523767
Status error;
37533768
CompilerType swift_type =
37543769
swift_ast_ctx->GetTypeFromMangledTypename(type_name, error);

source/Target/Target.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,14 @@ SwiftASTContextReader Target::GetScratchSwiftASTContext(
25342534
return SwiftASTContextReader(GetSwiftScratchContextLock(), swift_ast_ctx);
25352535
}
25362536

2537+
SwiftASTContextReader Target::GetScratchSwiftASTContext(Status &error,
2538+
ValueObject &valobj,
2539+
bool create_on_demand) {
2540+
ExecutionContext ctx = valobj.GetExecutionContextRef().Lock(false);
2541+
auto *exe_scope = ctx.GetBestExecutionContextScope();
2542+
return GetScratchSwiftASTContext(error, *exe_scope);
2543+
}
2544+
25372545
static SharedMutex *
25382546
GetSwiftScratchContextMutex(const ExecutionContext *exe_ctx) {
25392547
if (!exe_ctx)

0 commit comments

Comments
 (0)