Skip to content

Commit d3f6b87

Browse files
[SwiftSyntax] Publicize DiagnosticEngine.addConsumer and add hasErrors property (#14996)
* [SwiftSyntax] Publicize DiagnosticEngine.addConsumer and add hasErrors property * [Syntax] Make DiagnosticEngine.hasErrors public
1 parent 11e3d16 commit d3f6b87

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

test/SwiftSyntax/DiagnosticTest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Diagnostics.test("DiagnosticEmission") {
3333
let fixLoc = loc()
3434

3535
let engine = DiagnosticEngine()
36+
expectFalse(engine.hasErrors)
3637

3738
engine.diagnose(.cannotConvert(fromType: "Int", toType: "Bool"),
3839
location: startLoc) {
@@ -42,10 +43,11 @@ Diagnostics.test("DiagnosticEmission") {
4243

4344
expectEqual(engine.diagnostics.count, 1)
4445
guard let diag = engine.diagnostics.first else { return }
45-
expectEqual(diag.message.text,
46+
expectEqual(diag.message.text,
4647
"cannot convert value of type 'Int' to 'Bool'")
4748
expectEqual(diag.message.severity, .error)
4849
expectEqual(diag.notes.count, 1)
50+
expectTrue(engine.hasErrors)
4951

5052
guard let note = diag.notes.first else { return }
5153
expectEqual(note.message.text, "check for explicit equality to '0'")

tools/SwiftSyntax/DiagnosticEngine.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class DiagnosticEngine {
2727
public private(set) var diagnostics = [Diagnostic]()
2828

2929
/// Adds the provided consumer to the consumers list.
30-
func addConsumer(_ consumer: DiagnosticConsumer) {
30+
public func addConsumer(_ consumer: DiagnosticConsumer) {
3131
consumers.append(consumer)
3232

3333
// Start the consumer with all previous diagnostics.
@@ -44,14 +44,19 @@ public class DiagnosticEngine {
4444
public func diagnose(_ message: Diagnostic.Message,
4545
location: SourceLocation? = nil,
4646
actions: ((inout Diagnostic.Builder) -> Void)? = nil) {
47-
let diagnostic = Diagnostic(message: message, location: location,
47+
let diagnostic = Diagnostic(message: message, location: location,
4848
actions: actions)
4949
diagnostics.append(diagnostic)
5050
for consumer in consumers {
5151
consumer.handle(diagnostic)
5252
}
5353
}
5454

55+
/// If any of the diagnostics in this engine have the `error` severity.
56+
public var hasErrors: Bool {
57+
return diagnostics.contains(where: { $0.message.severity == .error })
58+
}
59+
5560
/// Tells each consumer to finalize their diagnostic output.
5661
deinit {
5762
for consumer in consumers {

0 commit comments

Comments
 (0)