From 6aaf42a5dc5081adc4f2b718bf287f910a032e31 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 15 Jun 2023 16:33:24 -0700 Subject: [PATCH 1/2] [Swift/Macros] Update for serialization change in Swift Plugin search options are now serialized as a single list of options paired with the kind. (cherry picked from commit 72316058ef6c1fe18a5992938b7656084d25fc9b) --- .../TypeSystem/Swift/SwiftASTContext.cpp | 154 +++++++++--------- 1 file changed, 79 insertions(+), 75 deletions(-) diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp index 2a4fb0aa56c41..b041ed3f25e6b 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp @@ -1311,87 +1311,91 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation, return false; }; - // Discover, rewrite, and unique compiler plugin paths. - for (auto path : extended_validation_info.getPluginSearchPaths()) { - // System plugins shipping with the compiler. - // Rewrite them to go through an ABI-compatible swift-plugin-server. - if (known_plugin_search_paths.insert(path).second) { - if (known_external_plugin_search_paths.insert(path).second) { - std::string server = get_plugin_server( - path, [&]() { return GetPluginServer(path); }); - if (server.empty()) - continue; - if (exists(path)) - plugin_search_options.emplace_back( - swift::PluginSearchOption::ExternalPluginPath{path.str(), - server}); + for (auto &opt : extended_validation_info.getPluginSearchOptions()) { + if (opt.first == "-plugin-path") { + StringRef path = opt.second; + // System plugins shipping with the compiler. + // Rewrite them to go through an ABI-compatible swift-plugin-server. + if (known_plugin_search_paths.insert(path).second) { + if (known_external_plugin_search_paths.insert(path).second) { + std::string server = get_plugin_server( + path, [&]() { return GetPluginServer(path); }); + if (server.empty()) + continue; + if (exists(path)) + plugin_search_options.emplace_back( + swift::PluginSearchOption::ExternalPluginPath{path.str(), + server}); + } } + continue; } - } - for (auto path : - extended_validation_info.getExternalPluginSearchPaths()) { - // Sandboxed system plugins shipping with some compiler. - // Keep the original plugin server path, it needs to be ABI - // compatible with the version of SwiftSyntax used by the plugin. - auto plugin_server = path.split('#'); - llvm::StringRef plugin = plugin_server.first; - std::string server = get_plugin_server( - plugin, [&]() { return plugin_server.second.str(); }); - if (server.empty()) + if (opt.first == "-external-plugin-path") { + // Sandboxed system plugins shipping with some compiler. + // Keep the original plugin server path, it needs to be ABI + // compatible with the version of SwiftSyntax used by the plugin. + auto plugin_server = opt.second.split('#'); + llvm::StringRef plugin = plugin_server.first; + std::string server = get_plugin_server( + plugin, [&]() { return plugin_server.second.str(); }); + if (server.empty()) + continue; + if (known_external_plugin_search_paths.insert(plugin).second) + if (exists(plugin)) + plugin_search_options.emplace_back( + swift::PluginSearchOption::ExternalPluginPath{plugin.str(), + server}); continue; - if (known_external_plugin_search_paths.insert(plugin).second) - if (exists(plugin)) - plugin_search_options.emplace_back( - swift::PluginSearchOption::ExternalPluginPath{plugin.str(), - server}); - } + } + if (opt.first == "-load-plugin-library") { + // Compiler plugin libraries. + StringRef dylib = opt.second; + if (known_compiler_plugin_library_paths.insert(dylib).second) + if (exists(dylib)) { + // We never want to directly load any plugins, since a crash in + // the plugin would bring down LLDB. Here, we assume that the + // correct plugin server for a direct compiler plugin is the one + // from the SDK the compiler was building for. This is just a + // heuristic. + // This works because the Swift compiler enforces + // '-load-plugin-library' dylibs to be named + // libModuleName.[dylib|so|dll] just like + // '-external-plugin-path'. + llvm::SmallString<0> dir(dylib); + llvm::sys::path::remove_filename(dir); + std::string server = get_plugin_server(dir, [&]() { + return GetPluginServerForSDK(invocation.getSDKPath()); + }); + if (server.empty()) + continue; - for (auto dylib : - extended_validation_info.getCompilerPluginLibraryPaths()) { - // Compiler plugin libraries. - if (known_compiler_plugin_library_paths.insert(dylib).second) - if (exists(dylib)) { - // We never want to directly load any plugins, since a crash in - // the plugin would bring down LLDB. Here, we assume that the - // correct plugin server for a direct compiler plugin is the one - // from the SDK the compiler was building for. This is just a - // heuristic. - llvm::SmallString<0> dir(dylib); - llvm::sys::path::remove_filename(dir); - std::string server = get_plugin_server(dir, [&]() { - return GetPluginServerForSDK(invocation.getSDKPath()); - }); - if (server.empty()) - continue; - - // FIXME: The Swift compiler expects external plugins - // to be named libModuleName.[dylib|so|dll]. This - // means this our translation attempts only work for - // macro libraries following this convention. cf. - // PluginLoader::lookupExternalLibraryPluginByModuleName(). - plugin_search_options.emplace_back( - swift::PluginSearchOption::ExternalPluginPath{dir.str().str(), - server}); - } + plugin_search_options.emplace_back( + swift::PluginSearchOption::ExternalPluginPath{ + dir.str().str(), server}); + } + continue; + } + if (opt.first == "-load-plugin-executable") { + // Compiler plugin executables. + auto plugin_modules = opt.second.split('#'); + llvm::StringRef plugin = plugin_modules.first; + llvm::StringRef modules_list = plugin_modules.second; + llvm::SmallVector modules; + modules_list.split(modules, ","); + std::vector modules_vec; + for (auto m : modules) + modules_vec.push_back(m.str()); + if (known_compiler_plugin_executable_paths.insert(opt.second) + .second) + if (exists(plugin)) + plugin_search_options.emplace_back( + swift::PluginSearchOption::LoadPluginExecutable{ + plugin.str(), modules_vec}); + continue; + } + llvm_unreachable("unhandled plugin search option kind"); } - for (auto path : - extended_validation_info.getCompilerPluginExecutablePaths()) { - // Compiler plugin executables. - auto plugin_modules = path.split('#'); - llvm::StringRef plugin = plugin_modules.first; - llvm::StringRef modules_list = plugin_modules.second; - llvm::SmallVector modules; - modules_list.split(modules, ","); - std::vector modules_vec; - for (auto m : modules) - modules_vec.push_back(m.str()); - if (known_compiler_plugin_executable_paths.insert(path).second) - if (exists(plugin)) - plugin_search_options.emplace_back( - swift::PluginSearchOption::LoadPluginExecutable{plugin.str(), - modules_vec}); - } return true; }; From 978629883105f20a4a99d241c0cf6cef3c5d074e Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Fri, 16 Jun 2023 11:39:12 -0700 Subject: [PATCH 2/2] [Swift/Macros] Update for PluginSearchOption::Kind (cherry picked from commit 93d4f9bad1979d6b5ff66a1ad0ec7780e16c4b3a) --- .../TypeSystem/Swift/SwiftASTContext.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp index b041ed3f25e6b..d1c7b4bd79222 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp @@ -1220,7 +1220,7 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation, getFrameworkSearchPaths(), framework_search_paths, .Path); - std::vector plugin_search_options; + std::vector plugin_search_options; llvm::StringSet<> known_plugin_search_paths; llvm::StringSet<> known_external_plugin_search_paths; llvm::StringSet<> known_compiler_plugin_library_paths; @@ -1312,7 +1312,8 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation, }; for (auto &opt : extended_validation_info.getPluginSearchOptions()) { - if (opt.first == "-plugin-path") { + switch (opt.first) { + case swift::PluginSearchOption::Kind::PluginPath: { StringRef path = opt.second; // System plugins shipping with the compiler. // Rewrite them to go through an ABI-compatible swift-plugin-server. @@ -1330,7 +1331,7 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation, } continue; } - if (opt.first == "-external-plugin-path") { + case swift::PluginSearchOption::Kind::ExternalPluginPath: { // Sandboxed system plugins shipping with some compiler. // Keep the original plugin server path, it needs to be ABI // compatible with the version of SwiftSyntax used by the plugin. @@ -1347,7 +1348,7 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation, server}); continue; } - if (opt.first == "-load-plugin-library") { + case swift::PluginSearchOption::Kind::LoadPluginLibrary: { // Compiler plugin libraries. StringRef dylib = opt.second; if (known_compiler_plugin_library_paths.insert(dylib).second) @@ -1375,7 +1376,7 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation, } continue; } - if (opt.first == "-load-plugin-executable") { + case swift::PluginSearchOption::Kind::LoadPluginExecutable: { // Compiler plugin executables. auto plugin_modules = opt.second.split('#'); llvm::StringRef plugin = plugin_modules.first; @@ -1393,6 +1394,7 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation, plugin.str(), modules_vec}); continue; } + } llvm_unreachable("unhandled plugin search option kind"); } @@ -2022,7 +2024,7 @@ static void ProcessModule( ModuleSP module_sp, std::string m_description, bool discover_implicit_search_paths, bool use_all_compiler_flags, Target &target, llvm::Triple triple, - std::vector &plugin_search_options, + std::vector &plugin_search_options, std::vector &module_search_paths, std::vector> &framework_search_paths, std::vector &extra_clang_args) { @@ -2168,7 +2170,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance( LLDB_SCOPED_TIMER(); std::string m_description = "SwiftASTContextForExpressions"; - std::vector plugin_search_options; + std::vector plugin_search_options; std::vector module_search_paths; std::vector> framework_search_paths; TargetSP target_sp = typeref_typesystem.GetTargetWP().lock(); @@ -4633,7 +4635,7 @@ void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) { bool use_all_compiler_flags = target_sp->GetUseAllCompilerFlags(); unsigned num_images = module_list.GetSize(); for (size_t mi = 0; mi != num_images; ++mi) { - std::vector plugin_search_options; + std::vector plugin_search_options; std::vector module_search_paths; std::vector> framework_search_paths; std::vector extra_clang_args;