@@ -24,10 +24,18 @@ enum PluginError: Error {
24
24
25
25
@_cdecl ( " swift_ASTGen_initializePlugin " )
26
26
public func _initializePlugin(
27
- opaqueHandle: UnsafeMutableRawPointer
28
- ) {
27
+ opaqueHandle: UnsafeMutableRawPointer ,
28
+ cxxDiagnosticEngine: UnsafeMutablePointer < UInt8 > ?
29
+ ) -> Bool {
29
30
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
+ }
31
39
}
32
40
33
41
@_cdecl ( " swift_ASTGen_deinitializePlugin " )
@@ -52,12 +60,7 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
52
60
let libraryPath = String ( cString: libraryPath)
53
61
let moduleName = String ( cString: moduleName)
54
62
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)
61
64
62
65
do {
63
66
let result = try plugin. sendMessageAndWaitWithoutLock (
@@ -136,20 +139,15 @@ struct CompilerPlugin {
136
139
}
137
140
138
141
/// 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
152
147
}
148
+ let ptr = UnsafeMutablePointer< Capability> . allocate( capacity: 1 )
149
+ ptr. initialize ( to: . init( capability) )
150
+ Plugin_setCapability ( opaqueHandle, UnsafeRawPointer ( ptr) )
153
151
}
154
152
155
153
func deinitialize( ) {
@@ -179,6 +177,14 @@ class PluginDiagnosticsEngine {
179
177
self . cxxDiagnosticEngine = cxxDiagnosticEngine
180
178
}
181
179
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
+
182
188
/// Register an 'ExportedSourceFile' to the engine. So the engine can get
183
189
/// C++ SourceLoc from a pair of filename and offset.
184
190
func add( exportedSourceFile: UnsafePointer < ExportedSourceFile > ) {
0 commit comments