@@ -138,24 +138,40 @@ public struct DiagnosticEngine {
138138 diagnose ( id, args, at: position, highlight: highlight, fixIts: fixIts)
139139 }
140140
141- public func diagnose( _ diagnostic: Diagnostic ) {
142- diagnose ( diagnostic. id, diagnostic. arguments, at: diagnostic. position)
141+ public func diagnose< SourceLocation: ProvidingSourceLocation > ( _ diagnostic: Diagnostic < SourceLocation > ) {
142+ let loc = diagnostic. location. getSourceLocation ( diagnosticEngine: self )
143+ diagnose ( diagnostic. id, diagnostic. arguments, at: loc)
143144 }
145+
146+ /// Loads the file at `path` and returns a `SourceLoc` pointing to `line` and `column` in the file.
147+ /// Returns nil if the file cannot be loaded.
148+ public func getLocationFromExternalSource( path: StringRef , line: Int , column: Int ) -> SourceLoc ? {
149+ return SourceLoc ( bridged: bridged. getLocationFromExternalSource ( path: path. _bridged, line: line, column: column) )
150+ }
151+ }
152+
153+ /// Something which can provide a `SourceLoc` for diagnostics.
154+ public protocol ProvidingSourceLocation {
155+ func getSourceLocation( diagnosticEngine: DiagnosticEngine ) -> SourceLoc ?
156+ }
157+
158+ extension SourceLoc : ProvidingSourceLocation {
159+ public func getSourceLocation( diagnosticEngine: DiagnosticEngine ) -> SourceLoc ? { self }
144160}
145161
146162/// A utility struct which allows throwing a Diagnostic.
147- public struct Diagnostic : Error {
163+ public struct Diagnostic < SourceLocation : ProvidingSourceLocation > : Error {
148164 public let id : DiagID
149165 public let arguments : [ DiagnosticArgument ]
150- public let position : SourceLoc ?
166+ public let location : SourceLocation
151167
152- public init ( _ id: DiagID , _ arguments: DiagnosticArgument ... , at position : SourceLoc ? ) {
153- self . init ( id, arguments, at: position )
168+ public init ( _ id: DiagID , _ arguments: DiagnosticArgument ... , at location : SourceLocation ) {
169+ self . init ( id, arguments, at: location )
154170 }
155171
156- public init ( _ id: DiagID , _ arguments: [ DiagnosticArgument ] , at position : SourceLoc ? ) {
172+ public init ( _ id: DiagID , _ arguments: [ DiagnosticArgument ] , at location : SourceLocation ) {
157173 self . id = id
158174 self . arguments = arguments
159- self . position = position
175+ self . location = location
160176 }
161177}
0 commit comments