File tree Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -121,7 +121,7 @@ if (SWIFT_SWIFT_PARSER)
121
121
OUTPUT_VARIABLE SWIFT_SYNTAX_MODULE_DIRS )
122
122
foreach (module_dir ${SWIFT_SYNTAX_MODULE_DIRS} )
123
123
file (GLOB module_files
124
- "${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR} /${module_dir} /*.swift* " )
124
+ "${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR} /${module_dir} /*.swiftinterface " )
125
125
add_custom_command (
126
126
TARGET swiftASTGen PRE_BUILD
127
127
COMMAND ${CMAKE_COMMAND} -E make_directory ${SWIFT_SYNTAX_LIBRARIES_DEST_DIR} /${module_dir}
Original file line number Diff line number Diff line change
1
+ import SwiftSyntax
2
+ import SwiftSyntaxBuilder
3
+ import _SwiftSyntaxMacros
4
+
5
+ public struct FileIDMacro : ExpressionMacro {
6
+ public static func apply(
7
+ _ macro: MacroExpansionExprSyntax , in context: MacroEvaluationContext
8
+ ) -> MacroResult < ExprSyntax > {
9
+ var fileName = context. sourceLocationConverter. location (
10
+ for: . init( utf8Offset: 0 )
11
+ ) . file ?? " <unknown file> "
12
+
13
+ // Only keep everything after the last slash.
14
+ if let lastSlash = fileName. lastIndex ( of: " / " ) {
15
+ fileName = String ( fileName [ fileName. index ( after: lastSlash) ... ] )
16
+ }
17
+
18
+ let fileLiteral : ExprSyntax = #"" \#( context. moduleName) / \#( fileName) ""#
19
+ if let leadingTrivia = macro. leadingTrivia {
20
+ return MacroResult ( fileLiteral. withLeadingTrivia ( leadingTrivia) )
21
+ }
22
+ return MacroResult ( fileLiteral)
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ // RUN: %empty-directory(%t)
2
+ // RUN: %target-build-swift -Xfrontend -disable-availability-checking -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -emit-library-path=%t/%target-library-name(MacroDefinition) -working-directory=%t -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift
3
+ // RUN: %target-build-swift -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser
4
+ // RUN: %target-run %t/main | %FileCheck %s
5
+ // REQUIRES: executable_test
6
+
7
+ // FIXME: Swift parser is not enabled on Linux CI yet.
8
+ // REQUIRES: OS=macosx
9
+
10
+ macro customFileID: String = MacroDefinition. FileIDMacro
11
+
12
+ func testFunc( a: Int , b: Int ) {
13
+ // CHECK: MacroUser/macro_external_exec.swift
14
+ print( " Result is \( #customFileID) " )
15
+ }
16
+
17
+ testFunc(a: 1, b: 2)
You can’t perform that action at this time.
0 commit comments