diff --git a/test-compiler-plugin/ExamplePlugin/Package.swift b/test-compiler-plugin/ExamplePlugin/Package.swift new file mode 100644 index 0000000..4595259 --- /dev/null +++ b/test-compiler-plugin/ExamplePlugin/Package.swift @@ -0,0 +1,23 @@ +// swift-tools-version: 5.7 + +import PackageDescription + +let package = Package( + name: "ExamplePlugin", + platforms: [ + .macOS(.v10_15), + ], + dependencies: [ + .package(path: "../../../swift-syntax") + ], + targets: [ + .executableTarget( + name: "ExamplePlugin", + dependencies: [ + .product(name: "SwiftCompilerPlugin", package: "swift-syntax"), + .product(name: "SwiftSyntax", package: "swift-syntax"), + .product(name: "SwiftSyntaxMacros", package: "swift-syntax"), + .product(name: "SwiftDiagnostics", package: "swift-syntax"), + ]), + ] +) diff --git a/test-compiler-plugin/ExamplePlugin/Sources/ExamplePlugin/ExamplePlugin.swift b/test-compiler-plugin/ExamplePlugin/Sources/ExamplePlugin/ExamplePlugin.swift new file mode 100644 index 0000000..5547a7f --- /dev/null +++ b/test-compiler-plugin/ExamplePlugin/Sources/ExamplePlugin/ExamplePlugin.swift @@ -0,0 +1,10 @@ +import SwiftCompilerPlugin +import SwiftSyntaxMacros + +@main +struct ThePlugin: CompilerPlugin { + var providingMarcos: [Macro.Type] = [ + EchoExpressionMacro.self, + MetadataMacro.self + ] +} diff --git a/test-compiler-plugin/ExamplePlugin/Sources/ExamplePlugin/Macros.swift b/test-compiler-plugin/ExamplePlugin/Sources/ExamplePlugin/Macros.swift new file mode 100644 index 0000000..9ab9ce4 --- /dev/null +++ b/test-compiler-plugin/ExamplePlugin/Sources/ExamplePlugin/Macros.swift @@ -0,0 +1,37 @@ +import SwiftSyntax +import SwiftSyntaxBuilder +import SwiftSyntaxMacros + +struct EchoExpressionMacro: ExpressionMacro { + static func expansion< + Node: FreestandingMacroExpansionSyntax, + Context: MacroExpansionContext + >( + of node: Node, + in context: Context + ) throws -> ExprSyntax { + let expr: ExprSyntax = node.argumentList.first!.expression + return expr.with(\.leadingTrivia, [.blockComment("/* echo */")]) + } +} + +struct MetadataMacro: MemberMacro { + static func expansion< + Declaration: DeclGroupSyntax, + Context: MacroExpansionContext + >( + of node: SwiftSyntax.AttributeSyntax, + providingMembersOf declaration: Declaration, + in context: Context + ) throws -> [DeclSyntax] { + guard let cls = declaration.as(ClassDeclSyntax.self) else { + return [] + } + let className = cls.identifier.trimmedDescription + return [ + """ + static var __metadata__ = ["name": "\(raw: className)"] + """ + ] + } +} diff --git a/test-compiler-plugin/file.swift b/test-compiler-plugin/file.swift new file mode 100644 index 0000000..93e09a0 --- /dev/null +++ b/test-compiler-plugin/file.swift @@ -0,0 +1,10 @@ +@freestanding(expression) +macro echo(_: T) -> T = #externalMacro(module: "ExamplePlugin", type: "EchoExpressionMacro") + +@attached(member) +macro Metadata() = #externalMacro(module: "ExamplePlugin", type: "MetadataMacro") + +@Metadata +class MyClass { + var value: Int = #echo(12) +} diff --git a/test-compiler-plugin/test.txt b/test-compiler-plugin/test.txt new file mode 100644 index 0000000..bb8f4b4 --- /dev/null +++ b/test-compiler-plugin/test.txt @@ -0,0 +1,23 @@ +// REQUIRES: platform=Darwin +// +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: %{swift-build} --package-path %S/ExamplePlugin --build-path %t +// +// RUN: %{swift} -frontend -typecheck -swift-version 5 \ +// RUN: -enable-experimental-feature Macros \ +// RUN: -dump-macro-expansions \ +// RUN: -load-plugin-executable %t/debug/ExamplePlugin#ExamplePlugin \ +// RUN: %S/file.swift > %t/expansions-dump.txt 2>&1 +// +// RUN: %{FileCheck} %s < %t/expansions-dump.txt + +// CHECK-LABEL: @__swiftmacro_4file7MyClassC8MetadatafMm_.swift +// CHECK-NEXT: ------------------------------ +// CHECK-NEXT: static var __metadata__ = ["name": "MyClass"] +// CHECK-NEXT: ------------------------------ + +// CHECK-LABEL: @__swiftmacro_4file7MyClassC5valueSivpfi4echofMf_.swift as Int +// CHECK-NEXT: ------------------------------ +// CHECK-NEXT: /* echo */12 +// CHECK-NEXT: ------------------------------