From 15cee66df111bbf24369efb649a3db04736e74ef Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 28 Sep 2021 12:50:27 -0700 Subject: [PATCH] Wrap even more stuff in anonymous namespaces --- src/complete.cpp | 2 + src/env.cpp | 4 ++ src/env_universal_common.cpp | 2 + src/event.cpp | 2 + src/function.cpp | 2 + src/highlight.cpp | 102 ++++++++++++++++------------------- src/iothread.cpp | 2 + src/pager.cpp | 2 +- src/path.cpp | 2 + src/reader.cpp | 52 +++++++++--------- src/screen.cpp | 20 ++++--- src/screen.h | 7 ++- src/wcstringutil.cpp | 2 +- src/wildcard.cpp | 4 ++ src/wutil.cpp | 2 +- 15 files changed, 111 insertions(+), 96 deletions(-) diff --git a/src/complete.cpp b/src/complete.cpp index 10d8462d1..8b8d7f2d1 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -88,6 +88,7 @@ static const wcstring &C_(const wcstring &s) { return s; } /// If option is non-empty, it specifies a switch for the command. If \c comp is also not empty, it /// contains a list of non-switch arguments that may only follow directly after the specified /// switch. +namespace { struct complete_entry_opt_t { // Text of the option (like 'foo'). wcstring option; @@ -164,6 +165,7 @@ bool completion_entry_t::remove_option(const wcstring &option, complete_option_t } return this->options.empty(); } +} // namespace /// Set of all completion entries. namespace std { diff --git a/src/env.cpp b/src/env.cpp index cd482ac69..c23ba1552 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -71,6 +71,7 @@ static relaxed_atomic_bool_t s_uvar_scope_is_global{false}; bool env_universal_barrier() { return env_stack_t::principal().universal_barrier(); } +namespace { struct electric_var_t { enum { freadonly = 1 << 0, // May not be modified by the user. @@ -116,6 +117,7 @@ const electric_var_t *electric_var_t::for_name(const wchar_t *name) { const electric_var_t *electric_var_t::for_name(const wcstring &name) { return electric_var_t::for_name(name.c_str()); } +} // namespace /// Check if a variable may not be set using the set command. static bool is_read_only(const wchar_t *key) { @@ -866,6 +868,7 @@ std::shared_ptr env_scoped_impl_t::snapshot() const { } // A struct that wraps up the result of setting or removing a variable. +namespace { struct mod_result_t { // The publicly visible status of the set call. int status{ENV_OK}; @@ -878,6 +881,7 @@ struct mod_result_t { explicit mod_result_t(int status) : status(status) {} }; +} // namespace /// A mutable subclass of env_scoped_impl_t. class env_stack_impl_t final : public env_scoped_impl_t { diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index 800f2ea2e..bb7e60061 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -1003,6 +1003,7 @@ static wcstring get_machine_identifier() { return result; } +namespace { class universal_notifier_shmem_poller_t final : public universal_notifier_t { #ifdef __CYGWIN__ // This is what our shared memory looks like. Everything here is stored in network byte order @@ -1490,6 +1491,7 @@ class universal_notifier_named_pipe_t final : public universal_notifier_t { } #endif }; +} // namespace universal_notifier_t::notifier_strategy_t universal_notifier_t::resolve_default_strategy() { #ifdef FISH_NOTIFYD_AVAILABLE diff --git a/src/event.cpp b/src/event.cpp index 97d146898..8fa64a025 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -25,6 +25,7 @@ #include "wcstringutil.h" #include "wutil.h" // IWYU pragma: keep +namespace { class pending_signals_t { static constexpr size_t SIGNAL_COUNT = NSIG; @@ -83,6 +84,7 @@ class pending_signals_t { return result; } }; +} // namespace static pending_signals_t s_pending_signals; diff --git a/src/function.cpp b/src/function.cpp index 59fa9b8cb..ead0c2775 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -33,6 +33,7 @@ #include "wcstringutil.h" #include "wutil.h" // IWYU pragma: keep +namespace { class function_info_t { public: /// Immutable properties of the function. @@ -87,6 +88,7 @@ bool function_set_t::allow_autoload(const wcstring &name) const { bool is_tombstoned = autoload_tombstones.count(name) > 0; return !has_explicit_func && !is_tombstoned; } +} // namespace /// Make sure that if the specified function is a dynamically loaded function, it has been fully /// loaded. diff --git a/src/highlight.cpp b/src/highlight.cpp index 817746f33..cbbab5b4b 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -771,6 +771,7 @@ static void color_string_internal(const wcstring &buffstr, highlight_spec_t base } } +namespace { /// Syntax highlighter helper. class highlighter_t { // The string we're highlighting. Note this is a reference memmber variable (to avoid copying)! @@ -805,8 +806,6 @@ class highlighter_t { void color_node(const ast::node_t &node, highlight_spec_t color); // Colors a range with a given color. void color_range(source_range_t range, highlight_spec_t color); - // return whether a plain statement is 'cd'. - bool is_cd(const ast::decorated_statement_t &stmt) const; /// \return a substring of our buffer. wcstring get_source(source_range_t r) const; @@ -941,14 +940,6 @@ static bool range_is_potential_path(const wcstring &src, const source_range_t &r return result; } -bool highlighter_t::is_cd(const ast::decorated_statement_t &stmt) const { - wcstring cmd_str; - if (this->io_ok && statement_get_expanded_command(this->buff, stmt, ctx, &cmd_str)) { - return cmd_str == L"cd"; - } - return false; -} - void highlighter_t::visit(const ast::keyword_base_t &kw) { highlight_role_t role = highlight_role_t::normal; switch (kw.kw) { @@ -1243,51 +1234,6 @@ void highlighter_t::visit(const ast::redirection_t &redir) { } } -/// Determine if a command is valid. -static bool command_is_valid(const wcstring &cmd, enum statement_decoration_t decoration, - const wcstring &working_directory, const environment_t &vars) { - // Determine which types we check, based on the decoration. - bool builtin_ok = true, function_ok = true, abbreviation_ok = true, command_ok = true, - implicit_cd_ok = true; - if (decoration == statement_decoration_t::command || - decoration == statement_decoration_t::exec) { - builtin_ok = false; - function_ok = false; - abbreviation_ok = false; - command_ok = true; - implicit_cd_ok = false; - } else if (decoration == statement_decoration_t::builtin) { - builtin_ok = true; - function_ok = false; - abbreviation_ok = false; - command_ok = false; - implicit_cd_ok = false; - } - - // Check them. - bool is_valid = false; - - // Builtins - if (!is_valid && builtin_ok) is_valid = builtin_exists(cmd); - - // Functions - if (!is_valid && function_ok) is_valid = function_exists_no_autoload(cmd); - - // Abbreviations - if (!is_valid && abbreviation_ok) is_valid = expand_abbreviation(cmd, vars).has_value(); - - // Regular commands - if (!is_valid && command_ok) is_valid = path_get_path(cmd, nullptr, vars); - - // Implicit cd - if (!is_valid && implicit_cd_ok) { - is_valid = path_as_implicit_cd(cmd, working_directory, vars).has_value(); - } - - // Return what we got. - return is_valid; -} - highlighter_t::color_array_t highlighter_t::highlight() { // If we are doing I/O, we must be in a background thread. if (io_ok) { @@ -1337,6 +1283,52 @@ highlighter_t::color_array_t highlighter_t::highlight() { return std::move(color_array); } +} // namespace + +/// Determine if a command is valid. +static bool command_is_valid(const wcstring &cmd, enum statement_decoration_t decoration, + const wcstring &working_directory, const environment_t &vars) { + // Determine which types we check, based on the decoration. + bool builtin_ok = true, function_ok = true, abbreviation_ok = true, command_ok = true, + implicit_cd_ok = true; + if (decoration == statement_decoration_t::command || + decoration == statement_decoration_t::exec) { + builtin_ok = false; + function_ok = false; + abbreviation_ok = false; + command_ok = true; + implicit_cd_ok = false; + } else if (decoration == statement_decoration_t::builtin) { + builtin_ok = true; + function_ok = false; + abbreviation_ok = false; + command_ok = false; + implicit_cd_ok = false; + } + + // Check them. + bool is_valid = false; + + // Builtins + if (!is_valid && builtin_ok) is_valid = builtin_exists(cmd); + + // Functions + if (!is_valid && function_ok) is_valid = function_exists_no_autoload(cmd); + + // Abbreviations + if (!is_valid && abbreviation_ok) is_valid = expand_abbreviation(cmd, vars).has_value(); + + // Regular commands + if (!is_valid && command_ok) is_valid = path_get_path(cmd, nullptr, vars); + + // Implicit cd + if (!is_valid && implicit_cd_ok) { + is_valid = path_as_implicit_cd(cmd, working_directory, vars).has_value(); + } + + // Return what we got. + return is_valid; +} std::string colorize(const wcstring &text, const std::vector &colors, const environment_t &vars) { diff --git a/src/iothread.cpp b/src/iothread.cpp index d639f3612..d21cfe0e8 100644 --- a/src/iothread.cpp +++ b/src/iothread.cpp @@ -44,6 +44,7 @@ using void_function_t = std::function; +namespace { struct work_request_t : noncopyable_t { void_function_t handler; explicit work_request_t(void_function_t &&f) : handler(std::move(f)) {} @@ -236,6 +237,7 @@ int thread_pool_t::perform(void_function_t &&func, bool cant_wait) { } return local_thread_count; } +} // namespace void iothread_perform_impl(void_function_t &&func, bool cant_wait) { ASSERT_IS_NOT_FORKED_CHILD(); diff --git a/src/pager.cpp b/src/pager.cpp index 0437d793f..110ddc5ce 100644 --- a/src/pager.cpp +++ b/src/pager.cpp @@ -39,7 +39,7 @@ using comp_info_list_t = std::vector; /// Text we use for the search field. #define SEARCH_FIELD_PROMPT _(L"search: ") -inline bool selection_direction_is_cardinal(selection_motion_t dir) { +static inline bool selection_direction_is_cardinal(selection_motion_t dir) { switch (dir) { case selection_motion_t::north: case selection_motion_t::east: diff --git a/src/path.cpp b/src/path.cpp index a08df5520..963a308ad 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -332,6 +332,7 @@ static int create_directory(const wcstring &d) { return ok ? 0 : -1; } +namespace { /// The following type wraps up a user's "base" directories, corresponding (conceptually if not /// actually) to XDG spec. struct base_directory_t { @@ -342,6 +343,7 @@ struct base_directory_t { bool success() const { return err == 0; } }; +} // namespace /// Attempt to get a base directory, creating it if necessary. If a variable named \p xdg_var is /// set, use that directory; otherwise use the path \p non_xdg_homepath rooted in $HOME. \return the diff --git a/src/reader.cpp b/src/reader.cpp index b5bc3c3c1..9656515ae 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -522,11 +522,34 @@ struct highlight_result_t { wcstring text; }; +/// readline_loop_state_t encapsulates the state used in a readline loop. +/// It is always stack allocated transient. This state should not be "publicly visible"; public +/// state should be in reader_data_t. +struct readline_loop_state_t { + /// The last command that was executed. + maybe_t last_cmd{}; + + /// If the last command was a yank, the length of yanking that occurred. + size_t yank_len{0}; + + /// If the last "complete" readline command has inserted text into the command line. + bool complete_did_insert{true}; + + /// List of completions. + completion_list_t comp; + + /// Whether the loop has finished, due to reaching the character limit or through executing a + /// command. + bool finished{false}; + + /// Maximum number of characters to read. + size_t nchars{std::numeric_limits::max()}; +}; + } // namespace -struct readline_loop_state_t; - /// Data wrapping up the visual selection. +namespace { struct selection_data_t { /// The position of the cursor when selection was initiated. size_t begin{0}; @@ -573,6 +596,7 @@ struct layout_data_t { wcstring mode_prompt_buff{}; wcstring right_prompt_buff{}; }; +} // namespace /// A struct describing the state of the interactive reader. These states can be stacked, in case /// reader_readline() calls are nested. This happens when the 'read' builtin is used. @@ -2848,30 +2872,6 @@ static bool event_is_normal_char(const char_event_t &evt) { return !fish_reserved_codepoint(c) && c > 31 && c != 127; } -/// readline_loop_state_t encapsulates the state used in a readline loop. -/// It is always stack allocated transient. This state should not be "publicly visible"; public -/// state should be in reader_data_t. -struct readline_loop_state_t { - /// The last command that was executed. - maybe_t last_cmd{}; - - /// If the last command was a yank, the length of yanking that occurred. - size_t yank_len{0}; - - /// If the last "complete" readline command has inserted text into the command line. - bool complete_did_insert{true}; - - /// List of completions. - completion_list_t comp; - - /// Whether the loop has finished, due to reaching the character limit or through executing a - /// command. - bool finished{false}; - - /// Maximum number of characters to read. - size_t nchars{std::numeric_limits::max()}; -}; - std::shared_ptr reader_get_history() { ASSERT_IS_MAIN_THREAD(); reader_data_t *data = current_data_or_null(); diff --git a/src/screen.cpp b/src/screen.cpp index 493a3c75d..1a60da1f6 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -46,15 +46,17 @@ /// The number of characters to indent new blocks. #define INDENT_STEP 4u -/// RAII class to begin and end buffering around stdoutput(). -class screen_t::scoped_buffer_t { - screen_t &screen_; +/// RAII class to begin and end buffering around an outputter. +namespace { +class scoped_buffer_t : noncopyable_t, nonmovable_t { + outputter_t &outp_; public: - explicit scoped_buffer_t(screen_t &s) : screen_(s) { screen_.outp().begin_buffering(); } + explicit scoped_buffer_t(outputter_t &outp) : outp_(outp) { outp_.begin_buffering(); } - ~scoped_buffer_t() { screen_.outp().end_buffering(); } + ~scoped_buffer_t() { outp_.end_buffering(); } }; +} // namespace // Singleton of the cached escape sequences seen in prompts and similar strings. // Note this is deliberately exported so that init_curses can clear it. @@ -564,7 +566,7 @@ void screen_t::desired_append_char(wchar_t b, highlight_spec_t c, int indent, si void screen_t::move(int new_x, int new_y) { if (this->actual.cursor.x == new_x && this->actual.cursor.y == new_y) return; - const scoped_buffer_t buffering(*this); + const scoped_buffer_t buffering(outp()); // If we are at the end of our window, then either the cursor stuck to the edge or it didn't. We // don't know! We can fix it up though. @@ -644,7 +646,7 @@ void screen_t::move(int new_x, int new_y) { /// Convert a wide character to a multibyte string and append it to the buffer. void screen_t::write_char(wchar_t c, size_t width) { - scoped_buffer_t outp(*this); + scoped_buffer_t buffering(outp()); this->actual.cursor.x += width; this->outp().writech(c); if (this->actual.cursor.x == this->actual.screen_width && allow_soft_wrap()) { @@ -727,7 +729,7 @@ void screen_t::update(const wcstring &left_prompt, const wcstring &right_prompt, }; layout_cache_t &cached_layouts = layout_cache_t::shared; - const scoped_buffer_t buffering(*this); + const scoped_buffer_t buffering(outp()); // Determine size of left and right prompt. Note these have already been truncated. const prompt_layout_t left_prompt_layout = cached_layouts.calc_prompt_layout(left_prompt); @@ -961,6 +963,7 @@ static bool is_dumb() { return !cursor_up || !cursor_down || !cursor_left || !cursor_right; } +namespace { struct screen_layout_t { // The left prompt that we're going to use. wcstring left_prompt; @@ -971,6 +974,7 @@ struct screen_layout_t { // The autosuggestion. wcstring autosuggestion; }; +} // namespace // Given a vector whose indexes are offsets and whose values are the widths of the string if // truncated at that offset, return the offset that fits in the given width. Returns diff --git a/src/screen.h b/src/screen.h index 6243b2d5b..23b5d06b4 100644 --- a/src/screen.h +++ b/src/screen.h @@ -131,8 +131,6 @@ class outputter_t; /// The class representing the current and desired screen contents. class screen_t { - outputter_t &outp_; - public: screen_t(); @@ -206,6 +204,9 @@ class screen_t { /// Update the cursor as if soft wrapping had been performed. bool handle_soft_wrap(int x, int y); + /// Receiver for our output. + outputter_t &outp_; + /// The internal representation of the desired screen contents. screen_data_t desired{}; /// The internal representation of the actual screen contents. @@ -238,8 +239,6 @@ class screen_t { /// Update the screen to match the desired output. void update(const wcstring &left_prompt, const wcstring &right_prompt, const environment_t &vars); - - class scoped_buffer_t; }; /// Issues an immediate clr_eos. diff --git a/src/wcstringutil.cpp b/src/wcstringutil.cpp index 9e33b0c12..71800061b 100644 --- a/src/wcstringutil.cpp +++ b/src/wcstringutil.cpp @@ -213,7 +213,7 @@ uint32_t string_fuzzy_match_t::rank() const { } template -size_t ifind_impl(const T &haystack, const T &needle) { +static size_t ifind_impl(const T &haystack, const T &needle) { using char_t = typename T::value_type; std::locale locale; diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 331a6e985..52c3646d2 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -177,6 +177,7 @@ static wcstring resolve_description(const wcstring &full_completion, wcstring *c return wcstring{}; } +namespace { // A transient parameter pack needed by wildcard_complete. struct wc_complete_pack_t { const wcstring &orig; // the original string, transient @@ -185,6 +186,7 @@ struct wc_complete_pack_t { wc_complete_pack_t(const wcstring &str, const description_func_t &df, expand_flags_t fl) : orig(str), desc_func(df), expand_flags(fl) {} }; +} // namespace // Weirdly specific and non-reusable helper function that makes its one call site much clearer. static bool has_prefix_match(const completion_receiver_t *comps, size_t first) { @@ -526,6 +528,7 @@ static bool wildcard_test_flags_then_complete(const wcstring &filepath, const wc wildcard_result_t::match; } +namespace { class wildcard_expander_t { // A function to call to check cancellation. cancel_checker_t cancel_checker; @@ -975,6 +978,7 @@ void wildcard_expander_t::expand(const wcstring &base_dir, const wchar_t *wc, } } } +} // namespace wildcard_result_t wildcard_expand_string(const wcstring &wc, const wcstring &working_directory, expand_flags_t flags, diff --git a/src/wutil.cpp b/src/wutil.cpp index 06eda4cb2..0611742b8 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -771,7 +771,7 @@ wcstring file_id_t::dump() const { } template -int compare(T a, T b) { +static int compare(T a, T b) { if (a < b) { return -1; } else if (a > b) {