Skip to content

Commit 50ea3de

Browse files
authored
Merge pull request swiftlang#63586 from bnbarham/load-should-respect-overlay
[Macros] Respect VFS overlays when loading plugins
2 parents ea1d0a3 + 43dd56b commit 50ea3de

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/AST/ASTContext.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6204,10 +6204,19 @@ BuiltinTupleType *ASTContext::getBuiltinTupleType() {
62046204

62056205
void ASTContext::loadCompilerPlugins() {
62066206
for (auto &path : SearchPathOpts.getCompilerPluginLibraryPaths()) {
6207+
auto fs = this->SourceMgr.getFileSystem();
6208+
SmallString<128> resolvedPath;
6209+
if (auto err = fs->getRealPath(path, resolvedPath)) {
6210+
Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path,
6211+
err.message());
6212+
continue;
6213+
}
6214+
62076215
void *lib = nullptr;
62086216
#if !defined(_WIN32)
6209-
lib = dlopen(path.c_str(), RTLD_LAZY|RTLD_LOCAL);
6217+
lib = dlopen(resolvedPath.c_str(), RTLD_LAZY|RTLD_LOCAL);
62106218
#endif
6219+
62116220
if (!lib) {
62126221
const char *errorMsg = "Unsupported platform";
62136222
#if !defined(_WIN32)
@@ -6218,7 +6227,7 @@ void ASTContext::loadCompilerPlugins() {
62186227
continue;
62196228
}
62206229

6221-
getImpl().LoadedPluginPaths.push_back({path, lib});
6230+
getImpl().LoadedPluginPaths.emplace_back(resolvedPath.str().str(), lib);
62226231
}
62236232
}
62246233

lib/Sema/TypeCheckMacros.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,12 @@ MacroDefinition MacroDefinitionRequest::evaluate(
301301
}
302302

303303
/// Load a plugin library based on a module name.
304-
static void *loadPluginByName(StringRef searchPath, StringRef moduleName) {
304+
static void *loadPluginByName(StringRef searchPath, StringRef moduleName, llvm::vfs::FileSystem &fs) {
305305
SmallString<128> fullPath(searchPath);
306306
llvm::sys::path::append(fullPath, "lib" + moduleName + LTDL_SHLIB_EXT);
307+
if (fs.getRealPath(fullPath, fullPath))
308+
return nullptr;
309+
307310
#if defined(_WIN32)
308311
return LoadLibraryA(fullPath.c_str());
309312
#else
@@ -314,9 +317,10 @@ static void *loadPluginByName(StringRef searchPath, StringRef moduleName) {
314317
void *CompilerPluginLoadRequest::evaluate(
315318
Evaluator &evaluator, ASTContext *ctx, Identifier moduleName
316319
) const {
320+
auto fs = ctx->SourceMgr.getFileSystem();
317321
auto &searchPathOpts = ctx->SearchPathOpts;
318322
for (const auto &path : searchPathOpts.PluginSearchPaths) {
319-
if (auto found = loadPluginByName(path, moduleName.str()))
323+
if (auto found = loadPluginByName(path, moduleName.str(), *fs))
320324
return found;
321325
}
322326

test/Macros/macro_vfs.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/hidden)
3+
// RUN: split-file %s %t
4+
// RUN: sed -e "s@VFS_DIR@%{/t:regex_replacement}/vfs@g" -e "s@EXTERNAL_DIR@%{/t:regex_replacement}/hidden@g" %t/base.yaml > %t/overlay.yaml
5+
6+
// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/hidden/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath -swift-version 5
7+
8+
// Check that loading plugins respects VFS overlays
9+
// RUN: %target-swift-frontend -typecheck -verify -swift-version 5 -enable-experimental-feature Macros -load-plugin-library %t/vfs/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS %t/macro.swift -vfsoverlay %t/overlay.yaml
10+
// RUN: %target-swift-frontend -typecheck -verify -swift-version 5 -enable-experimental-feature Macros -plugin-path %t/vfs -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS %t/macro.swift -vfsoverlay %t/overlay.yaml
11+
12+
// FIXME: Swift parser is not enabled on Linux CI yet.
13+
// REQUIRES: OS=macosx
14+
15+
//--- macro.swift
16+
// expected-no-diagnostics
17+
@freestanding(expression) macro customFileID() -> String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
18+
let _ = #customFileID
19+
20+
//--- base.yaml
21+
{
22+
version: 0,
23+
roots: [
24+
{
25+
type: "directory-remap",
26+
name: "VFS_DIR",
27+
external-contents: "EXTERNAL_DIR"
28+
}
29+
]
30+
}
31+

0 commit comments

Comments
 (0)