clang 24.0.0git
CIRDiagnosticHandler.cpp
Go to the documentation of this file.
1//===--- CIRDiagnosticHandler.cpp - Route MLIR diags to clang ---------===//
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
10
11#include "mlir/IR/BuiltinAttributes.h"
12#include "mlir/IR/Diagnostics.h"
13#include "mlir/IR/Location.h"
14#include "mlir/IR/MLIRContext.h"
15
21
22#include "llvm/Support/ErrorHandling.h"
23
24#include <cassert>
25
26namespace cir {
27
31 clang::FileManager &fileMgr)
32 : mlir::ScopedDiagnosticHandler(ctx), Diags(diags), SrcMgr(srcMgr),
33 FileMgr(fileMgr) {
34 setHandler([this](mlir::Diagnostic &D) { return handle(D); });
35}
36
37clang::SourceLocation CIRDiagnosticHandler::translateLoc(mlir::Location loc) {
38 // Walk common location wrappers to reach a usable file/line/column triple.
39 // Anything we can't translate becomes an invalid SourceLocation, which the
40 // diagnostics engine renders without a source line.
41 if (auto file = mlir::dyn_cast<mlir::FileLineColLoc>(loc)) {
42 // SourceManager::translateFileLineCol requires 1-based line/column.
43 // Module-level locations carry (0, 0); fall through unattached.
44 if (file.getLine() == 0 || file.getColumn() == 0)
45 return clang::SourceLocation();
46 auto fileRef = FileMgr.getOptionalFileRef(file.getFilename().getValue());
47 if (!fileRef)
48 return clang::SourceLocation();
49 return SrcMgr.translateFileLineCol(&fileRef->getFileEntry(), file.getLine(),
50 file.getColumn());
51 }
52 if (auto fused = mlir::dyn_cast<mlir::FusedLoc>(loc)) {
53 for (mlir::Location child : fused.getLocations()) {
54 clang::SourceLocation translated = translateLoc(child);
55 if (translated.isValid())
56 return translated;
57 }
58 return clang::SourceLocation();
59 }
60 if (auto callsite = mlir::dyn_cast<mlir::CallSiteLoc>(loc))
61 return translateLoc(callsite.getCallee());
62 if (auto named = mlir::dyn_cast<mlir::NameLoc>(loc))
63 return translateLoc(named.getChildLoc());
64 // OpaqueLoc / UnknownLoc and anything else fall through unattached.
65 return clang::SourceLocation();
66}
67
68void CIRDiagnosticHandler::emit(mlir::Diagnostic &diag, bool isNote) {
69 unsigned diagID;
70 if (isNote) {
71 assert(diag.getSeverity() == mlir::DiagnosticSeverity::Note &&
72 "a note attached to a diagnostic must have Note severity");
73 diagID = clang::diag::note_cir_mlir_diagnostic;
74 } else {
75 switch (diag.getSeverity()) {
76 case mlir::DiagnosticSeverity::Error:
77 diagID = clang::diag::err_cir_mlir_diagnostic;
78 break;
79 case mlir::DiagnosticSeverity::Warning:
80 diagID = clang::diag::warn_cir_mlir_diagnostic;
81 break;
82 case mlir::DiagnosticSeverity::Remark:
83 diagID = clang::diag::remark_cir_mlir_diagnostic;
84 break;
85 case mlir::DiagnosticSeverity::Note:
86 llvm_unreachable("a top-level MLIR diagnostic should not have Note "
87 "severity; notes arrive via getNotes()");
88 }
89 }
90 Diags.Report(translateLoc(diag.getLocation()), diagID) << diag.str();
91}
92
93mlir::LogicalResult CIRDiagnosticHandler::handle(mlir::Diagnostic &diag) {
94 emit(diag, /*isNote=*/false);
95 for (mlir::Diagnostic &note : diag.getNotes())
96 emit(note, /*isNote=*/true);
97 return mlir::success();
98}
99
100} // namespace cir
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.
Definition Diagnostic.h:234
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:52
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.