Skip to content

[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

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/swift-c/MockPlugin/MockPlugin.h
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
56 changes: 35 additions & 21 deletions test/Macros/macro_plugin_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +6 to +12
Copy link
Contributor

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

Copy link
Member Author

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.


// 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the binary name (mock-plugin) and the module name (TestPlugin) are both test-file-specific.
I don't think we can reuse much.

Copy link
Contributor

Choose a reason for hiding this comment

The 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")
Expand All @@ -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()

]}}
}
])
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_swift_tool_subdirectory(swift-ast-script)
add_swift_tool_subdirectory(swift-refactor)
add_swift_tool_subdirectory(libSwiftScan)
add_swift_tool_subdirectory(libStaticMirror)
add_swift_tool_subdirectory(libMockPlugin)

if(SWIFT_INCLUDE_TESTS OR SWIFT_INCLUDE_TEST_BINARIES)
add_swift_tool_subdirectory(swift-ide-test)
Expand Down
19 changes: 19 additions & 0 deletions tools/libMockPlugin/CMakeLists.txt
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)
Loading