Skip to content

Commit 452c624

Browse files
authored
Merge pull request #70305 from rintaro/macros-swiftdeps-serverdylib-rdar119324830
2 parents 81d19df + de14ca7 commit 452c624

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

include/swift/AST/PluginLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class PluginLoader {
8787
/// instance is simply returned.
8888
llvm::Expected<LoadedExecutablePlugin *>
8989
loadExecutablePlugin(llvm::StringRef path);
90+
91+
/// Add the specified path to the dependency tracker if needed.
92+
void recordDependency(StringRef path);
9093
};
9194

9295
} // namespace swift

lib/AST/PluginLoader.cpp

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

169169
// Track the dependency.
170-
if (DepTracker)
171-
DepTracker->addDependency(resolvedPath, /*IsSystem=*/false);
170+
recordDependency(path);
172171

173172
// Load the plugin.
174173
auto plugin = getRegistry()->loadLibraryPlugin(resolvedPath);
@@ -195,8 +194,7 @@ PluginLoader::loadExecutablePlugin(StringRef path) {
195194
}
196195

197196
// Track the dependency.
198-
if (DepTracker)
199-
DepTracker->addDependency(resolvedPath, /*IsSystem=*/false);
197+
recordDependency(path);
200198

201199
// Load the plugin.
202200
auto plugin =
@@ -214,3 +212,8 @@ PluginLoader::loadExecutablePlugin(StringRef path) {
214212

215213
return plugin;
216214
}
215+
216+
void PluginLoader::recordDependency(StringRef path) {
217+
if (DepTracker)
218+
DepTracker->addDependency(path, /*IsSystem=*/false);
219+
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ initializeExecutablePlugin(ASTContext &ctx,
278278
if (auto err = fs->getRealPath(libraryPath, resolvedLibraryPath)) {
279279
return llvm::createStringError(err, err.message());
280280
}
281+
282+
ctx.getPluginLoader().recordDependency(resolvedLibraryPath);
283+
281284
std::string resolvedLibraryPathStr(resolvedLibraryPath);
282285
std::string moduleNameStr(moduleName.str());
283286

test/Macros/macro_swiftdeps.swift

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
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
1617

17-
//#-- Prepare the macro executable plugin.
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
24+
25+
//#-- Prepare the macro executable plugin for -load-plugin-executable.
1826
// RUN: %swift-build-c-plugin -o %t/mock-plugin %t/src/plugin.c
1927

2028
//#-- Prepare the macro library.
@@ -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)