Skip to content

Commit

Permalink
macro fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
cachemeifyoucan committed Feb 14, 2024
1 parent 121fc4e commit 4433359
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 13 deletions.
15 changes: 3 additions & 12 deletions lib/AST/ModuleDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,20 +464,11 @@ void SwiftDependencyTracker::addCommonSearchPathDeps(
}

// Add plugin dylibs from the toolchain only by look through the plugin search
// directory and add those within the runtime resource dir.
SmallString<256> PluginDir(Opts.RuntimeResourcePath);
if (PluginDir.empty())
return;

llvm::sys::path::append(PluginDir, "host");
// Helper function to look for plugin to include into CASFS if provided
// directory is within the toolchain plugin.
// directory.
auto recordFiles = [&](StringRef Path) {
if (!Path.starts_with(PluginDir))
return;
std::error_code EC;
for (auto I = FS->dir_begin(Path, EC); I != llvm::vfs::directory_iterator();
I = I.increment(EC)) {
for (auto I = FS->dir_begin(Path, EC);
!EC && I != llvm::vfs::directory_iterator(); I = I.increment(EC)) {
if (I->type() != llvm::sys::fs::file_type::regular_file)
continue;
#if defined(_WIN32)
Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ initializeExecutablePlugin(ASTContext &ctx,
if (!libraryPath.empty()) {
#if SWIFT_BUILD_SWIFT_SYNTAX
llvm::SmallString<128> resolvedLibraryPath;
auto fs = ctx.SourceMgr.getFileSystem();
auto fs = ctx.ClangImporterOpts.HasClangIncludeTreeRoot
? llvm::vfs::getRealFileSystem()
: ctx.SourceMgr.getFileSystem();
if (auto err = fs->getRealPath(libraryPath, resolvedLibraryPath)) {
return llvm::createStringError(err, err.message());
}
Expand Down
94 changes: 94 additions & 0 deletions test/CAS/macro_plugin_external.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// REQUIRES: swift_swift_parser

/// Test loading and external library through `-load-plugin-library`
/// TODO: switch this test case to use `-external-plugin-path`.

// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/plugins)
//
//== Build the plugin library
// RUN: %host-build-swift \
// RUN: -swift-version 5 \
// RUN: -emit-library \
// RUN: -o %t/plugins/%target-library-name(MacroDefinition) \
// RUN: -module-name=MacroDefinition \
// RUN: %S/../Macros/Inputs/syntax_macro_definitions.swift \
// RUN: -g -no-toolchain-stdlib-rpath

// RUN: %target-swift-frontend -scan-dependencies -module-name MyApp -module-cache-path %t/clang-module-cache -O \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
// RUN: %s -o %t/deps.json -swift-version 5 -cache-compile-job -cas-path %t/cas -external-plugin-path %t/plugins#%swift-plugin-server

// RUN: %S/Inputs/SwiftDepsExtractor.py %t/deps.json MyApp casFSRootID > %t/fs.casid
// RUN: llvm-cas -cas %t/cas -ls-tree-recursive @%t/fs.casid | %FileCheck %s --check-prefix=FS

// FS: MacroDefinition

// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json clang:SwiftShims > %t/SwiftShims.cmd
// RUN: %swift_frontend_plain @%t/SwiftShims.cmd

// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json Swift > %t/Swift.cmd
// RUN: %swift_frontend_plain @%t/Swift.cmd

// RUN: %S/Inputs/SwiftDepsExtractor.py %t/deps.json Swift moduleCacheKey | tr -d '\n' > %t/Swift.key
// RUN: %S/Inputs/SwiftDepsExtractor.py %t/deps.json clang:SwiftShims moduleCacheKey | tr -d '\n' > %t/Shims.key
// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json MyApp > %t/MyApp.cmd

// RUN: echo "[{" > %/t/map.json
// RUN: echo "\"moduleName\": \"Swift\"," >> %/t/map.json
// RUN: echo "\"modulePath\": \"Swift.swiftmodule\"," >> %/t/map.json
// RUN: echo -n "\"moduleCacheKey\": " >> %/t/map.json
// RUN: cat %t/Swift.key >> %/t/map.json
// RUN: echo "," >> %/t/map.json
// RUN: echo "\"isFramework\": false" >> %/t/map.json
// RUN: echo "}," >> %/t/map.json
// RUN: echo "{" >> %/t/map.json
// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/map.json
// RUN: echo "\"clangModulePath\": \"SwiftShims.pcm\"," >> %/t/map.json
// RUN: echo -n "\"clangModuleCacheKey\": " >> %/t/map.json
// RUN: cat %t/Shims.key >> %/t/map.json
// RUN: echo "," >> %/t/map.json
// RUN: echo "\"isFramework\": false" >> %/t/map.json
// RUN: echo "}]" >> %/t/map.json
// RUN: llvm-cas --cas %t/cas --make-blob --data %t/map.json > %t/map.casid

// RUN: %target-swift-frontend \
// RUN: -typecheck -verify -cache-compile-job -cas-path %t/cas \
// RUN: -swift-version 5 -disable-implicit-swift-modules \
// RUN: -external-plugin-path %t/plugins/#%swift-plugin-server \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
// RUN: -module-name MyApp -explicit-swift-module-map-file @%t/map.casid \
// RUN: %s @%t/MyApp.cmd

@attached(extension, conformances: P, names: named(requirement))
macro DelegatedConformance() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceViaExtensionMacro")

protocol P {
static func requirement()
}

struct Wrapped: P {
static func requirement() {
print("Wrapped.requirement")
}
}

@DelegatedConformance
struct Generic<Element> {}

// CHECK: {"expandMacroResult":{"diagnostics":[],"expandedSource":"extension Generic: P where Element: P {\n static func requirement() {\n Element.requirement()\n }\n}"}}

func requiresP(_ value: (some P).Type) {
value.requirement()
}

requiresP(Generic<Wrapped>.self)

struct Outer {
@DelegatedConformance
struct Nested<Element> {}
}

// CHECK: {"expandMacroResult":{"diagnostics":[],"expandedSource":"extension Outer.Nested: P where Element: P {\n static func requirement() {\n Element.requirement()\n }\n}"}}

requiresP(Outer.Nested<Wrapped>.self)

0 comments on commit 4433359

Please sign in to comment.