Skip to content
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
10 changes: 10 additions & 0 deletions include/swift/AST/PluginRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class LoadedExecutablePlugin {
/// Callbacks to be called when the connection is restored.
llvm::SmallVector<std::function<void(void)> *, 0> onReconnect;

/// Flag to dump plugin messagings.
bool dumpMessaging = false;

/// Cleanup function to call ASTGen.
std::function<void(void)> cleanup;

Expand Down Expand Up @@ -121,6 +124,8 @@ class LoadedExecutablePlugin {

const void *getCapability() { return capability; };
void setCapability(const void *newValue) { capability = newValue; };

void setDumpMessaging(bool flag) { dumpMessaging = flag; }
};

class PluginRegistry {
Expand All @@ -131,7 +136,12 @@ class PluginRegistry {
llvm::StringMap<std::unique_ptr<LoadedExecutablePlugin>>
LoadedPluginExecutables;

/// Flag to dump plugin messagings.
bool dumpMessaging = false;

public:
PluginRegistry();

/// Load a dynamic link library specified by \p path.
/// If \p path plugin is already loaded, this returns the cached object.
llvm::Expected<void *> loadLibraryPlugin(llvm::StringRef path);
Expand Down
14 changes: 14 additions & 0 deletions lib/AST/PluginRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@

using namespace swift;

PluginRegistry::PluginRegistry() {
dumpMessaging = ::getenv("SWIFT_DUMP_PLUGIN_MESSAGING") != nullptr;
}

llvm::Expected<void *> PluginRegistry::loadLibraryPlugin(StringRef path) {
auto found = LoadedPluginLibraries.find(path);
if (found != LoadedPluginLibraries.end()) {
Expand Down Expand Up @@ -94,6 +98,8 @@ PluginRegistry::loadExecutablePlugin(StringRef path) {
auto plugin = std::unique_ptr<LoadedExecutablePlugin>(
new LoadedExecutablePlugin(path, stat.getLastModificationTime()));

plugin->setDumpMessaging(dumpMessaging);

// Launch here to see if it's actually executable, and diagnose (by returning
// an error) if necessary.
if (auto error = plugin->spawnIfNeeded()) {
Expand Down Expand Up @@ -213,6 +219,10 @@ ssize_t LoadedExecutablePlugin::PluginProcess::write(const void *buf,
llvm::Error LoadedExecutablePlugin::sendMessage(llvm::StringRef message) const {
ssize_t writtenSize = 0;

if (dumpMessaging) {
llvm::dbgs() << "->(plugin:" << Process->pid << ") " << message << "\n";
}

const char *data = message.data();
size_t size = message.size();

Expand Down Expand Up @@ -269,5 +279,9 @@ llvm::Expected<std::string> LoadedExecutablePlugin::waitForNextMessage() const {
message.append(buffer, readSize);
}

if (dumpMessaging) {
llvm::dbgs() << "<-(plugin:" << Process->pid << ") " << message << "\n";
}

return message;
}
8 changes: 0 additions & 8 deletions lib/ASTGen/Sources/ASTGen/PluginHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ struct CompilerPlugin {

private func sendMessage(_ message: HostToPluginMessage) throws {
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
// // FIXME: Add -dump-plugin-message option?
// data.withMemoryRebound(to: UInt8.self) { buffer in
// print(">> " + String(decoding: buffer, as: UTF8.self))
// }
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: data.count))
}
if hadError {
Expand All @@ -122,10 +118,6 @@ struct CompilerPlugin {
throw PluginError.failedToReceiveMessage
}
let data = UnsafeBufferPointer(start: result.baseAddress, count: result.size)
// // FIXME: Add -dump-plugin-message option?
// data.withMemoryRebound(to: UInt8.self) { buffer in
// print("<< " + String(decoding: buffer, as: UTF8.self))
// }
return try LLVMJSON.decode(PluginToHostMessage.self, from: data)
}

Expand Down
20 changes: 9 additions & 11 deletions test/Macros/macro_plugin_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@
// RUN: -o %t/mock-plugin \
// RUN: %t/plugin.c

// RUN: %swift-target-frontend \
// RUN: SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \
// RUN: -typecheck -verify \
// RUN: -swift-version 5 \
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
// RUN: -dump-macro-expansions \
// RUN: -module-name MyApp \
// RUN: %t/test.swift \
// RUN: 2>&1 | tee %t/macro-expansions.txt

// RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt

// CHECK: ->(plugin:[[#PID:]]) {"getCapability":{}}
// CHECK: <-(plugin:[[#PID]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testString","typeName":"TestStringMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":5,"offset":301},"source":"#testString(123)"}}}
// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"\"123\"\n + \"foo \""}}
// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testStringWithError","typeName":"TestStringWithErrorMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":336},"source":"#testStringWithError(321)"}}}
// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[{"fixIts":[],"highlights":[],"message":"message from plugin","notes":[],"position":{"fileName":"BUILD_DIR{{.*}}test.swift","offset":336},"severity":"error"}],"expandedSource":"\"bar\""}}

//--- test.swift
@freestanding(expression) macro testString(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro")
@freestanding(expression) macro testStringWithError(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringWithErrorMacro")
Expand All @@ -31,15 +38,6 @@ func test() {
// expected-error @-1 {{message from plugin}}
}

// CHECK: ------------------------------
// CHECK-NEXT: {{^}}"123"
// CHECK-NEXT: {{^}} + "foo "
// CHECK-NEXT: ------------------------------

// CHECK: ------------------------------
// CHECK-NEXT: {{^}}"bar"
// CHECK-NEXT: ------------------------------

//--- plugin.c
#include "swift-c/MockPlugin/MockPlugin.h"

Expand Down
19 changes: 16 additions & 3 deletions test/Macros/macro_plugin_error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,25 @@
// RUN: -o %t/mock-plugin \
// RUN: %t/plugin.c

// RUN: %swift-target-frontend \
// RUN: SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \
// RUN: -typecheck -verify \
// RUN: -swift-version 5 -enable-experimental-feature Macros \
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
// RUN: -dump-macro-expansions \
// RUN: %t/test.swift
// RUN: -module-name MyApp \
// RUN: %t/test.swift \
// RUN: 2>&1 | tee %t/macro-expansions.txt

// RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt

// CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":200},"source":"#fooMacro(1)"}}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"invalidResponse":{}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":8,"offset":304},"source":"#fooMacro(2)"}}}
// ^ This messages causes the mock plugin exit because there's no matching expected message.

// CHECK: ->(plugin:[[#PID2:]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":10,"offset":386},"source":"#fooMacro(3)"}}}
// CHECK-NEXT: <-(plugin:[[#PID2:]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"3.description"}}

//--- test.swift
@freestanding(expression) macro fooMacro(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "FooMacro")
Expand Down
34 changes: 22 additions & 12 deletions test/Macros/macro_plugin_server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,33 @@
// RUN: %S/Inputs/evil_macro_definitions.swift \
// RUN: -g -no-toolchain-stdlib-rpath

// RUN: %swift-target-frontend \
// RUN: SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \
// RUN: -typecheck -verify \
// RUN: -swift-version 5 \
// RUN: -swift-version 5 -enable-experimental-feature Macros \
// RUN: -external-plugin-path %t/plugins#%swift-plugin-server \
// RUN: -dump-macro-expansions \
// RUN: -module-name MyApp \
// RUN: %s \
// RUN: 2>&1 | tee %t/macro-expansions.txt

// RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt

// CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"features":["load-plugin-library"],"protocolVersion":4}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libMacroDefinition.dylib","moduleName":"MacroDefinition"}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libEvilMacros.dylib","moduleName":"EvilMacros"}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(a + b)"}}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"(a + b, \"a + b\")"}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"EvilMacros","name":"evil","typeName":"CrashingMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#evil(42)"}}}
// ^ This crashes the plugin server.

// CHECK-NEXT: ->(plugin:[[#PID2:]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libMacroDefinition.dylib","moduleName":"MacroDefinition"}}
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID2]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libEvilMacros.dylib","moduleName":"EvilMacros"}}
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID2]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(b + a)"}}}
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"(b + a, \"b + a\")"}}

@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
@freestanding(expression) macro evil(_ value: Int) -> String = #externalMacro(module: "EvilMacros", type: "CrashingMacro")
Expand All @@ -43,17 +60,10 @@ func testStringify(a: Int, b: Int) {
let s1: String = #stringify(a + b).1
print(s1)

let s2: String = #evil(42) // expected-error {{failedToReceiveMessage (from macro 'evil')}}
// expected-error @+1 {{failedToReceiveMessage (from macro 'evil')}}
let s2: String = #evil(42)
print(s2)

let s3: String = #stringify(b + a).1
print(s3)
}

// CHECK: {{^}}------------------------------
// CHECK-NEXT: {{^}}(a + b, "a + b")
// CHECK-NEXT: {{^}}------------------------------

// CHECK: {{^}}------------------------------
// CHECK-NEXT: {{^}}(b + a, "b + a")
// CHECK-NEXT: {{^}}------------------------------