Skip to content

Commit a60975d

Browse files
authored
Merge pull request #1122 from fredriss/revert-the-revert-to-fix-the-fix
Revert "Revert "[lldb] Update for removal of SourceFile::addImports""
2 parents bd95322 + 1660bd5 commit a60975d

File tree

6 files changed

+147
-111
lines changed

6 files changed

+147
-111
lines changed

lldb/include/lldb/Symbol/SwiftASTContext.h

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ enum class IRGenDebugInfoLevel : unsigned;
3939
class CanType;
4040
class DependencyTracker;
4141
class DWARFImporterDelegate;
42+
struct ImplicitImportInfo;
4243
class IRGenOptions;
4344
class NominalTypeDecl;
4445
class SearchPathOptions;
@@ -595,7 +596,13 @@ class SwiftASTContext : public TypeSystemSwift {
595596
/// \return the ExtraArgs of the ClangImporterOptions.
596597
const std::vector<std::string> &GetClangArguments();
597598

598-
swift::ModuleDecl *CreateModule(const SourceModule &module, Status &error);
599+
/// Attempt to create a Swift module, returning \c nullptr and setting
600+
/// \p error if unsuccessful.
601+
///
602+
/// \param importInfo Information about which modules should be implicitly
603+
/// imported by each file of the module.
604+
swift::ModuleDecl *CreateModule(const SourceModule &module, Status &error,
605+
swift::ImplicitImportInfo importInfo);
599606

600607
// This function should only be called when all search paths
601608
// for all items in a swift::ASTContext have been setup to
@@ -1064,16 +1071,30 @@ class SwiftASTContext : public TypeSystemSwift {
10641071

10651072
void SetCachedType(ConstString mangled, const lldb::TypeSP &type_sp) override;
10661073

1067-
static bool PerformUserImport(SwiftASTContext &swift_ast_context,
1068-
SymbolContext &sc,
1069-
ExecutionContextScope &exe_scope,
1070-
lldb::StackFrameWP &stack_frame_wp,
1071-
swift::SourceFile &source_file, Status &error);
1072-
1073-
static bool PerformAutoImport(SwiftASTContext &swift_ast_context,
1074-
SymbolContext &sc,
1075-
lldb::StackFrameWP &stack_frame_wp,
1076-
swift::SourceFile *source_file, Status &error);
1074+
/// Retrieves the modules that need to be implicitly imported in a given
1075+
/// execution scope. This includes the modules imported by both the compile
1076+
/// unit as well as any imports from previous expression evaluations.
1077+
static bool
1078+
GetImplicitImports(SwiftASTContext &swift_ast_context, SymbolContext &sc,
1079+
ExecutionContextScope &exe_scope,
1080+
lldb::StackFrameWP &stack_frame_wp,
1081+
llvm::SmallVectorImpl<swift::ModuleDecl *> &modules,
1082+
Status &error);
1083+
1084+
/// Cache the user's imports from a SourceFile in a given execution scope such
1085+
/// that they are carried over into future expression evaluations.
1086+
static bool CacheUserImports(SwiftASTContext &swift_ast_context,
1087+
SymbolContext &sc,
1088+
ExecutionContextScope &exe_scope,
1089+
lldb::StackFrameWP &stack_frame_wp,
1090+
swift::SourceFile &source_file, Status &error);
1091+
1092+
/// Retrieve the modules imported by the compilation unit.
1093+
static bool
1094+
GetCompileUnitImports(SwiftASTContext &swift_ast_context, SymbolContext &sc,
1095+
lldb::StackFrameWP &stack_frame_wp,
1096+
llvm::SmallVectorImpl<swift::ModuleDecl *> &modules,
1097+
Status &error);
10771098

10781099
protected:
10791100
/// This map uses the string value of ConstStrings as the key, and the

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,35 +1197,40 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
11971197
snprintf(expr_name_buf, sizeof(expr_name_buf), "__lldb_expr_%u",
11981198
options.GetExpressionNumber());
11991199

1200+
// Gather the modules that need to be implicitly imported.
1201+
// The Swift stdlib needs to be imported before the SwiftLanguageRuntime can
1202+
// be used.
1203+
Status implicit_import_error;
1204+
llvm::SmallVector<swift::ModuleDecl *, 16> additional_imports;
1205+
if (!SwiftASTContext::GetImplicitImports(*swift_ast_context, sc, exe_scope,
1206+
stack_frame_wp, additional_imports,
1207+
implicit_import_error)) {
1208+
return make_error<ModuleImportError>(llvm::Twine("in implicit-import:\n") +
1209+
implicit_import_error.AsCString());
1210+
}
1211+
1212+
swift::ImplicitImportInfo importInfo;
1213+
importInfo.StdlibKind = swift::ImplicitStdlibKind::Stdlib;
1214+
for (auto *module : additional_imports)
1215+
importInfo.AdditionalModules.emplace_back(module, /*exported*/ false);
1216+
12001217
auto module_id = ast_context->getIdentifier(expr_name_buf);
1201-
auto &module = *swift::ModuleDecl::create(module_id, *ast_context);
1202-
const auto implicit_import_kind =
1203-
swift::SourceFile::ImplicitModuleImportKind::Stdlib;
1218+
auto &module = *swift::ModuleDecl::create(module_id, *ast_context,
1219+
importInfo);
12041220

12051221
swift::SourceFileKind source_file_kind = swift::SourceFileKind::Library;
1206-
12071222
if (playground || repl) {
12081223
source_file_kind = swift::SourceFileKind::Main;
12091224
}
12101225

12111226
// Create the source file. Note, we disable delayed parsing for the
12121227
// swift expression parser.
12131228
swift::SourceFile *source_file = new (*ast_context) swift::SourceFile(
1214-
module, source_file_kind, buffer_id, implicit_import_kind,
1215-
/*Keep tokens*/ false, /*KeepSyntaxTree*/ false,
1229+
module, source_file_kind, buffer_id, /*Keep tokens*/ false,
1230+
/*KeepSyntaxTree*/ false,
12161231
swift::SourceFile::ParsingFlags::DisableDelayedBodies);
12171232
module.addFile(*source_file);
12181233

1219-
1220-
// The Swift stdlib needs to be imported before the
1221-
// SwiftLanguageRuntime can be used.
1222-
Status auto_import_error;
1223-
if (!SwiftASTContext::PerformAutoImport(*swift_ast_context, sc,
1224-
stack_frame_wp, source_file,
1225-
auto_import_error))
1226-
return make_error<ModuleImportError>(llvm::Twine("in auto-import:\n") +
1227-
auto_import_error.AsCString());
1228-
12291234
// Swift Modules that rely on shared libraries (not frameworks)
12301235
// don't record the link information in the swiftmodule file, so we
12311236
// can't really make them work without outside information.
@@ -1276,6 +1281,13 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
12761281
// inserting them in.
12771282
swift_ast_context->AddDebuggerClient(external_lookup);
12781283

1284+
if (swift_ast_context->HasErrors())
1285+
return make_error<SwiftASTContextError>();
1286+
1287+
// Resolve the file's imports, including the implicit ones returned from
1288+
// GetImplicitImports.
1289+
swift::performImportResolution(*source_file);
1290+
12791291
if (swift_ast_context->HasErrors())
12801292
return make_error<SwiftASTContextError>();
12811293

@@ -1327,21 +1339,16 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
13271339
stack_frame_sp.reset();
13281340
}
13291341

1330-
swift::performImportResolution(*source_file);
1331-
1332-
if (swift_ast_context->HasErrors())
1333-
return make_error<SwiftASTContextError>();
1334-
1335-
// Do the auto-importing after Name Binding, that's when the Imports
1336-
// for the source file are figured out.
1342+
// Cache the source file's imports such that they're accessible to future
1343+
// expression evaluations.
13371344
{
13381345
std::lock_guard<std::recursive_mutex> global_context_locker(
13391346
IRExecutionUnit::GetLLVMGlobalContextMutex());
13401347

13411348
Status auto_import_error;
1342-
if (!SwiftASTContext::PerformUserImport(*swift_ast_context, sc, exe_scope,
1343-
stack_frame_wp, *source_file,
1344-
auto_import_error)) {
1349+
if (!SwiftASTContext::CacheUserImports(*swift_ast_context, sc, exe_scope,
1350+
stack_frame_wp, *source_file,
1351+
auto_import_error)) {
13451352
return make_error<ModuleImportError>(llvm::Twine("in user-import:\n") +
13461353
auto_import_error.AsCString());
13471354
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -566,18 +566,16 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
566566
repl_module =
567567
swift_ast->GetModule(completion_module_info, error);
568568
if (repl_module == nullptr) {
569-
repl_module = swift_ast->CreateModule(completion_module_info, error);
570-
const swift::SourceFile::ImplicitModuleImportKind implicit_import_kind =
571-
swift::SourceFile::ImplicitModuleImportKind::Stdlib;
569+
swift::ImplicitImportInfo importInfo;
570+
importInfo.StdlibKind = swift::ImplicitStdlibKind::Stdlib;
571+
repl_module = swift_ast->CreateModule(completion_module_info, error,
572+
importInfo);
572573
llvm::Optional<unsigned> bufferID;
573574
swift::SourceFile *repl_source_file = new (*ast)
574575
swift::SourceFile(*repl_module, swift::SourceFileKind::REPL, bufferID,
575-
implicit_import_kind, /*Keep tokens*/false);
576-
577-
// Given this file is empty and only exists to import the standard
578-
// library, we can go ahead and just mark it as having been type checked.
579-
repl_source_file->ASTStage = swift::SourceFile::TypeChecked;
576+
/*Keep tokens*/false);
580577
repl_module->addFile(*repl_source_file);
578+
swift::performImportResolution(*repl_source_file);
581579
m_completion_module_initialized = true;
582580
}
583581
if (repl_module) {

lldb/source/Symbol/SwiftASTContext.cpp

Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,8 +3611,9 @@ SwiftASTContext::GetCachedModule(const SourceModule &module) {
36113611
return nullptr;
36123612
}
36133613

3614-
swift::ModuleDecl *SwiftASTContext::CreateModule(const SourceModule &module,
3615-
Status &error) {
3614+
swift::ModuleDecl *
3615+
SwiftASTContext::CreateModule(const SourceModule &module, Status &error,
3616+
swift::ImplicitImportInfo importInfo) {
36163617
VALID_OR_RETURN(nullptr);
36173618
if (!module.path.size()) {
36183619
error.SetErrorStringWithFormat("invalid module name (empty)");
@@ -3633,7 +3634,7 @@ swift::ModuleDecl *SwiftASTContext::CreateModule(const SourceModule &module,
36333634

36343635
swift::Identifier module_id(
36353636
ast->getIdentifier(module.path.front().GetCString()));
3636-
auto *module_decl = swift::ModuleDecl::create(module_id, *ast);
3637+
auto *module_decl = swift::ModuleDecl::create(module_id, *ast, importInfo);
36373638
if (!module_decl) {
36383639
error.SetErrorStringWithFormat("failed to create module for \"%s\"",
36393640
module.path.front().GetCString());
@@ -8355,14 +8356,12 @@ static void GetNameFromModule(swift::ModuleDecl *module, std::string &result) {
83558356
}
83568357
}
83578358

8358-
static bool
8359-
LoadOneModule(const SourceModule &module, SwiftASTContext &swift_ast_context,
8360-
lldb::StackFrameWP &stack_frame_wp,
8361-
llvm::SmallVectorImpl<swift::SourceFile::ImportedModuleDesc>
8362-
&additional_imports,
8363-
Status &error) {
8359+
static swift::ModuleDecl *LoadOneModule(const SourceModule &module,
8360+
SwiftASTContext &swift_ast_context,
8361+
lldb::StackFrameWP &stack_frame_wp,
8362+
Status &error) {
83648363
if (!module.path.size())
8365-
return false;
8364+
return nullptr;
83668365

83678366
error.Clear();
83688367
ConstString toplevel = module.path.front();
@@ -8401,7 +8400,7 @@ LoadOneModule(const SourceModule &module, SwiftASTContext &swift_ast_context,
84018400
toplevel.AsCString(), error.AsCString());
84028401

84038402
if (!swift_module || swift_ast_context.HasFatalErrors()) {
8404-
return false;
8403+
return nullptr;
84058404
}
84068405
}
84078406

@@ -8412,23 +8411,42 @@ LoadOneModule(const SourceModule &module, SwiftASTContext &swift_ast_context,
84128411
LOG_PRINTF(LIBLLDB_LOG_EXPRESSIONS, "Imported module %s from {%s}",
84138412
module.path.front().AsCString(), ss.GetData());
84148413
}
8414+
return swift_module;
8415+
}
8416+
8417+
bool SwiftASTContext::GetImplicitImports(
8418+
SwiftASTContext &swift_ast_context, SymbolContext &sc,
8419+
ExecutionContextScope &exe_scope, lldb::StackFrameWP &stack_frame_wp,
8420+
llvm::SmallVectorImpl<swift::ModuleDecl *> &modules, Status &error) {
8421+
if (!GetCompileUnitImports(swift_ast_context, sc, stack_frame_wp, modules,
8422+
error)) {
8423+
return false;
8424+
}
8425+
8426+
auto *persistent_expression_state =
8427+
sc.target_sp->GetSwiftPersistentExpressionState(exe_scope);
84158428

8416-
additional_imports.push_back(swift::SourceFile::ImportedModuleDesc(
8417-
std::make_pair(swift::ModuleDecl::AccessPathTy(), swift_module),
8418-
swift::SourceFile::ImportOptions()));
8429+
// Get the hand-loaded modules from the SwiftPersistentExpressionState.
8430+
for (ConstString name : persistent_expression_state->GetHandLoadedModules()) {
8431+
SourceModule module_info;
8432+
module_info.path.push_back(name);
8433+
auto *module = LoadOneModule(module_info, swift_ast_context, stack_frame_wp,
8434+
error);
8435+
if (!module)
8436+
return false;
8437+
8438+
modules.push_back(module);
8439+
}
84198440
return true;
84208441
}
84218442

8422-
bool SwiftASTContext::PerformUserImport(SwiftASTContext &swift_ast_context,
8423-
SymbolContext &sc,
8424-
ExecutionContextScope &exe_scope,
8425-
lldb::StackFrameWP &stack_frame_wp,
8426-
swift::SourceFile &source_file,
8427-
Status &error) {
8443+
bool SwiftASTContext::CacheUserImports(SwiftASTContext &swift_ast_context,
8444+
SymbolContext &sc,
8445+
ExecutionContextScope &exe_scope,
8446+
lldb::StackFrameWP &stack_frame_wp,
8447+
swift::SourceFile &source_file,
8448+
Status &error) {
84288449
llvm::SmallString<1> m_description;
8429-
llvm::SmallVector<swift::SourceFile::ImportedModuleDesc, 2>
8430-
additional_imports;
8431-
84328450
llvm::SmallVector<swift::ModuleDecl::ImportedModule, 2> parsed_imports;
84338451

84348452
swift::ModuleDecl::ImportFilter import_filter;
@@ -8452,62 +8470,51 @@ bool SwiftASTContext::PerformUserImport(SwiftASTContext &swift_ast_context,
84528470
"Performing auto import on found module: %s.\n",
84538471
module_name.c_str());
84548472
if (!LoadOneModule(module_info, swift_ast_context, stack_frame_wp,
8455-
additional_imports, error))
8473+
error))
84568474
return false;
84578475

84588476
// How do we tell we are in REPL or playground mode?
84598477
persistent_expression_state->AddHandLoadedModule(module_const_str);
84608478
}
84618479
}
84628480
}
8463-
// Finally get the hand-loaded modules from the
8464-
// SwiftPersistentExpressionState and load them into this context:
8465-
for (ConstString name : persistent_expression_state->GetHandLoadedModules()) {
8466-
SourceModule module_info;
8467-
module_info.path.push_back(name);
8468-
if (!LoadOneModule(module_info, swift_ast_context, stack_frame_wp,
8469-
additional_imports, error))
8470-
return false;
8471-
}
8472-
8473-
source_file.addImports(additional_imports);
84748481
return true;
84758482
}
84768483

8477-
bool SwiftASTContext::PerformAutoImport(SwiftASTContext &swift_ast_context,
8478-
SymbolContext &sc,
8479-
lldb::StackFrameWP &stack_frame_wp,
8480-
swift::SourceFile *source_file,
8481-
Status &error) {
8482-
llvm::SmallVector<swift::SourceFile::ImportedModuleDesc, 2>
8483-
additional_imports;
8484-
8485-
// Import the Swift standard library and its dependecies.
8484+
bool SwiftASTContext::GetCompileUnitImports(
8485+
SwiftASTContext &swift_ast_context, SymbolContext &sc,
8486+
lldb::StackFrameWP &stack_frame_wp,
8487+
llvm::SmallVectorImpl<swift::ModuleDecl *> &modules, Status &error) {
8488+
// Import the Swift standard library and its dependencies.
84868489
SourceModule swift_module;
84878490
swift_module.path.push_back(ConstString("Swift"));
8488-
if (!LoadOneModule(swift_module, swift_ast_context, stack_frame_wp,
8489-
additional_imports, error))
8491+
auto *stdlib =
8492+
LoadOneModule(swift_module, swift_ast_context, stack_frame_wp, error);
8493+
if (!stdlib)
84908494
return false;
84918495

8496+
modules.push_back(stdlib);
8497+
84928498
CompileUnit *compile_unit = sc.comp_unit;
8493-
if (compile_unit && compile_unit->GetLanguage() == lldb::eLanguageTypeSwift)
8494-
for (const SourceModule &module : compile_unit->GetImportedModules()) {
8495-
// When building the Swift stdlib with debug info these will
8496-
// show up in "Swift.o", but we already imported them and
8497-
// manually importing them will fail.
8498-
if (module.path.size() &&
8499-
llvm::StringSwitch<bool>(module.path.front().GetStringRef())
8500-
.Cases("Swift", "SwiftShims", "Builtin", true)
8501-
.Default(false))
8502-
continue;
8499+
if (!compile_unit || compile_unit->GetLanguage() != lldb::eLanguageTypeSwift)
8500+
return true;
85038501

8504-
if (!LoadOneModule(module, swift_ast_context, stack_frame_wp,
8505-
additional_imports, error))
8506-
return false;
8507-
}
8508-
// source_file might be NULL outside of the expression parser, where
8509-
// we don't need to notify the source file of additional imports.
8510-
if (source_file)
8511-
source_file->addImports(additional_imports);
8502+
for (const SourceModule &module : compile_unit->GetImportedModules()) {
8503+
// When building the Swift stdlib with debug info these will
8504+
// show up in "Swift.o", but we already imported them and
8505+
// manually importing them will fail.
8506+
if (module.path.size() &&
8507+
llvm::StringSwitch<bool>(module.path.front().GetStringRef())
8508+
.Cases("Swift", "SwiftShims", "Builtin", true)
8509+
.Default(false))
8510+
continue;
8511+
8512+
auto *loaded_module =
8513+
LoadOneModule(module, swift_ast_context, stack_frame_wp, error);
8514+
if (!loaded_module)
8515+
return false;
8516+
8517+
modules.push_back(loaded_module);
8518+
}
85128519
return true;
85138520
}

lldb/source/Target/SwiftLanguageRuntime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,8 @@ void SwiftLanguageRuntime::RegisterGlobalError(Target &target, ConstString name,
11981198

11991199
Status module_creation_error;
12001200
swift::ModuleDecl *module_decl =
1201-
ast_context->CreateModule(module_info, module_creation_error);
1201+
ast_context->CreateModule(module_info, module_creation_error,
1202+
/*importInfo*/ {});
12021203

12031204
if (module_creation_error.Success() && module_decl) {
12041205
const bool is_static = false;

0 commit comments

Comments
 (0)