Skip to content

Commit 02c72cd

Browse files
authored
Merge pull request #64407 from rintaro/macros-dump-plugin-messaging
[Macros] Add env variable option to dump exectuable plugin messagings
2 parents 0f98ecc + d5b8c84 commit 02c72cd

File tree

6 files changed

+71
-34
lines changed

6 files changed

+71
-34
lines changed

include/swift/AST/PluginRegistry.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class LoadedExecutablePlugin {
6464
/// Callbacks to be called when the connection is restored.
6565
llvm::SmallVector<std::function<void(void)> *, 0> onReconnect;
6666

67+
/// Flag to dump plugin messagings.
68+
bool dumpMessaging = false;
69+
6770
/// Cleanup function to call ASTGen.
6871
std::function<void(void)> cleanup;
6972

@@ -121,6 +124,8 @@ class LoadedExecutablePlugin {
121124

122125
const void *getCapability() { return capability; };
123126
void setCapability(const void *newValue) { capability = newValue; };
127+
128+
void setDumpMessaging(bool flag) { dumpMessaging = flag; }
124129
};
125130

126131
class PluginRegistry {
@@ -131,7 +136,12 @@ class PluginRegistry {
131136
llvm::StringMap<std::unique_ptr<LoadedExecutablePlugin>>
132137
LoadedPluginExecutables;
133138

139+
/// Flag to dump plugin messagings.
140+
bool dumpMessaging = false;
141+
134142
public:
143+
PluginRegistry();
144+
135145
/// Load a dynamic link library specified by \p path.
136146
/// If \p path plugin is already loaded, this returns the cached object.
137147
llvm::Expected<void *> loadLibraryPlugin(llvm::StringRef path);

lib/AST/PluginRegistry.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
using namespace swift;
4242

43+
PluginRegistry::PluginRegistry() {
44+
dumpMessaging = ::getenv("SWIFT_DUMP_PLUGIN_MESSAGING") != nullptr;
45+
}
46+
4347
llvm::Expected<void *> PluginRegistry::loadLibraryPlugin(StringRef path) {
4448
auto found = LoadedPluginLibraries.find(path);
4549
if (found != LoadedPluginLibraries.end()) {
@@ -94,6 +98,8 @@ PluginRegistry::loadExecutablePlugin(StringRef path) {
9498
auto plugin = std::unique_ptr<LoadedExecutablePlugin>(
9599
new LoadedExecutablePlugin(path, stat.getLastModificationTime()));
96100

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

222+
if (dumpMessaging) {
223+
llvm::dbgs() << "->(plugin:" << Process->pid << ") " << message << "\n";
224+
}
225+
216226
const char *data = message.data();
217227
size_t size = message.size();
218228

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

282+
if (dumpMessaging) {
283+
llvm::dbgs() << "<-(plugin:" << Process->pid << ") " << message << "\n";
284+
}
285+
272286
return message;
273287
}

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ struct CompilerPlugin {
103103

104104
private func sendMessage(_ message: HostToPluginMessage) throws {
105105
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
106-
// // FIXME: Add -dump-plugin-message option?
107-
// data.withMemoryRebound(to: UInt8.self) { buffer in
108-
// print(">> " + String(decoding: buffer, as: UTF8.self))
109-
// }
110106
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: data.count))
111107
}
112108
if hadError {
@@ -122,10 +118,6 @@ struct CompilerPlugin {
122118
throw PluginError.failedToReceiveMessage
123119
}
124120
let data = UnsafeBufferPointer(start: result.baseAddress, count: result.size)
125-
// // FIXME: Add -dump-plugin-message option?
126-
// data.withMemoryRebound(to: UInt8.self) { buffer in
127-
// print("<< " + String(decoding: buffer, as: UTF8.self))
128-
// }
129121
return try LLVMJSON.decode(PluginToHostMessage.self, from: data)
130122
}
131123

test/Macros/macro_plugin_basic.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@
1111
// RUN: -o %t/mock-plugin \
1212
// RUN: %t/plugin.c
1313

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

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

24+
// CHECK: ->(plugin:[[#PID:]]) {"getCapability":{}}
25+
// CHECK: <-(plugin:[[#PID]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
26+
// 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)"}}}
27+
// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"\"123\"\n + \"foo \""}}
28+
// 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)"}}}
29+
// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[{"fixIts":[],"highlights":[],"message":"message from plugin","notes":[],"position":{"fileName":"BUILD_DIR{{.*}}test.swift","offset":336},"severity":"error"}],"expandedSource":"\"bar\""}}
30+
2431
//--- test.swift
2532
@freestanding(expression) macro testString(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro")
2633
@freestanding(expression) macro testStringWithError(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringWithErrorMacro")
@@ -31,15 +38,6 @@ func test() {
3138
// expected-error @-1 {{message from plugin}}
3239
}
3340

34-
// CHECK: ------------------------------
35-
// CHECK-NEXT: {{^}}"123"
36-
// CHECK-NEXT: {{^}} + "foo "
37-
// CHECK-NEXT: ------------------------------
38-
39-
// CHECK: ------------------------------
40-
// CHECK-NEXT: {{^}}"bar"
41-
// CHECK-NEXT: ------------------------------
42-
4341
//--- plugin.c
4442
#include "swift-c/MockPlugin/MockPlugin.h"
4543

test/Macros/macro_plugin_error.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@
1111
// RUN: -o %t/mock-plugin \
1212
// RUN: %t/plugin.c
1313

14-
// RUN: %swift-target-frontend \
14+
// RUN: SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \
1515
// RUN: -typecheck -verify \
1616
// RUN: -swift-version 5 -enable-experimental-feature Macros \
1717
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
18-
// RUN: -dump-macro-expansions \
19-
// RUN: %t/test.swift
18+
// RUN: -module-name MyApp \
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
23+
24+
// CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{}}
25+
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
26+
// 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)"}}}
27+
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"invalidResponse":{}}
28+
// 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)"}}}
29+
// ^ This messages causes the mock plugin exit because there's no matching expected message.
30+
31+
// 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)"}}}
32+
// CHECK-NEXT: <-(plugin:[[#PID2:]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"3.description"}}
2033

2134
//--- test.swift
2235
@freestanding(expression) macro fooMacro(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "FooMacro")

test/Macros/macro_plugin_server.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,33 @@
2525
// RUN: %S/Inputs/evil_macro_definitions.swift \
2626
// RUN: -g -no-toolchain-stdlib-rpath
2727

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

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

38+
// CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{}}
39+
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"features":["load-plugin-library"],"protocolVersion":4}}}
40+
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libMacroDefinition.dylib","moduleName":"MacroDefinition"}}
41+
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
42+
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libEvilMacros.dylib","moduleName":"EvilMacros"}}
43+
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
44+
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(a + b)"}}}
45+
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"(a + b, \"a + b\")"}}
46+
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"EvilMacros","name":"evil","typeName":"CrashingMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#evil(42)"}}}
47+
// ^ This crashes the plugin server.
48+
49+
// CHECK-NEXT: ->(plugin:[[#PID2:]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libMacroDefinition.dylib","moduleName":"MacroDefinition"}}
50+
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
51+
// CHECK-NEXT: ->(plugin:[[#PID2]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libEvilMacros.dylib","moduleName":"EvilMacros"}}
52+
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
53+
// CHECK-NEXT: ->(plugin:[[#PID2]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(b + a)"}}}
54+
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"(b + a, \"b + a\")"}}
3855

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

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

4967
let s3: String = #stringify(b + a).1
5068
print(s3)
5169
}
52-
53-
// CHECK: {{^}}------------------------------
54-
// CHECK-NEXT: {{^}}(a + b, "a + b")
55-
// CHECK-NEXT: {{^}}------------------------------
56-
57-
// CHECK: {{^}}------------------------------
58-
// CHECK-NEXT: {{^}}(b + a, "b + a")
59-
// CHECK-NEXT: {{^}}------------------------------

0 commit comments

Comments
 (0)