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
This commit is contained in:
Simon Marchi 2022-01-24 20:00:46 -05:00
parent b583c328e7
commit 5d10a2041e
17 changed files with 37 additions and 36 deletions

View file

@ -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.

View file

@ -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. */

View file

@ -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,

View file

@ -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<char> canon

View file

@ -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 ();
}

View file

@ -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:

View file

@ -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)
{

View file

@ -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. */

View file

@ -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
{

View file

@ -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

View file

@ -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)
{

View file

@ -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);

View file

@ -181,12 +181,14 @@ tui_locator_window::make_status_line () const
string.puts (pc_buf);
}
if (string.size () < status_size)
string.puts (n_spaces (status_size - string.size ()));
else if (string.size () > status_size)
string.string ().erase (status_size, string.size ());
std::string string_val = string.release ();
return std::move (string.string ());
if (string.size () < status_size)
string_val.append (status_size - string.size (), ' ');
else if (string.size () > status_size)
string_val.erase (status_size, string.size ());
return string_val;
}
/* Get a printable name for the function at the address. The symbol

View file

@ -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)
{

View file

@ -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. */

View file

@ -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 ();

View file

@ -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