Skip to content

Commit 5791a2c

Browse files
committed
[Macros] Plugin search options group
'load-plugin-library', 'load-plugin-executable', '-plugin-path' and '-external-plugin-path' should be searched in the order they are specified in the arguments. Previously, for example '-plugin-path' used to precede '-external-plugin-path' regardless of the position in the arguments.
1 parent 7143203 commit 5791a2c

16 files changed

+218
-274
lines changed

include/swift/AST/PluginLoader.h

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,19 @@ class PluginLoader {
5252
void setRegistry(PluginRegistry *newValue);
5353
PluginRegistry *getRegistry();
5454

55-
/// Lookup a library plugin that can handle \p moduleName and return the path
56-
/// to it from `-load-plugin-library`.
57-
/// The path returned can be loaded by 'loadLibraryPlugin' method.
58-
llvm::Optional<std::string>
59-
lookupExplicitLibraryPluginByModuleName(Identifier moduleName);
60-
61-
/// Lookup a library plugin that can handle \p moduleName and return the path
62-
/// to it from `-plugin-path`.
63-
/// The path returned can be loaded by 'loadLibraryPlugin' method.
64-
llvm::Optional<std::string>
65-
lookupLibraryPluginInSearchPathByModuleName(Identifier moduleName);
66-
67-
/// Lookup an executable plugin that is declared to handle \p moduleName
68-
/// module by '-load-plugin-executable'.
69-
/// The path returned can be loaded by 'loadExecutablePlugin' method.
70-
llvm::Optional<StringRef>
71-
lookupExecutablePluginByModuleName(Identifier moduleName);
72-
73-
/// Look for dynamic libraries in paths from `-external-plugin-path` and
74-
/// return a pair of `(library path, plugin server executable)` if found.
75-
/// These paths are valid within the VFS, use `FS.getRealPath()` for their
76-
/// underlying path.
77-
llvm::Optional<std::pair<std::string, std::string>>
78-
lookupExternalLibraryPluginByModuleName(Identifier moduleName);
55+
/// Lookup a plugin that can handle \p moduleName and return the path(s) to
56+
/// it. The path returned can be loaded by 'load(Library|Executable)Plugin()'.
57+
/// The return value is a pair of a "library path" and a "executable path".
58+
///
59+
/// * (libPath: empty, execPath: empty) - plugin not found.
60+
/// * (libPath: some, execPath: empty) - load the library path by
61+
/// 'loadLibraryPlugin()'.
62+
/// * (libPath: empty, execPath: some) - load the executable path by
63+
/// 'loadExecutablePlugin()'.
64+
/// * (libPath: some, execPath: some) - load the executable path by
65+
/// 'loadExecutablePlugin()' and let the plugin load the libPath via IPC.
66+
std::pair<std::string, std::string>
67+
lookupPluginByModuleName(Identifier moduleName);
7968

8069
/// Load the specified dylib plugin path resolving the path with the
8170
/// current VFS. If it fails to load the plugin, a diagnostic is emitted, and

include/swift/AST/SearchPathOptions.h

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/Basic/ArrayRefView.h"
1717
#include "swift/Basic/PathRemapper.h"
18+
#include "swift/Basic/TaggedUnion.h"
1819
#include "llvm/ADT/Hashing.h"
1920
#include "llvm/ADT/IntrusiveRefCntPtr.h"
2021
#include "llvm/ADT/StringMap.h"
@@ -34,7 +35,6 @@ enum class ModuleSearchPathKind {
3435
Framework,
3536
DarwinImplicitFramework,
3637
RuntimeLibrary,
37-
CompilerPlugin,
3838
};
3939

4040
/// A single module search path that can come from different sources, e.g.
@@ -187,6 +187,26 @@ struct ExternalPluginSearchPathAndServerPath {
187187
std::string ServerPath;
188188
};
189189

190+
namespace PluginSearchOption {
191+
struct LoadPluginLibrary {
192+
std::string LibraryPath;
193+
};
194+
struct LoadPluginExecutable {
195+
std::string ExecutablePath;
196+
std::vector<std::string> ModuleNames;
197+
};
198+
struct PluginPath {
199+
std::string SearchPath;
200+
};
201+
struct ExternalPluginPath {
202+
std::string SearchPath;
203+
std::string ServerPath;
204+
};
205+
206+
using Value = TaggedUnion<LoadPluginLibrary, LoadPluginExecutable, PluginPath,
207+
ExternalPluginPath>;
208+
} // namespace PluginSearchOption
209+
190210
/// Options for controlling search path behavior.
191211
class SearchPathOptions {
192212
/// To call \c addImportSearchPath and \c addFrameworkSearchPath from
@@ -259,14 +279,6 @@ class SearchPathOptions {
259279
ImportSearchPaths.size() - 1);
260280
}
261281

262-
void addCompilerPluginLibraryPath(StringRef Path, llvm::vfs::FileSystem *FS) {
263-
CompilerPluginLibraryPaths.push_back(Path.str());
264-
Lookup.searchPathAdded(FS, CompilerPluginLibraryPaths.back(),
265-
ModuleSearchPathKind::CompilerPlugin,
266-
/*isSystem=*/false,
267-
CompilerPluginLibraryPaths.size() - 1);
268-
}
269-
270282
/// Add a single framework search path. Must only be called from
271283
/// \c ASTContext::addSearchPath.
272284
void addFrameworkSearchPath(FrameworkSearchPath NewPath,
@@ -355,27 +367,6 @@ class SearchPathOptions {
355367
Lookup.searchPathsDidChange();
356368
}
357369

358-
void setCompilerPluginLibraryPaths(
359-
std::vector<std::string> NewCompilerPluginLibraryPaths) {
360-
CompilerPluginLibraryPaths = NewCompilerPluginLibraryPaths;
361-
Lookup.searchPathsDidChange();
362-
}
363-
364-
ArrayRef<std::string> getCompilerPluginLibraryPaths() const {
365-
return CompilerPluginLibraryPaths;
366-
}
367-
368-
void setCompilerPluginExecutablePaths(
369-
std::vector<PluginExecutablePathAndModuleNames> &&newValue) {
370-
CompilerPluginExecutablePaths = std::move(newValue);
371-
Lookup.searchPathsDidChange();
372-
}
373-
374-
ArrayRef<PluginExecutablePathAndModuleNames>
375-
getCompilerPluginExecutablePaths() const {
376-
return CompilerPluginExecutablePaths;
377-
}
378-
379370
/// Path(s) to virtual filesystem overlay YAML files.
380371
std::vector<std::string> VFSOverlayFiles;
381372

@@ -391,15 +382,8 @@ class SearchPathOptions {
391382
/// preference.
392383
std::vector<std::string> RuntimeLibraryPaths;
393384

394-
/// Paths that contain compiler plugins loaded on demand for, e.g.,
395-
/// macro implementations.
396-
std::vector<std::string> PluginSearchPaths;
397-
398-
/// Pairs of external compiler plugin search paths and the corresponding
399-
/// plugin server executables.
400-
/// e.g. {"/path/to/usr/lib/swift/host/plugins",
401-
/// "/path/to/usr/bin/plugin-server"}
402-
std::vector<ExternalPluginSearchPathAndServerPath> ExternalPluginSearchPaths;
385+
/// Plugin search path options.
386+
std::vector<PluginSearchOption::Value> PluginSearchOpts;
403387

404388
/// Don't look in for compiler-provided modules.
405389
bool SkipRuntimeLibraryImportPaths = false;

include/swift/Frontend/Frontend.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,6 @@ class CompilerInvocation {
198198
SearchPathOpts.VFSOverlayFiles = Overlays;
199199
}
200200

201-
void setCompilerPluginLibraryPaths(const std::vector<std::string> &Paths) {
202-
SearchPathOpts.setCompilerPluginLibraryPaths(Paths);
203-
}
204-
205-
ArrayRef<std::string> getCompilerPluginLibraryPaths() {
206-
return SearchPathOpts.getCompilerPluginLibraryPaths();
207-
}
208-
209201
void setExtraClangArgs(const std::vector<std::string> &Args) {
210202
ClangImporterOpts.ExtraArgs = Args;
211203
}

include/swift/Option/Options.td

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,6 @@ def I : JoinedOrSeparate<["-"], "I">,
304304
def I_EQ : Joined<["-"], "I=">, Flags<[FrontendOption, ArgumentIsPath]>,
305305
Alias<I>;
306306

307-
def plugin_path : Separate<["-"], "plugin-path">,
308-
Flags<[FrontendOption, ArgumentIsPath, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
309-
HelpText<"Add directory to the plugin search path">;
310-
311-
def external_plugin_path : Separate<["-"], "external-plugin-path">,
312-
Flags<[FrontendOption, ArgumentIsPath, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
313-
HelpText<"Add directory to the plugin search path with a plugin server executable">,
314-
MetaVarName<"<path>#<plugin-server-path>">;
315-
316307
def import_underlying_module : Flag<["-"], "import-underlying-module">,
317308
Flags<[FrontendOption, NoInteractiveOption]>,
318309
HelpText<"Implicitly imports the Objective-C half of a module">;
@@ -1852,15 +1843,27 @@ def clang_include_tree_root: Separate<["-"], "clang-include-tree-root">,
18521843
Flags<[FrontendOption, NoDriverOption]>,
18531844
HelpText<"Clang Include Tree CASID">, MetaVarName<"<cas-id>">;
18541845

1846+
1847+
def plugin_search_Group : OptionGroup<"<plugin search options>">;
1848+
1849+
def plugin_path : Separate<["-"], "plugin-path">, Group<plugin_search_Group>,
1850+
Flags<[FrontendOption, ArgumentIsPath, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
1851+
HelpText<"Add directory to the plugin search path">;
1852+
1853+
def external_plugin_path : Separate<["-"], "external-plugin-path">, Group<plugin_search_Group>,
1854+
Flags<[FrontendOption, ArgumentIsPath, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
1855+
HelpText<"Add directory to the plugin search path with a plugin server executable">,
1856+
MetaVarName<"<path>#<plugin-server-path>">;
1857+
18551858
def load_plugin_library:
1856-
Separate<["-"], "load-plugin-library">,
1859+
Separate<["-"], "load-plugin-library">, Group<plugin_search_Group>,
18571860
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,
18581861
HelpText<"Path to a dynamic library containing compiler plugins such as "
18591862
"macros">,
18601863
MetaVarName<"<path>">;
18611864

18621865
def load_plugin_executable:
1863-
Separate<["-"], "load-plugin-executable">,
1866+
Separate<["-"], "load-plugin-executable">, Group<plugin_search_Group>,
18641867
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,
18651868
HelpText<"Path to an executable compiler plugins and providing module names "
18661869
"such as macros">,

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,10 @@ llvm::StringRef swift::getProtocolName(KnownProtocolKind kind) {
103103
}
104104

105105
namespace {
106-
enum class SearchPathKind : uint8_t {
107-
Import = 1 << 0,
108-
Framework = 1 << 1,
109-
CompilerPlugin = 1 << 2
110-
};
106+
enum class SearchPathKind : uint8_t {
107+
Import = 1 << 0,
108+
Framework = 1 << 1,
109+
};
111110
} // end anonymous namespace
112111

113112
using AssociativityCacheType =
@@ -694,8 +693,6 @@ ASTContext::ASTContext(
694693
getImpl().SearchPathsSet[path] |= SearchPathKind::Import;
695694
for (const auto &framepath : SearchPathOpts.getFrameworkSearchPaths())
696695
getImpl().SearchPathsSet[framepath.Path] |= SearchPathKind::Framework;
697-
for (StringRef path : SearchPathOpts.getCompilerPluginLibraryPaths())
698-
getImpl().SearchPathsSet[path] |= SearchPathKind::CompilerPlugin;
699696

700697
// Register any request-evaluator functions available at the AST layer.
701698
registerAccessRequestFunctions(evaluator);

lib/AST/PluginLoader.cpp

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
using namespace swift;
2121

2222
void PluginLoader::createModuleToExecutablePluginMap() {
23-
for (auto &arg : Ctx.SearchPathOpts.getCompilerPluginExecutablePaths()) {
24-
// Create a moduleName -> pluginPath mapping.
25-
assert(!arg.ExecutablePath.empty() && "empty plugin path");
26-
StringRef pathStr = Ctx.AllocateCopy(arg.ExecutablePath);
27-
for (auto moduleName : arg.ModuleNames) {
28-
ExecutablePluginPaths[Ctx.getIdentifier(moduleName)] = pathStr;
23+
for (auto &elem : Ctx.SearchPathOpts.PluginSearchOpts) {
24+
if (auto *arg = elem.dyn_cast<PluginSearchOption::LoadPluginExecutable>()) {
25+
// Create a moduleName -> pluginPath mapping.
26+
assert(!arg->ExecutablePath.empty() && "empty plugin path");
27+
StringRef pathStr = Ctx.AllocateCopy(arg->ExecutablePath);
28+
for (auto moduleName : arg->ModuleNames) {
29+
ExecutablePluginPaths[Ctx.getIdentifier(moduleName)] = pathStr;
30+
}
2931
}
3032
}
3133
}
@@ -46,69 +48,59 @@ PluginRegistry *PluginLoader::getRegistry() {
4648
return Registry;
4749
}
4850

49-
llvm::Optional<std::string>
50-
PluginLoader::lookupExplicitLibraryPluginByModuleName(Identifier moduleName) {
51-
// Look for 'lib${module name}(.dylib|.so)'.
52-
SmallString<64> expectedBasename;
53-
expectedBasename.append("lib");
54-
expectedBasename.append(moduleName.str());
55-
expectedBasename.append(LTDL_SHLIB_EXT);
56-
57-
// Try '-load-plugin-library'.
58-
for (const auto &libPath :
59-
Ctx.SearchPathOpts.getCompilerPluginLibraryPaths()) {
60-
if (llvm::sys::path::filename(libPath) == expectedBasename) {
61-
return libPath;
62-
}
63-
}
64-
return None;
65-
}
51+
std::pair<std::string, std::string>
52+
PluginLoader::lookupPluginByModuleName(Identifier moduleName) {
53+
auto fs = Ctx.SourceMgr.getFileSystem();
6654

67-
llvm::Optional<std::string>
68-
PluginLoader::lookupLibraryPluginInSearchPathByModuleName(
69-
Identifier moduleName) {
7055
// Look for 'lib${module name}(.dylib|.so)'.
71-
SmallString<64> expectedBasename;
72-
expectedBasename.append("lib");
73-
expectedBasename.append(moduleName.str());
74-
expectedBasename.append(LTDL_SHLIB_EXT);
75-
76-
// Try '-plugin-path'.
77-
auto fs = Ctx.SourceMgr.getFileSystem();
78-
for (const auto &searchPath : Ctx.SearchPathOpts.PluginSearchPaths) {
79-
SmallString<128> fullPath(searchPath);
80-
llvm::sys::path::append(fullPath, expectedBasename);
81-
if (fs->exists(fullPath)) {
82-
return std::string(fullPath);
56+
// FIXME: Shared library prefix might be different between platforms.
57+
SmallString<64> pluginLibBasename;
58+
pluginLibBasename.append("lib");
59+
pluginLibBasename.append(moduleName.str());
60+
pluginLibBasename.append(LTDL_SHLIB_EXT);
61+
62+
// FIXME: Should we create a lookup table keyed by module name?
63+
for (auto &entry : Ctx.SearchPathOpts.PluginSearchOpts) {
64+
using namespace PluginSearchOption;
65+
// Try '-load-plugin-library'.
66+
if (auto *val = entry.dyn_cast<LoadPluginLibrary>()) {
67+
if (llvm::sys::path::filename(val->LibraryPath) == pluginLibBasename) {
68+
return {val->LibraryPath, ""};
69+
}
70+
continue;
8371
}
84-
}
85-
86-
return None;
87-
}
8872

89-
Optional<std::pair<std::string, std::string>>
90-
PluginLoader::lookupExternalLibraryPluginByModuleName(Identifier moduleName) {
91-
auto fs = Ctx.SourceMgr.getFileSystem();
73+
// Try '-load-plugin-executable'.
74+
if (auto *v = entry.dyn_cast<LoadPluginExecutable>()) {
75+
auto found = ExecutablePluginPaths.find(moduleName);
76+
if (found != ExecutablePluginPaths.end()) {
77+
return {"", std::string(found->second)};
78+
}
79+
continue;
80+
}
9281

93-
for (auto &pair : Ctx.SearchPathOpts.ExternalPluginSearchPaths) {
94-
SmallString<128> fullPath(pair.SearchPath);
95-
llvm::sys::path::append(fullPath,
96-
"lib" + moduleName.str() + LTDL_SHLIB_EXT);
82+
// Try '-plugin-path'.
83+
if (auto *v = entry.dyn_cast<PluginPath>()) {
84+
SmallString<128> fullPath(v->SearchPath);
85+
llvm::sys::path::append(fullPath, pluginLibBasename);
86+
if (fs->exists(fullPath)) {
87+
return {std::string(fullPath), ""};
88+
}
89+
continue;
90+
}
9791

98-
if (fs->exists(fullPath)) {
99-
return {{std::string(fullPath), pair.ServerPath}};
92+
// Try '-external-plugin-path'.
93+
if (auto *v = entry.dyn_cast<ExternalPluginPath>()) {
94+
SmallString<128> fullPath(v->SearchPath);
95+
llvm::sys::path::append(fullPath, pluginLibBasename);
96+
if (fs->exists(fullPath)) {
97+
return {std::string(fullPath), v->ServerPath};
98+
}
99+
continue;
100100
}
101101
}
102-
return None;
103-
}
104102

105-
Optional<StringRef>
106-
PluginLoader::lookupExecutablePluginByModuleName(Identifier moduleName) {
107-
auto &execPluginPaths = ExecutablePluginPaths;
108-
auto found = execPluginPaths.find(moduleName);
109-
if (found == execPluginPaths.end())
110-
return None;
111-
return found->second;
103+
return {};
112104
}
113105

114106
LoadedLibraryPlugin *PluginLoader::loadLibraryPlugin(StringRef path) {

lib/AST/SearchPathOptions.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ void ModuleSearchPathLookup::rebuildLookupTable(const SearchPathOptions *Opts,
7373
/*isSystem=*/true, Entry.index());
7474
}
7575

76-
for (auto Entry : llvm::enumerate(Opts->getCompilerPluginLibraryPaths())) {
77-
addFilesInPathToLookupTable(FS, Entry.value(),
78-
ModuleSearchPathKind::CompilerPlugin,
79-
/*isSystem=*/false, Entry.index());
80-
}
81-
8276
State.FileSystem = FS;
8377
State.IsOSDarwin = IsOSDarwin;
8478
State.Opts = Opts;

0 commit comments

Comments
 (0)