@@ -67,6 +67,60 @@ struct S5 {
67
67
var test : Int = 10
68
68
}
69
69
70
+ @attached ( preamble)
71
+ macro Traced( ) = #externalMacro( module: " MacroDefinition " , type: " TracedPreambleMacro " )
72
+
73
+ @attached ( preamble, names: named ( logger) )
74
+ macro Logged( ) = #externalMacro( module: " MacroDefinition " , type: " LoggerMacro " )
75
+
76
+ @Traced
77
+ @Logged
78
+ func doubleTheValue( value: Int ) -> Int {
79
+ return value * 2
80
+ }
81
+
82
+ @attached ( body)
83
+ macro Remote( ) = #externalMacro( module: " MacroDefinition " , type: " RemoteBodyMacro " )
84
+
85
+ @available ( SwiftStdlib 5 . 1 , * )
86
+ @Remote
87
+ func f( a: Int , b: String ) async throws -> String
88
+
89
+ protocol ConjureRemoteValue {
90
+ static func conjureValue( ) -> Self
91
+ }
92
+
93
+ extension String : ConjureRemoteValue {
94
+ static func conjureValue( ) -> String { " " }
95
+ }
96
+
97
+ struct Logger {
98
+ func log( entering function: String ) {
99
+ print ( " Logger entering \( function) " )
100
+ }
101
+
102
+ func log( _ message: String ) {
103
+ print ( " --- \( message) " )
104
+ }
105
+
106
+ func log( exiting function: String ) {
107
+ print ( " Logger exiting \( function) " )
108
+ }
109
+ }
110
+
111
+ func log( _ message: String ) {
112
+ print ( message)
113
+ }
114
+
115
+ @available ( SwiftStdlib 5 . 1 , * )
116
+ func remoteCall< Result: ConjureRemoteValue > ( function: String , arguments: [ String : Any ] ) async throws -> Result {
117
+ let printedArgs = arguments. keys. sorted ( ) . map { key in
118
+ " \( key) : \( arguments [ key] !) "
119
+ } . joined ( separator: " , " )
120
+ print ( " Remote call \( function) ( \( printedArgs) ) " )
121
+ return Result . conjureValue ( )
122
+ }
123
+
70
124
// REQUIRES: swift_swift_parser, executable_test, shell
71
125
72
126
// RUN: %empty-directory(%t)
@@ -78,6 +132,7 @@ struct S5 {
78
132
// RUN: -swift-version 5 \
79
133
// RUN: -load-plugin-library %t/%target-library-name(MacroDefinition) \
80
134
// RUN: -module-name MacroUser \
135
+ // RUN: -enable-experimental-feature BodyMacros \
81
136
// RUN: )
82
137
83
138
// RUN: COMPILER_ARGS=( \
@@ -297,3 +352,29 @@ struct S5 {
297
352
//##-- Expansion on the peer macro attached to pattern binding decl
298
353
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=66:4 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=EXPAND_PEER_ON_VAR %s
299
354
// EXPAND_PEER_ON_VAR: 67:21-67:21 (@__swiftmacro_9MacroUser2S5V4test21AddPeerStoredPropertyfMp_.swift) "public var _foo: Int = 100"
355
+
356
+ //##-- Expansion on a preamble macro.
357
+ // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=76:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=PREAMBLE_EXPAND %s
358
+ // PREAMBLE_EXPAND: source.edit.kind.active:
359
+ // PREAMBLE_EXPAND-NEXT: 78:40-78:40 (@__swiftmacro_9MacroUser14doubleTheValue6TracedfMq_.swift) "log("Entering doubleTheValue(value: \(value))")
360
+ // PREAMBLE_EXPAND: defer {
361
+ // PREAMBLE_EXPAND-NEXT: log("Exiting doubleTheValue(value:)")
362
+ // PREAMBLE_EXPAND-NEXT: }"
363
+ // PREAMBLE_EXPAND-NEXT: source.edit.kind.active
364
+
365
+ // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=77:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=PREAMBLE2_EXPAND %s
366
+ // PREAMBLE2_EXPAND: source.edit.kind.active:
367
+ // PREAMBLE2_EXPAND-NEXT: 78:40-78:40 (@__swiftmacro_9MacroUser14doubleTheValue6LoggedfMq_.swift) "let logger = Logger()
368
+ // PREAMBLE2_EXPAND-NEXT:logger.log(entering: "doubleTheValue(value: \(value))")
369
+ // PREAMBLE2_EXPAND-NEXT:defer {
370
+ // PREAMBLE2_EXPAND-NEXT: logger.log(exiting: "doubleTheValue(value:)")
371
+ // PREAMBLE2_EXPAND-NEXT:}"
372
+ // PREAMBLE2_EXPAND-NEXT:source.edit.kind.active:
373
+
374
+ //##-- Expansion on a body macro
375
+ // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=86:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=BODY_EXPAND %s
376
+ // BODY_EXPAND: source.edit.kind.active:
377
+ // BODY_EXPAND-NEXT: 87:49-87:49 (@__swiftmacro_9MacroUser1f6RemotefMb_.swift) "{
378
+ // BODY_EXPAND-NEXT: return try await remoteCall(function: "f", arguments: ["a": a, "b": b])
379
+ // BODY_EXPAND-NEXT: }"
380
+ // BODY_EXPAND-NEXT: source.edit.kind.active:
0 commit comments