Browse Source

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
gdb-12-branch
Simon Marchi 4 years ago
parent
commit
5d10a2041e
  1. 2
      gdb/ada-lang.c
  2. 2
      gdb/ada-valprint.c
  3. 4
      gdb/ada-varobj.c
  4. 6
      gdb/c-exp.y
  5. 2
      gdb/cli/cli-setshow.c
  6. 2
      gdb/compile/compile-c-support.c
  7. 2
      gdb/guile/scm-type.c
  8. 2
      gdb/location.c
  9. 3
      gdb/maint-test-options.c
  10. 2
      gdb/remote.c
  11. 6
      gdb/tui/tui-disasm.c
  12. 2
      gdb/tui/tui-regs.c
  13. 8
      gdb/tui/tui-stack.c
  14. 2
      gdb/typeprint.c
  15. 20
      gdb/ui-file.h
  16. 2
      gdb/utils.c
  17. 2
      gdb/varobj.c

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

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

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

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

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

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

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

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

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

2
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

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

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

8
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

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

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

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

2
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

Loading…
Cancel
Save