Skip to content

Commit 46256c8

Browse files
Merge pull request #10946 from adrian-prantl/154373965
[lldb] Restore Playground functionality
2 parents 16a54eb + d2b0977 commit 46256c8

24 files changed

+155
-166
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ Status SwiftREPL::DoInitialization() {
302302
return Status::FromError(type_system_or_err.takeError());
303303
std::static_pointer_cast<TypeSystemSwiftTypeRefForExpressions>(
304304
*type_system_or_err)
305-
->SetCompilerOptions(m_compiler_options.c_str());
305+
->SetCompilerOptions(/*repl=*/true, /*playgrounds=*/false,
306+
m_compiler_options.c_str());
306307

307308
std::string format_str = "${ansi.negative}Swift " +
308309
swift::version::getCompilerVersion() +

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,9 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
805805
"unknown error");
806806
// Notify SwiftASTContext that this is a Playground.
807807
if (m_options.GetPlaygroundTransformEnabled())
808-
m_swift_scratch_ctx->SetCompilerOptions("");
808+
m_swift_scratch_ctx->SetCompilerOptions(
809+
m_options.GetREPLEnabled(), m_options.GetPlaygroundTransformEnabled(),
810+
"");
809811

810812
// For playgrounds, the target triple should be used for expression
811813
// evaluation, not the current module. This requires disabling precise

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,16 +2689,14 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
26892689
return ModuleSP();
26902690
}
26912691

