-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathmacro_plugin_searchorder.swift
126 lines (107 loc) · 4.35 KB
/
macro_plugin_searchorder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// REQUIRES: swift_swift_parser, executable_test
// RUN: %empty-directory(%t)
// RUN: mkdir -p %t/src
// RUN: mkdir -p %t/bin
// RUN: mkdir -p %t/lib/tmp
// RUN: mkdir -p %t/lib/plugins
// RUN: mkdir -p %t/external
// RUN: mkdir -p %t/libexec
// RUN: split-file %s %t/src
//#-- For -plugin-path
// RUN: %host-build-swift \
// RUN: -swift-version 5 \
// RUN: -emit-library -o %t/lib/plugins/%target-library-name(MacroDefinition) \
// RUN: -module-name MacroDefinition \
// RUN: -D PLUGIN_PATH \
// RUN: %t/src/MacroDefinition.swift
//#-- For -load-plugin-library
// RUN: %host-build-swift \
// RUN: -swift-version 5 \
// RUN: -emit-library -o %t/lib/tmp/%target-library-name(MacroDefinition) \
// RUN: -module-name MacroDefinition \
// RUN: -D LOAD_PLUGIN_LIBRARY \
// RUN: %t/src/MacroDefinition.swift
//#-- For -external-plugin-path
// RUN: %host-build-swift \
// RUN: -swift-version 5 \
// RUN: -emit-library -o %t/external/%target-library-name(MacroDefinition) \
// RUN: -module-name MacroDefinition \
// RUN: -D EXTERNAL_PLUGIN_PATH \
// RUN: %t/src/MacroDefinition.swift
//#-- For -load-plugin-executable
// RUN: %swift-build-c-plugin -o %t/libexec/MacroDefinitionPlugin %t/src/MacroDefinition.c
//#-- Expect -load-plugin-library
// RUN: %target-build-swift %t/src/test.swift \
// RUN: -module-name test \
// RUN: -load-plugin-library %t/lib/tmp/%target-library-name(MacroDefinition) \
// RUN: -plugin-path %t/lib/plugins \
// RUN: -external-plugin-path %t/external#%swift-plugin-server \
// RUN: -load-plugin-executable %t/libexec/MacroDefinitionPlugin#MacroDefinition \
// RUN: -o %t/main1
// RUN: %target-codesign %t/main1
// RUN: %target-run %t/main1 | %FileCheck --check-prefix=CHECK_LOAD_PLUGIN_LIBRARY %s
//#-- Expect -load-plugin-executable
// RUN: %target-build-swift %t/src/test.swift \
// RUN: -module-name test \
// RUN: -load-plugin-executable %t/libexec/MacroDefinitionPlugin#MacroDefinition \
// RUN: -plugin-path %t/lib/plugins \
// RUN: -external-plugin-path %t/external#%swift-plugin-server \
// RUN: -o %t/main2
// RUN: %target-codesign %t/main2
// RUN: %target-run %t/main2 | %FileCheck --check-prefix=CHECK_LOAD_PLUGIN_EXECUTABLE %s
//#-- Expect -plugin-path
// RUN: %target-build-swift %t/src/test.swift \
// RUN: -module-name test \
// RUN: -plugin-path %t/lib/plugins \
// RUN: -load-plugin-library %t/lib/tmp/%target-library-name(MacroDefinition) \
// RUN: -external-plugin-path %t/external#%swift-plugin-server \
// RUN: -o %t/main3
// RUN: %target-codesign %t/main3
// RUN: %target-run %t/main3 | %FileCheck --check-prefix=CHECK_PLUGIN_PATH %s
//#-- Expect -external-plugin-path
// RUN: %target-build-swift %t/src/test.swift \
// RUN: -module-name test \
// RUN: -external-plugin-path %t/external#%swift-plugin-server \
// RUN: -plugin-path %t/lib/plugins \
// RUN: -load-plugin-executable %t/libexec/MacroDefinitionPlugin#MacroDefinition \
// RUN: -o %t/main4
// RUN: %target-codesign %t/main4
// RUN: %target-run %t/main4 | %FileCheck --check-prefix=CHECK_EXTERNAL_PLUGIN_PATH %s
// CHECK_LOAD_PLUGIN_LIBRARY: load-plugin-library
// CHECK_LOAD_PLUGIN_EXECUTABLE: load-plugin-executable
// CHECK_PLUGIN_PATH: plugin-path
// CHECK_EXTERNAL_PLUGIN_PATH: external-plugin-path
//--- test.swift
@freestanding(expression) macro testMacro() -> String = #externalMacro(module: "MacroDefinition", type: "TestMacro")
print(#testMacro)
//--- MacroDefinition.swift
import SwiftSyntax
import SwiftSyntaxMacros
public struct TestMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
#if PLUGIN_PATH
return #""plugin-path""#
#elseif LOAD_PLUGIN_LIBRARY
return #""load-plugin-library""#
#elseif EXTERNAL_PLUGIN_PATH
return #""external-plugin-path""#
#endif
}
}
//--- MacroDefinition.c
#include "swift-c/MockPlugin/MockPlugin.h"
MOCK_PLUGIN([
{
"expect": {"getCapability": {}},
"response": {"getCapabilityResult": {"capability": {"protocolVersion": 1}}}
},
{
"expect": {"expandFreestandingMacro": {
"macro": {"moduleName": "MacroDefinition", "typeName": "TestMacro"},
"syntax": {"kind": "expression", "source": "#testMacro"}}},
"response": {"expandFreestandingMacroResult": {"expandedSource": "\"load-plugin-executable\"", "diagnostics": []}}
}
])