From e91d68266c12769129658754d6ef27499254da29 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Wed, 22 May 2019 13:34:03 -0700 Subject: [PATCH] Eliminate reader_current_filename Store this in the parser libdata instead. --- src/builtin_source.cpp | 4 ++-- src/exec.cpp | 9 ++++----- src/fish.cpp | 14 ++++++-------- src/function.cpp | 2 +- src/parser.cpp | 8 ++------ src/parser.h | 4 ++++ src/reader.cpp | 18 ------------------ src/reader.h | 11 ----------- 8 files changed, 19 insertions(+), 51 deletions(-) diff --git a/src/builtin_source.cpp b/src/builtin_source.cpp index 9198992e7..5bdc7c4b2 100644 --- a/src/builtin_source.cpp +++ b/src/builtin_source.cpp @@ -74,7 +74,8 @@ int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) { } const block_t *sb = parser.push_block(block_t::source_block(fn_intern)); - reader_push_current_filename(fn_intern); + auto &ld = parser.libdata(); + scoped_push filename_push{&ld.current_filename, fn_intern}; // This is slightly subtle. If this is a bare `source` with no args then `argv + optind` already // points to the end of argv. Otherwise we want to skip the file name to get to the args if any. @@ -95,6 +96,5 @@ int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) { // Do not close fd after calling reader_read. reader_read automatically closes it before calling // eval. - reader_pop_current_filename(); return retval; } diff --git a/src/exec.cpp b/src/exec.cpp index 68d9fb949..e4b0a402c 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -676,7 +676,7 @@ static bool handle_builtin_output(parser_t &parser, const std::shared_ptr /// Executes an external command. /// \return true on success, false if there is an exec error. -static bool exec_external_command(env_stack_t &vars, const std::shared_ptr &j, process_t *p, +static bool exec_external_command(parser_t &parser, const std::shared_ptr &j, process_t *p, const io_chain_t &proc_io_chain) { assert(p->type == process_type_t::external && "Process is not external"); // Get argv and envv before we fork. @@ -694,14 +694,13 @@ static bool exec_external_command(env_stack_t &vars, const std::shared_ptrget(); std::string actual_cmd_str = wcs2string(p->actual_cmd); const char *actual_cmd = actual_cmd_str.c_str(); - const wchar_t *file = reader_current_filename(); + const wchar_t *file = parser.libdata().current_filename; #if FISH_USE_POSIX_SPAWN // Prefer to use posix_spawn, since it's faster on some systems like OS X. @@ -979,7 +978,7 @@ static bool exec_process_in_job(parser_t &parser, process_t *p, std::shared_ptr< } case process_type_t::external: { - if (!exec_external_command(parser.vars(), j, p, process_net_io_chain)) { + if (!exec_external_command(parser, j, p, process_net_io_chain)) { return false; } break; diff --git a/src/fish.cpp b/src/fish.cpp index e79b96811..389dfc4d2 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -47,6 +47,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "function.h" #include "future_feature_flags.h" #include "history.h" +#include "intern.h" #include "io.h" #include "parser.h" #include "path.h" @@ -466,18 +467,15 @@ int main(int argc, char **argv) { } parser.vars().set(L"argv", ENV_DEFAULT, list); - const wcstring rel_filename = str2wcstring(file); - - reader_push_current_filename(rel_filename.c_str()); - + auto &ld = parser.libdata(); + wcstring rel_filename = str2wcstring(file); + scoped_push filename_push{&ld.current_filename, + intern(rel_filename.c_str())}; res = reader_read(fd, {}); - if (res) { debug(1, _(L"Error while reading file %ls\n"), - reader_current_filename() ? reader_current_filename() - : _(L"Standard input")); + ld.current_filename ? ld.current_filename : _(L"Standard input")); } - reader_pop_current_filename(); } } } diff --git a/src/function.cpp b/src/function.cpp index cc2f1f0f9..37e889e9d 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -190,7 +190,7 @@ void function_add(const function_data_t &data, const parser_t &parser) { bool is_autoload = funcset->autoloader.autoload_in_progress(data.name); // Create and store a new function. - const wchar_t *filename = reader_current_filename(); + const wchar_t *filename = parser.libdata().current_filename; auto ins = funcset->funcs.emplace(data.name, function_info_t(data, parser.vars(), filename, is_autoload)); assert(ins.second && "Function should not already be present in the table"); diff --git a/src/parser.cpp b/src/parser.cpp index e3def49ed..100aa13ac 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -498,12 +498,8 @@ const wchar_t *parser_t::current_filename() const { } } - // We query a global array for the current file name, but only do that if we are the principal - // parser. - if (this == &principal_parser()) { - return reader_current_filename(); - } - return NULL; + // Fall back to the file being sourced. + return libdata().current_filename; } wcstring parser_t::current_line() { diff --git a/src/parser.h b/src/parser.h index e2661df47..3fa3c1d93 100644 --- a/src/parser.h +++ b/src/parser.h @@ -156,6 +156,10 @@ struct library_data_t { /// Whether we should return from the current function. bool returning{false}; + + /// The current filename we are evaluating, either from builtin source or on the command line. + /// This is an intern'd string. + const wchar_t *current_filename{}; }; class parser_t : public std::enable_shared_from_this { diff --git a/src/reader.cpp b/src/reader.cpp index 02836ec8d..58e811099 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -482,9 +482,6 @@ class reader_data_t : public std::enable_shared_from_this { /// handled by the fish interrupt handler. static volatile sig_atomic_t is_interactive_read; -/// The stack containing names of files that are being parsed. -static std::stack> current_filename; - /// This variable is set to true by the signal handler when ^C is pressed. static volatile sig_atomic_t interrupted = 0; @@ -689,21 +686,6 @@ void reader_handle_sigint() { interrupted = 1; } -const wchar_t *reader_current_filename() { - ASSERT_IS_MAIN_THREAD(); - return current_filename.empty() ? NULL : current_filename.top(); -} - -void reader_push_current_filename(const wchar_t *fn) { - ASSERT_IS_MAIN_THREAD(); - current_filename.push(intern(fn)); -} - -void reader_pop_current_filename() { - ASSERT_IS_MAIN_THREAD(); - current_filename.pop(); -} - /// Make sure buffers are large enough to hold the current string length. void reader_data_t::command_line_changed(const editable_line_t *el) { ASSERT_IS_MAIN_THREAD(); diff --git a/src/reader.h b/src/reader.h index 445dc8880..45c3431ea 100644 --- a/src/reader.h +++ b/src/reader.h @@ -64,20 +64,9 @@ void reader_init(); /// Restore the term mode at startup. void restore_term_mode(); -/// Returns the filename of the file currently read. -const wchar_t *reader_current_filename(); - -/// Push a new filename on the stack of read files. -/// -/// \param fn The fileanme to push -void reader_push_current_filename(const wchar_t *fn); - /// Change the history file for the current command reading context. void reader_change_history(const wcstring &name); -/// Pop the current filename from the stack of read files. -void reader_pop_current_filename(); - /// Write the title to the titlebar. This function is called just before a new application starts /// executing and just after it finishes. ///