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.
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 \
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
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
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
8199public 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