Skip to content

[Diagnostics] Support "swift" style diagnostics at EOF #77755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ public func addQueuedDiagnostic(
text: UnsafePointer<UInt8>,
textLength: Int,
severity: BridgedDiagnosticSeverity,
position: BridgedSourceLoc,
cLoc: BridgedSourceLoc,
highlightRangesPtr: UnsafePointer<BridgedSourceLoc>?,
numHighlightRanges: Int
) {
let queuedDiagnostics = queuedDiagnosticsPtr.assumingMemoryBound(
to: QueuedDiagnostics.self
)

guard let rawPosition = position.getOpaquePointerValue() else {
guard let rawPosition = cLoc.getOpaquePointerValue() else {
return
}

Expand All @@ -280,18 +280,33 @@ public func addQueuedDiagnostic(
return false
}

return rawPosition >= baseAddress && rawPosition < baseAddress + sf.buffer.count
return rawPosition >= baseAddress && rawPosition <= baseAddress + sf.buffer.count
}
guard let sourceFile = sourceFile else {
// FIXME: Hard to report an error here...
return
}

// Find the token at that offset.
let sourceFileBaseAddress = UnsafeRawPointer(sourceFile.buffer.baseAddress!)
let sourceFileEndAddress = sourceFileBaseAddress + sourceFile.buffer.count
let offset = rawPosition - sourceFileBaseAddress
guard let token = sourceFile.syntax.token(at: AbsolutePosition(utf8Offset: offset)) else {
let position = AbsolutePosition(utf8Offset: offset)

// Find the token at that offset.
let node: Syntax
if let token = sourceFile.syntax.token(at: position) {
node = Syntax(token)
} else if position == sourceFile.syntax.endPosition {
// FIXME: EOF token is not included in '.token(at: position)'
// We might want to include it, but want to avoid special handling.
// Also 'sourceFile.syntax' is not guaranteed to be 'SourceFileSyntax'.
if let token = sourceFile.syntax.lastToken(viewMode: .all) {
node = Syntax(token)
} else {
node = sourceFile.syntax
}
} else {
// position out of range.
return
}

Expand Down Expand Up @@ -346,7 +361,8 @@ public func addQueuedDiagnostic(

let textBuffer = UnsafeBufferPointer(start: text, count: textLength)
let diagnostic = Diagnostic(
node: Syntax(token),
node: node,
position: position,
message: SimpleDiagnostic(
message: String(decoding: textBuffer, as: UTF8.self),
severity: severity.asSeverity
Expand Down
5 changes: 3 additions & 2 deletions test/NameLookup/nonempty-brace-in-brace.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// RUN: not %target-swift-frontend -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK-NO-ASSERTION

// CHECK-NO-ASSERTION-NOT: Assertion
// Used to trip an assertion


public struct Foo {
func bar() {
var copySelf = self
Expand All @@ -14,5 +16,4 @@ private extension String {}




// CHECK-NO-ASSERTION-NOT: Assertion
// EOF
13 changes: 13 additions & 0 deletions test/diagnostics/pretty-printed-diagnostics-eof.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: not %target-swift-frontend -diagnostic-style=swift -typecheck %/s 2>&1 | %FileCheck %s

// REQUIRES: swift_swift_parser

// CHECK: {{SOURCE_DIR[/\]test[/\]diagnostics[/\]pretty-printed-diagnostics-eof\.swift}}:[[#LINE:]]:1: error: expected '}' in struct
// CHECK: [[#LINE-2]] | struct MyStruct {
// CHECK-NEXT: | `- note: to match this opening '{'
// CHECK-NEXT: [[#LINE-1]] | func foo() {}
// CHECK-NEXT: [[#LINE]] |
// CHECK-NEXT: | `- error: expected '}' in struct

struct MyStruct {
func foo() {}