Skip to content

Commit be9c199

Browse files
author
git apple-llvm automerger
committed
Merge commit 'd185373138e6' from swift/release/6.2 into stable/20240723
2 parents 3b3a3d9 + d185373 commit be9c199

File tree

5 files changed

+87
-68
lines changed

5 files changed

+87
-68
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,18 +1272,22 @@ SwiftExpressionParser::ParseAndImport(
12721272
SwiftASTContext::ScopedDiagnostics &expr_diagnostics,
12731273
SwiftExpressionParser::SILVariableMap &variable_map, unsigned &buffer_id,
12741274
DiagnosticManager &diagnostic_manager) {
1275+
12751276
Log *log = GetLog(LLDBLog::Expressions);
12761277
bool repl = m_options.GetREPLEnabled();
12771278
bool playground = m_options.GetPlaygroundTransformEnabled();
1279+
1280+
// Install a progress meter.
1281+
auto progress_raii = m_swift_ast_ctx.GetModuleImportProgressRAII(
1282+
"Importing modules used in expression");
1283+
12781284
// If we are using the playground, hand import the necessary
12791285
// modules.
12801286
//
12811287
// FIXME: We won't have to do this once the playground adds import
12821288
// statements for the things it needs itself.
12831289
if (playground) {
1284-
SourceModule module_info;
1285-
module_info.path.emplace_back("Swift");
1286-
auto module_or_err = m_swift_ast_ctx.GetModule(module_info);
1290+
auto module_or_err = m_swift_ast_ctx.ImportStdlib();
12871291

12881292
if (!module_or_err) {
12891293
LLDB_LOG(log, "couldn't load Swift Standard Library");

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,7 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
840840
if (!persistent_state)
841841
return error("could not start parsing (no persistent data)");
842842

843-
SourceModule module_info;
844-
module_info.path.emplace_back("Swift");
845-
auto module_decl_or_err = m_swift_ast_ctx->GetModule(module_info);
843+
auto module_decl_or_err = m_swift_ast_ctx->ImportStdlib();
846844
if (!module_decl_or_err)
847845
return error("could not load Swift Standard Library",
848846
llvm::toString(module_decl_or_err.takeError()).c_str());

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

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3873,6 +3873,54 @@ void SwiftASTContext::CacheModule(std::string module_name,
38733873
m_swift_module_cache.insert({module_name, *module});
38743874
}
38753875

3876+
/// An RAII object to install a progress report callback.
3877+
SwiftASTContext::ModuleImportProgressRAII::ModuleImportProgressRAII(
3878+
SwiftASTContext &ctx, std::string category)
3879+
: m_ts(ctx.shared_from_this()), m_progress(category) {
3880+
if (!m_ts)
3881+
return;
3882+
ThreadSafeASTContext ast = ctx.GetASTContext();
3883+
if (!ast)
3884+
return;
3885+
ast->SetPreModuleImportCallback(
3886+
[&](llvm::StringRef name, swift::ASTContext::ModuleImportKind kind) {
3887+
switch (kind) {
3888+
case swift::ASTContext::Module:
3889+
m_progress.Increment(1, name.str());
3890+
break;
3891+
case swift::ASTContext::Overlay:
3892+
m_progress.Increment(1, name.str() + " (overlay)");
3893+
break;
3894+
case swift::ASTContext::BridgingHeader: {
3895+
// Module imports generate remarks, which are logged, but bridging
3896+
// headers don't.
3897+
auto &m_description = ctx.GetDescription();
3898+
HEALTH_LOG_PRINTF("Compiling bridging header: %s",
3899+
name.str().c_str());
3900+
m_progress.Increment(1, "Compiling bridging header: " + name.str());
3901+
break;
3902+
}
3903+
}
3904+
});
3905+
}
3906+
3907+
SwiftASTContext::ModuleImportProgressRAII::~ModuleImportProgressRAII() {
3908+
if (!m_ts)
3909+
return;
3910+
ThreadSafeASTContext ast =
3911+
llvm::cast<SwiftASTContext>(m_ts.get())->GetASTContext();
3912+
if (!ast)
3913+
return;
3914+
ast->SetPreModuleImportCallback(
3915+
[](llvm::StringRef, swift::ASTContext::ModuleImportKind) {});
3916+
}
3917+
3918+
std::unique_ptr<SwiftASTContext::ModuleImportProgressRAII>
3919+
SwiftASTContext::GetModuleImportProgressRAII(std::string category) {
3920+
return std::make_unique<SwiftASTContext::ModuleImportProgressRAII>(*this,
3921+
category);
3922+
}
3923+
38763924
llvm::Expected<swift::ModuleDecl &>
38773925
SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
38783926
if (cached)
@@ -3912,36 +3960,6 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
39123960
// Create a diagnostic consumer for the diagnostics produced by the import.
39133961
auto import_diags = getScopedDiagnosticConsumer();
39143962

3915-
// Report progress on module importing by using a callback function in
3916-
// swift::ASTContext.
3917-
std::unique_ptr<Progress> progress;
3918-
ast->SetPreModuleImportCallback(
3919-
[&progress](llvm::StringRef module_name,
3920-
swift::ASTContext::ModuleImportKind kind) {
3921-
if (!progress)
3922-
progress = std::make_unique<Progress>("Importing Swift modules");
3923-
switch (kind) {
3924-
case swift::ASTContext::Module:
3925-
progress->Increment(1, module_name.str());
3926-
break;
3927-
case swift::ASTContext::Overlay:
3928-
progress->Increment(1, module_name.str() + " (overlay)");
3929-
break;
3930-
case swift::ASTContext::BridgingHeader:
3931-
progress->Increment(1, "Compiling bridging header: " +
3932-
module_name.str());
3933-
break;
3934-
}
3935-
});
3936-
3937-
// Clear the callback function on scope exit to prevent an out-of-scope access
3938-
// of the progress local variable
3939-
auto on_exit = llvm::make_scope_exit([&]() {
3940-
ast->SetPreModuleImportCallback(
3941-
[](llvm::StringRef module_name,
3942-
swift::ASTContext::ModuleImportKind kind) {});
3943-
});
3944-
39453963
swift::ModuleDecl *module_decl = ast->getModuleByName(module_name);
39463964

39473965
// Error handling.
@@ -3968,6 +3986,13 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
39683986
return *module_decl;
39693987
}
39703988

3989+
llvm::Expected<swift::ModuleDecl &>
3990+
SwiftASTContext::ImportStdlib() {
3991+
SourceModule module_info;
3992+
module_info.path.emplace_back(swift::STDLIB_NAME);
3993+
return GetModule(module_info);
3994+
}
3995+
39713996
llvm::Expected<swift::ModuleDecl &>
39723997
SwiftASTContext::GetModule(const FileSpec &module_spec) {
39733998
VALID_OR_RETURN(llvm::createStringError("no context"));
@@ -4522,15 +4547,9 @@ void SwiftASTContext::ImportSectionModules(
45224547
Module &module, const std::vector<std::string> &module_names) {
45234548
VALID_OR_RETURN();
45244549

4525-
Progress progress("Loading Swift module dependencies",
4526-
module.GetFileSpec().GetFilename().GetString(),
4527-
module_names.size());
4528-
4529-
size_t completion = 0;
4550+
auto module_import_progress_raii =
4551+
GetModuleImportProgressRAII("Importing Swift section modules");
45304552
for (const std::string &module_name : module_names) {
4531-
// We have to increment the completion value even if we can't get the module
4532-
// object to stay in-sync with the total progress reporting.
4533-
progress.Increment(++completion, module_name);
45344553
SourceModule module_info;
45354554
module_info.path.push_back(ConstString(module_name));
45364555
auto module_or_err = GetModule(module_info);
@@ -9130,9 +9149,6 @@ bool SwiftASTContextForExpressions::CacheUserImports(
91309149

91319150
auto src_file_imports = source_file.getImports();
91329151

9133-
Progress progress("Importing modules used in expression");
9134-
size_t completion = 0;
9135-
91369152
/// Find all explicit imports in the expression.
91379153
struct UserImportFinder : public swift::ASTWalker {
91389154
llvm::SmallDenseSet<swift::ModuleDecl*, 1> imports;
@@ -9148,9 +9164,6 @@ bool SwiftASTContextForExpressions::CacheUserImports(
91489164
source_file.walk(import_finder);
91499165

91509166
for (const auto &attributed_import : src_file_imports) {
9151-
progress.Increment(
9152-
++completion,
9153-
attributed_import.module.importedModule->getModuleFilename().str());
91549167
swift::ModuleDecl *module = attributed_import.module.importedModule;
91559168
if (module && import_finder.imports.count(module)) {
91569169
std::string module_name;
@@ -9250,7 +9263,7 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
92509263

92519264
// Import the Swift standard library and its dependencies.
92529265
SourceModule swift_module;
9253-
swift_module.path.emplace_back("Swift");
9266+
swift_module.path.emplace_back(swift::STDLIB_NAME);
92549267
auto *stdlib = LoadOneModule(swift_module, *this, process_sp,
92559268
/*import_dylibs=*/true, error);
92569269
if (!stdlib)
@@ -9299,13 +9312,11 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
92999312
}
93009313

93019314
LOG_PRINTF(GetLog(LLDBLog::Types), "Importing dependencies of current CU");
9302-
9303-
std::string category = "Importing Swift module dependencies for ";
9315+
std::string category = "Importing dependencies for ";
93049316
category += compile_unit->GetPrimaryFile().GetFilename().GetString();
9305-
Progress progress(category, "", cu_imports.size());
9306-
size_t completion = 0;
9317+
auto module_import_progress_raii = GetModuleImportProgressRAII(category);
9318+
93079319
for (const SourceModule &module : cu_imports) {
9308-
progress.Increment(++completion, llvm::join(module.path, "."));
93099320
// When building the Swift stdlib with debug info these will
93109321
// show up in "Swift.o", but we already imported them and
93119322
// manually importing them will fail.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Plugins/TypeSystem/Swift/TypeSystemSwift.h"
1818
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
1919

20+
#include "lldb/Core/Progress.h"
2021
#include "lldb/Core/SwiftForward.h"
2122
#include "lldb/Core/ThreadSafeDenseSet.h"
2223
#include "lldb/Expression/DiagnosticManager.h"
@@ -325,13 +326,28 @@ class SwiftASTContext : public TypeSystemSwift {
325326
CreateModule(std::string module_name, swift::ImplicitImportInfo importInfo,
326327
swift::ModuleDecl::PopulateFilesFn populateFiles);
327328

329+
/// An RAII object to install a progress report callback.
330+
class ModuleImportProgressRAII {
331+
lldb::TypeSystemSP m_ts;
332+
Progress m_progress;
333+
334+
public:
335+
ModuleImportProgressRAII(SwiftASTContext &ctx, std::string category);
336+
~ModuleImportProgressRAII();
337+
};
338+
339+
/// Install and return a module import RAII object.
340+
std::unique_ptr<ModuleImportProgressRAII>
341+
GetModuleImportProgressRAII(std::string category);
342+
328343
// This function should only be called when all search paths
329344
// for all items in a swift::ASTContext have been setup to
330345
// allow for imports to happen correctly. Use with caution,
331346
// or use the GetModule() call that takes a FileSpec.
332347
llvm::Expected<swift::ModuleDecl &> GetModule(const SourceModule &module,
333348
bool *cached = nullptr);
334349
llvm::Expected<swift::ModuleDecl &> GetModule(const FileSpec &module_spec);
350+
llvm::Expected<swift::ModuleDecl &> ImportStdlib();
335351

336352
void CacheModule(std::string module_name, swift::ModuleDecl *module);
337353

lldb/test/API/functionalities/progress_reporting/swift_progress_reporting/TestSwiftProgressReporting.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ def test_swift_progress_report(self):
3535
self.runCmd("v s")
3636

3737
beacons = [
38-
"Loading Swift module",
3938
"Compiling bridging header",
4039
"Importing modules used in expression",
4140
"Setting up Swift reflection",
42-
"Importing Swift module dependencies for main.swift",
43-
"Importing Swift modules",
41+
"Importing dependencies for main.swift",
42+
"Importing dependencies for main.swift: Foundation",
4443
"Importing Swift standard library",
4544
]
4645

@@ -51,15 +50,6 @@ def test_swift_progress_report(self):
5150
if self.TraceOn():
5251
print(ret_args[0])
5352

54-
# When importing Swift modules, make sure that we don't get two reports
55-
# in a row with the title "Importing Swift modules", i.e. there should be
56-
# a report with that title followed by a report with that title and details
57-
# attached.
58-
if ret_args[0] == "Importing Swift modules":
59-
next_event = lldbutil.fetch_next_event(self, self.listener, self.broadcaster)
60-
next_ret_args = lldb.SBDebugger.GetProgressFromEvent(next_event)
61-
self.assertRegexpMatches(next_ret_args[0], r"Importing Swift modules:+")
62-
6353
for beacon in beacons:
6454
if beacon in ret_args[0]:
6555
beacons.remove(beacon)

0 commit comments

Comments
 (0)