|
| 1 | +// REQUIRES: swift_swift_parser |
| 2 | + |
| 3 | +// sandbox-exec is only avaiable in Darwin |
| 4 | +// REQUIRES: OS=macosx |
| 5 | + |
| 6 | +// RUN: %empty-directory(%t) |
| 7 | +// RUN: %empty-directory(%t/plugins) |
| 8 | + |
| 9 | +// RUN: split-file %s %t |
| 10 | + |
| 11 | +//== Build the plugins |
| 12 | +// RUN: %host-build-swift \ |
| 13 | +// RUN: -swift-version 5 \ |
| 14 | +// RUN: -emit-library \ |
| 15 | +// RUN: -o %t/plugins/%target-library-name(MacroDefinition) \ |
| 16 | +// RUN: -module-name=MacroDefinition \ |
| 17 | +// RUN: %t/MacroDefinition.swift \ |
| 18 | +// RUN: -g -no-toolchain-stdlib-rpath |
| 19 | + |
| 20 | +// RUN: %swift-build-cxx-plugin -o %t/mock-plugin %t/TestPlugin.c |
| 21 | + |
| 22 | +//== Nested sandbox. Expected to fail because sandbox-exec doesn't support nested sandboxing. |
| 23 | +// RUN: not sandbox-exec -p '(version 1)(allow default)' \ |
| 24 | +// RUN: %swift-target-frontend \ |
| 25 | +// RUN: -typecheck -verify \ |
| 26 | +// RUN: -swift-version 5 \ |
| 27 | +// RUN: -external-plugin-path %t/plugins#%swift-plugin-server \ |
| 28 | +// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ |
| 29 | +// RUN: -module-name MyApp \ |
| 30 | +// RUN: %t/test.swift |
| 31 | + |
| 32 | +//== Avoid nested sandbox by -disable-sandbox |
| 33 | +// RUN: sandbox-exec -p '(version 1)(allow default)' \ |
| 34 | +// RUN: %swift-target-frontend \ |
| 35 | +// RUN: -disable-sandbox \ |
| 36 | +// RUN: -typecheck -verify \ |
| 37 | +// RUN: -swift-version 5 \ |
| 38 | +// RUN: -external-plugin-path %t/plugins#%swift-plugin-server \ |
| 39 | +// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ |
| 40 | +// RUN: -module-name MyApp \ |
| 41 | +// RUN: %t/test.swift |
| 42 | + |
| 43 | + |
| 44 | +//--- MacroDefinition.swift |
| 45 | +import SwiftSyntax |
| 46 | +import SwiftSyntaxBuilder |
| 47 | +import SwiftSyntaxMacros |
| 48 | + |
| 49 | +public struct StringifyMacro: ExpressionMacro { |
| 50 | + public static func expansion( |
| 51 | + of macro: some FreestandingMacroExpansionSyntax, |
| 52 | + in context: some MacroExpansionContext |
| 53 | + ) -> ExprSyntax { |
| 54 | + guard let argument = macro.arguments.first?.expression else { |
| 55 | + fatalError("boom") |
| 56 | + } |
| 57 | + |
| 58 | + return "(\(argument), \(StringLiteralExprSyntax(content: argument.description)))" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +//--- TestPlugin.c |
| 63 | +#include "swift-c/MockPlugin/MockPlugin.h" |
| 64 | + |
| 65 | +MOCK_PLUGIN([ |
| 66 | + { |
| 67 | + "expect": {"getCapability": {}}, |
| 68 | + "response": {"getCapabilityResult": {"capability": {"protocolVersion": 1}}} |
| 69 | + }, |
| 70 | + { |
| 71 | + "expect": {"expandFreestandingMacro": { |
| 72 | + "macro": {"moduleName": "TestPlugin", "typeName": "TestStringMacro"}}}, |
| 73 | + "response": {"expandMacroResult": {"expandedSource": "\"test string\"", "diagnostics": []}} |
| 74 | + } |
| 75 | +]) |
| 76 | + |
| 77 | +//--- test.swift |
| 78 | +@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro") |
| 79 | +@freestanding(expression) macro testString() -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro") |
| 80 | + |
| 81 | +func test() { |
| 82 | + let _: String = #stringify(42).1 |
| 83 | + let _: String = #testString |
| 84 | +} |
0 commit comments