|
24 | 24 | using namespace mlir;
|
25 | 25 |
|
26 | 26 | //===----------------------------------------------------------------------===//
|
27 |
| -// Translation Parser |
| 27 | +// Diagnostic Filter |
| 28 | +//===----------------------------------------------------------------------===// |
| 29 | + |
| 30 | +namespace { |
| 31 | +/// A scoped diagnostic handler that marks non-error diagnostics as handled. As |
| 32 | +/// a result, the main diagnostic handler does not print non-error diagnostics. |
| 33 | +class ErrorDiagnosticFilter : public ScopedDiagnosticHandler { |
| 34 | +public: |
| 35 | + ErrorDiagnosticFilter(MLIRContext *ctx) : ScopedDiagnosticHandler(ctx) { |
| 36 | + setHandler([](Diagnostic &diag) { |
| 37 | + if (diag.getSeverity() != DiagnosticSeverity::Error) |
| 38 | + return success(); |
| 39 | + return failure(); |
| 40 | + }); |
| 41 | + } |
| 42 | +}; |
| 43 | +} // namespace |
| 44 | + |
| 45 | +//===----------------------------------------------------------------------===// |
| 46 | +// Translate Entry Point |
28 | 47 | //===----------------------------------------------------------------------===//
|
29 | 48 |
|
30 | 49 | LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
|
@@ -55,6 +74,12 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
|
55 | 74 | "expected-* lines on the corresponding line"),
|
56 | 75 | llvm::cl::init(false));
|
57 | 76 |
|
| 77 | + static llvm::cl::opt<bool> errorDiagnosticsOnly( |
| 78 | + "error-diagnostics-only", |
| 79 | + llvm::cl::desc("Filter all non-error diagnostics " |
| 80 | + "(discouraged: testing only!)"), |
| 81 | + llvm::cl::init(false)); |
| 82 | + |
58 | 83 | llvm::InitLLVM y(argc, argv);
|
59 | 84 |
|
60 | 85 | // Add flags for all the registered translations.
|
@@ -121,12 +146,17 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
|
121 | 146 |
|
122 | 147 | if (verifyDiagnostics) {
|
123 | 148 | // In the diagnostic verification flow, we ignore whether the
|
124 |
| - // translation failed (in most cases, it is expected to fail). |
125 |
| - // Instead, we check if the diagnostics were produced as expected. |
| 149 | + // translation failed (in most cases, it is expected to fail) and we do |
| 150 | + // not filter non-error diagnostics even if `errorDiagnosticsOnly` is |
| 151 | + // set. Instead, we check if the diagnostics were produced as expected. |
126 | 152 | SourceMgrDiagnosticVerifierHandler sourceMgrHandler(*sourceMgr,
|
127 | 153 | &context);
|
128 | 154 | (void)(*translationRequested)(sourceMgr, os, &context);
|
129 | 155 | result = sourceMgrHandler.verify();
|
| 156 | + } else if (errorDiagnosticsOnly) { |
| 157 | + SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context); |
| 158 | + ErrorDiagnosticFilter diagnosticFilter(&context); |
| 159 | + result = (*translationRequested)(sourceMgr, *stream, &context); |
130 | 160 | } else {
|
131 | 161 | SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
|
132 | 162 | result = (*translationRequested)(sourceMgr, *stream, &context);
|
|
0 commit comments