Skip to content

Commit 5b39197

Browse files
committed
[Macros/Plugin] Make 'features' a set of enum values
1 parent 025d874 commit 5b39197

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
4646
cxxDiagnosticEngine: UnsafeMutablePointer<UInt8>
4747
) -> Bool {
4848
let plugin = CompilerPlugin(opaqueHandle: opaqueHandle)
49-
assert(plugin.capability.features?.contains("loadPluginLibrary") == true)
49+
assert(plugin.capability?.features.contains(.loadPluginLibrary) == true)
5050
let libraryPath = String(cString: libraryPath)
5151
let moduleName = String(cString: moduleName)
5252
let diagEngine = PluginDiagnosticsEngine(cxxDiagnosticEngine: cxxDiagnosticEngine)
@@ -67,6 +67,24 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
6767
}
6868

6969
struct CompilerPlugin {
70+
struct Capability {
71+
enum Feature: String {
72+
case loadPluginLibrary = "load-plugin-library"
73+
}
74+
75+
var protocolVersion: Int
76+
var features: Set<Feature>
77+
78+
init(_ message: PluginMessage.PluginCapability) {
79+
self.protocolVersion = message.protocolVersion
80+
if let features = message.features {
81+
self.features = Set(features.compactMap(Feature.init(rawValue:)))
82+
} else {
83+
self.features = []
84+
}
85+
}
86+
}
87+
7088
let opaqueHandle: UnsafeMutableRawPointer
7189

7290
private func withLock<R>(_ body: () throws -> R) rethrows -> R {
@@ -111,26 +129,26 @@ struct CompilerPlugin {
111129
}
112130

113131
func initialize() {
114-
self.withLock {
115-
// Get capability.
116-
let response: PluginToHostMessage
117-
do {
132+
// Don't use `sendMessageAndWait` because we want to keep the lock until
133+
// setting the returned value.
134+
do {
135+
try self.withLock {
136+
// Get capability.
118137
try self.sendMessage(.getCapability)
119-
response = try self.waitForNextMessage()
120-
} catch {
121-
assertionFailure(String(describing: error))
122-
return
123-
}
124-
switch response {
125-
case .getCapabilityResult(capability: let capability):
126-
let ptr = UnsafeMutablePointer<PluginMessage.PluginCapability>.allocate(capacity: 1)
127-
ptr.initialize(to: capability)
138+
let response = try self.waitForNextMessage()
139+
guard case .getCapabilityResult(let capability) = response else {
140+
throw PluginError.invalidReponseKind
141+
}
142+
let ptr = UnsafeMutablePointer<Capability>.allocate(capacity: 1)
143+
ptr.initialize(to: .init(capability))
128144
Plugin_setCapability(opaqueHandle, UnsafeRawPointer(ptr))
129-
default:
130-
assertionFailure("invalid response")
131145
}
146+
} catch {
147+
assertionFailure(String(describing: error))
148+
return
132149
}
133150
}
151+
134152
func deinitialize() {
135153
self.withLock {
136154
if let ptr = Plugin_getCapability(opaqueHandle) {
@@ -142,11 +160,11 @@ struct CompilerPlugin {
142160
}
143161
}
144162

145-
var capability: PluginMessage.PluginCapability {
163+
var capability: Capability? {
146164
if let ptr = Plugin_getCapability(opaqueHandle) {
147-
return ptr.assumingMemoryBound(to: PluginMessage.PluginCapability.self).pointee
165+
return ptr.assumingMemoryBound(to: Capability.self).pointee
148166
}
149-
return PluginMessage.PluginCapability(protocolVersion: 0)
167+
return nil
150168
}
151169
}
152170

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-
/// * loadPluginLibrary: 'loadPluginLibrary' message is implemented.
74+
/// * 'load-plugin-library': 'loadPluginLibrary' message is implemented.
7575
var features: [String]?
7676
}
7777

0 commit comments

Comments
 (0)