Skip to content

Commit 1d14f19

Browse files
committed
[Macro] Add env variable option to dump exectuable plugin messagings
Set 'SWIFT_DUMP_PLUGIN_MESSAGING' env variable to enable it.
1 parent aeab985 commit 1d14f19

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

include/swift/AST/PluginRegistry.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class LoadedExecutablePlugin {
3232
/// value from ASTGen.
3333
const void *capability = nullptr;
3434

35+
/// Flag to dump plugin messagings.
36+
bool dumpMessaging = false;
37+
3538
/// Cleanup function to call ASTGen.
3639
std::function<void(void)> cleanup;
3740

@@ -67,6 +70,8 @@ class LoadedExecutablePlugin {
6770

6871
const void *getCapability() { return capability; };
6972
void setCapability(const void *newValue) { capability = newValue; };
73+
74+
void setDumpMessaging(bool flag) { dumpMessaging = flag; }
7075
};
7176

7277
class PluginRegistry {

lib/AST/PluginRegistry.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ PluginRegistry::loadExecutablePlugin(StringRef path) {
111111
childInfo->Pid, stat.getLastModificationTime(),
112112
childInfo->ReadFileDescriptor, childInfo->WriteFileDescriptor));
113113

114+
const char *dumpMessaging = ::getenv("SWIFT_DUMP_PLUGIN_MESSAGING");
115+
plugin->setDumpMessaging(dumpMessaging != nullptr);
116+
114117
return plugin.get();
115118
}
116119

@@ -181,6 +184,10 @@ ssize_t LoadedExecutablePlugin::write(const void *buf, size_t nbyte) const {
181184
llvm::Error LoadedExecutablePlugin::sendMessage(llvm::StringRef message) const {
182185
ssize_t writtenSize = 0;
183186

187+
if (dumpMessaging) {
188+
llvm::dbgs() << "->(plugin:" << pid << ") " << message << "\n";
189+
}
190+
184191
const char *data = message.data();
185192
size_t size = message.size();
186193

@@ -233,5 +240,9 @@ llvm::Expected<std::string> LoadedExecutablePlugin::waitForNextMessage() const {
233240
message.append(buffer, readSize);
234241
}
235242

243+
if (dumpMessaging) {
244+
llvm::dbgs() << "<-(plugin:" << pid << ") " << message << "\n";
245+
}
246+
236247
return message;
237248
}

lib/ASTGen/Sources/ASTGen/PluginHost.swift

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

4949
private func sendMessage(_ message: HostToPluginMessage) throws {
5050
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
51-
// // FIXME: Add -dump-plugin-message option?
52-
// data.withMemoryRebound(to: UInt8.self) { buffer in
53-
// print(">> " + String(decoding: buffer, as: UTF8.self))
54-
// }
5551
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: data.count))
5652
}
5753
if hadError {
@@ -67,10 +63,6 @@ struct CompilerPlugin {
6763
throw PluginError.failedToReceiveMessage
6864
}
6965
let data = UnsafeBufferPointer(start: result.baseAddress, count: result.size)
70-
// // FIXME: Add -dump-plugin-message option?
71-
// data.withMemoryRebound(to: UInt8.self) { buffer in
72-
// print("<< " + String(decoding: buffer, as: UTF8.self))
73-
// }
7466
return try LLVMJSON.decode(PluginToHostMessage.self, from: data)
7567
}
7668

0 commit comments

Comments
 (0)