From bdfa6ed12ee358a30a1318f502a2a5a4df573d6f Mon Sep 17 00:00:00 2001 From: Andy Chu Date: Fri, 1 May 2020 23:16:39 -0700 Subject: [PATCH] [translation] Able to throw error::Strict. Figured out that we need to catch it by reference... --- build/mycpp.sh | 3 ++- core/error.py | 34 +++++++++++++++++----------------- cpp/core_error.h | 14 +++++++++++++- cpp/preamble.h | 10 +++++----- cpp/unit_tests.cc | 20 ++++++++++++++++++++ 5 files changed, 57 insertions(+), 24 deletions(-) diff --git a/build/mycpp.sh b/build/mycpp.sh index baf04a559c..4ed0c9fd70 100755 --- a/build/mycpp.sh +++ b/build/mycpp.sh @@ -659,7 +659,8 @@ cpp-unit-tests() { local name='unit_tests' mkdir -p _bin - compile _bin/$name cpp/unit_tests.cc cpp/frontend_match.cc mycpp/mylib.cc + compile _bin/$name cpp/unit_tests.cc \ + cpp/frontend_match.cc mycpp/mylib.cc _bin/$name } diff --git a/core/error.py b/core/error.py index 10bcea5ee2..43c8f8245b 100644 --- a/core/error.py +++ b/core/error.py @@ -133,31 +133,31 @@ class Runtime(_ErrorWithLocation): """A non-fatal runtime error, e.g. for builtins.""" -class FatalRuntime(_ErrorWithLocation): - """An exception that propagates to the top level. + class FatalRuntime(_ErrorWithLocation): + """An exception that propagates to the top level. - Used in the evaluators, and also also used in test builtin for invalid - argument. - """ + Used in the evaluators, and also also used in test builtin for invalid + argument. + """ -class Strict(FatalRuntime): - """Depending on shell options, these errors may be caught and ignored. + class Strict(FatalRuntime): + """Depending on shell options, these errors may be caught and ignored. - For example, if options like these are ON: + For example, if options like these are ON: - set -o strict_arith - set -o strict_word_eval + set -o strict_arith + set -o strict_word_eval - then we re-raise the error so it's caught by the top level. Otherwise - we catch it and return a dummy value like '' or -1 (i.e. what bash commonly - does.) + then we re-raise the error so it's caught by the top level. Otherwise + we catch it and return a dummy value like '' or -1 (i.e. what bash commonly + does.) - TODO: Have levels, like: + TODO: Have levels, like: - OIL_STRICT_PRINT=2 # print warnings at level 2 and above - OIL_STRICT_DIE=1 # abort the program at level 1 and above - """ + OIL_STRICT_PRINT=2 # print warnings at level 2 and above + OIL_STRICT_DIE=1 # abort the program at level 1 and above + """ if mylib.PYTHON: diff --git a/cpp/core_error.h b/cpp/core_error.h index fe9430817f..11ffb3d83a 100644 --- a/cpp/core_error.h +++ b/cpp/core_error.h @@ -15,7 +15,7 @@ using syntax_asdl::word_part_t; using syntax_asdl::word_t; // This definition is different in Python than C++. Not worth auto-translating. -class _ErrorWithLocation { +class _ErrorWithLocation : public std::exception { public: _ErrorWithLocation(Str* user_str, int span_id) : user_str_(user_str), @@ -89,6 +89,18 @@ class RedirectEval : public _ErrorWithLocation { } }; +class FatalRuntime: public _ErrorWithLocation { + public: + FatalRuntime(Str* user_str) : _ErrorWithLocation(user_str, -1) { + } +}; + +class Strict : public FatalRuntime { + public: + Strict(Str* user_str) : FatalRuntime(user_str) { + } +}; + // Stub class ErrExit : public _ErrorWithLocation { public: diff --git a/cpp/preamble.h b/cpp/preamble.h index aedb188804..5cdf0a8c32 100644 --- a/cpp/preamble.h +++ b/cpp/preamble.h @@ -114,24 +114,24 @@ Str* repr(void* obj) { } [[noreturn]] void e_strict(Str* s, int span_id) { - assert(0); + throw error::Strict(s); } [[noreturn]] void e_strict(Str* s, syntax_asdl::Token* token) { - assert(0); + throw error::Strict(s); } [[noreturn]] void e_strict(Str* s, syntax_asdl::word_part_t* part) { - assert(0); + throw error::Strict(s); } [[noreturn]] void e_strict(Str* s, syntax_asdl::word_t* w) { - assert(0); + throw error::Strict(s); } // Used without args in osh/string_ops.py [[noreturn]] void e_strict(Str* s) { - assert(0); + throw error::Strict(s); } // e.g. used in core/state.py diff --git a/cpp/unit_tests.cc b/cpp/unit_tests.cc index c170ec5c72..733926b47e 100644 --- a/cpp/unit_tests.cc +++ b/cpp/unit_tests.cc @@ -4,6 +4,7 @@ #include "id.h" #include "libc.h" // cell, etc #include "osh_eval_stubs.h" // util::BackslashEscape +#include "preamble.h" #include "runtime_asdl.h" // cell, etc TEST show_sizeof() { @@ -75,6 +76,24 @@ TEST libc_test() { PASS(); } +// HACK! asdl/runtime.py isn't translated, but core_error.h uses it... +namespace runtime { + int NO_SPID = -1; +}; + +TEST exceptions() { + bool caught = false; + try { + e_strict(new Str("foo")); + } catch (error::Strict& e) { // Catch by reference! + //log("%p ", e); + caught = true; + } + ASSERT(caught); + + PASS(); +} + GREATEST_MAIN_DEFS(); int main(int argc, char **argv) { @@ -83,6 +102,7 @@ int main(int argc, char **argv) { RUN_TEST(match_test); RUN_TEST(util_test); RUN_TEST(libc_test); + RUN_TEST(exceptions); GREATEST_MAIN_END(); /* display results */ return 0; }