@@ -1899,6 +1899,22 @@ void SwiftASTContext::AddExtraClangArgs(
18991899 RemoveExplicitModules (importer_options.ExtraArgs );
19001900}
19011901
1902+ bool SwiftASTContext::IsModuleAvailableInCAS (const std::string &key) {
1903+ auto id = m_cas->parseID (key);
1904+ if (!id) {
1905+ HEALTH_LOG_PRINTF (" failed to parse CASID when loading module: %s" ,
1906+ toString (id.takeError ()).c_str ());
1907+ return false ;
1908+ }
1909+ auto lookup = m_action_cache->get (*id);
1910+ if (!lookup) {
1911+ HEALTH_LOG_PRINTF (" module lookup failure through action cache: %s" ,
1912+ toString (lookup.takeError ()).c_str ());
1913+ return false ;
1914+ }
1915+ return (bool )*lookup;
1916+ };
1917+
19021918void SwiftASTContext::AddExtraClangCC1Args (
19031919 const std::vector<std::string> &source,
19041920 const std::vector<std::pair<std::string, bool >> module_search_paths,
@@ -1970,26 +1986,9 @@ void SwiftASTContext::AddExtraClangCC1Args(
19701986 invocation.getCASOpts ().PluginOptions =
19711987 GetCASOptions ().CASOpts .PluginOptions ;
19721988
1973- // Check the module availability in CAS, if not, fallback to regular load.
1974- auto CheckModuleInCAS = [&](const std::string &key) {
1975- auto id = m_cas->parseID (key);
1976- if (!id) {
1977- HEALTH_LOG_PRINTF (" failed to parse CASID when loading module: %s" ,
1978- toString (id.takeError ()).c_str ());
1979- return false ;
1980- }
1981- auto lookup = m_action_cache->get (*id);
1982- if (!lookup) {
1983- HEALTH_LOG_PRINTF (" module lookup failure through action cache: %s" ,
1984- toString (lookup.takeError ()).c_str ());
1985- return false ;
1986- }
1987- return (bool )*lookup;
1988- };
1989-
19901989 use_cas_module = llvm::all_of (
19911990 invocation.getFrontendOpts ().ModuleCacheKeys , [&](const auto &entry) {
1992- auto exist = CheckModuleInCAS (entry.second );
1991+ bool exist = IsModuleAvailableInCAS (entry.second );
19931992 if (!exist)
19941993 HEALTH_LOG_PRINTF (" module '%s' cannot be load "
19951994 " from CAS using key: %s, fallback to "
@@ -3748,7 +3747,6 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
37483747 std::string moduleCachePath =
37493748 GetCompilerInvocation ().getClangModuleCachePath ().str ();
37503749 std::unique_ptr<swift::ClangImporter> clang_importer_up;
3751- auto &clang_importer_options = GetClangImporterOptions ();
37523750 if (!m_ast_context_up->SearchPathOpts .getSDKPath ().empty () ||
37533751 TargetHasNoSDK ()) {
37543752 // Create the DWARFImporterDelegate.
@@ -3848,15 +3846,34 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
38483846 m_ast_context_up->addModuleLoader (std::move (memory_buffer_loader_up));
38493847 }
38503848
3849+ // 2. Create the explicit swift module loader.
3850+ if (props.GetUseSwiftExplicitModuleLoader ()) {
3851+ auto &search_path_opts = GetCompilerInvocation ().getSearchPathOptions ();
3852+ std::unique_ptr<swift::ModuleLoader> esml_up =
3853+ swift::ExplicitSwiftModuleLoader::create (
3854+ *m_ast_context_up, m_dependency_tracker.get (), loading_mode,
3855+ search_path_opts.ExplicitSwiftModuleMapPath ,
3856+ search_path_opts.ExplicitSwiftModuleInputs ,
3857+ /* IgnoreSwiftSourceInfo*/ false );
3858+ if (esml_up) {
3859+ m_explicit_swift_module_loader =
3860+ static_cast <swift::ExplicitSwiftModuleLoader *>(esml_up.get ());
3861+ m_ast_context_up->addModuleLoader (std::move (esml_up), /* isClang=*/ false ,
3862+ /* isDwarf=*/ false ,
3863+ /* isInterface=*/ false ,
3864+ /* isExplicit=*/ true );
3865+ }
3866+ }
3867+
38513868 // Add a module interface checker.
38523869 m_ast_context_up->addModuleInterfaceChecker (
3853- std::make_unique<swift::ModuleInterfaceCheckerImpl>(*m_ast_context_up,
3854- moduleCachePath, prebuiltModuleCachePath,
3855- swift::ModuleInterfaceLoaderOptions ()));
3870+ std::make_unique<swift::ModuleInterfaceCheckerImpl>(
3871+ *m_ast_context_up, moduleCachePath, prebuiltModuleCachePath,
3872+ swift::ModuleInterfaceLoaderOptions ()));
38563873
3857- // 2 . Create and install the module interface loader.
3874+ // 3 . Create and install the module interface loader.
38583875 //
3859- // The ordering of 2-4 is the same as the Swift compiler's 1-3 ,
3876+ // The ordering of 2-4 is the same as the Swift compiler's 2-4 ,
38603877 // where unintuitively the serialized module loader comes before the
38613878 // module interface loader. The reason for this is that the module
38623879 // interface loader is actually 2-in-1 and secretly attempts to load
@@ -3868,21 +3885,22 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
38683885 if (loading_mode != swift::ModuleLoadingMode::OnlySerialized) {
38693886 std::unique_ptr<swift::ModuleLoader> module_interface_loader_up (
38703887 swift::ModuleInterfaceLoader::create (
3871- *m_ast_context_up, *static_cast <swift::ModuleInterfaceCheckerImpl*>(
3872- m_ast_context_up->getModuleInterfaceChecker ()), m_dependency_tracker.get (),
3873- loading_mode));
3888+ *m_ast_context_up,
3889+ *static_cast <swift::ModuleInterfaceCheckerImpl *>(
3890+ m_ast_context_up->getModuleInterfaceChecker ()),
3891+ m_dependency_tracker.get (), loading_mode));
38743892 if (module_interface_loader_up)
38753893 m_ast_context_up->addModuleLoader (std::move (module_interface_loader_up));
38763894 }
38773895
3878- // 3 . Create and install the serialized module loader.
3896+ // 4 . Create and install the serialized module loader.
38793897 std::unique_ptr<swift::ModuleLoader> serialized_module_loader_up (
38803898 swift::ImplicitSerializedModuleLoader::create (
38813899 *m_ast_context_up, m_dependency_tracker.get (), loading_mode));
38823900 if (serialized_module_loader_up)
38833901 m_ast_context_up->addModuleLoader (std::move (serialized_module_loader_up));
38843902
3885- // 4 . Install the clang importer.
3903+ // 5 . Install the clang importer.
38863904 if (clang_importer_up) {
38873905 m_clangimporter = (swift::ClangImporter *)clang_importer_up.get ();
38883906 m_ast_context_up->addModuleLoader (std::move (clang_importer_up),
@@ -4080,6 +4098,23 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
40804098 // Create a diagnostic consumer for the diagnostics produced by the import.
40814099 auto import_diags = getScopedDiagnosticConsumer ();
40824100
4101+ // Is this an explicitly specified explicit Swift module?
4102+ StringRef module_path = module .search_path .GetStringRef ();
4103+ bool is_esml_module = (module_path.ends_with (" .swiftmodule" ) &&
4104+ llvm::sys::fs::exists (module_path)) ||
4105+ (m_cas && IsModuleAvailableInCAS (module_path.str ()));
4106+ if (is_esml_module) {
4107+ std::string path = module_path.str ();
4108+ bool unloaded = false ;
4109+ if (m_explicit_swift_module_loader) {
4110+ ast->addExplicitModulePath (module_name, path);
4111+ if (auto *memory_loader = GetMemoryBufferModuleLoader ())
4112+ unloaded = memory_loader->unregisterMemoryBuffer (module_name);
4113+ }
4114+ HEALTH_LOG_PRINTF (" found explicit module \" %s\" %s" , path.c_str (),
4115+ unloaded ? " ; replacing AST section module" : " " );
4116+ }
4117+
40834118 swift::ModuleDecl *module_decl = ast->getModuleByName (module_name);
40844119
40854120 // Error handling.
@@ -4102,6 +4137,16 @@ SwiftASTContext::GetModule(const SourceModule &module, bool *cached) {
41024137 LOG_PRINTF (GetLog (LLDBLog::Types), " (\" %s\" ) -- found %s" ,
41034138 module_name.c_str (), module_decl->getName ().str ().str ().c_str ());
41044139
4140+ if (is_esml_module) {
4141+ // Simulate the effect of the BypassResilience flag in the
4142+ // MemoryBufferSerializedModuleLoader. Explicitly specified
4143+ // modules are not typically produced from textual interfaces. By
4144+ // disabling resilience, the debugger can directly access private
4145+ // members.
4146+ // if (!module_decl->isBuiltFromInterface())
4147+ // module_decl->setBypassResilience();
4148+ }
4149+
41054150 m_swift_module_cache.insert ({module_name, *module_decl});
41064151 return *module_decl;
41074152}
0 commit comments