//===- Diagnostics.h - MLIR Diagnostics -------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file defines utilities for emitting diagnostics. // //===----------------------------------------------------------------------===// #ifndef MLIR_IR_DIAGNOSTICS_H #define MLIR_IR_DIAGNOSTICS_H #include "mlir/IR/Location.h" #include <functional> #include <optional> namespace llvm { class MemoryBuffer; class SMLoc; class SourceMgr; } // namespace llvm namespace mlir { class DiagnosticEngine; class MLIRContext; class Operation; class OperationName; class OpPrintingFlags; class Type; class Value; namespace detail { struct DiagnosticEngineImpl; } // namespace detail /// Defines the different supported severity of a diagnostic. enum class DiagnosticSeverity { … }; //===----------------------------------------------------------------------===// // DiagnosticArgument //===----------------------------------------------------------------------===// /// A variant type that holds a single argument for a diagnostic. class DiagnosticArgument { … }; inline raw_ostream &operator<<(raw_ostream &os, const DiagnosticArgument &arg) { … } //===----------------------------------------------------------------------===// // Diagnostic //===----------------------------------------------------------------------===// /// This class contains all of the information necessary to report a diagnostic /// to the DiagnosticEngine. It should generally not be constructed directly, /// and instead used transitively via InFlightDiagnostic. class Diagnostic { … }; inline raw_ostream &operator<<(raw_ostream &os, const Diagnostic &diag) { … } //===----------------------------------------------------------------------===// // InFlightDiagnostic //===----------------------------------------------------------------------===// /// This class represents a diagnostic that is inflight and set to be reported. /// This allows for last minute modifications of the diagnostic before it is /// emitted by a DiagnosticEngine. class InFlightDiagnostic { … }; //===----------------------------------------------------------------------===// // DiagnosticEngine //===----------------------------------------------------------------------===// /// This class is the main interface for diagnostics. The DiagnosticEngine /// manages the registration of diagnostic handlers as well as the core API for /// diagnostic emission. This class should not be constructed directly, but /// instead interfaced with via an MLIRContext instance. class DiagnosticEngine { … }; /// Utility method to emit an error message using this location. InFlightDiagnostic emitError(Location loc); InFlightDiagnostic emitError(Location loc, const Twine &message); /// Utility method to emit a warning message using this location. InFlightDiagnostic emitWarning(Location loc); InFlightDiagnostic emitWarning(Location loc, const Twine &message); /// Utility method to emit a remark message using this location. InFlightDiagnostic emitRemark(Location loc); InFlightDiagnostic emitRemark(Location loc, const Twine &message); /// Overloads of the above emission functions that take an optionally null /// location. If the location is null, no diagnostic is emitted and a failure is /// returned. Given that the provided location may be null, these methods take /// the diagnostic arguments directly instead of relying on the returned /// InFlightDiagnostic. template <typename... Args> LogicalResult emitOptionalError(std::optional<Location> loc, Args &&...args) { … } template <typename... Args> LogicalResult emitOptionalWarning(std::optional<Location> loc, Args &&...args) { … } template <typename... Args> LogicalResult emitOptionalRemark(std::optional<Location> loc, Args &&...args) { … } //===----------------------------------------------------------------------===// // ScopedDiagnosticHandler //===----------------------------------------------------------------------===// /// This diagnostic handler is a simple RAII class that registers and erases a /// diagnostic handler on a given context. This class can be either be used /// directly, or in conjunction with a derived diagnostic handler. class ScopedDiagnosticHandler { … }; //===----------------------------------------------------------------------===// // SourceMgrDiagnosticHandler //===----------------------------------------------------------------------===// namespace detail { struct SourceMgrDiagnosticHandlerImpl; } // namespace detail /// This class is a utility diagnostic handler for use with llvm::SourceMgr. class SourceMgrDiagnosticHandler : public ScopedDiagnosticHandler { … }; //===----------------------------------------------------------------------===// // SourceMgrDiagnosticVerifierHandler //===----------------------------------------------------------------------===// namespace detail { struct SourceMgrDiagnosticVerifierHandlerImpl; } // namespace detail /// This class is a utility diagnostic handler for use with llvm::SourceMgr that /// verifies that emitted diagnostics match 'expected-*' lines on the /// corresponding line of the source file. class SourceMgrDiagnosticVerifierHandler : public SourceMgrDiagnosticHandler { … }; //===----------------------------------------------------------------------===// // ParallelDiagnosticHandler //===----------------------------------------------------------------------===// namespace detail { struct ParallelDiagnosticHandlerImpl; } // namespace detail /// This class is a utility diagnostic handler for use when multi-threading some /// part of the compiler where diagnostics may be emitted. This handler ensures /// a deterministic ordering to the emitted diagnostics that mirrors that of a /// single-threaded compilation. class ParallelDiagnosticHandler { … }; } // namespace mlir #endif