Add FatalIf utility to simplify some Fatal cases
diff --git a/src/support/utilities.h b/src/support/utilities.h index a2fff7f..021d9d4 100644 --- a/src/support/utilities.h +++ b/src/support/utilities.h
@@ -58,24 +58,46 @@ return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } -// For fatal errors which could arise from input (i.e. not assertion failures) -class Fatal { +// Helper for non-asserting failures. +// Used to collapse code like: +// if (foo() && !bar()) { +// Fatal() << "Error: foo and not bar!"; +// } +// Into: +// FatalIf(foo() && !bar()) << "foo and not bar!"; +class FatalIf { + bool condition; public: - Fatal() { - std::cerr << "Fatal: "; + FatalIf(bool condition) : condition(condition) { + if (condition) { + std::cerr << "Fatal: "; + } } template<typename T> - Fatal &operator<<(T arg) { - std::cerr << arg; + FatalIf &operator<<(T arg) { + if (condition) { + std::cerr << arg; + } return *this; } + ~FatalIf() { + if (condition) { + std::cerr << "\n"; + exit(1); + } + } +}; + +// For fatal errors which could arise from input (i.e. not assertion failures) +class Fatal : public FatalIf { + public: + Fatal() : FatalIf(true) { } WASM_NORETURN ~Fatal() { std::cerr << "\n"; exit(1); } }; - } // namespace wasm #endif // wasm_support_utilities_h
diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp index 1cf7947..aeca002 100644 --- a/src/tools/asm2wasm.cpp +++ b/src/tools/asm2wasm.cpp
@@ -131,9 +131,8 @@ } if (options.runningDefaultOptimizationPasses()) { - if (options.passes.size() > 1) { - Fatal() << "asm2wasm can only run default optimization passes (-O, -Ox, etc.), and not specific additional passes"; - } + FatalIf(options.passes.size() > 1) + << "asm2wasm can only run default optimization passes (-O, -Ox, etc.), and not specific additional passes"; } const auto &tm_it = options.extra.find("total memory");
diff --git a/src/tools/s2wasm.cpp b/src/tools/s2wasm.cpp index dc6b1f3..cc6a599 100644 --- a/src/tools/s2wasm.cpp +++ b/src/tools/s2wasm.cpp
@@ -177,10 +177,9 @@ emitBinary = false; } - if (allowMemoryGrowth && !generateEmscriptenGlue) { - Fatal() << "Error: adding memory growth code without Emscripten glue. " - "This doesn't do anything.\n"; - } + FatalIf(allowMemoryGrowth && !generateEmscriptenGlue) + << "Error: adding memory growth code without Emscripten glue. " + "This doesn't do anything.\n"; auto debugFlag = options.debug ? Flags::Debug : Flags::Release; auto input(read_file<std::string>(options.extra["infile"], Flags::Text, debugFlag)); @@ -221,7 +220,7 @@ auto archiveFile(read_file<std::vector<char>>(m, Flags::Binary, debugFlag)); bool error; Archive lib(archiveFile, error); - if (error) Fatal() << "Error opening archive " << m << "\n"; + FatalIf(error) << "Error opening archive " << m << "\n"; linker.linkArchive(lib); }
diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp index 7d00e81..85a3321 100644 --- a/src/tools/wasm-emscripten-finalize.cpp +++ b/src/tools/wasm-emscripten-finalize.cpp
@@ -74,12 +74,8 @@ }); options.parse(argc, argv); - if (infile == "") { - Fatal() << "Need to specify an infile\n"; - } - if (outfile == "" && emitBinary) { - Fatal() << "Need to specify an outfile, or use text output\n"; - } + FatalIf(infile == "") << "Need to specify an infile\n"; + FatalIf(outfile == "" && emitBinary) << "Need to specify an outfile, or use text output\n"; Module wasm; ModuleReader reader; @@ -91,16 +87,10 @@ } Export* dataEndExport = wasm.getExport("__data_end"); - if (dataEndExport == nullptr) { - Fatal() << "__data_end export not found"; - } + FatalIf(dataEndExport == nullptr) << "__data_end export not found"; Global* dataEnd = wasm.getGlobal(dataEndExport->value); - if (dataEnd == nullptr) { - Fatal() << "__data_end global not found"; - } - if (dataEnd->type != Type::i32) { - Fatal() << "__data_end global has wrong type"; - } + FatalIf(dataEnd == nullptr) << "__data_end global not found"; + FatalIf(dataEnd->type != Type::i32) << "__data_end global has wrong type"; Const* dataEndConst = dataEnd->init->cast<Const>(); uint32_t dataSize = dataEndConst->value.geti32() - globalBase;