From: Simon Marchi Date: Tue, 25 Jan 2022 01:00:46 +0000 (-0500) Subject: gdb: add string_file::release method X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5d10a2041eb8;p=binutils-gdb.git gdb: add string_file::release method A common pattern for string_file is to want to move out the internal string buffer, because it is the result of the computation that we want to return. It is the reason why string_file::string returns a non-const reference, as explained in the comment. I think it would make sense to have a dedicated method for that instead and make string_file::string return a const reference. This allows removing the explicit std::move in the typical case. Note that compile_program::compute was missing a move, meaning that the resulting string was copied. With the new version, it's not possible to forget to move. Change-Id: Ieaefa35b73daa7930b2f3a26988b6e3b4121bb79 --- diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 642527e068c..5a67853455d 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -6915,7 +6915,7 @@ type_as_string (struct type *type) type_print (type, "", &tmp_stream, -1); - return std::move (tmp_stream.string ()); + return tmp_stream.release (); } /* Given a type TYPE, look up the type of the component of type named NAME. diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c index 269de259332..70e46965c17 100644 --- a/gdb/ada-valprint.c +++ b/gdb/ada-valprint.c @@ -307,7 +307,7 @@ ada_print_floating (const gdb_byte *valaddr, struct type *type, print_floating (valaddr, type, &tmp_stream); - std::string &s = tmp_stream.string (); + std::string s = tmp_stream.release (); size_t skip_count = 0; /* Modify for Ada rules. */ diff --git a/gdb/ada-varobj.c b/gdb/ada-varobj.c index fae4f872e17..3c664687572 100644 --- a/gdb/ada-varobj.c +++ b/gdb/ada-varobj.c @@ -82,7 +82,7 @@ ada_varobj_scalar_image (struct type *type, LONGEST val) string_file buf; ada_print_scalar (type, val, &buf); - return std::move (buf.string ()); + return buf.release (); } /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair designates @@ -817,7 +817,7 @@ ada_varobj_get_value_image (struct value *value, string_file buffer; common_val_print (value, &buffer, 0, opts, current_language); - return std::move (buffer.string ()); + return buffer.release (); } /* Assuming that the (VALUE, TYPE) pair designates an array varobj, diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 85499de0640..464ed44a229 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -1780,11 +1780,11 @@ oper: OPERATOR NEW | OPERATOR OBJC_LBRAC ']' { $$ = operator_stoken ("[]"); } | OPERATOR conversion_type_id - { string_file buf; - + { + string_file buf; c_print_type ($2, NULL, &buf, -1, 0, &type_print_raw_options); - std::string name = std::move (buf.string ()); + std::string name = buf.release (); /* This also needs canonicalization. */ gdb::unique_xmalloc_ptr canon diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c index 4f563d9e0e1..99f35eb5092 100644 --- a/gdb/cli/cli-setshow.c +++ b/gdb/cli/cli-setshow.c @@ -659,7 +659,7 @@ get_setshow_command_value_string (const setting &var) gdb_assert_not_reached ("bad var_type"); } - return std::move (stb.string ()); + return stb.release (); } diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c index dd0dc914d97..4d9abfb5e89 100644 --- a/gdb/compile/compile-c-support.c +++ b/gdb/compile/compile-c-support.c @@ -635,7 +635,7 @@ public: PopUserExpressionPolicy::pop_user_expression (&buf); AddCodeFooterPolicy::add_code_footer (m_instance->scope (), &buf); - return buf.string (); + return buf.release (); } private: diff --git a/gdb/guile/scm-type.c b/gdb/guile/scm-type.c index c23305a961c..987660ceaf9 100644 --- a/gdb/guile/scm-type.c +++ b/gdb/guile/scm-type.c @@ -109,7 +109,7 @@ tyscm_type_name (struct type *type) string_file stb; LA_PRINT_TYPE (type, "", &stb, -1, 0, &type_print_raw_options); - return std::move (stb.string ()); + return stb.release (); } catch (const gdb_exception &except) { diff --git a/gdb/location.c b/gdb/location.c index d4dfc3b8258..299ef7ecadd 100644 --- a/gdb/location.c +++ b/gdb/location.c @@ -447,7 +447,7 @@ explicit_to_string_internal (bool as_linespec, explicit_loc->line_offset.offset); } - return std::move (buf.string ()); + return buf.release (); } /* See description in location.h. */ diff --git a/gdb/maint-test-options.c b/gdb/maint-test-options.c index b9b538d78bb..09175c3a3cf 100644 --- a/gdb/maint-test-options.c +++ b/gdb/maint-test-options.c @@ -295,8 +295,7 @@ save_completion_result (const test_options_opts &opts, bool res, stream.puts ("1 "); opts.dump (&stream, text); - maintenance_test_options_command_completion_text - = std::move (stream.string ()); + maintenance_test_options_command_completion_text = stream.release (); } else { diff --git a/gdb/remote.c b/gdb/remote.c index b093ad86675..bb41a18daf9 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -9519,7 +9519,7 @@ escape_buffer (const char *buf, int n) string_file stb; stb.putstrn (buf, n, '\\'); - return std::move (stb.string ()); + return stb.release (); } int diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c index f40d4e2e9f1..445503a5af9 100644 --- a/gdb/tui/tui-disasm.c +++ b/gdb/tui/tui-disasm.c @@ -130,14 +130,12 @@ tui_disassemble (struct gdbarch *gdbarch, } /* Capture the disassembled instruction. */ - tal.insn = std::move (gdb_dis_out.string ()); - gdb_dis_out.clear (); + tal.insn = gdb_dis_out.release (); /* And capture the address the instruction is at. */ tal.addr = orig_pc; print_address (gdbarch, orig_pc, &gdb_dis_out); - tal.addr_string = std::move (gdb_dis_out.string ()); - gdb_dis_out.clear (); + tal.addr_string = std::move (gdb_dis_out.release ()); if (addr_size != nullptr) { diff --git a/gdb/tui/tui-regs.c b/gdb/tui/tui-regs.c index 16b6c081253..d53ce549fb0 100644 --- a/gdb/tui/tui-regs.c +++ b/gdb/tui/tui-regs.c @@ -100,7 +100,7 @@ tui_register_format (struct frame_info *frame, int regnum) gdbarch_print_registers_info (gdbarch, &stream, frame, regnum, 1); /* Remove the possible \n. */ - std::string &str = stream.string (); + std::string str = stream.release (); if (!str.empty () && str.back () == '\n') str.resize (str.size () - 1); diff --git a/gdb/tui/tui-stack.c b/gdb/tui/tui-stack.c index 0489a5f3250..be8ffbd0c12 100644 --- a/gdb/tui/tui-stack.c +++ b/gdb/tui/tui-stack.c @@ -181,12 +181,14 @@ tui_locator_window::make_status_line () const string.puts (pc_buf); } + std::string string_val = string.release (); + if (string.size () < status_size) - string.puts (n_spaces (status_size - string.size ())); + string_val.append (status_size - string.size (), ' '); else if (string.size () > status_size) - string.string ().erase (status_size, string.size ()); + string_val.erase (status_size, string.size ()); - return std::move (string.string ()); + return string_val; } /* Get a printable name for the function at the address. The symbol diff --git a/gdb/typeprint.c b/gdb/typeprint.c index 158e6d91b3f..d68970b9d67 100644 --- a/gdb/typeprint.c +++ b/gdb/typeprint.c @@ -406,7 +406,7 @@ type_to_string (struct type *type) string_file stb; type_print (type, "", &stb, -1); - return std::move (stb.string ()); + return stb.release (); } catch (const gdb_exception &except) { diff --git a/gdb/ui-file.h b/gdb/ui-file.h index c097abf0c29..7c7b00d91c6 100644 --- a/gdb/ui-file.h +++ b/gdb/ui-file.h @@ -160,17 +160,19 @@ public: /* string_file-specific public API. */ /* Accesses the std::string containing the entire output collected - so far. + so far. */ + const std::string &string () { return m_string; } - Returns a non-const reference so that it's easy to move the - string contents out of the string_file. E.g.: + /* Return an std::string containing the entire output collected so far. - string_file buf; - buf.printf (....); - buf.printf (....); - return std::move (buf.string ()); - */ - std::string &string () { return m_string; } + The internal buffer is cleared, such that it's ready to build a new + string. */ + std::string release () + { + std::string ret = std::move (m_string); + m_string.clear (); + return ret; + } /* Provide a few convenience methods with the same API as the underlying std::string. */ diff --git a/gdb/utils.c b/gdb/utils.c index ce7885f79af..92266890de4 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -1986,7 +1986,7 @@ vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) /* Print the message. */ string_file sfile; cli_ui_out (&sfile, 0).vmessage (ui_file_style (), format, args); - std::string linebuffer = std::move (sfile.string ()); + const std::string &linebuffer = sfile.string (); fputs_unfiltered (linebuffer.c_str (), stream); size_t len = linebuffer.length (); diff --git a/gdb/varobj.c b/gdb/varobj.c index 3aec027a5b6..80216a455e8 100644 --- a/gdb/varobj.c +++ b/gdb/varobj.c @@ -2242,7 +2242,7 @@ varobj_value_get_print_value (struct value *value, /* All other cases. */ common_val_print (value, &stb, 0, &opts, current_language); - return std::move (stb.string ()); + return stb.release (); } bool