From c19407ab0f694431d99b0af89270d8aa90647261 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 22 Dec 2019 16:27:03 -0800 Subject: [PATCH] Default parser_t::eval()'s block type to top This is the parameter value at every call site except one. Just make it the default. --- src/builtin.cpp | 2 +- src/builtin_eval.cpp | 3 +-- src/event.cpp | 2 +- src/exec.cpp | 5 ++--- src/fish.cpp | 4 ++-- src/fish_tests.cpp | 14 ++++++-------- src/input.cpp | 2 +- src/parser.cpp | 10 +++++----- src/parser.h | 13 ++++++++----- src/reader.cpp | 4 ++-- 10 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/builtin.cpp b/src/builtin.cpp index 7af538e95..469feb416 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -165,7 +165,7 @@ void builtin_print_help(parser_t &parser, io_streams_t &streams, const wchar_t * // If it's an error, redirect the output of __fish_print_help to stderr ios.push_back(std::make_shared(STDOUT_FILENO, STDERR_FILENO)); } - parser.eval(cmd, ios, block_type_t::top); + parser.eval(cmd, ios); // ignore the exit status of __fish_print_help } diff --git a/src/builtin_eval.cpp b/src/builtin_eval.cpp index f53b5be1b..7a3acc9b9 100644 --- a/src/builtin_eval.cpp +++ b/src/builtin_eval.cpp @@ -27,8 +27,7 @@ int builtin_eval(parser_t &parser, io_streams_t &streams, wchar_t **argv) { const auto cached_exec_count = parser.libdata().exec_count; int status = STATUS_CMD_OK; if (argc > 1) { - if (parser.eval(std::move(new_cmd), *streams.io_chain, block_type_t::top) != - eval_result_t::ok) { + if (parser.eval(std::move(new_cmd), *streams.io_chain) != eval_result_t::ok) { status = STATUS_CMD_ERROR; } else if (cached_exec_count == parser.libdata().exec_count) { // Issue #5692, in particular, to catch `eval ""`, `eval "begin; end;"`, etc. diff --git a/src/event.cpp b/src/event.cpp index 011c3e007..3382fa2a9 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -286,7 +286,7 @@ static void event_fire_internal(parser_t &parser, const event_t &event) { auto prev_statuses = parser.get_last_statuses(); block_t *b = parser.push_block(block_t::event_block(event)); - parser.eval(buffer, io_chain_t(), block_type_t::top); + parser.eval(buffer, io_chain_t()); parser.pop_block(b); parser.set_last_statuses(std::move(prev_statuses)); } diff --git a/src/exec.cpp b/src/exec.cpp index 664afcc5c..c5d4a414e 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -733,7 +733,7 @@ static proc_performer_t get_performer_for_process(process_t *p, const job_t *job tnode_t node = p->internal_block_node; assert(source && node && "Process is missing node info"); return [=](parser_t &parser) { - eval_result_t res = parser.eval_node(source, node, block_type_t::top, lineage); + eval_result_t res = parser.eval_node(source, node, lineage); switch (res) { case eval_result_t::ok: case eval_result_t::error: @@ -758,8 +758,7 @@ static proc_performer_t get_performer_for_process(process_t *p, const job_t *job const auto &ld = parser.libdata(); auto saved_exec_count = ld.exec_count; const block_t *fb = function_prepare_environment(parser, *argv, *props); - auto res = parser.eval_node(props->parsed_source, props->body_node, block_type_t::top, - lineage); + auto res = parser.eval_node(props->parsed_source, props->body_node, lineage); function_restore_environment(parser, fb); switch (res) { diff --git a/src/fish.cpp b/src/fish.cpp index 80ca51949..82ff5387e 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -228,7 +228,7 @@ static void source_config_in_directory(const wcstring &dir) { const wcstring cmd = L"builtin source " + escaped_pathname; parser_t &parser = parser_t::principal_parser(); set_is_within_fish_initialization(true); - parser.eval(cmd, io_chain_t(), block_type_t::top); + parser.eval(cmd, io_chain_t()); set_is_within_fish_initialization(false); } @@ -254,7 +254,7 @@ int run_command_list(std::vector *cmds, const io_chain_t &io) { for (const auto &cmd : *cmds) { const wcstring cmd_wcs = str2wcstring(cmd); - eval_result_t eval_res = parser.eval(cmd_wcs, io, block_type_t::top); + eval_result_t eval_res = parser.eval(cmd_wcs, io); res = (eval_res == eval_result_t::ok ? 0 : 1); } diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 3720f7315..b1597bb61 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -1003,17 +1003,16 @@ static void test_parser() { // Ensure that we don't crash on infinite self recursion and mutual recursion. These must use // the principal parser because we cannot yet execute jobs on other parsers. say(L"Testing recursion detection"); - parser->eval(L"function recursive ; recursive ; end ; recursive; ", io_chain_t(), - block_type_t::top); + parser->eval(L"function recursive ; recursive ; end ; recursive; ", io_chain_t()); #if 0 // This is disabled since it produces a long backtrace. We should find a way to either visually // compress the backtrace, or disable error spewing. parser->.eval(L"function recursive1 ; recursive2 ; end ; " - L"function recursive2 ; recursive1 ; end ; recursive1; ", io_chain_t(), block_type_t::top); + L"function recursive2 ; recursive1 ; end ; recursive1; ", io_chain_t()); #endif say(L"Testing empty function name"); - parser->eval(L"function '' ; echo fail; exit 42 ; end ; ''", io_chain_t(), block_type_t::top); + parser->eval(L"function '' ; echo fail; exit 42 ; end ; ''", io_chain_t()); say(L"Testing eval_args"); completion_list_t comps = parser_t::expand_argument_list( @@ -1033,8 +1032,7 @@ static void test_1_cancellation(const wchar_t *src) { usleep(delay * 1E6); pthread_kill(thread, SIGINT); }); - eval_result_t ret = - parser_t::principal_parser().eval(src, io_chain_t{filler}, block_type_t::top); + eval_result_t ret = parser_t::principal_parser().eval(src, io_chain_t{filler}); auto buffer = io_bufferfill_t::finish(std::move(filler)); if (buffer->buffer().size() != 0) { err(L"Expected 0 bytes in out_buff, but instead found %lu bytes, for command %ls\n", @@ -1076,7 +1074,7 @@ static void test_cancellation() { bool iis = is_interactive_session(); set_interactive_session(true); const wchar_t *child_self_destructor = L"while true ; sh -c 'sleep .25; kill -s INT $$' ; end"; - parser_t::principal_parser().eval(child_self_destructor, io_chain_t(), block_type_t::top); + parser_t::principal_parser().eval(child_self_destructor, io_chain_t()); set_interactive_session(iis); // Restore signal handling. @@ -5200,7 +5198,7 @@ static void test_illegal_command_exit_code() { parser_t &parser = parser_t::principal_parser(); for (const auto &test : tests) { - parser.eval(test.txt, empty_ios, block_type_t::top); + parser.eval(test.txt, empty_ios); int exit_status = parser.get_last_status(); if (exit_status != test.result) { diff --git a/src/input.cpp b/src/input.cpp index a194fab2a..9eba49675 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -389,7 +389,7 @@ void inputter_t::mapping_execute(const input_mapping_t &m, bool allow_commands) // see that until all other commands have also been run. auto last_statuses = parser_->get_last_statuses(); for (const wcstring &cmd : m.commands) { - parser_->eval(cmd, io_chain_t(), block_type_t::top); + parser_->eval(cmd, io_chain_t{}); } parser_->set_last_statuses(std::move(last_statuses)); event_queue_.push_front(char_event_type_t::check_exit); diff --git a/src/parser.cpp b/src/parser.cpp index 3f721dcbb..e901b61d6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -634,14 +634,14 @@ eval_result_t parser_t::eval(parsed_source_ref_t ps, const io_chain_t &io, lineage.block_io = io; // Execute the first node. tnode_t start{&ps->tree, &ps->tree.front()}; - return this->eval_node(ps, start, block_type, std::move(lineage)); + return this->eval_node(ps, start, std::move(lineage), block_type); } return eval_result_t::ok; } template -eval_result_t parser_t::eval_node(parsed_source_ref_t ps, tnode_t node, block_type_t block_type, - job_lineage_t lineage) { +eval_result_t parser_t::eval_node(parsed_source_ref_t ps, tnode_t node, job_lineage_t lineage, + block_type_t block_type) { static_assert( std::is_same::value || std::is_same::value, "Unexpected node type"); @@ -684,9 +684,9 @@ eval_result_t parser_t::eval_node(parsed_source_ref_t ps, tnode_t node, block // Explicit instantiations. TODO: use overloads instead? template eval_result_t parser_t::eval_node(parsed_source_ref_t, tnode_t, - enum block_type_t, job_lineage_t lineage); + job_lineage_t, block_type_t); template eval_result_t parser_t::eval_node(parsed_source_ref_t, tnode_t, - enum block_type_t, job_lineage_t lineage); + job_lineage_t, block_type_t); void parser_t::get_backtrace(const wcstring &src, const parse_error_list_t &errors, wcstring &output) const { diff --git a/src/parser.h b/src/parser.h index e45372c89..6357643bf 100644 --- a/src/parser.h +++ b/src/parser.h @@ -259,20 +259,23 @@ class parser_t : public std::enable_shared_from_this { /// /// \param cmd the string to evaluate /// \param io io redirections to perform on all started jobs - /// \param block_type The type of block to push on the block stack + /// \param block_type The type of block to push on the block stack, which must be either 'top' + /// or 'subst'. /// /// \return the eval result, - eval_result_t eval(const wcstring &cmd, const io_chain_t &io, enum block_type_t block_type); + eval_result_t eval(const wcstring &cmd, const io_chain_t &io, + block_type_t block_type = block_type_t::top); /// Evaluate the parsed source ps. /// Because the source has been parsed, a syntax error is impossible. - eval_result_t eval(parsed_source_ref_t ps, const io_chain_t &io, enum block_type_t block_type); + eval_result_t eval(parsed_source_ref_t ps, const io_chain_t &io, + block_type_t block_type = block_type_t::top); /// Evaluates a node. /// The node type must be grammar::statement or grammar::job_list. template - eval_result_t eval_node(parsed_source_ref_t ps, tnode_t node, block_type_t block_type, - job_lineage_t lineage); + eval_result_t eval_node(parsed_source_ref_t ps, tnode_t node, job_lineage_t lineage, + block_type_t block_type = block_type_t::top); /// Evaluate line as a list of parameters, i.e. tokenize it and perform parameter expansion and /// cmdsubst execution on the tokens. Errors are ignored. If a parser is provided, it is used diff --git a/src/reader.cpp b/src/reader.cpp index 6a857aa9b..a9be240d2 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1952,7 +1952,7 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) { gettimeofday(&time_before, nullptr); - parser.eval(cmd, io_chain_t(), block_type_t::top); + parser.eval(cmd, io_chain_t{}); job_reap(parser, true); gettimeofday(&time_after, nullptr); @@ -3551,7 +3551,7 @@ static int read_ni(parser_t &parser, int fd, const io_chain_t &io) { parsed_source_ref_t pstree; if (!parse_util_detect_errors(str, &errors, false /* do not accept incomplete */, &pstree)) { - parser.eval(pstree, io, block_type_t::top); + parser.eval(pstree, io); } else { wcstring sb; parser.get_backtrace(str, errors, sb);