Skip to content

Commit fafc0ee

Browse files
committed
simplify processBuffer code to please MSVC
1 parent c6b1fcd commit fafc0ee

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

mlir/tools/mlir-irdl-to-cpp/mlir-irdl-to-cpp.cpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
#include "llvm/Support/ToolOutputFile.h"
2828

2929
using namespace mlir;
30-
/// Parses the memory buffer. If successfully, run a series of passes against
31-
/// it and print the result.
30+
3231
static LogicalResult
3332
processBuffer(llvm::raw_ostream &os,
3433
std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
@@ -43,40 +42,32 @@ processBuffer(llvm::raw_ostream &os,
4342

4443
ctx.printOpOnDiagnostic(!verifyDiagnostics);
4544

46-
using SourceDiagnosticHandlerVariant =
47-
std::variant<SourceMgrDiagnosticHandler,
48-
SourceMgrDiagnosticVerifierHandler>;
49-
50-
auto sourceMgrHandler =
51-
verifyDiagnostics
52-
? SourceDiagnosticHandlerVariant(
53-
std::in_place_type_t<SourceMgrDiagnosticVerifierHandler>{},
54-
*sourceMgr, &ctx)
55-
: SourceDiagnosticHandlerVariant(
56-
std::in_place_type_t<SourceMgrDiagnosticHandler>{}, *sourceMgr,
57-
&ctx);
58-
59-
const auto verifyOr =
60-
[verifier = std::get_if<SourceMgrDiagnosticVerifierHandler>(
61-
&sourceMgrHandler)](LogicalResult res) -> LogicalResult {
62-
return verifier ? verifier->verify() : res;
63-
};
45+
auto runTranslation = [&]() {
46+
ParserConfig parseConfig(&ctx);
47+
OwningOpRef<Operation *> op =
48+
parseSourceFileForTool(sourceMgr, parseConfig, true);
49+
if (!op)
50+
return failure();
6451

65-
ParserConfig parseConfig(&ctx);
66-
OwningOpRef<Operation *> op =
67-
parseSourceFileForTool(sourceMgr, parseConfig, true);
68-
if (!op)
69-
return verifyOr(failure());
52+
auto moduleOp = llvm::cast<ModuleOp>(*op);
53+
llvm::SmallVector<irdl::DialectOp> dialects{
54+
moduleOp.getOps<irdl::DialectOp>(),
55+
};
7056

71-
auto moduleOp = llvm::cast<ModuleOp>(*op);
72-
llvm::SmallVector<irdl::DialectOp> dialects{
73-
moduleOp.getOps<irdl::DialectOp>(),
57+
return irdl::translateIRDLDialectToCpp(dialects, os);
7458
};
7559

76-
if (failed(irdl::translateIRDLDialectToCpp(dialects, os)))
77-
return verifyOr(failure());
60+
if (!verifyDiagnostics) {
61+
// If no errors are expected, return translation result.
62+
SourceMgrDiagnosticHandler srcManagerHandler(*sourceMgr, &ctx);
63+
return runTranslation();
64+
}
7865

79-
return verifyOr(success());
66+
// If errors are expected, ignore translation result and check for
67+
// diagnostics.
68+
SourceMgrDiagnosticVerifierHandler srcManagerHandler(*sourceMgr, &ctx);
69+
(void)runTranslation();
70+
return srcManagerHandler.verify();
8071
}
8172

8273
static LogicalResult translateIRDLToCpp(int argc, char **argv) {

0 commit comments

Comments
 (0)