Skip to content

Commit 015830e

Browse files
authored
Merge pull request #77755 from rintaro/diags-swiftstyle-eof
[Diagnostics] Support "swift" style diagnostics at EOF
2 parents 5822bc3 + 40c8d81 commit 015830e

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/NameLookup/nonempty-brace-in-brace.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: not %target-swift-frontend -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK-NO-ASSERTION
22

3+
// CHECK-NO-ASSERTION-NOT: Assertion
34
// Used to trip an assertion
45

6+
57
public struct Foo {
68
func bar() {
79
var copySelf = self
@@ -14,5 +16,4 @@ private extension String {}
1416

1517

1618

17-
18-
// CHECK-NO-ASSERTION-NOT: Assertion
19+
// EOF
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: not %target-swift-frontend -diagnostic-style=swift -typecheck %/s 2>&1 | %FileCheck %s
2+
3+
// REQUIRES: swift_swift_parser
4+
5+
// CHECK: {{SOURCE_DIR[/\]test[/\]diagnostics[/\]pretty-printed-diagnostics-eof\.swift}}:[[#LINE:]]:1: error: expected '}' in struct
6+
// CHECK: [[#LINE-2]] | struct MyStruct {
7+
// CHECK-NEXT: | `- note: to match this opening '{'
8+
// CHECK-NEXT: [[#LINE-1]] | func foo() {}
9+
// CHECK-NEXT: [[#LINE]] |
10+
// CHECK-NEXT: | `- error: expected '}' in struct
11+
12+
struct MyStruct {
13+
func foo() {}

0 commit comments

Comments
 (0)