@@ -3871,6 +3871,54 @@ void SwiftASTContext::CacheModule(std::string module_name,
3871
3871
m_swift_module_cache.insert ({module_name, *module });
3872
3872
}
3873
3873
3874
+ // / An RAII object to install a progress report callback.
3875
+ SwiftASTContext::ModuleImportProgressRAII::ModuleImportProgressRAII (
3876
+ SwiftASTContext &ctx, std::string category)
3877
+ : m_ts(ctx.shared_from_this()), m_progress(category) {
3878
+ if (!m_ts)
3879
+ return ;
3880
+ ThreadSafeASTContext ast = ctx.GetASTContext ();
3881
+ if (!ast)
3882
+ return ;
3883
+ ast->SetPreModuleImportCallback (
3884
+ [&](llvm::StringRef name, swift::ASTContext::ModuleImportKind kind) {
3885
+ switch (kind) {
3886
+ case swift::ASTContext::Module:
3887
+ m_progress.Increment (1 , name.str ());
3888
+ break ;
3889
+ case swift::ASTContext::Overlay:
3890
+ m_progress.Increment (1 , name.str () + " (overlay)" );
3891
+ break ;
3892
+ case swift::ASTContext::BridgingHeader: {
3893
+ // Module imports generate remarks, which are logged, but bridging
3894
+ // headers don't.
3895
+ auto &m_description = ctx.GetDescription ();
3896
+ HEALTH_LOG_PRINTF (" Compiling bridging header: %s" ,
3897
+ name.str ().c_str ());
3898
+ m_progress.Increment (1 , " Compiling bridging header: " + name.str ());
3899
+ break ;
3900
+ }
3901
+ }
3902
+ });
3903
+ }
3904
+
3905
+ SwiftASTContext::ModuleImportProgressRAII::~ModuleImportProgressRAII () {
3906
+ if (!m_ts)
3907
+ return ;
3908
+ ThreadSafeASTContext ast =
3909
+ llvm::cast<SwiftASTContext>(m_ts.get ())->GetASTContext ();
3910
+ if (!ast)
3911
+ return ;
3912
+ ast->SetPreModuleImportCallback (
3913
+ [](llvm::StringRef, swift::ASTContext::ModuleImportKind) {});
3914
+ }
3915
+
3916
+ std::unique_ptr<SwiftASTContext::ModuleImportProgressRAII>
3917
+ SwiftASTContext::GetModuleImportProgressRAII (std::string category) {
3918
+ return std::make_unique<SwiftASTContext::ModuleImportProgressRAII>(*this ,
3919
+ category);
3920
+ }
3921
+
3874
3922
llvm::Expected<swift::ModuleDecl &>
3875
3923
SwiftASTContext::GetModule (const SourceModule &module , bool *cached) {
3876
3924
if (cached)
@@ -3910,36 +3958,6 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
3910
3958
// Create a diagnostic consumer for the diagnostics produced by the import.
3911
3959
auto import_diags = getScopedDiagnosticConsumer ();
3912
3960
3913
- // Report progress on module importing by using a callback function in
3914
- // swift::ASTContext.
3915
- std::unique_ptr<Progress> progress;
3916
- ast->SetPreModuleImportCallback (
3917
- [&progress](llvm::StringRef module_name,
3918
- swift::ASTContext::ModuleImportKind kind) {
3919
- if (!progress)
3920
- progress = std::make_unique<Progress>(" Importing Swift modules" );
3921
- switch (kind) {
3922
- case swift::ASTContext::Module:
3923
- progress->Increment (1 , module_name.str ());
3924
- break ;
3925
- case swift::ASTContext::Overlay:
3926
- progress->Increment (1 , module_name.str () + " (overlay)" );
3927
- break ;
3928
- case swift::ASTContext::BridgingHeader:
3929
- progress->Increment (1 , " Compiling bridging header: " +
3930
- module_name.str ());
3931
- break ;
3932
- }
3933
- });
3934
-
3935
- // Clear the callback function on scope exit to prevent an out-of-scope access
3936
- // of the progress local variable
3937
- auto on_exit = llvm::make_scope_exit ([&]() {
3938
- ast->SetPreModuleImportCallback (
3939
- [](llvm::StringRef module_name,
3940
- swift::ASTContext::ModuleImportKind kind) {});
3941
- });
3942
-
3943
3961
swift::ModuleDecl *module_decl = ast->getModuleByName (module_name);
3944
3962
3945
3963
// Error handling.
@@ -3966,6 +3984,13 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
3966
3984
return *module_decl;
3967
3985
}
3968
3986
3987
+ llvm::Expected<swift::ModuleDecl &>
3988
+ SwiftASTContext::ImportStdlib () {
3989
+ SourceModule module_info;
3990
+ module_info.path .emplace_back (swift::STDLIB_NAME);
3991
+ return GetModule (module_info);
3992
+ }
3993
+
3969
3994
llvm::Expected<swift::ModuleDecl &>
3970
3995
SwiftASTContext::GetModule (const FileSpec &module_spec) {
3971
3996
VALID_OR_RETURN (llvm::createStringError (" no context" ));
@@ -4520,15 +4545,9 @@ void SwiftASTContext::ImportSectionModules(
4520
4545
Module &module , const std::vector<std::string> &module_names) {
4521
4546
VALID_OR_RETURN ();
4522
4547
4523
- Progress progress (" Loading Swift module dependencies" ,
4524
- module .GetFileSpec ().GetFilename ().GetString (),
4525
- module_names.size ());
4526
-
4527
- size_t completion = 0 ;
4548
+ auto module_import_progress_raii =
4549
+ GetModuleImportProgressRAII (" Importing Swift section modules" );
4528
4550
for (const std::string &module_name : module_names) {
4529
- // We have to increment the completion value even if we can't get the module
4530
- // object to stay in-sync with the total progress reporting.
4531
- progress.Increment (++completion, module_name);
4532
4551
SourceModule module_info;
4533
4552
module_info.path .push_back (ConstString (module_name));
4534
4553
auto module_or_err = GetModule (module_info);
@@ -9128,9 +9147,6 @@ bool SwiftASTContextForExpressions::CacheUserImports(
9128
9147
9129
9148
auto src_file_imports = source_file.getImports ();
9130
9149
9131
- Progress progress (" Importing modules used in expression" );
9132
- size_t completion = 0 ;
9133
-
9134
9150
// / Find all explicit imports in the expression.
9135
9151
struct UserImportFinder : public swift ::ASTWalker {
9136
9152
llvm::SmallDenseSet<swift::ModuleDecl*, 1 > imports;
@@ -9146,9 +9162,6 @@ bool SwiftASTContextForExpressions::CacheUserImports(
9146
9162
source_file.walk (import_finder);
9147
9163
9148
9164
for (const auto &attributed_import : src_file_imports) {
9149
- progress.Increment (
9150
- ++completion,
9151
- attributed_import.module .importedModule ->getModuleFilename ().str ());
9152
9165
swift::ModuleDecl *module = attributed_import.module .importedModule ;
9153
9166
if (module && import_finder.imports .count (module )) {
9154
9167
std::string module_name;
@@ -9248,7 +9261,7 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
9248
9261
9249
9262
// Import the Swift standard library and its dependencies.
9250
9263
SourceModule swift_module;
9251
- swift_module.path .emplace_back (" Swift " );
9264
+ swift_module.path .emplace_back (swift::STDLIB_NAME );
9252
9265
auto *stdlib = LoadOneModule (swift_module, *this , process_sp,
9253
9266
/* import_dylibs=*/ true , error);
9254
9267
if (!stdlib)
@@ -9297,13 +9310,11 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
9297
9310
}
9298
9311
9299
9312
LOG_PRINTF (GetLog (LLDBLog::Types), " Importing dependencies of current CU" );
9300
-
9301
- std::string category = " Importing Swift module dependencies for " ;
9313
+ std::string category = " Importing dependencies for " ;
9302
9314
category += compile_unit->GetPrimaryFile ().GetFilename ().GetString ();
9303
- Progress progress (category, " " , cu_imports. size () );
9304
- size_t completion = 0 ;
9315
+ auto module_import_progress_raii = GetModuleImportProgressRAII (category );
9316
+
9305
9317
for (const SourceModule &module : cu_imports) {
9306
- progress.Increment (++completion, llvm::join (module .path , " ." ));
9307
9318
// When building the Swift stdlib with debug info these will
9308
9319
// show up in "Swift.o", but we already imported them and
9309
9320
// manually importing them will fail.
0 commit comments