Skip to content

Remove DiagnosticEngine #325

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
Oct 8, 2021
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Note: This is in reverse chronological order, so newer entries are added to the
* The `SwiftSyntaxParser` type and a few related types now live in their own module (also named `SwiftSyntaxParser`). This allows using `SwiftSyntax` for code generation purposes without having a compatible `_InternalSwiftSyntaxParser.dylib` around.

`import SwiftSyntaxParser` where necessary.
* `DiagnosticEngine` has been removed. Instead, `SyntaxParser` takes a closure through which it emits parser diagnostics. Depending on your needs, use the closure to handle the diagnostics or write + hook up your own diagnostics engine.

## Swift 5.3

Expand Down
33 changes: 0 additions & 33 deletions Sources/SwiftSyntax/DiagnosticConsumer.swift

This file was deleted.

102 changes: 0 additions & 102 deletions Sources/SwiftSyntax/DiagnosticEngine.swift

This file was deleted.

79 changes: 0 additions & 79 deletions Sources/SwiftSyntax/JSONDiagnosticConsumer.swift

This file was deleted.

59 changes: 0 additions & 59 deletions Sources/SwiftSyntax/PrintingDiagnosticConsumer.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// This file provides the Diagnostic, Note, and FixIt types.
//===----------------------------------------------------------------------===//

import SwiftSyntax

/// A FixIt represents a change to source code in order to "correct" a
/// diagnostic.
public enum FixIt: Codable, CustomDebugStringConvertible {
Expand Down Expand Up @@ -117,7 +119,7 @@ public struct Note: Codable {
}

/// Converts this Note to a Diagnostic for serialization.
func asDiagnostic() -> Diagnostic {
public func asDiagnostic() -> Diagnostic {
return Diagnostic(message: message, location: location, notes: [],
highlights: highlights, fixIts: fixIts)
}
Expand Down
31 changes: 14 additions & 17 deletions Sources/SwiftSyntaxParser/SyntaxParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ public enum SyntaxParser {
/// - source: The source string to parse.
/// - parseTransition: Optional mechanism for incremental re-parsing.
/// - filenameForDiagnostics: Optional file name used for SourceLocation.
/// - diagnosticEngine: Optional diagnotic engine to where the parser will
/// emit diagnostics
/// - diagnosticHandler: Optional callback that will be called for all
/// diagnostics the parser emits
/// - Returns: A top-level Syntax node representing the contents of the tree,
/// if the parse was successful.
/// - Throws: `ParserError`
public static func parse(
source: String,
parseTransition: IncrementalParseTransition? = nil,
filenameForDiagnostics: String = "",
diagnosticEngine: DiagnosticEngine? = nil
diagnosticHandler: ((Diagnostic) -> Void)? = nil
) throws -> SourceFileSyntax {
guard nodeHashVerifyResult && cnodeLayoutHashVerifyResult else {
throw ParserError.parserCompatibilityCheckFailed
Expand All @@ -79,7 +79,7 @@ public enum SyntaxParser {
utf8Source.makeContiguousUTF8()

let rawSyntax = parseRaw(utf8Source, parseTransition, filenameForDiagnostics,
diagnosticEngine)
diagnosticHandler)

let base = _SyntaxParserInterop.nodeFromRetainedOpaqueRawSyntax(rawSyntax)
guard let file = base.as(SourceFileSyntax.self) else {
Expand All @@ -92,27 +92,27 @@ public enum SyntaxParser {
///
/// - Parameters:
/// - url: The file URL to parse.
/// - diagnosticEngine: Optional diagnotic engine to where the parser will
/// emit diagnostics
/// - diagnosticHandler: Optional callback that will be called for all
/// diagnostics the parser emits
/// - Returns: A top-level Syntax node representing the contents of the tree,
/// if the parse was successful.
/// - Throws: `ParserError`
public static func parse(_ url: URL,
diagnosticEngine: DiagnosticEngine? = nil) throws -> SourceFileSyntax {
diagnosticHandler: ((Diagnostic) -> Void)? = nil) throws -> SourceFileSyntax {
// Avoid using `String(contentsOf:)` because it creates a wrapped NSString.
let fileData = try Data(contentsOf: url)
let source = fileData.withUnsafeBytes { buf in
return String(decoding: buf.bindMemory(to: UInt8.self), as: UTF8.self)
}
return try parse(source: source, filenameForDiagnostics: url.path,
diagnosticEngine: diagnosticEngine)
diagnosticHandler: diagnosticHandler)
}

private static func parseRaw(
_ source: String,
_ parseTransition: IncrementalParseTransition?,
_ filenameForDiagnostics: String,
_ diagnosticEngine: DiagnosticEngine?
_ diagnosticHandler: ((Diagnostic) -> Void)?
) -> CClientNode {
precondition(source.isContiguousUTF8)
let c_parser = swiftparse_parser_create()
Expand Down Expand Up @@ -144,18 +144,15 @@ public enum SyntaxParser {
var pendingNotes: [Note] = []
defer {
// Consume the pending diagnostic if present
if let diagnosticEngine = diagnosticEngine {
if let diagnosticHandler = diagnosticHandler {
if let pendingDiag = pendingDiag {
diagnosticEngine.diagnose(Diagnostic(pendingDiag, pendingNotes))
diagnosticHandler(Diagnostic(pendingDiag, pendingNotes))
}
}
}
// Set up diagnostics consumer if requested by the caller.
if let diagnosticEngine = diagnosticEngine {
// If requested, we should set up a source location converter to calculate
// line and column.
let converter = diagnosticEngine.needsLineColumn ?
SourceLocationConverter(file: filenameForDiagnostics, source: source) : nil
if let diagnosticHandler = diagnosticHandler {
let converter = SourceLocationConverter(file: filenameForDiagnostics, source: source)
let diagHandler = { (diag: CDiagnostic!) in
// If the coming diagnostic is a note, we cache the pending note
if swiftparse_diagnostic_get_severity(diag) ==
Expand All @@ -164,7 +161,7 @@ public enum SyntaxParser {
} else {
// Cosume the pending diagnostic
if let pendingDiag = pendingDiag {
diagnosticEngine.diagnose(Diagnostic(pendingDiag, pendingNotes))
diagnosticHandler(Diagnostic(pendingDiag, pendingNotes))
// Clear pending notes
pendingNotes = []
}
Expand Down
Loading