Skip to content

Commit c9d8622

Browse files
authored
Merge pull request #70307 from rintaro/5.10-macros-swiftdeps-serverdylib-rdar119324830
[5.10][Macros] Track macro dylib path dependencies when using plugin server
2 parents 3a7676c + d0ae14c commit c9d8622

File tree

4 files changed

+79
-15
lines changed

4 files changed

+79
-15
lines changed

include/swift/AST/PluginLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class PluginLoader {
8686
/// NOTE: This method is idempotent. If the plugin is already loaded, the same
8787
/// instance is simply returned.
8888
LoadedExecutablePlugin *loadExecutablePlugin(llvm::StringRef path);
89+
90+
/// Add the specified path to the dependency tracker if needed.
91+
void recordDependency(StringRef path);
8992
};
9093

9194
} // namespace swift

lib/AST/PluginLoader.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ LoadedLibraryPlugin *PluginLoader::loadLibraryPlugin(StringRef path) {
168168
}
169169

170170
// Track the dependency.
171-
if (DepTracker)
172-
DepTracker->addDependency(resolvedPath, /*IsSystem=*/false);
171+
recordDependency(path);
173172

174173
// Load the plugin.
175174
auto plugin = getRegistry()->loadLibraryPlugin(resolvedPath);
@@ -192,8 +191,7 @@ LoadedExecutablePlugin *PluginLoader::loadExecutablePlugin(StringRef path) {
192191
}
193192

194193
// Track the dependency.
195-
if (DepTracker)
196-
DepTracker->addDependency(resolvedPath, /*IsSystem=*/false);
194+
recordDependency(path);
197195

198196
// Load the plugin.
199197
auto plugin =
@@ -206,3 +204,8 @@ LoadedExecutablePlugin *PluginLoader::loadExecutablePlugin(StringRef path) {
206204

207205
return plugin.get();
208206
}
207+
208+
void PluginLoader::recordDependency(StringRef path) {
209+
if (DepTracker)
210+
DepTracker->addDependency(path, /*IsSystem=*/false);
211+
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ initializeExecutablePlugin(ASTContext &ctx,
325325
executablePlugin->getExecutablePath(), err.message());
326326
return nullptr;
327327
}
328+
329+
ctx.getPluginLoader().recordDependency(resolvedLibraryPath);
330+
328331
std::string resolvedLibraryPathStr(resolvedLibraryPath);
329332
std::string moduleNameStr(moduleName.str());
330333

test/Macros/macro_swiftdeps.swift

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22

33
// RUN: %empty-directory(%t)
44
// RUN: %empty-directory(%t/plugin)
5+
// RUN: %empty-directory(%t/external-plugin)
56
// RUN: %empty-directory(%t/lib)
67
// RUN: %empty-directory(%t/src)
78

89
// RUN: split-file %s %t/src
910

10-
//#-- Prepare the macro shared library plugin.
11+
//#-- Prepare the macro shared library plugin for -plugin-path.
1112
// RUN: %host-build-swift \
1213
// RUN: -swift-version 5 \
13-
// RUN: -emit-library -o %t/plugin/%target-library-name(MacroDefinition) \
14-
// RUN: -module-name MacroDefinition \
15-
// RUN: %S/Inputs/syntax_macro_definitions.swift
14+
// RUN: -emit-library -o %t/plugin/%target-library-name(StringifyPlugin) \
15+
// RUN: -module-name StringifyPlugin \
16+
// RUN: %t/src/StringifyPlugin.swift
17+
18+
//#-- Prepare the macro shared library plugin for -external-plugin-path.
19+
// RUN: %host-build-swift \
20+
// RUN: -swift-version 5 \
21+
// RUN: -emit-library -o %t/external-plugin/%target-library-name(AssertPlugin) \
22+
// RUN: -module-name AssertPlugin \
23+
// RUN: %t/src/AssertPlugin.swift
1624

