11#include "mlir/IR/BuiltinAttributes.h"
12#include "mlir/IR/Diagnostics.h"
13#include "mlir/IR/Location.h"
14#include "mlir/IR/MLIRContext.h"
22#include "llvm/ADT/Twine.h"
23#include "llvm/Support/ErrorHandling.h"
35 :
mlir::ScopedDiagnosticHandler(ctx), Diags(diags), SrcMgr(srcMgr),
37 setHandler([
this](mlir::Diagnostic &D) {
return handle(D); });
44 if (
auto file = mlir::dyn_cast<mlir::FileLineColLoc>(loc)) {
47 if (file.getLine() == 0 || file.getColumn() == 0)
55 if (
auto fused = mlir::dyn_cast<mlir::FusedLoc>(loc)) {
56 for (mlir::Location child : fused.getLocations()) {
61 return clang::SourceLocation();
63 if (
auto callsite = mlir::dyn_cast<mlir::CallSiteLoc>(loc))
64 return translateLoc(callsite.getCallee());
65 if (
auto named = mlir::dyn_cast<mlir::NameLoc>(loc))
66 return translateLoc(named.getChildLoc());
68 return clang::SourceLocation();
73static std::optional<mlir::FileLineColLoc>
75 if (
auto file = mlir::dyn_cast<mlir::FileLineColLoc>(loc)) {
76 if (file.getLine() == 0 || file.getColumn() == 0)
80 if (
auto fused = mlir::dyn_cast<mlir::FusedLoc>(loc)) {
81 for (mlir::Location child : fused.getLocations())
86 if (
auto callsite = mlir::dyn_cast<mlir::CallSiteLoc>(loc))
88 if (
auto named = mlir::dyn_cast<mlir::NameLoc>(loc))
93void CIRDiagnosticHandler::emit(mlir::Diagnostic &diag,
bool isNote) {
96 assert(diag.getSeverity() == mlir::DiagnosticSeverity::Note &&
97 "a note attached to a diagnostic must have Note severity");
98 diagID = clang::diag::note_cir_mlir_diagnostic;
100 switch (diag.getSeverity()) {
101 case mlir::DiagnosticSeverity::Error:
102 diagID = clang::diag::err_cir_mlir_diagnostic;
104 case mlir::DiagnosticSeverity::Warning:
105 diagID = clang::diag::warn_cir_mlir_diagnostic;
107 case mlir::DiagnosticSeverity::Remark:
108 diagID = clang::diag::remark_cir_mlir_diagnostic;
110 case mlir::DiagnosticSeverity::Note:
111 llvm_unreachable(
"a top-level MLIR diagnostic should not have Note "
112 "severity; notes arrive via getNotes()");
115 clang::SourceLocation loc = translateLoc(diag.getLocation());
116 std::string message = diag.str();
121 if (std::optional<mlir::FileLineColLoc> file =
123 message = (llvm::Twine(file->getFilename().getValue()) +
":" +
124 llvm::Twine(file->getLine()) +
":" +
125 llvm::Twine(file->getColumn()) +
": " + message)
127 Diags.Report(loc, diagID) << message;
130mlir::LogicalResult CIRDiagnosticHandler::handle(mlir::Diagnostic &diag) {
132 for (mlir::Diagnostic ¬e : diag.getNotes())
134 return mlir::success();
Defines the Diagnostic-related interfaces.
Defines the clang::FileManager interface and associated types.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
CIRDiagnosticHandler(mlir::MLIRContext *ctx, clang::DiagnosticsEngine &diags, clang::SourceManager &srcMgr, clang::FileManager &fileMgr)
Concrete class used by the front-end to report problems and issues.
Implements support for file system lookup, file system caching, and directory search management.
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true, bool IsText=true)
Get a FileEntryRef if it exists, without doing anything on error.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
static std::optional< mlir::FileLineColLoc > findFileLineColLoc(mlir::Location loc)