@@ -24,10 +24,18 @@ enum PluginError: Error {
2424
2525@_cdecl ( " swift_ASTGen_initializePlugin " )
2626public func _initializePlugin(
27- opaqueHandle: UnsafeMutableRawPointer
28- ) {
27+ opaqueHandle: UnsafeMutableRawPointer ,
28+ cxxDiagnosticEngine: UnsafeMutablePointer < UInt8 > ?
29+ ) -> Bool {
2930 let plugin = CompilerPlugin ( opaqueHandle: opaqueHandle)
30- plugin. initialize ( )
31+ do {
32+ try plugin. initialize ( )
33+ return true
34+ } catch {
35+ // Diagnostics are emitted in the caller.
36+ // FIXME: Return what happened or emit diagnostics here.
37+ return false
38+ }
3139}
3240
3341@_cdecl ( " swift_ASTGen_deinitializePlugin " )
@@ -52,12 +60,7 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
5260 let libraryPath = String ( cString: libraryPath)
5361 let moduleName = String ( cString: moduleName)
5462
55- let diagEngine : PluginDiagnosticsEngine ?
56- if let cxxDiagnosticEngine = cxxDiagnosticEngine {
57- diagEngine = PluginDiagnosticsEngine ( cxxDiagnosticEngine: cxxDiagnosticEngine)
58- } else {
59- diagEngine = nil
60- }
63+ let diagEngine = PluginDiagnosticsEngine ( cxxDiagnosticEngine: cxxDiagnosticEngine)
6164
6265 do {
6366 let result = try plugin. sendMessageAndWaitWithoutLock (
@@ -136,20 +139,15 @@ struct CompilerPlugin {
136139 }
137140
138141 /// Initialize the plugin. This should be called inside lock.
139- func initialize( ) {
140- do {
141- // Get capability.
142- let response = try self . sendMessageAndWaitWithoutLock ( . getCapability)
143- guard case . getCapabilityResult( let capability) = response else {
144- throw PluginError . invalidReponseKind
145- }
146- let ptr = UnsafeMutablePointer< Capability> . allocate( capacity: 1 )
147- ptr. initialize ( to: . init( capability) )
148- Plugin_setCapability ( opaqueHandle, UnsafeRawPointer ( ptr) )
149- } catch {
150- assertionFailure ( String ( describing: error) )
151- return
142+ func initialize( ) throws {
143+ // Get capability.
144+ let response = try self . sendMessageAndWaitWithoutLock ( . getCapability)
145+ guard case . getCapabilityResult( let capability) = response else {
146+ throw PluginError . invalidReponseKind
152147 }
148+ let ptr = UnsafeMutablePointer< Capability> . allocate( capacity: 1 )
149+ ptr. initialize ( to: . init( capability) )
150+ Plugin_setCapability ( opaqueHandle, UnsafeRawPointer ( ptr) )
153151 }
154152
155153 func deinitialize( ) {
@@ -179,6 +177,14 @@ class PluginDiagnosticsEngine {
179177 self . cxxDiagnosticEngine = cxxDiagnosticEngine
180178 }
181179
180+ /// Failable convenience initializer for optional cxx engine pointer.
181+ convenience init ? ( cxxDiagnosticEngine: UnsafeMutablePointer < UInt8 > ? ) {
182+ guard let cxxDiagnosticEngine = cxxDiagnosticEngine else {
183+ return nil
184+ }
185+ self . init ( cxxDiagnosticEngine: cxxDiagnosticEngine)
186+ }
187+
182188 /// Register an 'ExportedSourceFile' to the engine. So the engine can get
183189 /// C++ SourceLoc from a pair of filename and offset.
184190 func add( exportedSourceFile: UnsafePointer < ExportedSourceFile > ) {
0 commit comments