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:
parent
b583c328e7
commit
5d10a2041e
17 changed files with 37 additions and 36 deletions
|
@ -6915,7 +6915,7 @@ type_as_string (struct type *type)
|
||||||
|
|
||||||
type_print (type, "", &tmp_stream, -1);
|
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.
|
/* Given a type TYPE, look up the type of the component of type named NAME.
|
||||||
|
|
|
@ -307,7 +307,7 @@ ada_print_floating (const gdb_byte *valaddr, struct type *type,
|
||||||
|
|
||||||
print_floating (valaddr, type, &tmp_stream);
|
print_floating (valaddr, type, &tmp_stream);
|
||||||
|
|
||||||
std::string &s = tmp_stream.string ();
|
std::string s = tmp_stream.release ();
|
||||||
size_t skip_count = 0;
|
size_t skip_count = 0;
|
||||||
|
|
||||||
/* Modify for Ada rules. */
|
/* Modify for Ada rules. */
|
||||||
|
|
|
@ -82,7 +82,7 @@ ada_varobj_scalar_image (struct type *type, LONGEST val)
|
||||||
string_file buf;
|
string_file buf;
|
||||||
|
|
||||||
ada_print_scalar (type, val, &buf);
|
ada_print_scalar (type, val, &buf);
|
||||||
return std::move (buf.string ());
|
return buf.release ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair designates
|
/* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair designates
|
||||||
|
@ -817,7 +817,7 @@ ada_varobj_get_value_image (struct value *value,
|
||||||
string_file buffer;
|
string_file buffer;
|
||||||
|
|
||||||
common_val_print (value, &buffer, 0, opts, current_language);
|
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,
|
/* Assuming that the (VALUE, TYPE) pair designates an array varobj,
|
||||||
|
|
|
@ -1780,11 +1780,11 @@ oper: OPERATOR NEW
|
||||||
| OPERATOR OBJC_LBRAC ']'
|
| OPERATOR OBJC_LBRAC ']'
|
||||||
{ $$ = operator_stoken ("[]"); }
|
{ $$ = operator_stoken ("[]"); }
|
||||||
| OPERATOR conversion_type_id
|
| OPERATOR conversion_type_id
|
||||||
{ string_file buf;
|
{
|
||||||
|
string_file buf;
|
||||||
c_print_type ($2, NULL, &buf, -1, 0,
|
c_print_type ($2, NULL, &buf, -1, 0,
|
||||||
&type_print_raw_options);
|
&type_print_raw_options);
|
||||||
std::string name = std::move (buf.string ());
|
std::string name = buf.release ();
|
||||||
|
|
||||||
/* This also needs canonicalization. */
|
/* This also needs canonicalization. */
|
||||||
gdb::unique_xmalloc_ptr<char> canon
|
gdb::unique_xmalloc_ptr<char> canon
|
||||||
|
|
|
@ -659,7 +659,7 @@ get_setshow_command_value_string (const setting &var)
|
||||||
gdb_assert_not_reached ("bad var_type");
|
gdb_assert_not_reached ("bad var_type");
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::move (stb.string ());
|
return stb.release ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -635,7 +635,7 @@ public:
|
||||||
PopUserExpressionPolicy::pop_user_expression (&buf);
|
PopUserExpressionPolicy::pop_user_expression (&buf);
|
||||||
|
|
||||||
AddCodeFooterPolicy::add_code_footer (m_instance->scope (), &buf);
|
AddCodeFooterPolicy::add_code_footer (m_instance->scope (), &buf);
|
||||||
return buf.string ();
|
return buf.release ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -109,7 +109,7 @@ tyscm_type_name (struct type *type)
|
||||||
string_file stb;
|
string_file stb;
|
||||||
|
|
||||||
LA_PRINT_TYPE (type, "", &stb, -1, 0, &type_print_raw_options);
|
LA_PRINT_TYPE (type, "", &stb, -1, 0, &type_print_raw_options);
|
||||||
return std::move (stb.string ());
|
return stb.release ();
|
||||||
}
|
}
|
||||||
catch (const gdb_exception &except)
|
catch (const gdb_exception &except)
|
||||||
{
|
{
|
||||||
|
|
|
@ -447,7 +447,7 @@ explicit_to_string_internal (bool as_linespec,
|
||||||
explicit_loc->line_offset.offset);
|
explicit_loc->line_offset.offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::move (buf.string ());
|
return buf.release ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* See description in location.h. */
|
/* See description in location.h. */
|
||||||
|
|
|
@ -295,8 +295,7 @@ save_completion_result (const test_options_opts &opts, bool res,
|
||||||
|
|
||||||
stream.puts ("1 ");
|
stream.puts ("1 ");
|
||||||
opts.dump (&stream, text);
|
opts.dump (&stream, text);
|
||||||
maintenance_test_options_command_completion_text
|
maintenance_test_options_command_completion_text = stream.release ();
|
||||||
= std::move (stream.string ());
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -9519,7 +9519,7 @@ escape_buffer (const char *buf, int n)
|
||||||
string_file stb;
|
string_file stb;
|
||||||
|
|
||||||
stb.putstrn (buf, n, '\\');
|
stb.putstrn (buf, n, '\\');
|
||||||
return std::move (stb.string ());
|
return stb.release ();
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -130,14 +130,12 @@ tui_disassemble (struct gdbarch *gdbarch,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Capture the disassembled instruction. */
|
/* Capture the disassembled instruction. */
|
||||||
tal.insn = std::move (gdb_dis_out.string ());
|
tal.insn = gdb_dis_out.release ();
|
||||||
gdb_dis_out.clear ();
|
|
||||||
|
|
||||||
/* And capture the address the instruction is at. */
|
/* And capture the address the instruction is at. */
|
||||||
tal.addr = orig_pc;
|
tal.addr = orig_pc;
|
||||||
print_address (gdbarch, orig_pc, &gdb_dis_out);
|
print_address (gdbarch, orig_pc, &gdb_dis_out);
|
||||||
tal.addr_string = std::move (gdb_dis_out.string ());
|
tal.addr_string = std::move (gdb_dis_out.release ());
|
||||||
gdb_dis_out.clear ();
|
|
||||||
|
|
||||||
if (addr_size != nullptr)
|
if (addr_size != nullptr)
|
||||||
{
|
{
|
||||||
|
|
|
@ -100,7 +100,7 @@ tui_register_format (struct frame_info *frame, int regnum)
|
||||||
gdbarch_print_registers_info (gdbarch, &stream, frame, regnum, 1);
|
gdbarch_print_registers_info (gdbarch, &stream, frame, regnum, 1);
|
||||||
|
|
||||||
/* Remove the possible \n. */
|
/* Remove the possible \n. */
|
||||||
std::string &str = stream.string ();
|
std::string str = stream.release ();
|
||||||
if (!str.empty () && str.back () == '\n')
|
if (!str.empty () && str.back () == '\n')
|
||||||
str.resize (str.size () - 1);
|
str.resize (str.size () - 1);
|
||||||
|
|
||||||
|
|
|
@ -181,12 +181,14 @@ tui_locator_window::make_status_line () const
|
||||||
string.puts (pc_buf);
|
string.puts (pc_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.size () < status_size)
|
std::string string_val = string.release ();
|
||||||
string.puts (n_spaces (status_size - string.size ()));
|
|
||||||
else if (string.size () > status_size)
|
|
||||||
string.string ().erase (status_size, string.size ());
|
|
||||||
|
|
||||||
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
|
/* Get a printable name for the function at the address. The symbol
|
||||||
|
|
|
@ -406,7 +406,7 @@ type_to_string (struct type *type)
|
||||||
string_file stb;
|
string_file stb;
|
||||||
|
|
||||||
type_print (type, "", &stb, -1);
|
type_print (type, "", &stb, -1);
|
||||||
return std::move (stb.string ());
|
return stb.release ();
|
||||||
}
|
}
|
||||||
catch (const gdb_exception &except)
|
catch (const gdb_exception &except)
|
||||||
{
|
{
|
||||||
|
|
|
@ -160,17 +160,19 @@ public:
|
||||||
/* string_file-specific public API. */
|
/* string_file-specific public API. */
|
||||||
|
|
||||||
/* Accesses the std::string containing the entire output collected
|
/* 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
|
/* Return an std::string containing the entire output collected so far.
|
||||||
string contents out of the string_file. E.g.:
|
|
||||||
|
|
||||||
string_file buf;
|
The internal buffer is cleared, such that it's ready to build a new
|
||||||
buf.printf (....);
|
string. */
|
||||||
buf.printf (....);
|
std::string release ()
|
||||||
return std::move (buf.string ());
|
{
|
||||||
*/
|
std::string ret = std::move (m_string);
|
||||||
std::string &string () { return m_string; }
|
m_string.clear ();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/* Provide a few convenience methods with the same API as the
|
/* Provide a few convenience methods with the same API as the
|
||||||
underlying std::string. */
|
underlying std::string. */
|
||||||
|
|
|
@ -1986,7 +1986,7 @@ vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
|
||||||
/* Print the message. */
|
/* Print the message. */
|
||||||
string_file sfile;
|
string_file sfile;
|
||||||
cli_ui_out (&sfile, 0).vmessage (ui_file_style (), format, args);
|
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);
|
fputs_unfiltered (linebuffer.c_str (), stream);
|
||||||
|
|
||||||
size_t len = linebuffer.length ();
|
size_t len = linebuffer.length ();
|
||||||
|
|
|
@ -2242,7 +2242,7 @@ varobj_value_get_print_value (struct value *value,
|
||||||
/* All other cases. */
|
/* All other cases. */
|
||||||
common_val_print (value, &stb, 0, &opts, current_language);
|
common_val_print (value, &stb, 0, &opts, current_language);
|
||||||
|
|
||||||
return std::move (stb.string ());
|
return stb.release ();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue