Skip to content

Commit be92898

Browse files
Merge pull request #9532 from adrian-prantl/137003294
[lldb] Implement support for Clang Submodule imports in Swift contexts.
2 parents 3c8a0af + 9fb8853 commit be92898

File tree

13 files changed

+275
-256
lines changed

13 files changed

+275
-256
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,18 +1242,17 @@ SwiftExpressionParser::ParseAndImport(
12421242
// FIXME: We won't have to do this once the playground adds import
12431243
// statements for the things it needs itself.
12441244
if (playground) {
1245-
Status error;
12461245
SourceModule module_info;
12471246
module_info.path.emplace_back("Swift");
1248-
swift::ModuleDecl *module = m_swift_ast_ctx.GetModule(module_info, error);
1247+
auto module_or_err = m_swift_ast_ctx.GetModule(module_info);
12491248

1250-
if (error.Fail() || !module) {
1249+
if (!module_or_err) {
12511250
LLDB_LOG(log, "couldn't load Swift Standard Library");
1252-
return error.ToError();
1251+
return module_or_err.takeError();
12531252
}
12541253

12551254
m_swift_ast_ctx.AddHandLoadedModule(ConstString("Swift"),
1256-
swift::ImportedModule(module));
1255+
swift::ImportedModule(&*module_or_err));
12571256
}
12581257

12591258
std::string main_filename;
@@ -2100,7 +2099,10 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
21002099
ThreadSafeASTContext ast_context = GetASTContext(diagnostic_manager);
21012100
ast_context->addLoadedModule(module);
21022101
}
2103-
m_swift_ast_ctx.CacheModule(module);
2102+
std::string module_name;
2103+
if (module)
2104+
module_name = module->getName().get();
2105+
m_swift_ast_ctx.CacheModule(module_name, module);
21042106
if (m_sc.target_sp) {
21052107
auto *persistent_state =
21062108
m_sc.target_sp->GetSwiftPersistentExpressionState(*m_exe_scope);

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,16 +598,26 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
598598
SourceModule completion_module_info;
599599
completion_module_info.path.push_back(ConstString("repl"));
600600
swift::ModuleDecl *repl_module = nullptr;
601-
if (m_completion_module_initialized)
602-
repl_module = swift_ast->GetModule(completion_module_info, error);
601+
if (m_completion_module_initialized) {
602+
auto m_or_err = swift_ast->GetModule(completion_module_info);
603+
if (!m_or_err)
604+
llvm::consumeError(m_or_err.takeError());
605+
else
606+
repl_module = &*m_or_err;
607+
}
603608
if (!repl_module) {
604609
swift::ImplicitImportInfo importInfo;
605610
importInfo.StdlibKind = swift::ImplicitStdlibKind::Stdlib;
606-
repl_module = swift_ast->CreateModule(completion_module_info, error,
607-
importInfo);
611+
auto repl_module_or_err = swift_ast->CreateModule(
612+
completion_module_info.path.back().GetString(), importInfo);
613+
if (!repl_module_or_err) {
614+
llvm::consumeError(repl_module_or_err.takeError());
615+
return;
616+
}
617+
repl_module = &*repl_module_or_err;
608618
auto bufferID = (*ast)->SourceMgr.addMemBufferCopy("// swift repl\n");
609-
swift::SourceFile *repl_source_file = new (**ast) swift::SourceFile(
610-
*repl_module, swift::SourceFileKind::Main, bufferID);
619+
swift::SourceFile *repl_source_file = new (**ast)
620+
swift::SourceFile(*repl_module, swift::SourceFileKind::Main, bufferID);
611621
repl_module->addFile(*repl_source_file);
612622
swift::performImportResolution(*repl_source_file);
613623
m_completion_module_initialized = true;

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -761,17 +761,15 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
761761
if (!persistent_state)
762762
return error("could not start parsing (no persistent data)");
763763

764-
Status status;
765764
SourceModule module_info;
766765
module_info.path.emplace_back("Swift");
767-
swift::ModuleDecl *module_decl =
768-
m_swift_ast_ctx->GetModule(module_info, status);
766+
auto module_decl_or_err = m_swift_ast_ctx->GetModule(module_info);
767+
if (!module_decl_or_err)
768+
return error("could not load Swift Standard Library",
769+
llvm::toString(module_decl_or_err.takeError()).c_str());
769770

770-
if (status.Fail() || !module_decl)
771-
return error("could not load Swift Standard Library", status.AsCString());
772-
773-
m_swift_ast_ctx->AddHandLoadedModule(ConstString("Swift"),
774-
swift::ImportedModule(module_decl));
771+
m_swift_ast_ctx->AddHandLoadedModule(
772+
ConstString("Swift"), swift::ImportedModule(&*module_decl_or_err));
775773
m_result_delegate.RegisterPersistentState(persistent_state);
776774
m_error_delegate.RegisterPersistentState(persistent_state);
777775

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,9 +1444,9 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
14441444
std::vector<llvm::StringRef> name_parts;
14451445
SplitDottedName(input, name_parts);
14461446

1447-
std::function<void(swift::ModuleDecl *)> lookup_func =
1447+
std::function<void(const swift::ModuleDecl *)> lookup_func =
14481448
[&ast_ctx, input, name_parts,
1449-
&results](swift::ModuleDecl *module) -> void {
1449+
&results](const swift::ModuleDecl *module) -> void {
14501450
for (auto imported_module :
14511451
swift::namelookup::getAllImports(module)) {
14521452
auto module = imported_module.importedModule;
@@ -1499,7 +1499,7 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
14991499
};
15001500

15011501
for (; iter != end; iter++)
1502-
lookup_func(iter->second);
1502+
lookup_func(&iter->second);
15031503
}
15041504
}
15051505
}

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,66 +1469,67 @@ void SwiftLanguageRuntime::RegisterGlobalError(Target &target, ConstString name,
14691469

14701470
auto *swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
14711471
type_system_or_err->get());
1472-
if (swift_ast_ctx && !swift_ast_ctx->HasFatalErrors()) {
1473-
std::string module_name = "$__lldb_module_for_";
1474-
module_name.append(&name.GetCString()[1]);
1475-
SourceModule module_info;
1476-
module_info.path.push_back(ConstString(module_name));
1477-
1478-
Status module_creation_error;
1479-
swift::ModuleDecl *module_decl =
1480-
swift_ast_ctx->CreateModule(module_info, module_creation_error,
1481-
/*importInfo*/ {});
1482-
1483-
if (module_creation_error.Success() && module_decl) {
1484-
const bool is_static = false;
1485-
const auto introducer = swift::VarDecl::Introducer::Let;
1486-
1487-
swift::VarDecl *var_decl = new (*swift_ast_ctx->GetASTContext())
1488-
swift::VarDecl(is_static, introducer, swift::SourceLoc(),
1489-
swift_ast_ctx->GetIdentifier(name.GetCString()),
1490-
module_decl);
1491-
var_decl->setInterfaceType(
1492-
llvm::expectedToStdOptional(
1493-
swift_ast_ctx->GetSwiftType(swift_ast_ctx->GetErrorType()))
1494-
.value_or(swift::Type()));
1495-
var_decl->setDebuggerVar(true);
1496-
1497-
SwiftPersistentExpressionState *persistent_state =
1498-
llvm::cast<SwiftPersistentExpressionState>(
1499-
target.GetPersistentExpressionStateForLanguage(
1500-
lldb::eLanguageTypeSwift));
1501-
if (!persistent_state)
1502-
return;
1503-
1504-
persistent_state->RegisterSwiftPersistentDecl({swift_ast_ctx, var_decl});
1505-
1506-
ConstString mangled_name;
1507-
1508-
{
1509-
swift::Mangle::ASTMangler mangler(true);
1510-
mangled_name = ConstString(mangler.mangleGlobalVariableFull(var_decl));
1511-
}
1472+
if (!swift_ast_ctx || swift_ast_ctx->HasFatalErrors())
1473+
return;
1474+
std::string module_name = "$__lldb_module_for_";
1475+
module_name.append(&name.GetCString()[1]);
1476+
SourceModule module_info;
1477+
module_info.path.push_back(ConstString(module_name));
1478+
1479+
swift::ModuleDecl *module_decl = nullptr;
1480+
auto module_decl_or_err = swift_ast_ctx->CreateModule(module_name,
1481+
/*importInfo*/ {});
1482+
if (!module_decl_or_err)
1483+
llvm::consumeError(module_decl_or_err.takeError());
1484+
else
1485+
module_decl = &*module_decl_or_err;
1486+
if (!module_decl)
1487+
return;
1488+
const bool is_static = false;
1489+
const auto introducer = swift::VarDecl::Introducer::Let;
1490+
1491+
swift::VarDecl *var_decl = new (*swift_ast_ctx->GetASTContext())
1492+
swift::VarDecl(is_static, introducer, swift::SourceLoc(),
1493+
swift_ast_ctx->GetIdentifier(name.GetCString()),
1494+
module_decl);
1495+
var_decl->setInterfaceType(
1496+
llvm::expectedToStdOptional(
1497+
swift_ast_ctx->GetSwiftType(swift_ast_ctx->GetErrorType()))
1498+
.value_or(swift::Type()));
1499+
var_decl->setDebuggerVar(true);
1500+
1501+
SwiftPersistentExpressionState *persistent_state =
1502+
llvm::cast<SwiftPersistentExpressionState>(
1503+
target.GetPersistentExpressionStateForLanguage(
1504+
lldb::eLanguageTypeSwift));
1505+
if (!persistent_state)
1506+
return;
1507+
1508+
persistent_state->RegisterSwiftPersistentDecl({swift_ast_ctx, var_decl});
15121509

1513-
lldb::addr_t symbol_addr;
1510+
ConstString mangled_name;
15141511

1515-
{
1516-
ProcessSP process_sp(target.GetProcessSP());
1517-
Status alloc_error;
1512+
{
1513+
swift::Mangle::ASTMangler mangler(true);
1514+
mangled_name = ConstString(mangler.mangleGlobalVariableFull(var_decl));
1515+
}
15181516

1519-
symbol_addr = process_sp->AllocateMemory(
1520-
process_sp->GetAddressByteSize(),
1521-
lldb::ePermissionsWritable | lldb::ePermissionsReadable,
1522-
alloc_error);
1517+
lldb::addr_t symbol_addr;
15231518

1524-
if (alloc_error.Success() && symbol_addr != LLDB_INVALID_ADDRESS) {
1525-
Status write_error;
1526-
process_sp->WritePointerToMemory(symbol_addr, addr, write_error);
1519+
{
1520+
ProcessSP process_sp(target.GetProcessSP());
1521+
Status alloc_error;
15271522

1528-
if (write_error.Success()) {
1529-
persistent_state->RegisterSymbol(mangled_name, symbol_addr);
1530-
}
1531-
}
1523+
symbol_addr = process_sp->AllocateMemory(
1524+
process_sp->GetAddressByteSize(),
1525+
lldb::ePermissionsWritable | lldb::ePermissionsReadable, alloc_error);
1526+
1527+
if (alloc_error.Success() && symbol_addr != LLDB_INVALID_ADDRESS) {
1528+
Status write_error;
1529+
process_sp->WritePointerToMemory(symbol_addr, addr, write_error);
1530+
1531+
if (write_error.Success()) {
1532+
persistent_state->RegisterSymbol(mangled_name, symbol_addr);
15321533
}
15331534
}
15341535
}

0 commit comments

Comments
 (0)