-
Notifications
You must be signed in to change notification settings - Fork 382
/
MacroSystemTests.swift
521 lines (462 loc) · 14.4 KB
/
MacroSystemTests.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftDiagnostics
import SwiftParser
import SwiftSyntax
import SwiftSyntaxBuilder
import _SwiftSyntaxMacros
import _SwiftSyntaxTestSupport
import XCTest
// MARK: Example macros
public struct StringifyMacro: ExpressionMacro {
public static func expansion(
of macro: MacroExpansionExprSyntax,
in context: inout MacroExpansionContext
) -> ExprSyntax {
guard let argument = macro.argumentList.first?.expression else {
// FIXME: Create a diagnostic for the missing argument?
return ExprSyntax(macro)
}
return "(\(argument), \(StringLiteralExprSyntax(content: argument.description)))"
}
}
/// Replace the label of the first element in the tuple with the given
/// new label.
private func replaceFirstLabel(
of tuple: TupleExprElementListSyntax,
with newLabel: String
) -> TupleExprElementListSyntax {
guard let firstElement = tuple.first else {
return tuple
}
return tuple.replacing(
childAt: 0,
with: firstElement.withLabel(.identifier(newLabel))
)
}
public struct ColorLiteralMacro: ExpressionMacro {
public static func expansion(
of macro: MacroExpansionExprSyntax,
in context: inout MacroExpansionContext
) -> ExprSyntax {
let argList = replaceFirstLabel(
of: macro.argumentList,
with: "_colorLiteralRed"
)
let initSyntax: ExprSyntax = ".init(\(argList))"
if let leadingTrivia = macro.leadingTrivia {
return initSyntax.withLeadingTrivia(leadingTrivia)
}
return initSyntax
}
}
public struct FileLiteralMacro: ExpressionMacro {
public static func expansion(
of macro: MacroExpansionExprSyntax,
in context: inout MacroExpansionContext
) -> ExprSyntax {
let argList = replaceFirstLabel(
of: macro.argumentList,
with: "fileReferenceLiteralResourceName"
)
let initSyntax: ExprSyntax = ".init(\(argList))"
if let leadingTrivia = macro.leadingTrivia {
return initSyntax.withLeadingTrivia(leadingTrivia)
}
return initSyntax
}
}
public struct ImageLiteralMacro: ExpressionMacro {
public static func expansion(
of macro: MacroExpansionExprSyntax,
in context: inout MacroExpansionContext
) -> ExprSyntax {
let argList = replaceFirstLabel(
of: macro.argumentList,
with: "imageLiteralResourceName"
)
let initSyntax: ExprSyntax = ".init(\(argList))"
if let leadingTrivia = macro.leadingTrivia {
return initSyntax.withLeadingTrivia(leadingTrivia)
}
return initSyntax
}
}
public struct FileIDMacro: ExpressionMacro {
public static func expansion(
of macro: MacroExpansionExprSyntax,
in context: inout MacroExpansionContext
) -> ExprSyntax {
// FIXME: Compiler has more sophisticated file ID computation
let fileID = "\(context.moduleName)/\(context.fileName)"
let fileLiteral: ExprSyntax = "\(literal: fileID)"
if let leadingTrivia = macro.leadingTrivia {
return fileLiteral.withLeadingTrivia(leadingTrivia)
}
return fileLiteral
}
}
/// Macro whose only purpose is to ensure that we cannot see "out" of the
/// macro expansion syntax node we were given.
struct CheckContextIndependenceMacro: ExpressionMacro {
static func expansion(
of macro: MacroExpansionExprSyntax,
in context: inout MacroExpansionContext
) -> ExprSyntax {
// Should not have a parent.
XCTAssertNil(macro.parent)
// Absolute starting position should be zero.
XCTAssertEqual(macro.position.utf8Offset, 0)
return ExprSyntax(macro)
}
}
enum CustomError: Error, CustomStringConvertible {
case message(String)
var description: String {
switch self {
case .message(let text):
return text
}
}
}
struct SimpleDiagnosticMessage: DiagnosticMessage {
let message: String
let diagnosticID: MessageID
let severity: DiagnosticSeverity
}
extension SimpleDiagnosticMessage: FixItMessage {
var fixItID: MessageID { diagnosticID }
}
public struct ErrorMacro: FreestandingDeclarationMacro {
public static func expansion(
of node: MacroExpansionDeclSyntax,
in context: inout MacroExpansionContext
) throws -> [DeclSyntax] {
guard let firstElement = node.argumentList.first,
let stringLiteral = firstElement.expression
.as(StringLiteralExprSyntax.self),
stringLiteral.segments.count == 1,
case let .stringSegment(messageString) = stringLiteral.segments[0]
else {
throw CustomError.message("#error macro requires a string literal")
}
context.diagnose(
Diagnostic(
node: Syntax(node),
message: SimpleDiagnosticMessage(
message: messageString.content.description,
diagnosticID: MessageID(domain: "test", id: "error"),
severity: .error
)
)
)
return []
}
}
struct DefineBitwidthNumberedStructsMacro: FreestandingDeclarationMacro {
static func expansion(
of node: MacroExpansionDeclSyntax,
in context: inout MacroExpansionContext
) throws -> [DeclSyntax] {
guard let firstElement = node.argumentList.first,
let stringLiteral = firstElement.expression
.as(StringLiteralExprSyntax.self),
stringLiteral.segments.count == 1,
case let .stringSegment(prefix) = stringLiteral.segments[0]
else {
throw CustomError.message(
"#bitwidthNumberedStructs macro requires a string literal"
)
}
return [8, 16, 32, 64].map { bitwidth in
"""
struct \(raw: prefix)\(raw: String(bitwidth)) { }
"""
}
}
}
public struct AddCompletionHandler: PeerDeclarationMacro {
public static func expansion(
of node: CustomAttributeSyntax,
attachedTo declaration: DeclSyntax,
in context: inout MacroExpansionContext
) throws -> [DeclSyntax] {
// Only on functions at the moment. We could handle initializers as well
// with a bit of work.
guard let funcDecl = declaration.as(FunctionDeclSyntax.self) else {
throw CustomError.message("@addCompletionHandler only works on functions")
}
// This only makes sense for async functions.
if funcDecl.signature.asyncOrReasyncKeyword == nil {
throw CustomError.message(
"@addCompletionHandler requires an async function"
)
}
// Form the completion handler parameter.
let resultType: TypeSyntax? = funcDecl.signature.output?.returnType.withoutTrivia()
let completionHandlerParam =
FunctionParameterSyntax(
firstName: .identifier("completionHandler"),
colon: .colonToken(trailingTrivia: .space),
type: "(\(resultType ?? "")) -> Void" as TypeSyntax
)
// Add the completion handler parameter to the parameter list.
let parameterList = funcDecl.signature.input.parameterList
let newParameterList: FunctionParameterListSyntax
if let lastParam = parameterList.last {
// We need to add a trailing comma to the preceding list.
newParameterList = parameterList.removingLast()
.appending(
lastParam.withTrailingComma(
.commaToken(trailingTrivia: .space)
)
)
.appending(completionHandlerParam)
} else {
newParameterList = parameterList.appending(completionHandlerParam)
}
let callArguments: [String] = try parameterList.map { param in
guard let argName = param.secondName ?? param.firstName else {
throw CustomError.message(
"@addCompletionHandler argument must have a name"
)
}
if let paramName = param.firstName, paramName.text != "_" {
return "\(paramName.withoutTrivia()): \(argName.withoutTrivia())"
}
return "\(argName.withoutTrivia())"
}
let call: ExprSyntax =
"\(funcDecl.identifier)(\(raw: callArguments.joined(separator: ", ")))"
// FIXME: We should make CodeBlockSyntax ExpressibleByStringInterpolation,
// so that the full body could go here.
let newBody: ExprSyntax =
"""
Task {
completionHandler(await \(call))
}
"""
// Drop the @addCompletionHandler attribute from the new declaration.
let newAttributeList = AttributeListSyntax(
funcDecl.attributes?.filter {
guard case let .customAttribute(customAttr) = $0 else {
return true
}
return customAttr != node
} ?? []
)
let newFunc =
funcDecl
.withSignature(
funcDecl.signature
.withAsyncOrReasyncKeyword(nil) // drop async
.withOutput(nil) // drop result type
.withInput( // add completion handler parameter
funcDecl.signature.input.withParameterList(newParameterList)
.withoutTrailingTrivia()
)
)
.withBody(
CodeBlockSyntax(
leftBrace: .leftBraceToken(leadingTrivia: .space),
statements: CodeBlockItemListSyntax(
[CodeBlockItemSyntax(item: .expr(newBody))]
),
rightBrace: .rightBraceToken(leadingTrivia: .newline)
)
)
.withAttributes(newAttributeList)
.withLeadingTrivia(.newlines(2))
return [DeclSyntax(newFunc)]
}
}
// MARK: Assertion helper functions
/// Assert that expanding the given macros in the original source produces
/// the given expanded source code.
///
/// - Parameters:
/// - macros: The macros that should be expanded, provided as a dictionary
/// mapping macro names (e.g., `"stringify"`) to implementation types
/// (e.g., `StringifyMacro.self`).
/// - testModuleName: The name of the test module to use.
/// - testFileName: The name of the test file name to use.
/// - originalSource: The original source code, which is expected to contain
/// macros in various places (e.g., `#stringify(x + y)`).
/// - expandedSource: The source code that we expect to see after performing
/// macro expansion on the original source.
public func AssertMacroExpansion(
macros: [String: Macro.Type],
testModuleName: String = "TestModule",
testFileName: String = "test.swift",
_ originalSource: String,
_ expandedSource: String,
diagnosticStrings: [String] = [],
file: StaticString = #file,
line: UInt = #line
) {
// Parse the original source file.
let origSourceFile = Parser.parse(source: originalSource)
// Expand all macros in the source.
var context = MacroExpansionContext(
moduleName: testModuleName,
fileName: testFileName
)
let expandedSourceFile = origSourceFile.expand(macros: macros, in: &context)
AssertStringsEqualWithDiff(
expandedSourceFile.description,
expandedSource,
file: file,
line: line
)
let diags = context.diagnostics
XCTAssertEqual(diags.count, diagnosticStrings.count)
for (actualDiag, expectedDiag) in zip(diags, diagnosticStrings) {
let actualMessage = actualDiag.message
XCTAssertEqual(actualMessage, expectedDiag)
}
}
// MARK: Tests
/// The set of test macros we use here.
public let testMacros: [String: Macro.Type] = [
"checkContext": CheckContextIndependenceMacro.self,
"colorLiteral": ColorLiteralMacro.self,
"fileID": FileIDMacro.self,
"imageLiteral": ImageLiteralMacro.self,
"stringify": StringifyMacro.self,
"myError": ErrorMacro.self,
"bitwidthNumberedStructs": DefineBitwidthNumberedStructsMacro.self,
"addCompletionHandler": AddCompletionHandler.self,
]
final class MacroSystemTests: XCTestCase {
func testExpressionExpansion() {
AssertMacroExpansion(
macros: testMacros,
"""
let b = #stringify(x + y)
#colorLiteral(red: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
""",
"""
let b = (x + y, "x + y")
.init(_colorLiteralRed: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
"""
)
}
func testStringifyExpression() {
AssertMacroExpansion(
macros: ["stringify": StringifyMacro.self],
"""
_ = #stringify({ () -> Bool in
print("hello")
return true
})
""",
"""
_ = ({ () -> Bool in
print("hello")
return true
}, #"{ () -> Bool in\\#n print("hello")\\#n return true\\#n}"#)
"""
)
}
func testFileExpansions() {
AssertMacroExpansion(
macros: testMacros,
testModuleName: "MyModule",
testFileName: "taylor.swift",
"""
let b = #fileID
""",
"""
let b = "MyModule/taylor.swift"
"""
)
}
func testContextUniqueLocalNames() {
var context = MacroExpansionContext(
moduleName: "MyModule",
fileName: "taylor.swift"
)
let t1 = context.createUniqueLocalName()
let t2 = context.createUniqueLocalName()
XCTAssertNotEqual(t1.description, t2.description)
XCTAssertEqual(t1.description, "__macro_local_0")
}
func testContextIndependence() {
AssertMacroExpansion(
macros: ["checkContext": CheckContextIndependenceMacro.self],
"""
let b = #checkContext
""",
"""
let b = #checkContext
"""
)
}
func testErrorExpansion() {
AssertMacroExpansion(
macros: testMacros,
"""
#myError("please don't do that")
struct X {
func f() { }
#myError(bad)
func g() {
#myError("worse")
}
}
""",
"""
struct X {
func f() { }
func g() {
}
}
""",
diagnosticStrings: [
"please don't do that",
"#error macro requires a string literal",
"worse",
]
)
}
func testBitwidthNumberedStructsExpansion() {
AssertMacroExpansion(
macros: testMacros,
"""
#bitwidthNumberedStructs("MyInt")
""",
"""
struct MyInt8 { }
struct MyInt16 { }
struct MyInt32 { }
struct MyInt64 { }
"""
)
}
func testAddCompletionHandler() {
AssertMacroExpansion(
macros: testMacros,
"""
@addCompletionHandler
func f(a: Int, for b: String, _ value: Double) async -> String { }
""",
"""
func f(a: Int, for b: String, _ value: Double) async -> String { }
func f(a: Int, for b: String, _ value: Double, completionHandler: (String) -> Void) {
Task {
completionHandler(await f(a: a, for: b, value))
}
}
"""
)
}
}