1725
//#-- Prepare the macro executable plugin.
1826
// RUN: %swift-build-cxx-plugin -o %t/mock-plugin %t/src/plugin.c
@@ -23,6 +31,7 @@
2331
// RUN: -emit-module -o %t/lib/MacroLib.swiftmodule \
2432
// RUN: -module-name MacroLib \
2533
// RUN: -plugin-path %t/plugin \
34+
// RUN: -external-plugin-path %t/external-plugin#%swift-plugin-server \
2635
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
2736
// RUN: -primary-file %t/src/macro_library.swift \
2837
// RUN: -emit-reference-dependencies-path %t/macro_library.swiftdeps \
@@ -35,7 +44,9 @@
3544
// RUN: -swift-version 5 -typecheck \
3645
// RUN: -primary-file %t/src/test.swift \
3746
// RUN: %t/src/other.swift \
38-
// RUN: -I %t/lib -plugin-path %t/plugin \
47+
// RUN: -I %t/lib \
48+
// RUN: -plugin-path %t/plugin \
49+
// RUN: -external-plugin-path %t/external-plugin#%swift-plugin-server \
3950
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
4051
// RUN: -emit-reference-dependencies-path %t/without_macro.swiftdeps \
4152
// RUN: -emit-dependencies-path %t/without_macro.d
@@ -48,7 +59,9 @@
4859
// RUN: -swift-version 5 -typecheck \
4960
// RUN: -primary-file %t/src/test.swift \
5061
// RUN: %t/src/other.swift \
51-
// RUN: -I %t/lib -plugin-path %t/plugin \
62+
// RUN: -I %t/lib \
63+
// RUN: -plugin-path %t/plugin \
64+
// RUN: -external-plugin-path %t/external-plugin#%swift-plugin-server \
5265
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
5366
// RUN: -emit-reference-dependencies-path %t/with_macro_primary.swiftdeps \
5467
// RUN: -emit-dependencies-path %t/with_macro_primary.d
@@ -61,21 +74,26 @@
6174
// RUN: -swift-version 5 -typecheck \
6275
// RUN: %t/src/test.swift \
6376
// RUN: -primary-file %t/src/other.swift \
64-
// RUN: -I %t/lib -plugin-path %t/plugin \
77+
// RUN: -I %t/lib \
78+
// RUN: -plugin-path %t/plugin \
79+
// RUN: -external-plugin-path %t/external-plugin#%swift-plugin-server \
6580
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
6681
// RUN: -emit-reference-dependencies-path %t/with_macro_nonprimary.swiftdeps \
6782
// RUN: -emit-dependencies-path %t/with_macro_nonprimary.d
6883
// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t/with_macro_nonprimary.swiftdeps > %t/with_macro_nonprimary.swiftdeps.processed
6984
// RUN: %FileCheck --check-prefix WITHOUT_PLUGIN %s < %t/with_macro_nonprimary.swiftdeps.processed
7085

71-
// WITH_PLUGIN: externalDepend interface '' '{{.*}}mock-plugin' false
72-
// WITH_PLUGIN: externalDepend interface '' '{{.*}}MacroDefinition.{{(dylib|so|dll)}}' false
86+
// WITH_PLUGIN-DAG: externalDepend interface '' '{{.*}}mock-plugin' false
87+
// WITH_PLUGIN-DAG: externalDepend interface '' '{{.*}}StringifyPlugin.{{(dylib|so|dll)}}' false
88+
// WITH_PLUGIN-DAG: externalDepend interface '' '{{.*}}AssertPlugin.{{(dylib|so|dll)}}' false
7389

74-
// WITHOUT_PLUGIN-NOT: MacroDefinition
90+
// WITHOUT_PLUGIN-NOT: StringifyPlugin
91+
// WITHOUT_PLUGIN-NOT: AssertPlugin
7592
// WITHOUT_PLUGIN-NOT: mock-plugin
7693

7794
//--- macro_library.swift
78-
@freestanding(expression) public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
95+
@freestanding(expression) public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "StringifyPlugin", type: "StringifyMacro")
96+
@freestanding(expression) public macro assert(_ value: Bool) = #externalMacro(module: "AssertPlugin", type: "AssertMacro")
7997
@freestanding(expression) public macro testString(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro")
8098

8199
public func funcInMacroLib() {}
@@ -90,6 +108,7 @@ func test(a: Int, b: Int) {
90108
#if USE_MACRO
91109
_ = #stringify(a + b)
92110
_ = #testString(123)
111+
#assert(true)
93112
#endif
94113
}
95114

@@ -101,6 +120,42 @@ func test() {
101120
funcInMacroLib()
102121
}
103122

123+
//--- StringifyPlugin.swift
124+
import SwiftSyntax
125+
import SwiftSyntaxBuilder
126+
import SwiftSyntaxMacros
127+
128+
public struct StringifyMacro: ExpressionMacro {
129+
public static func expansion(
130+
of macro: some FreestandingMacroExpansionSyntax,
131+
in context: some MacroExpansionContext
132+
) -> ExprSyntax {
133+
guard let argument = macro.arguments.first?.expression else {
134+
fatalError("boom")
135+
}
136+
137+
return "(\(argument), \(StringLiteralExprSyntax(content: argument.description)))"
138+
}
139+
}
140+
141+
//--- AssertPlugin.swift
142+
import SwiftSyntax
143+
import SwiftSyntaxBuilder
144+
import SwiftSyntaxMacros
145+
146+
public struct AssertMacro: ExpressionMacro {
147+
public static func expansion(
148+
of macro: some FreestandingMacroExpansionSyntax,
149+
in context: some MacroExpansionContext
150+
) -> ExprSyntax {
151+
guard let argument = macro.arguments.first?.expression else {
152+
fatalError("boom")
153+
}
154+
155+
return "assert(\(argument))"
156+
}
157+
}
158+
104159
//--- plugin.c
105160
#include "swift-c/MockPlugin/MockPlugin.h"
106161

0 commit comments

Comments
 (0)