-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Macros] Add a library to make simple executable plugins for testing #63927
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//===--- MockPlugin.h ---------------------------------------------*- C -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_C_MOCK_PLUGIN_H | ||
#define SWIFT_C_MOCK_PLUGIN_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
int _mock_plugin_main(const char *); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
/// Usage: MOCK_PLUGIN(JSON) | ||
/// 'JSON' is a *bare* JSON value. | ||
#define MOCK_PLUGIN(...) \ | ||
int main() { return _mock_plugin_main(#__VA_ARGS__); } | ||
|
||
#endif // SWIFT_C_MOCK_PLUGIN_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,23 @@ | |
// RUN: %empty-directory(%t) | ||
// RUN: split-file %s %t | ||
|
||
// RUN: sed -i '' -e 's#PYTHON_EXEC_PATH#%{python}#' %t/plugin | ||
// RUN: sed -i '' -e 's#UTILS_DIR_PATH#%utils#' %t/plugin | ||
// RUN: chmod +x %t/plugin | ||
// RUN: %clang \ | ||
// RUN: -isysroot %sdk \ | ||
// RUN: -I %swift_src_root/include \ | ||
// RUN: -L %swift-lib-dir -l_swiftMockPlugin \ | ||
// RUN: -Wl,-rpath,%swift-lib-dir \ | ||
// RUN: -o %t/mock-plugin \ | ||
// RUN: %t/plugin.c | ||
|
||
// RUN: %swift-target-frontend -typecheck -verify -swift-version 5 -enable-experimental-feature Macros -load-plugin-executable %t/plugin#TestPlugin %t/test.swift | ||
// RUN: %swift-target-frontend \ | ||
// RUN: -typecheck -verify \ | ||
// RUN: -swift-version 5 -enable-experimental-feature Macros \ | ||
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ | ||
Comment on lines
+14
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could maybe also have a substitution for this one that could take an optional name... not sure how useful it would be though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the binary name ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be fine with them all being mock-plugin/TestPlugin :P. But you could give them as inputs 🤷 |
||
// RUN: -dump-macro-expansions \ | ||
// RUN: %t/test.swift \ | ||
// RUN: 2>&1 | tee %t/macro-expansions.txt | ||
|
||
// RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt | ||
|
||
//--- test.swift | ||
@freestanding(expression) macro testString(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro") | ||
|
@@ -19,41 +31,43 @@ func test() { | |
// expected-error @-1 {{message from plugin}} | ||
} | ||
|
||
//--- plugin | ||
#!PYTHON_EXEC_PATH | ||
import sys | ||
sys.path.append('UTILS_DIR_PATH') | ||
// CHECK: ------------------------------ | ||
// CHECK-NEXT: {{^}}"123" | ||
// CHECK-NEXT: {{^}} + "foo " | ||
// CHECK-NEXT: ------------------------------ | ||
|
||
// CHECK: ------------------------------ | ||
// CHECK-NEXT: {{^}}"bar" | ||
// CHECK-NEXT: ------------------------------ | ||
|
||
import mock_plugin | ||
//--- plugin.c | ||
#include "swift-c/MockPlugin/MockPlugin.h" | ||
|
||
mock_plugin.TEST_SPEC = [ | ||
MOCK_PLUGIN([ | ||
{ | ||
"expect": {"getCapability": {}}, | ||
"response": {"getCapabilityResult": {"capability": {"protocolVersion": 1}}}, | ||
"response": {"getCapabilityResult": {"capability": {"protocolVersion": 1}}} | ||
}, | ||
{ | ||
"expect": {"expandFreestandingMacro": { | ||
"macro": {"moduleName": "TestPlugin", "typeName": "TestStringMacro"}, | ||
"syntax": {"kind": "expression", "source": "#testString(123)"}}}, | ||
"response": {"expandFreestandingMacroResult": {"expandedSource": "\"123\"", "diagnostics": []}}, | ||
"response": {"expandFreestandingMacroResult": {"expandedSource": "\"123\"\n + \"foo \"", "diagnostics": []}} | ||
}, | ||
{ | ||
"expect": {"expandFreestandingMacro": { | ||
"macro": {"moduleName": "TestPlugin", "typeName": "TestStringWithErrorMacro"}, | ||
"syntax": {"kind": "expression", "source": "#testStringWithError(321)"}}}, | ||
"response": {"expandFreestandingMacroResult": { | ||
"expandedSource": "\"123\"", | ||
"expandedSource": "\"bar\"", | ||
"diagnostics": [ | ||
{"severity": "error", | ||
"position": {"offset": "={req[expandFreestandingMacro][syntax][location][offset]}", | ||
"fileName": "{req[expandFreestandingMacro][syntax][location][fileName]}"}, | ||
"position": {"offset": "=req.expandFreestandingMacro.syntax.location.offset", | ||
"fileName": "=req.expandFreestandingMacro.syntax.location.fileName"}, | ||
"message":"message from plugin", | ||
"highlights": [], | ||
"notes": [], | ||
"fixIts": []} | ||
]}}, | ||
}, | ||
] | ||
|
||
mock_plugin.main() | ||
|
||
]}} | ||
} | ||
]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
set(MOCK_PLUGIN_LIB_NAME "_swiftMockPlugin") | ||
|
||
set(LLVM_EXPORTED_SYMBOL_FILE | ||
${CMAKE_CURRENT_SOURCE_DIR}/libMockPlugin.exports) | ||
|
||
add_swift_host_library(libMockPlugin SHARED | ||
MockPlugin.cpp | ||
c-include-check.c | ||
LLVM_LINK_COMPONENTS support) | ||
|
||
set_target_properties(libMockPlugin | ||
PROPERTIES | ||
OUTPUT_NAME ${MOCK_PLUGIN_LIB_NAME}) | ||
|
||
add_llvm_symbol_exports(libMockPlugin ${LLVM_EXPORTED_SYMBOL_FILE}) | ||
|
||
add_dependencies(tools libMockPlugin) | ||
# Adds -dead_strip option | ||
add_link_opts(libStaticMirror) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add a lit.cfg substitution that handles the flags part of this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do it when we introduce second usage.