mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This patch provides an API and Serialization for clang-style diagnostics from within Swift. Libraries can use this API to pop their own custom diagnostics that can be serialized to JSON.
62 lines
2.1 KiB
Swift
62 lines
2.1 KiB
Swift
//===------------ DiagnosticEngine.swift - Diagnostic Engine --------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
// This file provides the DiagnosticEngine, the class used to register
|
|
// and output diagnostics.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Foundation
|
|
|
|
/// The DiagnosticEngine allows Swift tools to emit diagnostics.
|
|
public class DiagnosticEngine {
|
|
/// Creates a new DiagnosticEngine with no diagnostics.
|
|
public init() {
|
|
}
|
|
|
|
/// The list of consumers of the diagnostic passing through this engine.
|
|
internal var consumers = [DiagnosticConsumer]()
|
|
|
|
public private(set) var diagnostics = [Diagnostic]()
|
|
|
|
/// Adds the provided consumer to the consumers list.
|
|
func addConsumer(_ consumer: DiagnosticConsumer) {
|
|
consumers.append(consumer)
|
|
|
|
// Start the consumer with all previous diagnostics.
|
|
for diagnostic in diagnostics {
|
|
consumer.handle(diagnostic)
|
|
}
|
|
}
|
|
|
|
/// Registers a diagnostic with the diagnostic engine.
|
|
/// - parameters:
|
|
/// - message: The message for the diagnostic. This message includes
|
|
/// a severity and text that will be conveyed when the diagnostic
|
|
/// is serialized.
|
|
public func diagnose(_ message: Diagnostic.Message,
|
|
location: SourceLocation? = nil,
|
|
actions: ((inout Diagnostic.Builder) -> Void)? = nil) {
|
|
let diagnostic = Diagnostic(message: message, location: location,
|
|
actions: actions)
|
|
diagnostics.append(diagnostic)
|
|
for consumer in consumers {
|
|
consumer.handle(diagnostic)
|
|
}
|
|
}
|
|
|
|
/// Tells each consumer to finalize their diagnostic output.
|
|
deinit {
|
|
for consumer in consumers {
|
|
consumer.finalize()
|
|
}
|
|
}
|
|
}
|