Skip to content

Commit 9bd340c

Browse files
committed
Diagnostic: use DiagnosticEngine instead of DiagnosticConsumer in the parser API
1 parent c98e865 commit 9bd340c

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

Sources/SwiftSyntax/DiagnosticConsumer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public protocol DiagnosticConsumer {
1818

1919
/// Whether the collected diagnostics should calculate line:column pair; true
2020
/// by default.
21-
var calculateLineColumn: Bool { get }
21+
var needsLineColumn: Bool { get }
2222

2323
/// Handle the provided diagnostic which has just been registered with the
2424
/// DiagnosticEngine.
@@ -29,5 +29,5 @@ public protocol DiagnosticConsumer {
2929
}
3030

3131
public extension DiagnosticConsumer {
32-
var calculateLineColumn: Bool { return true }
32+
var needsLineColumn: Bool { return true }
3333
}

Sources/SwiftSyntax/DiagnosticEngine.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class DiagnosticEngine {
2626

2727
public private(set) var diagnostics = [Diagnostic]()
2828

29+
internal var needsLineColumn: Bool {
30+
// Check if any consumer is interested in line and column.
31+
return consumers.first { $0.needsLineColumn } != nil
32+
}
33+
2934
/// Adds the provided consumer to the consumers list.
3035
public func addConsumer(_ consumer: DiagnosticConsumer) {
3136
consumers.append(consumer)
@@ -36,6 +41,13 @@ public class DiagnosticEngine {
3641
}
3742
}
3843

44+
internal func diagnose(_ diagnostic: Diagnostic) {
45+
diagnostics.append(diagnostic)
46+
for consumer in consumers {
47+
consumer.handle(diagnostic)
48+
}
49+
}
50+
3951
/// Registers a diagnostic with the diagnostic engine.
4052
/// - parameters:
4153
/// - message: The message for the diagnostic. This message includes
@@ -44,12 +56,7 @@ public class DiagnosticEngine {
4456
public func diagnose(_ message: Diagnostic.Message,
4557
location: SourceLocation? = nil,
4658
actions: ((inout Diagnostic.Builder) -> Void)? = nil) {
47-
let diagnostic = Diagnostic(message: message, location: location,
48-
actions: actions)
49-
diagnostics.append(diagnostic)
50-
for consumer in consumers {
51-
consumer.handle(diagnostic)
52-
}
59+
diagnose(Diagnostic(message: message, location: location, actions: actions))
5360
}
5461

5562
/// If any of the diagnostics in this engine have the `error` severity.

Sources/SwiftSyntax/SyntaxParser.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public enum SyntaxParser {
7171
source: String,
7272
file: String = "",
7373
parseTransition: IncrementalParseTransition? = nil,
74-
diagConsumer: DiagnosticConsumer? = nil
74+
diagnosticEngine: DiagnosticEngine? = nil
7575
) throws -> SourceFileSyntax {
7676
guard nodeHashVerifyResult else {
7777
throw ParserError.parserCompatibilityCheckFailed
@@ -82,7 +82,7 @@ public enum SyntaxParser {
8282
var utf8Source = source
8383
utf8Source.makeNativeUTF8IfNeeded()
8484

85-
let rawSyntax = parseRaw(file, utf8Source, parseTransition, diagConsumer)
85+
let rawSyntax = parseRaw(file, utf8Source, parseTransition, diagnosticEngine)
8686

8787
guard let file = makeSyntax(.forRoot(rawSyntax)) as? SourceFileSyntax else {
8888
throw ParserError.invalidSyntaxData
@@ -98,22 +98,22 @@ public enum SyntaxParser {
9898
/// if the parse was successful.
9999
/// - Throws: `ParserError`
100100
public static func parse(_ url: URL,
101-
diagConsumer: DiagnosticConsumer? = nil) throws -> SourceFileSyntax {
101+
diagnosticEngine: DiagnosticEngine? = nil) throws -> SourceFileSyntax {
102102
// Avoid using `String(contentsOf:)` because it creates a wrapped NSString.
103103
var fileData = try Data(contentsOf: url)
104104
fileData.append(0) // null terminate.
105105
let source = fileData.withUnsafeBytes { (ptr: UnsafePointer<CChar>) in
106106
return String(cString: ptr)
107107
}
108108
return try parse(source: source, file: url.absoluteString,
109-
diagConsumer: diagConsumer)
109+
diagnosticEngine: diagnosticEngine)
110110
}
111111

112112
private static func parseRaw(
113113
_ file: String,
114114
_ source: String,
115115
_ parseTransition: IncrementalParseTransition?,
116-
_ diagConsumer: DiagnosticConsumer?
116+
_ diagnosticEngine: DiagnosticEngine?
117117
) -> RawSyntax {
118118
assert(source.isNativeUTF8)
119119
let c_parser = swiftparse_parser_create()
@@ -148,17 +148,17 @@ public enum SyntaxParser {
148148
var pendingNotes: [Note] = []
149149
defer {
150150
// Consume the pending diagnostic if present
151-
if let diagConsumer = diagConsumer {
151+
if let diagnosticEngine = diagnosticEngine {
152152
if let pendingDiag = pendingDiag {
153-
diagConsumer.handle(Diagnostic(pendingDiag, pendingNotes))
153+
diagnosticEngine.diagnose(Diagnostic(pendingDiag, pendingNotes))
154154
}
155155
}
156156
}
157157
// Set up diagnostics consumer if requested by the caller.
158-
if let diagConsumer = diagConsumer {
158+
if let diagnosticEngine = diagnosticEngine {
159159
// If requested, we should set up a source location converter to calculate
160160
// line and column.
161-
let converter = diagConsumer.calculateLineColumn ?
161+
let converter = diagnosticEngine.needsLineColumn ?
162162
SourceLocationConverter(file: file, source: source) : nil
163163
let diagHandler = { (diag: CDiagnostic!) in
164164
// If the coming diagnostic is a note, we cache the pending note
@@ -168,7 +168,7 @@ public enum SyntaxParser {
168168
} else {
169169
// Cosume the pending diagnostic
170170
if let pendingDiag = pendingDiag {
171-
diagConsumer.handle(Diagnostic(pendingDiag, pendingNotes))
171+
diagnosticEngine.diagnose(Diagnostic(pendingDiag, pendingNotes))
172172
// Clear pending notes
173173
pendingNotes = []
174174
}
@@ -239,7 +239,7 @@ extension Note {
239239
extension Diagnostic {
240240
init(diag: CDiagnostic, using converter: SourceLocationConverter?) {
241241
// Collect highlighted ranges
242-
let hightlights = (0..<swiftparse_diagnostic_get_range_count(diag)).map {
242+
let highlights = (0..<swiftparse_diagnostic_get_range_count(diag)).map {
243243
return SourceRange(swiftparse_diagnostic_get_range(diag, $0), converter)
244244
}
245245
// Collect fixits
@@ -249,7 +249,7 @@ extension Diagnostic {
249249
self.init(message: Diagnostic.Message(diag),
250250
location: SourceLocation(offset: Int(swiftparse_diagnostic_get_source_loc(diag)),
251251
converter: converter),
252-
notes: [], highlights: hightlights, fixIts: fixits)
252+
notes: [], highlights: highlights, fixIts: fixits)
253253
}
254254

255255
init(_ diag: Diagnostic, _ notes: [Note]) {

Sources/lit-test-helper/main.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,9 @@ func printParserDiags(args: CommandLineArguments) throws {
415415
print("\(counter.error) error(s) \(counter.warning) warnings(s) \(counter.note) note(s)")
416416
}
417417
}
418-
_ = try SyntaxParser.parse(treeURL, diagConsumer: ParserDiagPrinter())
418+
let diagEngine = DiagnosticEngine()
419+
diagEngine.addConsumer(ParserDiagPrinter())
420+
_ = try SyntaxParser.parse(treeURL, diagnosticEngine: diagEngine)
419421
}
420422

421423
do {

0 commit comments

Comments
 (0)