| 1 | //===--- CIRDiagnosticHandler.h - Route MLIR diags to clang ----*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef CLANG_LIB_CIR_FRONTENDACTION_CIRDIAGNOSTICHANDLER_H |
| 10 | #define CLANG_LIB_CIR_FRONTENDACTION_CIRDIAGNOSTICHANDLER_H |
| 11 | |
| 12 | #include "mlir/IR/Diagnostics.h" |
| 13 | |
| 14 | namespace clang { |
| 15 | class DiagnosticsEngine; |
| 16 | class FileManager; |
| 17 | class SourceLocation; |
| 18 | class SourceManager; |
| 19 | } // namespace clang |
| 20 | |
| 21 | namespace mlir { |
| 22 | class Location; |
| 23 | class MLIRContext; |
| 24 | } // namespace mlir |
| 25 | |
| 26 | namespace cir { |
| 27 | |
| 28 | /// Routes MLIR-side diagnostics emitted during CIR passes, the verifier, and |
| 29 | /// CIR-to-LLVM lowering through the surrounding clang::DiagnosticsEngine. |
| 30 | /// |
| 31 | /// MLIR locations carrying file/line/column info are translated to a |
| 32 | /// clang::SourceLocation via the active SourceManager + FileManager so errors |
| 33 | /// surface in the user's preferred format. Locations that cannot be translated |
| 34 | /// fall back to an invalid clang::SourceLocation. |
| 35 | class CIRDiagnosticHandler : public mlir::ScopedDiagnosticHandler { |
| 36 | public: |
| 37 | CIRDiagnosticHandler(mlir::MLIRContext *ctx, clang::DiagnosticsEngine &diags, |
| 38 | clang::SourceManager &srcMgr, |
| 39 | clang::FileManager &fileMgr); |
| 40 | |
| 41 | private: |
| 42 | mlir::LogicalResult handle(mlir::Diagnostic &diag); |
| 43 | void emit(mlir::Diagnostic &diag, bool isNote); |
| 44 | clang::SourceLocation translateLoc(mlir::Location loc); |
| 45 | |
| 46 | clang::DiagnosticsEngine &Diags; |
| 47 | clang::SourceManager &SrcMgr; |
| 48 | clang::FileManager &FileMgr; |
| 49 | }; |
| 50 | |
| 51 | } // namespace cir |
| 52 | |
| 53 | #endif // CLANG_LIB_CIR_FRONTENDACTION_CIRDIAGNOSTICHANDLER_H |
| 54 | |