@@ -15,11 +15,13 @@ import CBasicBridging
1515import SwiftSyntax
1616import swiftLLVMJSON
1717
18- enum PluginError : Error {
19- case stalePlugin
20- case failedToSendMessage
21- case failedToReceiveMessage
22- case invalidReponseKind
18+ enum PluginError : String , Error , CustomStringConvertible {
19+ case stalePlugin = " plugin is stale "
20+ case failedToSendMessage = " failed to send request to plugin "
21+ case failedToReceiveMessage = " failed to receive result from plugin "
22+ case invalidReponseKind = " plugin returned invalid result "
23+
24+ var description : String { rawValue }
2325}
2426
2527@_cdecl ( " swift_ASTGen_initializePlugin " )
@@ -28,12 +30,15 @@ public func _initializePlugin(
2830 cxxDiagnosticEngine: UnsafeMutablePointer < UInt8 > ?
2931) -> Bool {
3032 let plugin = CompilerPlugin ( opaqueHandle: opaqueHandle)
33+ let diagEngine = PluginDiagnosticsEngine ( cxxDiagnosticEngine: cxxDiagnosticEngine)
34+
3135 do {
3236 try plugin. initialize ( )
3337 return true
3438 } catch {
35- // Diagnostics are emitted in the caller.
36- // FIXME: Return what happened or emit diagnostics here.
39+ diagEngine? . diagnose (
40+ message: " compiler plugin not loaded: ' \( plugin. executableFilePath) ; failed to initialize " ,
41+ severity: . warning)
3742 return false
3843 }
3944}
@@ -56,12 +61,19 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
5661 cxxDiagnosticEngine: UnsafeMutablePointer < UInt8 > ?
5762) -> Bool {
5863 let plugin = CompilerPlugin ( opaqueHandle: opaqueHandle)
64+ let diagEngine = PluginDiagnosticsEngine ( cxxDiagnosticEngine: cxxDiagnosticEngine)
65+
66+ if plugin. capability? . features. contains ( . loadPluginLibrary) != true {
67+ // This happens only if invalid plugin server was passed to `-external-plugin-path`.
68+ diagEngine? . diagnose (
69+ message: " compiler plugin not loaded: ' \( libraryPath) ; invalid plugin server " ,
70+ severity: . warning)
71+ return false
72+ }
5973 assert ( plugin. capability? . features. contains ( . loadPluginLibrary) == true )
6074 let libraryPath = String ( cString: libraryPath)
6175 let moduleName = String ( cString: moduleName)
6276
63- let diagEngine = PluginDiagnosticsEngine ( cxxDiagnosticEngine: cxxDiagnosticEngine)
64-
6577 do {
6678 let result = try plugin. sendMessageAndWaitWithoutLock (
6779 . loadPluginLibrary( libraryPath: libraryPath, moduleName: moduleName)
@@ -72,7 +84,9 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
7284 diagEngine? . emit ( diagnostics) ;
7385 return loaded
7486 } catch {
75- diagEngine? . diagnose ( error: error)
87+ diagEngine? . diagnose (
88+ message: " compiler plugin not loaded: ' \( libraryPath) ; \( error) " ,
89+ severity: . warning)
7690 return false
7791 }
7892}
@@ -167,6 +181,10 @@ struct CompilerPlugin {
167181 }
168182 return nil
169183 }
184+
185+ var executableFilePath : String {
186+ return String ( cString: Plugin_getExecutableFilePath ( opaqueHandle) )
187+ }
170188}
171189
172190class PluginDiagnosticsEngine {
@@ -288,6 +306,10 @@ class PluginDiagnosticsEngine {
288306 )
289307 }
290308
309+ func diagnose( message: String , severity: PluginMessage . Diagnostic . Severity ) {
310+ self . emitSingle ( message: message, severity: severity, position: . invalid)
311+ }
312+
291313 /// Produce the C++ source location for a given position based on a
292314 /// syntax node.
293315 private func cxxSourceLocation(
0 commit comments