@@ -262,15 +262,15 @@ public func addQueuedDiagnostic(
262262 text: UnsafePointer < UInt8 > ,
263263 textLength: Int ,
264264 severity: BridgedDiagnosticSeverity ,
265- position : BridgedSourceLoc ,
265+ cLoc : BridgedSourceLoc ,
266266 highlightRangesPtr: UnsafePointer < BridgedSourceLoc > ? ,
267267 numHighlightRanges: Int
268268) {
269269 let queuedDiagnostics = queuedDiagnosticsPtr. assumingMemoryBound (
270270 to: QueuedDiagnostics . self
271271 )
272272
273- guard let rawPosition = position . getOpaquePointerValue ( ) else {
273+ guard let rawPosition = cLoc . getOpaquePointerValue ( ) else {
274274 return
275275 }
276276
@@ -280,18 +280,33 @@ public func addQueuedDiagnostic(
280280 return false
281281 }
282282
283- return rawPosition >= baseAddress && rawPosition < baseAddress + sf. buffer. count
283+ return rawPosition >= baseAddress && rawPosition <= baseAddress + sf. buffer. count
284284 }
285285 guard let sourceFile = sourceFile else {
286286 // FIXME: Hard to report an error here...
287287 return
288288 }
289289
290- // Find the token at that offset.
291290 let sourceFileBaseAddress = UnsafeRawPointer ( sourceFile. buffer. baseAddress!)
292291 let sourceFileEndAddress = sourceFileBaseAddress + sourceFile. buffer. count
293292 let offset = rawPosition - sourceFileBaseAddress
294- guard let token = sourceFile. syntax. token ( at: AbsolutePosition ( utf8Offset: offset) ) else {
293+ let position = AbsolutePosition ( utf8Offset: offset)
294+
295+ // Find the token at that offset.
296+ let node : Syntax
297+ if let token = sourceFile. syntax. token ( at: position) {
298+ node = Syntax ( token)
299+ } else if position == sourceFile. syntax. endPosition {
300+ // FIXME: EOF token is not included in '.token(at: position)'
301+ // We might want to include it, but want to avoid special handling.
302+ // Also 'sourceFile.syntax' is not guaranteed to be 'SourceFileSyntax'.
303+ if let token = sourceFile. syntax. lastToken ( viewMode: . all) {
304+ node = Syntax ( token)
305+ } else {
306+ node = sourceFile. syntax
307+ }
308+ } else {
309+ // position out of range.
295310 return
296311 }
297312
@@ -346,7 +361,8 @@ public func addQueuedDiagnostic(
346361
347362 let textBuffer = UnsafeBufferPointer ( start: text, count: textLength)
348363 let diagnostic = Diagnostic (
349- node: Syntax ( token) ,
364+ node: node,
365+ position: position,
350366 message: SimpleDiagnostic (
351367 message: String ( decoding: textBuffer, as: UTF8 . self) ,
352368 severity: severity. asSeverity
0 commit comments