From 5999d402eceb906cddd38716c03124908d50ad42 Mon Sep 17 00:00:00 2001 From: Andi Drebes Date: Wed, 3 Nov 2021 12:05:12 +0100 Subject: [PATCH] enhance(compiler): CompilerEngine: Forward parse errors instead of printing them By default, `mlir::SourceMgrDiagnosticVerifierHandler` used by `CompilerEngine::compile()` prints parse errors to `llvm::errs()`. This makes it impossible for a caller of `CompilerEngine::compile()` to process parse errors or to suppress the emission of error messages to the standard error stream altogether. This change captures parse errors in a string via a string-backed output stream and forwards the error message in the `llvm::Error` instance of the return value. --- compiler/lib/Support/CompilerEngine.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/compiler/lib/Support/CompilerEngine.cpp b/compiler/lib/Support/CompilerEngine.cpp index 2cfa84cd4..84142c228 100644 --- a/compiler/lib/Support/CompilerEngine.cpp +++ b/compiler/lib/Support/CompilerEngine.cpp @@ -150,11 +150,15 @@ llvm::Error CompilerEngine::determineFHEParameters(CompilationResult &res) { // on the target dialect. llvm::Expected CompilerEngine::compile(llvm::SourceMgr &sm, Target target) { + std::string diagnosticsMsg; + llvm::raw_string_ostream diagnosticsOS(diagnosticsMsg); + CompilationResult res(this->compilationContext); mlir::MLIRContext &mlirContext = *this->compilationContext->getMLIRContext(); - mlir::SourceMgrDiagnosticVerifierHandler smHandler(sm, &mlirContext); + mlir::SourceMgrDiagnosticVerifierHandler smHandler(sm, &mlirContext, + diagnosticsOS); mlirContext.printOpOnDiagnostic(false); mlir::OwningModuleRef mlirModuleRef = @@ -167,8 +171,11 @@ CompilerEngine::compile(llvm::SourceMgr &sm, Target target) { return res; } - if (!mlirModuleRef) - return StreamStringError("Could not parse source"); + if (!mlirModuleRef) { + diagnosticsOS.flush(); + return StreamStringError("Could not parse source") + << (diagnosticsMsg.empty() ? "" : ": ") << diagnosticsMsg; + } res.mlirModuleRef = std::move(mlirModuleRef); mlir::ModuleOp module = res.mlirModuleRef->get();