File tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change
1
+ // swift-tools-version: 5.7
2
+
3
+ import PackageDescription
4
+
5
+ let package = Package (
6
+ name: " ExamplePlugin " ,
7
+ platforms: [
8
+ . macOS( . v10_15) ,
9
+ ] ,
10
+ dependencies: [
11
+ . package ( path: " ../../../swift-syntax " )
12
+ ] ,
13
+ targets: [
14
+ . executableTarget(
15
+ name: " ExamplePlugin " ,
16
+ dependencies: [
17
+ . product( name: " SwiftCompilerPlugin " , package : " swift-syntax " ) ,
18
+ . product( name: " SwiftSyntax " , package : " swift-syntax " ) ,
19
+ . product( name: " SwiftSyntaxMacros " , package : " swift-syntax " ) ,
20
+ . product( name: " SwiftDiagnostics " , package : " swift-syntax " ) ,
21
+ ] ) ,
22
+ ]
23
+ )
Original file line number Diff line number Diff line change
1
+ import SwiftCompilerPlugin
2
+ import SwiftSyntaxMacros
3
+
4
+ @main
5
+ struct ThePlugin : CompilerPlugin {
6
+ var providingMarcos : [ Macro . Type ] = [
7
+ EchoExpressionMacro . self,
8
+ MetadataMacro . self
9
+ ]
10
+ }
Original file line number Diff line number Diff line change
1
+ import SwiftSyntax
2
+ import SwiftSyntaxBuilder
3
+ import SwiftSyntaxMacros
4
+
5
+ struct EchoExpressionMacro : ExpressionMacro {
6
+ static func expansion<
7
+ Node: FreestandingMacroExpansionSyntax ,
8
+ Context: MacroExpansionContext
9
+ > (
10
+ of node: Node ,
11
+ in context: Context
12
+ ) throws -> ExprSyntax {
13
+ let expr : ExprSyntax = node. argumentList. first!. expression
14
+ return expr. with ( \. leadingTrivia, [ . blockComment( " /* echo */" ) ] )
15
+ }
16
+ }
17
+
18
+ struct MetadataMacro : MemberMacro {
19
+ static func expansion<
20
+ Declaration: DeclGroupSyntax ,
21
+ Context: MacroExpansionContext
22
+ > (
23
+ of node: SwiftSyntax . AttributeSyntax ,
24
+ providingMembersOf declaration: Declaration ,
25
+ in context: Context
26
+ ) throws -> [ DeclSyntax ] {
27
+ guard let cls = declaration. as ( ClassDeclSyntax . self) else {
28
+ return [ ]
29
+ }
30
+ let className = cls. identifier. trimmedDescription
31
+ return [
32
+ """
33
+ static var __metadata__ = [ " name " : " \( raw: className) " ]
34
+ """
35
+ ]
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ @freestanding ( expression)
2
+ macro echo< T> ( _: T ) -> T = #externalMacro( module: " ExamplePlugin " , type: " EchoExpressionMacro " )
3
+
4
+ @attached ( member)
5
+ macro Metadata( ) = #externalMacro( module: " ExamplePlugin " , type: " MetadataMacro " )
6
+
7
+ @Metadata
8
+ class MyClass {
9
+ var value : Int = #echo( 12 )
10
+ }
Original file line number Diff line number Diff line change
1
+ // REQUIRES: platform=Darwin
2
+ //
3
+ // RUN: rm -rf %t
4
+ // RUN: mkdir -p %t
5
+ // RUN: %{swift-build} --package-path %S/ExamplePlugin --build-path %t
6
+ //
7
+ // RUN: %{swift} -frontend -typecheck -swift-version 5 \
8
+ // RUN: -enable-experimental-feature Macros \
9
+ // RUN: -dump-macro-expansions \
10
+ // RUN: -load-plugin-executable %t/debug/ExamplePlugin#ExamplePlugin \
11
+ // RUN: %S/file.swift > %t/expansions-dump.txt 2>&1
12
+ //
13
+ // RUN: %{FileCheck} %s < %t/expansions-dump.txt
14
+
15
+ // CHECK-LABEL: @__swiftmacro_4file7MyClassC8MetadatafMm_.swift
16
+ // CHECK-NEXT: ------------------------------
17
+ // CHECK-NEXT: static var __metadata__ = ["name": "MyClass"]
18
+ // CHECK-NEXT: ------------------------------
19
+
20
+ // CHECK-LABEL: @__swiftmacro_4file7MyClassC5valueSivpfi4echofMf_.swift as Int
21
+ // CHECK-NEXT: ------------------------------
22
+ // CHECK-NEXT: /* echo */12
23
+ // CHECK-NEXT: ------------------------------
You can’t perform that action at this time.
0 commit comments