@@ -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
6969struct 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
0 commit comments