Use unique_xmalloc_ptr for mi_parse::command

This changes mi_parse::command to be a unique_xmalloc_ptr and fixes up
all the uses.  This avoids some manual memory management.  std::string
is not used here due to how the Python API works -- this approach
avoids an extra copy there.

Reviewed-by: Keith Seitz <keiths@redhat.com>
This commit is contained in:
Tom Tromey 2023-06-09 10:35:20 -06:00
parent 550194db38
commit 8ca8b801ed
5 changed files with 20 additions and 20 deletions

View file

@ -52,10 +52,10 @@ struct mi_command_mi : public mi_command
parse->parse_argv ();
if (parse->argv == nullptr)
error (_("Problem parsing arguments: %s %s"), parse->command,
error (_("Problem parsing arguments: %s %s"), parse->command.get (),
parse->args ());
this->m_argv_function (parse->command, parse->argv, parse->argc);
this->m_argv_function (parse->command.get (), parse->argv, parse->argc);
}
private:

View file

@ -1821,7 +1821,8 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
if (mi_debug_p)
gdb_printf (gdb_stdlog,
" token=`%s' command=`%s' args=`%s'\n",
context->token.c_str (), context->command, context->args ());
context->token.c_str (), context->command.get (),
context->args ());
mi_cmd_execute (context);
@ -1836,7 +1837,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
gdb_puts (context->token.c_str (), mi->raw_stdout);
/* There's no particularly good reason why target-connect results
in not ^done. Should kill ^connected for MI3. */
gdb_puts (strcmp (context->command, "target-select") == 0
gdb_puts (strcmp (context->command.get (), "target-select") == 0
? "^connected" : "^done", mi->raw_stdout);
mi_out_put (uiout, mi->raw_stdout);
mi_out_rewind (uiout);
@ -1858,10 +1859,10 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
/* This "feature" will be removed as soon as we have a
complete set of mi commands. */
/* Echo the command on the console. */
gdb_printf (gdb_stdlog, "%s\n", context->command);
gdb_printf (gdb_stdlog, "%s\n", context->command.get ());
/* Call the "console" interpreter. */
argv[0] = INTERP_CONSOLE;
argv[1] = context->command;
argv[1] = context->command.get ();
mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
/* If we changed interpreters, DON'T print out anything. */

View file

@ -215,7 +215,6 @@ mi_parse::parse_argv ()
mi_parse::~mi_parse ()
{
xfree (command);
freeargv (argv);
}
@ -307,7 +306,7 @@ mi_parse::make (const char *cmd, std::string *token)
if (*chp != '-')
{
chp = skip_spaces (chp);
parse->command = xstrdup (chp);
parse->command = make_unique_xstrdup (chp);
parse->op = CLI_COMMAND;
return parse;
@ -319,16 +318,14 @@ mi_parse::make (const char *cmd, std::string *token)
for (; *chp && !isspace (*chp); chp++)
;
parse->command = (char *) xmalloc (chp - tmp + 1);
memcpy (parse->command, tmp, chp - tmp);
parse->command[chp - tmp] = '\0';
parse->command = make_unique_xstrndup (tmp, chp - tmp);
}
/* Find the command in the MI table. */
parse->cmd = mi_cmd_lookup (parse->command);
parse->cmd = mi_cmd_lookup (parse->command.get ());
if (parse->cmd == NULL)
throw_error (UNDEFINED_COMMAND_ERROR,
_("Undefined MI command: %s"), parse->command);
_("Undefined MI command: %s"), parse->command.get ());
/* Skip white space following the command. */
chp = skip_spaces (chp);
@ -418,19 +415,19 @@ mi_parse::make (gdb::unique_xmalloc_ptr<char> command,
{
std::unique_ptr<struct mi_parse> parse (new struct mi_parse);
parse->command = command.release ();
parse->command = std::move (command);
parse->token = "";
if (parse->command[0] != '-')
if (parse->command.get ()[0] != '-')
throw_error (UNDEFINED_COMMAND_ERROR,
_("MI command '%s' does not start with '-'"),
parse->command);
parse->command.get ());
/* Find the command in the MI table. */
parse->cmd = mi_cmd_lookup (parse->command + 1);
parse->cmd = mi_cmd_lookup (parse->command.get () + 1);
if (parse->cmd == NULL)
throw_error (UNDEFINED_COMMAND_ERROR,
_("Undefined MI command: %s"), parse->command);
_("Undefined MI command: %s"), parse->command.get ());
/* This over-allocates slightly, but it seems unimportant. */
parse->argv = XCNEWVEC (char *, args.size () + 1);

View file

@ -72,7 +72,9 @@ struct mi_parse
const char *args ();
enum mi_command_type op = MI_COMMAND;
char *command = nullptr;
/* This is not std::string because it avoids a copy in the Python
API case. */
gdb::unique_xmalloc_ptr<char> command;
std::string token;
const struct mi_command *cmd = nullptr;
struct mi_timestamp *cmd_start = nullptr;

View file

@ -358,7 +358,7 @@ mi_command_py::invoke (struct mi_parse *parse) const
parse->parse_argv ();
if (parse->argv == nullptr)
error (_("Problem parsing arguments: %s %s"), parse->command,
error (_("Problem parsing arguments: %s %s"), parse->command.get (),
parse->args ());