@@ -3873,6 +3873,54 @@ void SwiftASTContext::CacheModule(std::string module_name,
3873
3873
m_swift_module_cache.insert ({module_name, *module });
3874
3874
}
3875
3875
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
+
3876
3924
llvm::Expected<swift::ModuleDecl &>
3877
3925
SwiftASTContext::GetModule (const SourceModule &module , bool *cached) {
3878
3926
if (cached)
@@ -3912,36 +3960,6 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
3912
3960
// Create a diagnostic consumer for the diagnostics produced by the import.
3913
3961
auto import_diags = getScopedDiagnosticConsumer ();
3914
3962
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
-
3945
3963
swift::ModuleDecl *module_decl = ast->getModuleByName (module_name);
3946
3964
3947
3965
// Error handling.
@@ -3968,6 +3986,13 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
3968
3986
return *module_decl;
3969
3987
}
3970
3988
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
+
3971
3996
llvm::Expected<swift::ModuleDecl &>
3972
3997
SwiftASTContext::GetModule (const FileSpec &module_spec) {
3973
3998
VALID_OR_RETURN (llvm::createStringError (" no context" ));
@@ -4522,15 +4547,9 @@ void SwiftASTContext::ImportSectionModules(
4522
4547
Module &module , const std::vector<std::string> &module_names) {
4523
4548
VALID_OR_RETURN ();
4524
4549
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" );
4530
4552
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);
4534
4553
SourceModule module_info;
4535
4554
module_info.path .push_back (ConstString (module_name));
4536
4555
auto module_or_err = GetModule (module_info);
@@ -9130,9 +9149,6 @@ bool SwiftASTContextForExpressions::CacheUserImports(
9130
9149
9131
9150
auto src_file_imports = source_file.getImports ();
9132
9151
9133
- Progress progress (" Importing modules used in expression" );
9134
- size_t completion = 0 ;
9135
-
9136
9152
// / Find all explicit imports in the expression.
9137
9153
struct UserImportFinder : public swift ::ASTWalker {
9138
9154
llvm::SmallDenseSet<swift::ModuleDecl*, 1 > imports;
@@ -9148,9 +9164,6 @@ bool SwiftASTContextForExpressions::CacheUserImports(
9148
9164
source_file.walk (import_finder);
9149
9165
9150
9166
for (const auto &attributed_import : src_file_imports) {
9151
- progress.Increment (
9152
- ++completion,
9153
- attributed_import.module .importedModule ->getModuleFilename ().str ());
9154
9167
swift::ModuleDecl *module = attributed_import.module .importedModule ;
9155
9168
if (module && import_finder.imports .count (module )) {
9156
9169
std::string module_name;
@@ -9250,7 +9263,7 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
9250
9263
9251
9264
// Import the Swift standard library and its dependencies.
9252
9265
SourceModule swift_module;
9253
- swift_module.path .emplace_back (" Swift " );
9266
+ swift_module.path .emplace_back (swift::STDLIB_NAME );
9254
9267
auto *stdlib = LoadOneModule (swift_module, *this , process_sp,
9255
9268
/* import_dylibs=*/ true , error);
9256
9269
if (!stdlib)
@@ -9299,13 +9312,11 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
9299
9312
}
9300
9313
9301
9314
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 " ;
9304
9316
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
+
9307
9319
for (const SourceModule &module : cu_imports) {
9308
- progress.Increment (++completion, llvm::join (module .path , " ." ));
9309
9320
// When building the Swift stdlib with debug info these will
9310
9321
// show up in "Swift.o", but we already imported them and
9311
9322
// manually importing them will fail.
0 commit comments