diff --git a/compiler/src/main.cpp b/compiler/src/main.cpp index 3da4102da..3a338aa88 100644 --- a/compiler/src/main.cpp +++ b/compiler/src/main.cpp @@ -24,6 +24,12 @@ llvm::cl::opt output("o", llvm::cl::value_desc("filename"), llvm::cl::init("-")); +llvm::cl::opt verifyDiagnostics( + "verify-diagnostics", + llvm::cl::desc("Check that emitted diagnostics match " + "expected-* lines on the corresponding line"), + llvm::cl::init(false)); + llvm::cl::opt splitInputFile( "split-input-file", llvm::cl::desc("Split the input file into pieces and process each " @@ -32,15 +38,28 @@ llvm::cl::opt splitInputFile( }; // namespace cmdline // Process a single source buffer +// +// If `verifyDiagnostics` is `true`, the procedure only checks if the +// diagnostic messages provided in the source buffer using +// `expected-error` are produced. +// +// If `verifyDiagnostics` is `false`, the procedure checks if the +// parsed module is valid. mlir::LogicalResult processInputBuffer(mlir::MLIRContext &context, std::unique_ptr buffer, - llvm::raw_ostream &os) { + llvm::raw_ostream &os, bool verifyDiagnostics) { llvm::SourceMgr sourceMgr; sourceMgr.AddNewSourceBuffer(std::move(buffer), llvm::SMLoc()); + mlir::SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr, + &context); + auto module = mlir::parseSourceFile(sourceMgr, &context); + if (verifyDiagnostics) + return sourceMgrHandler.verify(); + if (!module) return mlir::failure(); @@ -65,6 +84,9 @@ mlir::LogicalResult compilerMain(int argc, char **argv) { context.getOrLoadDialect(); context.getOrLoadDialect(); + if (cmdline::verifyDiagnostics) + context.printOpOnDiagnostic(false); + auto output = mlir::openOutputFile(cmdline::output, &errorMessage); if (!output) { @@ -90,12 +112,14 @@ mlir::LogicalResult compilerMain(int argc, char **argv) { std::move(file), [&](std::unique_ptr inputBuffer, llvm::raw_ostream &os) { - return processInputBuffer(context, std::move(inputBuffer), os); + return processInputBuffer(context, std::move(inputBuffer), os, + cmdline::verifyDiagnostics); }, output->os()))) return mlir::failure(); } else { - return processInputBuffer(context, std::move(file), output->os()); + return processInputBuffer(context, std::move(file), output->os(), + cmdline::verifyDiagnostics); } }