Skip to content

Commit 92e5ad6

Browse files
committed
[Macros] swift-plugin-server cosmetic tweaks
1 parent 1d7148c commit 92e5ad6

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

lib/ASTGen/Sources/ASTGen/PluginMessages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal enum PluginToHostMessage: Codable {
7171
var protocolVersion: Int
7272

7373
/// Optional features this plugin provides.
74-
/// * resolveLibraryMacro: 'resolveLibraryMacro' message is implemented.
74+
/// * loadPluginLibrary: 'loadPluginLibrary' message is implemented.
7575
var features: [String]?
7676
}
7777

tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,37 @@ final class PluginHostConnection: MessageConnection {
116116
}
117117
}
118118

119-
/// Send a serialized message data to the message channel.
119+
/// Send a serialized message to the message channel.
120120
private func sendMessageData(_ data: UnsafeBufferPointer<Int8>) throws {
121121
// Write the header (a 64-bit length field in little endian byte order).
122122
var header: UInt64 = UInt64(data.count).littleEndian
123123
let writtenSize = try Swift.withUnsafeBytes(of: &header) { buffer in
124-
try self.write(data: UnsafeRawBufferPointer(buffer))
124+
try self.write(buffer: UnsafeRawBufferPointer(buffer))
125125
}
126-
guard writtenSize == 8 else {
126+
guard writtenSize == MemoryLayout.size(ofValue: header) else {
127127
throw PluginServerError(message: "failed to write message header")
128128
}
129129

130130
// Write the body.
131-
guard try self.write(data: UnsafeRawBufferPointer(data)) == data.count else {
132-
throw PluginServerError(message: "failed to write message data")
131+
guard try self.write(buffer: UnsafeRawBufferPointer(data)) == data.count else {
132+
throw PluginServerError(message: "failed to write message body")
133133
}
134134
}
135135

136-
/// Read a serialized message data from the message channel and call the 'body'
136+
/// Read a serialized message from the message channel and call the 'body'
137137
/// with the data.
138138
private func withReadingMessageData<R>(_ body: (UnsafeBufferPointer<Int8>) throws -> R) throws -> R? {
139-
// Read the header.
139+
// Read the header (a 64-bit length field in little endian byte order).
140140
var header: UInt64 = 0
141141
let readSize = try Swift.withUnsafeMutableBytes(of: &header) { buffer in
142142
try self.read(into: UnsafeMutableRawBufferPointer(buffer))
143143
}
144-
if readSize == 0 {
145-
// The host closed the pipe.
146-
return nil
147-
}
148-
guard readSize == 8 else {
144+
guard readSize == MemoryLayout.size(ofValue: header) else {
145+
if readSize == 0 {
146+
// The host closed the pipe.
147+
return nil
148+
}
149+
// Otherwise, some error happened.
149150
throw PluginServerError(message: "failed to read message header")
150151
}
151152

@@ -154,18 +155,21 @@ final class PluginHostConnection: MessageConnection {
154155
let data = UnsafeMutableBufferPointer<Int8>.allocate(capacity: count)
155156
defer { data.deallocate() }
156157
guard try self.read(into: UnsafeMutableRawBufferPointer(data)) == count else {
157-
throw PluginServerError(message: "failed to read message data")
158+
throw PluginServerError(message: "failed to read message body")
158159
}
159160

160161
// Invoke the handler.
161162
return try body(UnsafeBufferPointer(data))
162163
}
163164

164-
/// Write the 'data' to the message channel.
165+
/// Write the 'buffer' to the message channel.
165166
/// Returns the number of bytes succeeded to write.
166-
private func write(data: UnsafeRawBufferPointer) throws -> Int {
167-
var bytesToWrite = data.count
168-
var ptr = data.baseAddress!
167+
private func write(buffer: UnsafeRawBufferPointer) throws -> Int {
168+
var bytesToWrite = buffer.count
169+
guard bytesToWrite > 0 else {
170+
return 0
171+
}
172+
var ptr = buffer.baseAddress!
169173

170174
while (bytesToWrite > 0) {
171175
let writtenSize = PluginServer_write(handle, ptr, bytesToWrite)
@@ -176,13 +180,16 @@ final class PluginHostConnection: MessageConnection {
176180
ptr = ptr.advanced(by: writtenSize)
177181
bytesToWrite -= writtenSize
178182
}
179-
return data.count - bytesToWrite
183+
return buffer.count - bytesToWrite
180184
}
181185

182186
/// Read data from the message channel into the 'buffer' up to 'buffer.count' bytes.
183187
/// Returns the number of bytes succeeded to read.
184188
private func read(into buffer: UnsafeMutableRawBufferPointer) throws -> Int {
185189
var bytesToRead = buffer.count
190+
guard bytesToRead > 0 else {
191+
return 0
192+
}
186193
var ptr = buffer.baseAddress!
187194

188195
while bytesToRead > 0 {

0 commit comments

Comments
 (0)