Continue revising diagnostic handling to simplify and generalize it, and improve related infra.
- Add a new -verify mode to the mlir-opt tool that allows writing test cases
for optimization and other passes that produce diagnostics.
- Refactor existing the -check-parser-errors flag to mlir-opt into a new
-split-input-file option which is orthogonal to -verify.
- Eliminate the special error hook the parser maintained and use the standard
MLIRContext's one instead.
- Enhance the default MLIRContext error reporter to print file/line/col of
errors when it is available.
- Add new createChecked() methods to the builder that create ops and invoke
the verify hook on them, use this to detected unhandled code in the
RaiseControlFlow pass.
- Teach mlir-opt about expected-error @+, it previously only worked with @-
PiperOrigin-RevId: 211305770
diff --git a/lib/Parser/Lexer.cpp b/lib/Parser/Lexer.cpp
index 043acd7..b4f8e1d 100644
--- a/lib/Parser/Lexer.cpp
+++ b/lib/Parser/Lexer.cpp
@@ -20,8 +20,9 @@
//===----------------------------------------------------------------------===//
#include "Lexer.h"
+#include "mlir/IR/Location.h"
+#include "mlir/IR/MLIRContext.h"
#include "llvm/Support/SourceMgr.h"
-#include <cctype>
using namespace mlir;
using llvm::SMLoc;
using llvm::SourceMgr;
@@ -32,17 +33,30 @@
return c == '$' || c == '.' || c == '_' || c == '-';
}
-Lexer::Lexer(llvm::SourceMgr &sourceMgr, SMDiagnosticHandlerTy errorReporter)
- : sourceMgr(sourceMgr), errorReporter(errorReporter) {
+Lexer::Lexer(llvm::SourceMgr &sourceMgr, MLIRContext *context)
+ : sourceMgr(sourceMgr), context(context) {
auto bufferID = sourceMgr.getMainFileID();
curBuffer = sourceMgr.getMemoryBuffer(bufferID)->getBuffer();
curPtr = curBuffer.begin();
}
+/// Encode the specified source location information into an attribute for
+/// attachment to the IR.
+Location *Lexer::getEncodedSourceLocation(llvm::SMLoc loc) {
+ auto &sourceMgr = getSourceMgr();
+ unsigned mainFileID = sourceMgr.getMainFileID();
+ auto lineAndColumn = sourceMgr.getLineAndColumn(loc, mainFileID);
+ auto *buffer = sourceMgr.getMemoryBuffer(mainFileID);
+ auto filename = UniquedFilename::get(buffer->getBufferIdentifier(), context);
+
+ return FileLineColLoc::get(filename, lineAndColumn.first,
+ lineAndColumn.second, context);
+}
+
/// emitError - Emit an error message and return an Token::error token.
Token Lexer::emitError(const char *loc, const Twine &message) {
- errorReporter(sourceMgr.GetMessage(SMLoc::getFromPointer(loc),
- SourceMgr::DK_Error, message));
+ context->emitDiagnostic(getEncodedSourceLocation(SMLoc::getFromPointer(loc)),
+ message, MLIRContext::DiagnosticKind::Error);
return formToken(Token::error, loc);
}