Skip to content

[Macros] Add test for macro compiler plugins #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions test-compiler-plugin/ExamplePlugin/Package.swift
Original file line number Diff line number Diff line change
@@ -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"),
]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SwiftCompilerPlugin
import SwiftSyntaxMacros

@main
struct ThePlugin: CompilerPlugin {
var providingMarcos: [Macro.Type] = [
EchoExpressionMacro.self,
MetadataMacro.self
]
}
Original file line number Diff line number Diff line change
@@ -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)"]
"""
]
}
}
10 changes: 10 additions & 0 deletions test-compiler-plugin/file.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@freestanding(expression)
macro echo<T>(_: T) -> T = #externalMacro(module: "ExamplePlugin", type: "EchoExpressionMacro")

@attached(member)
macro Metadata() = #externalMacro(module: "ExamplePlugin", type: "MetadataMacro")

@Metadata
class MyClass {
var value: Int = #echo(12)
}
23 changes: 23 additions & 0 deletions test-compiler-plugin/test.txt
Original file line number Diff line number Diff line change
@@ -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: ------------------------------