Skip to content

[lldb] Implement support for Clang Submodule imports in Swift contexts. #9532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1242,18 +1242,17 @@ SwiftExpressionParser::ParseAndImport(
// FIXME: We won't have to do this once the playground adds import
// statements for the things it needs itself.
if (playground) {
Status error;
SourceModule module_info;
module_info.path.emplace_back("Swift");
swift::ModuleDecl *module = m_swift_ast_ctx.GetModule(module_info, error);
auto module_or_err = m_swift_ast_ctx.GetModule(module_info);

if (error.Fail() || !module) {
if (!module_or_err) {
LLDB_LOG(log, "couldn't load Swift Standard Library");
return error.ToError();
return module_or_err.takeError();
}

m_swift_ast_ctx.AddHandLoadedModule(ConstString("Swift"),
swift::ImportedModule(module));
swift::ImportedModule(&*module_or_err));
}

std::string main_filename;
Expand Down Expand Up @@ -2100,7 +2099,10 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
ThreadSafeASTContext ast_context = GetASTContext(diagnostic_manager);
ast_context->addLoadedModule(module);
}
m_swift_ast_ctx.CacheModule(module);
std::string module_name;
if (module)
module_name = module->getName().get();
m_swift_ast_ctx.CacheModule(module_name, module);
if (m_sc.target_sp) {
auto *persistent_state =
m_sc.target_sp->GetSwiftPersistentExpressionState(*m_exe_scope);
Expand Down
22 changes: 16 additions & 6 deletions lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,16 +598,26 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
SourceModule completion_module_info;
completion_module_info.path.push_back(ConstString("repl"));
swift::ModuleDecl *repl_module = nullptr;
if (m_completion_module_initialized)
repl_module = swift_ast->GetModule(completion_module_info, error);
if (m_completion_module_initialized) {
auto m_or_err = swift_ast->GetModule(completion_module_info);
if (!m_or_err)
llvm::consumeError(m_or_err.takeError());
else
repl_module = &*m_or_err;
}
if (!repl_module) {
swift::ImplicitImportInfo importInfo;
importInfo.StdlibKind = swift::ImplicitStdlibKind::Stdlib;
repl_module = swift_ast->CreateModule(completion_module_info, error,
importInfo);
auto repl_module_or_err = swift_ast->CreateModule(
completion_module_info.path.back().GetString(), importInfo);
if (!repl_module_or_err) {
llvm::consumeError(repl_module_or_err.takeError());
return;
}
repl_module = &*repl_module_or_err;
auto bufferID = (*ast)->SourceMgr.addMemBufferCopy("// swift repl\n");
swift::SourceFile *repl_source_file = new (**ast) swift::SourceFile(
*repl_module, swift::SourceFileKind::Main, bufferID);
swift::SourceFile *repl_source_file = new (**ast)
swift::SourceFile(*repl_module, swift::SourceFileKind::Main, bufferID);
repl_module->addFile(*repl_source_file);
swift::performImportResolution(*repl_source_file);
m_completion_module_initialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,17 +761,15 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
if (!persistent_state)
return error("could not start parsing (no persistent data)");

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

if (status.Fail() || !module_decl)
return error("could not load Swift Standard Library", status.AsCString());

m_swift_ast_ctx->AddHandLoadedModule(ConstString("Swift"),
swift::ImportedModule(module_decl));
m_swift_ast_ctx->AddHandLoadedModule(
ConstString("Swift"), swift::ImportedModule(&*module_decl_or_err));
m_result_delegate.RegisterPersistentState(persistent_state);
m_error_delegate.RegisterPersistentState(persistent_state);

Expand Down
6 changes: 3 additions & 3 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1444,9 +1444,9 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
std::vector<llvm::StringRef> name_parts;
SplitDottedName(input, name_parts);

std::function<void(swift::ModuleDecl *)> lookup_func =
std::function<void(const swift::ModuleDecl *)> lookup_func =
[&ast_ctx, input, name_parts,
&results](swift::ModuleDecl *module) -> void {
&results](const swift::ModuleDecl *module) -> void {
for (auto imported_module :
swift::namelookup::getAllImports(module)) {
auto module = imported_module.importedModule;
Expand Down Expand Up @@ -1499,7 +1499,7 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
};

for (; iter != end; iter++)
lookup_func(iter->second);
lookup_func(&iter->second);
}
}
}
Expand Down
111 changes: 56 additions & 55 deletions lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1469,66 +1469,67 @@ void SwiftLanguageRuntime::RegisterGlobalError(Target &target, ConstString name,

auto *swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
type_system_or_err->get());
if (swift_ast_ctx && !swift_ast_ctx->HasFatalErrors()) {
std::string module_name = "$__lldb_module_for_";
module_name.append(&name.GetCString()[1]);
SourceModule module_info;
module_info.path.push_back(ConstString(module_name));

Status module_creation_error;
swift::ModuleDecl *module_decl =
swift_ast_ctx->CreateModule(module_info, module_creation_error,
/*importInfo*/ {});

if (module_creation_error.Success() && module_decl) {
const bool is_static = false;
const auto introducer = swift::VarDecl::Introducer::Let;

swift::VarDecl *var_decl = new (*swift_ast_ctx->GetASTContext())
swift::VarDecl(is_static, introducer, swift::SourceLoc(),
swift_ast_ctx->GetIdentifier(name.GetCString()),
module_decl);
var_decl->setInterfaceType(
llvm::expectedToStdOptional(
swift_ast_ctx->GetSwiftType(swift_ast_ctx->GetErrorType()))
.value_or(swift::Type()));
var_decl->setDebuggerVar(true);

SwiftPersistentExpressionState *persistent_state =
llvm::cast<SwiftPersistentExpressionState>(
target.GetPersistentExpressionStateForLanguage(
lldb::eLanguageTypeSwift));
if (!persistent_state)
return;

persistent_state->RegisterSwiftPersistentDecl({swift_ast_ctx, var_decl});

ConstString mangled_name;

{
swift::Mangle::ASTMangler mangler(true);
mangled_name = ConstString(mangler.mangleGlobalVariableFull(var_decl));
}
if (!swift_ast_ctx || swift_ast_ctx->HasFatalErrors())
return;
std::string module_name = "$__lldb_module_for_";
module_name.append(&name.GetCString()[1]);
SourceModule module_info;
module_info.path.push_back(ConstString(module_name));

swift::ModuleDecl *module_decl = nullptr;
auto module_decl_or_err = swift_ast_ctx->CreateModule(module_name,
/*importInfo*/ {});
if (!module_decl_or_err)
llvm::consumeError(module_decl_or_err.takeError());
else
module_decl = &*module_decl_or_err;
if (!module_decl)
return;
const bool is_static = false;
const auto introducer = swift::VarDecl::Introducer::Let;

swift::VarDecl *var_decl = new (*swift_ast_ctx->GetASTContext())
swift::VarDecl(is_static, introducer, swift::SourceLoc(),
swift_ast_ctx->GetIdentifier(name.GetCString()),
module_decl);
var_decl->setInterfaceType(
llvm::expectedToStdOptional(
swift_ast_ctx->GetSwiftType(swift_ast_ctx->GetErrorType()))
.value_or(swift::Type()));
var_decl->setDebuggerVar(true);

SwiftPersistentExpressionState *persistent_state =
llvm::cast<SwiftPersistentExpressionState>(
target.GetPersistentExpressionStateForLanguage(
lldb::eLanguageTypeSwift));
if (!persistent_state)
return;

persistent_state->RegisterSwiftPersistentDecl({swift_ast_ctx, var_decl});

lldb::addr_t symbol_addr;
ConstString mangled_name;

{
ProcessSP process_sp(target.GetProcessSP());
Status alloc_error;
{
swift::Mangle::ASTMangler mangler(true);
mangled_name = ConstString(mangler.mangleGlobalVariableFull(var_decl));
}

symbol_addr = process_sp->AllocateMemory(
process_sp->GetAddressByteSize(),
lldb::ePermissionsWritable | lldb::ePermissionsReadable,
alloc_error);
lldb::addr_t symbol_addr;

if (alloc_error.Success() && symbol_addr != LLDB_INVALID_ADDRESS) {
Status write_error;
process_sp->WritePointerToMemory(symbol_addr, addr, write_error);
{
ProcessSP process_sp(target.GetProcessSP());
Status alloc_error;

if (write_error.Success()) {
persistent_state->RegisterSymbol(mangled_name, symbol_addr);
}
}
symbol_addr = process_sp->AllocateMemory(
process_sp->GetAddressByteSize(),
lldb::ePermissionsWritable | lldb::ePermissionsReadable, alloc_error);

if (alloc_error.Success() && symbol_addr != LLDB_INVALID_ADDRESS) {
Status write_error;
process_sp->WritePointerToMemory(symbol_addr, addr, write_error);

if (write_error.Success()) {
persistent_state->RegisterSymbol(mangled_name, symbol_addr);
}
}
}
Expand Down
Loading