Skip to content

Commit acc849b

Browse files
authored
Merge pull request #9618 from hamishknight/lets-try-this-again
[lldb] Update for removal of `ModuleDecl::addFile`
2 parents 981c376 + e227a8f commit acc849b

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,19 +1296,21 @@ SwiftExpressionParser::ParseAndImport(
12961296
return make_error<SwiftASTContextError>();
12971297

12981298
auto module_id = ast_context->getIdentifier(expr_name_buf);
1299-
module = swift::ModuleDecl::create(module_id, **ast_context, importInfo);
1300-
1301-
swift::SourceFileKind source_file_kind = swift::SourceFileKind::Library;
1302-
if (playground || repl) {
1303-
source_file_kind = swift::SourceFileKind::Main;
1304-
}
1299+
module = swift::ModuleDecl::create(
1300+
module_id, **ast_context, importInfo,
1301+
[&](swift::ModuleDecl *module, auto addFile) {
1302+
swift::SourceFileKind source_file_kind = swift::SourceFileKind::Library;
1303+
if (playground || repl) {
1304+
source_file_kind = swift::SourceFileKind::Main;
1305+
}
13051306

1306-
// Create the source file. Note, we disable delayed parsing for the
1307-
// swift expression parser.
1308-
source_file = new (**ast_context) swift::SourceFile(
1309-
*module, source_file_kind, buffer_id,
1310-
swift::SourceFile::ParsingFlags::DisableDelayedBodies);
1311-
module->addFile(*source_file);
1307+
// Create the source file. Note, we disable delayed parsing for the
1308+
// swift expression parser.
1309+
source_file = new (**ast_context) swift::SourceFile(
1310+
*module, source_file_kind, buffer_id,
1311+
swift::SourceFile::ParsingFlags::DisableDelayedBodies);
1312+
addFile(source_file);
1313+
});
13121314
}
13131315
// Swift Modules that rely on shared libraries (not frameworks)
13141316
// don't record the link information in the swiftmodule file, so we

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,18 +614,22 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
614614
if (!repl_module) {
615615
swift::ImplicitImportInfo importInfo;
616616
importInfo.StdlibKind = swift::ImplicitStdlibKind::Stdlib;
617+
617618
auto repl_module_or_err = swift_ast->CreateModule(
618-
completion_module_info.path.back().GetString(), importInfo);
619+
completion_module_info.path.back().GetString(), importInfo,
620+
[&](swift::ModuleDecl *repl_module, auto addFile) {
621+
auto bufferID = (*ast)->SourceMgr.addMemBufferCopy("// swift repl\n");
622+
swift::SourceFile *repl_source_file = new (**ast) swift::SourceFile(
623+
*repl_module, swift::SourceFileKind::Main, bufferID);
624+
addFile(repl_source_file);
625+
});
619626
if (!repl_module_or_err) {
620627
llvm::consumeError(repl_module_or_err.takeError());
621628
return;
622629
}
623630
repl_module = &*repl_module_or_err;
624-
auto bufferID = (*ast)->SourceMgr.addMemBufferCopy("// swift repl\n");
625-
swift::SourceFile *repl_source_file = new (**ast)
626-
swift::SourceFile(*repl_module, swift::SourceFileKind::Main, bufferID);
627-
repl_module->addFile(*repl_source_file);
628-
swift::performImportResolution(*repl_source_file);
631+
632+
swift::performImportResolution(repl_module);
629633
m_completion_module_initialized = true;
630634
}
631635
if (repl_module) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,8 +1458,7 @@ void SwiftLanguageRuntime::RegisterGlobalError(Target &target, ConstString name,
14581458
module_info.path.push_back(ConstString(module_name));
14591459

14601460
swift::ModuleDecl *module_decl = nullptr;
1461-
auto module_decl_or_err = swift_ast_ctx->CreateModule(module_name,
1462-
/*importInfo*/ {});
1461+
auto module_decl_or_err = swift_ast_ctx->CreateEmptyModule(module_name);
14631462
if (!module_decl_or_err)
14641463
llvm::consumeError(module_decl_or_err.takeError());
14651464
else

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,8 +3825,14 @@ swift::ModuleDecl *SwiftASTContext::GetCachedModule(std::string module_name) {
38253825
}
38263826

38273827
llvm::Expected<swift::ModuleDecl &>
3828-
SwiftASTContext::CreateModule(std::string module_name,
3829-
swift::ImplicitImportInfo importInfo) {
3828+
SwiftASTContext::CreateEmptyModule(std::string module_name) {
3829+
return CreateModule(module_name, /*importInfo*/ {},
3830+
/*populateFiles*/ [](auto, auto) {});
3831+
}
3832+
3833+
llvm::Expected<swift::ModuleDecl &> SwiftASTContext::CreateModule(
3834+
std::string module_name, swift::ImplicitImportInfo importInfo,
3835+
swift::ModuleDecl::PopulateFilesFn populateFiles) {
38303836
VALID_OR_RETURN(llvm::createStringError("no context"));
38313837
if (module_name.empty())
38323838
return llvm::createStringError("invalid module name (empty)");
@@ -3840,7 +3846,8 @@ SwiftASTContext::CreateModule(std::string module_name,
38403846
return llvm::createStringError("invalid swift AST (nullptr)");
38413847

38423848
swift::Identifier module_id(ast->getIdentifier(module_name));
3843-
auto *module_decl = swift::ModuleDecl::create(module_id, **ast, importInfo);
3849+
auto *module_decl =
3850+
swift::ModuleDecl::create(module_id, **ast, importInfo, populateFiles);
38443851
if (!module_decl)
38453852
return llvm::createStringError(
38463853
llvm::formatv("failed to create module for \"{0}\"", module_name));
@@ -5120,9 +5127,8 @@ swift::ModuleDecl *SwiftASTContext::GetScratchModule() {
51205127

51215128
if (m_scratch_module == nullptr) {
51225129
ThreadSafeASTContext ast_ctx = GetASTContext();
5123-
m_scratch_module = swift::ModuleDecl::create(
5124-
GetASTContext()->getIdentifier("__lldb_scratch_module"),
5125-
**ast_ctx);
5130+
m_scratch_module = swift::ModuleDecl::createEmpty(
5131+
GetASTContext()->getIdentifier("__lldb_scratch_module"), **ast_ctx);
51265132
}
51275133
return m_scratch_module;
51285134
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,19 @@ class SwiftASTContext : public TypeSystemSwift {
309309
/// \return the ExtraArgs of the ClangImporterOptions.
310310
const std::vector<std::string> &GetClangArguments();
311311

312+
/// Attempt to create an empty Swift module.
313+
llvm::Expected<swift::ModuleDecl &>
314+
CreateEmptyModule(std::string module_name);
315+
312316
/// Attempt to create a Swift module.
313317
///
314318
/// \param importInfo Information about which modules should be implicitly
315319
/// imported by each file of the module.
320+
/// \param populateFiles A function which populates the files for the module.
321+
/// Once called, the module's list of files may not change.
316322
llvm::Expected<swift::ModuleDecl &>
317-
CreateModule(std::string module_name, swift::ImplicitImportInfo importInfo);
323+
CreateModule(std::string module_name, swift::ImplicitImportInfo importInfo,
324+
swift::ModuleDecl::PopulateFilesFn populateFiles);
318325

319326
// This function should only be called when all search paths
320327
// for all items in a swift::ASTContext have been setup to

0 commit comments

Comments
 (0)