Skip to content

Commit 761cbb0

Browse files
committed
[Macros] Add a library to make simple executable plugins for testing
Add `_swiftMockPlugin` library. Usage: #include "swift/swift-c/MockPlugin/MockPlugin.h" MOCK_PLUGIN([ {"expect": {...}, "response: {...}} ])
1 parent d6cf55b commit 761cbb0

File tree

8 files changed

+355
-155
lines changed

8 files changed

+355
-155
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- MockPlugin.h ---------------------------------------------*- C -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_C_MOCK_PLUGIN_H
14+
#define SWIFT_C_MOCK_PLUGIN_H
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
int _mock_plugin_main(const char *);
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif
25+
26+
/// Usage: MOCK_PLUGIN(JSON)
27+
/// 'JSON' is a *bare* JSON value.
28+
#define MOCK_PLUGIN(...) \
29+
int main() { return _mock_plugin_main(#__VA_ARGS__); }
30+
31+
#endif // SWIFT_C_MOCK_PLUGIN_H

test/Macros/macro_plugin_basic.swift

+35-21
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
// RUN: %empty-directory(%t)
44
// RUN: split-file %s %t
55

6-
// RUN: sed -i '' -e 's#PYTHON_EXEC_PATH#%{python}#' %t/plugin
7-
// RUN: sed -i '' -e 's#UTILS_DIR_PATH#%utils#' %t/plugin
8-
// RUN: chmod +x %t/plugin
6+
// RUN: %clang \
7+
// RUN: -isysroot %sdk \
8+
// RUN: -I %swift_src_root/include \
9+
// RUN: -L %swift-lib-dir -l_swiftMockPlugin \
10+
// RUN: -Wl,-rpath,%swift-lib-dir \
11+
// RUN: -o %t/mock-plugin \
12+
// RUN: %t/plugin.c
913

10-
// RUN: %swift-target-frontend -typecheck -verify -swift-version 5 -enable-experimental-feature Macros -load-plugin-executable %t/plugin#TestPlugin %t/test.swift
14+
// RUN: %swift-target-frontend \
15+
// RUN: -typecheck -verify \
16+
// RUN: -swift-version 5 -enable-experimental-feature Macros \
17+
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
18+
// RUN: -dump-macro-expansions \
19+
// RUN: %t/test.swift \
20+
// RUN: 2>&1 | tee %t/macro-expansions.txt
21+
22+
// RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt
1123

1224
//--- test.swift
1325
@freestanding(expression) macro testString(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro")
@@ -19,41 +31,43 @@ func test() {
1931
// expected-error @-1 {{message from plugin}}
2032
}
2133

22-
//--- plugin
23-
#!PYTHON_EXEC_PATH
24-
import sys
25-
sys.path.append('UTILS_DIR_PATH')
34+
// CHECK: ------------------------------
35+
// CHECK-NEXT: {{^}}"123"
36+
// CHECK-NEXT: {{^}} + "foo "
37+
// CHECK-NEXT: ------------------------------
38+
39+
// CHECK: ------------------------------
40+
// CHECK-NEXT: {{^}}"bar"
41+
// CHECK-NEXT: ------------------------------
2642

27-
import mock_plugin
43+
//--- plugin.c
44+
#include "swift-c/MockPlugin/MockPlugin.h"
2845

29-
mock_plugin.TEST_SPEC = [
46+
MOCK_PLUGIN([
3047
{
3148
"expect": {"getCapability": {}},
32-
"response": {"getCapabilityResult": {"capability": {"protocolVersion": 1}}},
49+
"response": {"getCapabilityResult": {"capability": {"protocolVersion": 1}}}
3350
},
3451
{
3552
"expect": {"expandFreestandingMacro": {
3653
"macro": {"moduleName": "TestPlugin", "typeName": "TestStringMacro"},
3754
"syntax": {"kind": "expression", "source": "#testString(123)"}}},
38-
"response": {"expandFreestandingMacroResult": {"expandedSource": "\"123\"", "diagnostics": []}},
55+
"response": {"expandFreestandingMacroResult": {"expandedSource": "\"123\"\n + \"foo \"", "diagnostics": []}}
3956
},
4057
{
4158
"expect": {"expandFreestandingMacro": {
4259
"macro": {"moduleName": "TestPlugin", "typeName": "TestStringWithErrorMacro"},
4360
"syntax": {"kind": "expression", "source": "#testStringWithError(321)"}}},
4461
"response": {"expandFreestandingMacroResult": {
45-
"expandedSource": "\"123\"",
62+
"expandedSource": "\"bar\"",
4663
"diagnostics": [
4764
{"severity": "error",
48-
"position": {"offset": "={req[expandFreestandingMacro][syntax][location][offset]}",
49-
"fileName": "{req[expandFreestandingMacro][syntax][location][fileName]}"},
65+
"position": {"offset": "=req.expandFreestandingMacro.syntax.location.offset",
66+
"fileName": "=req.expandFreestandingMacro.syntax.location.fileName"},
5067
"message":"message from plugin",
5168
"highlights": [],
5269
"notes": [],
5370
"fixIts": []}
54-
]}},
55-
},
56-
]
57-
58-
mock_plugin.main()
59-
71+
]}}
72+
}
73+
])

tools/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_swift_tool_subdirectory(swift-ast-script)
3333
add_swift_tool_subdirectory(swift-refactor)
3434
add_swift_tool_subdirectory(libSwiftScan)
3535
add_swift_tool_subdirectory(libStaticMirror)
36+
add_swift_tool_subdirectory(libMockPlugin)
3637

3738
if(SWIFT_INCLUDE_TESTS OR SWIFT_INCLUDE_TEST_BINARIES)
3839
add_swift_tool_subdirectory(swift-ide-test)

tools/libMockPlugin/CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
set(MOCK_PLUGIN_LIB_NAME "_swiftMockPlugin")
2+
3+
set(LLVM_EXPORTED_SYMBOL_FILE
4+
${CMAKE_CURRENT_SOURCE_DIR}/libMockPlugin.exports)
5+
6+
add_swift_host_library(libMockPlugin SHARED
7+
MockPlugin.cpp
8+
c-include-check.c
9+
LLVM_LINK_COMPONENTS support)
10+
11+
set_target_properties(libMockPlugin
12+
PROPERTIES
13+
OUTPUT_NAME ${MOCK_PLUGIN_LIB_NAME})
14+
15+
add_llvm_symbol_exports(libMockPlugin ${LLVM_EXPORTED_SYMBOL_FILE})
16+
17+
add_dependencies(tools libMockPlugin)
18+
# Adds -dead_strip option
19+
add_link_opts(libStaticMirror)

0 commit comments

Comments
 (0)