2692-
lldb::TypeSystemSP
2693-
SwiftASTContext::CreateInstance(const SymbolContext &sc,
2694-
TypeSystemSwiftTypeRef &typeref_typesystem,
2695-
const char *extra_options) {
2696-
bool is_repl = extra_options;
2692+
lldb::TypeSystemSP SwiftASTContext::CreateInstance(
2693+
const SymbolContext &sc, TypeSystemSwiftTypeRef &typeref_typesystem,
2694+
bool repl, bool playground, const char *extra_options) {
26972695
bool for_expressions =
26982696
llvm::isa<TypeSystemSwiftTypeRefForExpressions>(&typeref_typesystem);
26992697
// REPL requires an expression type system.
2700-
assert(!is_repl || for_expressions);
2701-
if (is_repl && !for_expressions)
2698+
assert(!repl || for_expressions);
2699+
if (repl && !for_expressions)
27022700
return {};
27032701

27042702
if (!ModuleList::GetGlobalModuleListProperties()
@@ -2707,26 +2705,24 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
27072705

27082706
CompileUnit *cu = sc.comp_unit;
27092707
const char *key = TypeSystemSwiftTypeRef::DeriveKeyFor(sc);
2708+
bool swift_context = cu && cu->GetLanguage() == eLanguageTypeSwift;
27102709
std::string m_description;
27112710
{
27122711
StreamString ss;
27132712
ss << "SwiftASTContext";
27142713
if (for_expressions)
27152714
ss << "ForExpressions";
27162715
ss << "(module: " << '"' << key << "\", " << "cu: " << '"';
2717-
if (cu)
2716+
if (cu && swift_context)
27182717
ss << cu->GetPrimaryFile().GetFilename();
27192718
else
2720-
ss << "null";
2719+
ss << "*";
27212720
ss << '"' << ')';
27222721
m_description = ss.GetString();
27232722
}
27242723

27252724
LLDB_SCOPED_TIMERF("%s::CreateInstance", m_description.c_str());
27262725

2727-
if (is_repl)
2728-
LOG_PRINTF(GetLog(LLDBLog::Types), "REPL detected");
2729-
27302726
// This function can either create an expression/scratch/repl context,
27312727
// or a SwiftAST fallback context for a TypeSystemSwiftTyperef.
27322728
// - SwiftASTContexForExpressions: target=non-null, module=null.
@@ -2770,8 +2766,9 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
27702766
if (ShouldEnableEmbeddedSwift(cu))
27712767
lang_opts.enableFeature(swift::Feature::Embedded);
27722768
}
2773-
auto defer_log = llvm::make_scope_exit(
2774-
[swift_ast_sp, is_repl] { swift_ast_sp->LogConfiguration(is_repl); });
2769+
auto defer_log = llvm::make_scope_exit([swift_ast_sp, repl, playground] {
2770+
swift_ast_sp->LogConfiguration(repl, playground);
2771+
});
27752772

27762773
LOG_PRINTF(GetLog(LLDBLog::Types), "(Target)");
27772774
auto logError = [&](const char *message) {
@@ -2797,7 +2794,12 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
27972794
ModuleList module_module;
27982795
if (!target_sp)
27992796
module_module.Append(module_sp);
2800-
ModuleList &modules = target_sp ? target_sp->GetImages() : module_module;
2797+
// Leave modules empty if not in a Swift context to avoid a fragile
2798+
// and expensive scan through all images. Unless this is a Playground, which
2799+
// has a non-Swift executable, and user code in a framework.
2800+
ModuleList &modules = (target_sp && (swift_context || playground))
2801+
? target_sp->GetImages()
2802+
: module_module;
28012803
const size_t num_images = modules.GetSize();
28022804

28032805
// Set the SDK path prior to doing search paths. Otherwise when we
@@ -2869,7 +2871,7 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28692871

28702872
ArchSpec preferred_arch;
28712873
llvm::Triple preferred_triple;
2872-
if (is_repl) {
2874+
if (repl) {
28732875
LOG_PRINTF(GetLog(LLDBLog::Types), "REPL: prefer target triple.");
28742876
preferred_arch = target_arch;
28752877
preferred_triple = target_triple;
@@ -3133,7 +3135,8 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
31333135
}
31343136
}
31353137
};
3136-
scan_module(module_sp, 0);
3138+
if (swift_context || playground)
3139+
scan_module(module_sp, 0);
31373140
for (size_t mi = 0; mi != num_images; ++mi) {
31383141
auto image_sp = modules.GetModuleAtIndex(mi);
31393142
if (!visited_modules.count(image_sp.get()))
@@ -5461,7 +5464,7 @@ void SwiftASTContext::ClearModuleDependentCaches() {
54615464
m_negative_type_cache.Clear();
54625465
}
54635466

5464-
void SwiftASTContext::LogConfiguration(bool is_repl) {
5467+
void SwiftASTContext::LogConfiguration(bool repl, bool playground) {
54655468
// It makes no sense to call VALID_OR_RETURN here. We specifically
54665469
// want the logs in the error case!
54675470
HEALTH_LOG_PRINTF("(SwiftASTContext*)%p:", static_cast<void *>(this));
@@ -5471,8 +5474,10 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
54715474
HEALTH_LOG_PRINTF(" (no AST context)");
54725475
return;
54735476
}
5474-
if (is_repl)
5477+
if (repl)
54755478
HEALTH_LOG_PRINTF(" REPL : true");
5479+
if (playground)
5480+
HEALTH_LOG_PRINTF(" Playground : true");
54765481
HEALTH_LOG_PRINTF(" Swift/C++ interop : %s",
54775482
ast_context->LangOpts.EnableCXXInterop ? "on" : "off");
54785483
HEALTH_LOG_PRINTF(" Swift/Objective-C interop : %s",

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ class SwiftASTContext : public TypeSystemSwift {
207207
/// context.
208208
static lldb::TypeSystemSP
209209
CreateInstance(const SymbolContext &sc,
210-
TypeSystemSwiftTypeRef &typeref_typesystem,
211-
const char *extra_options = nullptr);
210+
TypeSystemSwiftTypeRef &typeref_typesystem, bool repl = false,
211+
bool playground = false, const char *extra_options = nullptr);
212212

213213
static void EnumerateSupportedLanguages(
214214
std::set<lldb::LanguageType> &languages_for_types,
@@ -539,7 +539,7 @@ class SwiftASTContext : public TypeSystemSwift {
539539
swift::TBDGenOptions &GetTBDGenOptions();
540540

541541
void ClearModuleDependentCaches() override;
542-
void LogConfiguration(bool is_repl = false);
542+
void LogConfiguration(bool repl = false, bool playground = false);
543543
bool HasTarget();
544544
bool HasExplicitModules() const { return m_has_explicit_modules; }
545545
bool CheckProcessChanged();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ static lldb::TypeSystemSP CreateTypeSystemInstance(lldb::LanguageType language,
4545
} else if (target) {
4646
assert(!module);
4747
return std::shared_ptr<TypeSystemSwiftTypeRefForExpressions>(
48-
new TypeSystemSwiftTypeRefForExpressions(language, *target,
49-
extra_options));
48+
new TypeSystemSwiftTypeRefForExpressions(language, *target, false,
49+
false, extra_options));
5050
}
5151
llvm_unreachable("Neither type nor module given to CreateTypeSystemInstance");
5252
}

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,24 +1963,26 @@ TypeSystemSwiftTypeRef::TypeSystemSwiftTypeRef(Module &module) {
19631963
}
19641964

19651965
TypeSystemSwiftTypeRefForExpressions::TypeSystemSwiftTypeRefForExpressions(
1966-
lldb::LanguageType language, Target &target, const char *extra_options)
1966+
lldb::LanguageType language, Target &target, bool repl, bool playground,
1967+
const char *extra_options)
19671968
: m_target_wp(target.shared_from_this()),
19681969
m_persistent_state_up(new SwiftPersistentExpressionState) {
19691970
m_description = "TypeSystemSwiftTypeRefForExpressions";
19701971
LLDB_LOGF(GetLog(LLDBLog::Types),
19711972
"%s::TypeSystemSwiftTypeRefForExpressions()",
19721973
m_description.c_str());
19731974
// Is this a REPL or Playground?
1974-
if (extra_options) {
1975+
assert(!repl && !playground && !extra_options && "use SetCompilerOptions()");
1976+
if (repl || playground || extra_options) {
19751977
SymbolContext global_sc(target.shared_from_this(),
19761978
target.GetExecutableModule());
19771979
const char *key = DeriveKeyFor(global_sc);
19781980
m_swift_ast_context_map.insert(
19791981
{key,
19801982
{SwiftASTContext::CreateInstance(
19811983
global_sc,
1982-
*const_cast<TypeSystemSwiftTypeRefForExpressions *>(this),
1983-
extra_options),
1984+
*const_cast<TypeSystemSwiftTypeRefForExpressions *>(this), repl,
1985+
playground, extra_options),
19841986
0}});
19851987
}
19861988
}
@@ -2072,15 +2074,14 @@ ConstString TypeSystemSwiftTypeRef::GetSwiftModuleFor(const SymbolContext &sc) {
20722074
}
20732075

20742076
const char *TypeSystemSwiftTypeRef::DeriveKeyFor(const SymbolContext &sc) {
2075-
if (sc.function)
2077+
if (sc.comp_unit && sc.comp_unit->GetLanguage() == eLanguageTypeSwift)
20762078
if (ConstString name = GetSwiftModuleFor(sc))
20772079
return name.GetCString();
20782080

2079-
if (sc.module_sp) {
2080-
if (sc.module_sp->GetFileSpec())
2081-
return sc.module_sp->GetFileSpec().GetFilename().GetCString();
2082-
return sc.module_sp->GetObjectName().GetCString();
2083-
}
2081+
// Otherwise create a catch-all context per unique triple.
2082+
if (sc.module_sp)
2083+
return ConstString(sc.module_sp->GetArchitecture().GetTriple().str()).AsCString();
2084+
20842085
return nullptr;
20852086
}
20862087

@@ -2184,8 +2185,8 @@ SwiftASTContextSP TypeSystemSwiftTypeRefForExpressions::GetSwiftASTContext(
21842185

21852186
// Create a new SwiftASTContextForExpressions.
21862187
ts = SwiftASTContext::CreateInstance(
2187-
sc, *const_cast<TypeSystemSwiftTypeRefForExpressions *>(this),
2188-
m_compiler_options);
2188+
sc, *const_cast<TypeSystemSwiftTypeRefForExpressions *>(this), m_repl,
2189+
m_playground, m_compiler_options);
21892190
m_swift_ast_context_map.insert({key, {ts, retry_count}});
21902191
}
21912192

@@ -2563,6 +2564,9 @@ template <> bool Equivalent<CompilerType>(CompilerType l, CompilerType r) {
25632564
ConstString rhs = r.GetMangledTypeName();
25642565
if (lhs == ConstString("$sSiD") && rhs == ConstString("$sSuD"))
25652566
return true;
2567+
if (lhs.GetStringRef() == "$sSPySo0023unnamedstruct_hEEEdhdEaVGSgD" &&
2568+
rhs.GetStringRef() == "$ss13OpaquePointerVSgD")
2569+
return true;
25662570
// Ignore missing sugar.
25672571
swift::Demangle::Demangler dem;
25682572
auto l_node = GetDemangledType(dem, lhs.GetStringRef());

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,8 @@ class TypeSystemSwiftTypeRefForExpressions : public TypeSystemSwiftTypeRef {
626626
/// \}
627627

628628
TypeSystemSwiftTypeRefForExpressions(lldb::LanguageType language,
629-
Target &target,
629+
Target &target, bool repl,
630+
bool playground,
630631
const char *extra_options);
631632

632633
static TypeSystemSwiftTypeRefForExpressionsSP GetForTarget(Target &target);
@@ -638,7 +639,10 @@ class TypeSystemSwiftTypeRefForExpressions : public TypeSystemSwiftTypeRef {
638639
GetSwiftASTContextOrNull(const SymbolContext &sc) const override;
639640
/// This API needs to be called for a REPL or Playground before the first call
640641
/// to GetSwiftASTContext is being made.
641-
void SetCompilerOptions(const char *compiler_options) {
642+
void SetCompilerOptions(bool repl, bool playground,
643+
const char *compiler_options) {
644+
m_repl = repl;
645+
m_playground = playground;
642646
m_compiler_options = compiler_options;
643647
}
644648
lldb::TargetWP GetTargetWP() const override { return m_target_wp; }
@@ -668,6 +672,8 @@ class TypeSystemSwiftTypeRefForExpressions : public TypeSystemSwiftTypeRef {
668672
protected:
669673
lldb::TargetWP m_target_wp;
670674
unsigned m_generation = 0;
675+
bool m_repl = false;
676+
bool m_playground = false;
671677
const char *m_compiler_options = nullptr;
672678

673679
/// This exists to implement the PerformCompileUnitImports
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This file intentionally left blank.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AuxSources
12
import Dylib
23
f()
34
let comment = "and back again"

lldb/test/API/lang/swift/playgrounds/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
EXE = PlaygroundStub
22
SWIFT_SOURCES = PlaygroundStub.swift
3+
# The real playground stub has no debug info and is written in C.
4+
SWIFTFLAGS_EXTRAS = -gnone
35

46
# The deployment target we set is pre-ABI stability. The Swift driver will not
57
# point the RPATH at the system library. Do it manually.
68
LD_EXTRAS := -Xlinker -rpath -Xlinker /usr/lib/swift
7-
LD_EXTRAS += -L. -lPlaygroundsRuntime
9+
LD_EXTRAS += -L. -lPlaygroundsRuntime -F. -framework AuxSources
810

9-
PlaygroundStub: libPlaygroundsRuntime.dylib Dylib.framework
11+
PlaygroundStub: libPlaygroundsRuntime.dylib Dylib.framework AuxSources.framework
1012

1113
include Makefile.rules
1214

@@ -21,3 +23,10 @@ Dylib.framework: Dylib.swift
2123
DYLIB_SWIFT_SOURCES=Dylib.swift \
2224
DYLIB_NAME=Dylib \
2325
SWIFTFLAGS_EXTRAS="-Xcc -DNEW_OPTION_FROM_DYLIB=1"
26+
27+
AuxSources.framework: AuxSources.swift
28+
"$(MAKE)" -f $(MAKEFILE_RULES) \
29+
FRAMEWORK=AuxSources \
30+
DYLIB_SWIFT_SOURCES=AuxSources.swift \
31+
DYLIB_NAME=AuxSources \
32+
SWIFTFLAGS_EXTRAS="-Xcc -DHAVE_AUXSOURCES=1"

0 commit comments

Comments
 (0)