forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro_swiftdeps.swift
125 lines (106 loc) · 4.84 KB
/
macro_swiftdeps.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
// REQUIRES: swift_swift_parser
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/plugin)
// RUN: %empty-directory(%t/lib)
// RUN: %empty-directory(%t/src)
// RUN: split-file %s %t/src
//#-- Prepare the macro dylib plugin.
// RUN: %host-build-swift \
// RUN: -swift-version 5 \
// RUN: -emit-library -o %t/plugin/%target-library-name(MacroDefinition) \
// RUN: -module-name MacroDefinition \
// RUN: %S/Inputs/syntax_macro_definitions.swift \
// RUN: -g -no-toolchain-stdlib-rpath
//#-- Prepare the macro executable plugin.
// RUN: %clang \
// RUN: -isysroot %host_sdk \
// RUN: -I %swift_src_root/include \
// RUN: -L %swift-lib-dir -l_swiftMockPlugin \
// RUN: -Wl,-rpath,%swift-lib-dir \
// RUN: -o %t/mock-plugin \
// RUN: %t/src/plugin.c
//#-- Prepare the macro library.
// RUN: %target-swift-frontend \
// RUN: -swift-version 5 \
// RUN: -emit-module -o %t/lib/MacroLib.swiftmodule \
// RUN: -module-name MacroLib \
// RUN: -plugin-path %t/plugin \
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
// RUN: -primary-file %t/src/macro_library.swift \
// RUN: -emit-reference-dependencies-path %t/macro_library.swiftdeps \
// RUN: -emit-dependencies-path %t/macro_library.d
// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/macro_library.swiftdeps %t/macro_library.swiftdeps.processed
// RUN: %FileCheck --check-prefix WITH_PLUGIN %s < %t/macro_library.swiftdeps.processed
//#-- Without macro (no -D USE_MACRO)
// RUN: %target-swift-frontend \
// RUN: -swift-version 5 -typecheck \
// RUN: -primary-file %t/src/test.swift \
// RUN: %t/src/other.swift \
// RUN: -I %t/lib -plugin-path %t/plugin \
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
// RUN: -emit-reference-dependencies-path %t/without_macro.swiftdeps \
// RUN: -emit-dependencies-path %t/without_macro.d
// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/without_macro.swiftdeps %t/without_macro.swiftdeps.processed
// RUN: %FileCheck --check-prefix WITHOUT_PLUGIN %s < %t/without_macro.swiftdeps.processed
//#-- With macro - primary (-D USE_MACRO)
// RUN: %target-swift-frontend \
// RUN: -D USE_MACRO \
// RUN: -swift-version 5 -typecheck \
// RUN: -primary-file %t/src/test.swift \
// RUN: %t/src/other.swift \
// RUN: -I %t/lib -plugin-path %t/plugin \
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
// RUN: -emit-reference-dependencies-path %t/with_macro_primary.swiftdeps \
// RUN: -emit-dependencies-path %t/with_macro_primary.d
// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/with_macro_primary.swiftdeps %t/with_macro_primary.swiftdeps.processed
// RUN: %FileCheck --check-prefix WITH_PLUGIN %s < %t/with_macro_primary.swiftdeps.processed
//#-- With macro - non-primary (-D USE_MACRO)
// RUN: %target-swift-frontend \
// RUN: -D USE_MACRO \
// RUN: -swift-version 5 -typecheck \
// RUN: %t/src/test.swift \
// RUN: -primary-file %t/src/other.swift \
// RUN: -I %t/lib -plugin-path %t/plugin \
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
// RUN: -emit-reference-dependencies-path %t/with_macro_nonprimary.swiftdeps \
// RUN: -emit-dependencies-path %t/with_macro_nonprimary.d
// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/with_macro_nonprimary.swiftdeps %t/with_macro_nonprimary.swiftdeps.processed
// RUN: %FileCheck --check-prefix WITHOUT_PLUGIN %s < %t/with_macro_nonprimary.swiftdeps.processed
// WITH_PLUGIN: externalDepend interface '' 'BUILD_DIR{{.*}}mock-plugin' false
// WITH_PLUGIN: externalDepend interface '' 'BUILD_DIR{{.*}}libMacroDefinition.dylib' false
// WITHOUT_PLUGIN-NOT: MacroDefinition
// WITHOUT_PLUGIN-NOT: mock-plugin
//--- macro_library.swift
@freestanding(expression) public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
@freestanding(expression) public macro testString(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro")
public func funcInMacroLib() {}
//--- test.swift
import MacroLib
func test(a: Int, b: Int) {
// Just using MacroLib without macro
funcInMacroLib()
#if USE_MACRO
_ = #stringify(a + b)
_ = #testString(123)
#endif
}
//--- other.swift
import MacroLib
func test() {
// Just using MacroLib without macro
funcInMacroLib()
}
//--- plugin.c
#include "swift-c/MockPlugin/MockPlugin.h"
MOCK_PLUGIN([
{
"expect": {"getCapability": {}},
"response": {"getCapabilityResult": {"capability": {"protocolVersion": 1}}}
},
{
"expect": {"expandFreestandingMacro": {
"macro": {"moduleName": "TestPlugin", "typeName": "TestStringMacro"},
"syntax": {"kind": "expression", "source": "#testString(123)"}}},
"response": {"expandFreestandingMacroResult": {"expandedSource": "\"test\"", "diagnostics": []}}
}
])