]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Use unique_xmalloc_ptr for mi_parse::command
authorTom Tromey <tromey@adacore.com>
Fri, 9 Jun 2023 16:35:20 +0000 (10:35 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 20 Jun 2023 12:23:30 +0000 (06:23 -0600)
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>
gdb/mi/mi-cmds.c
gdb/mi/mi-main.c
gdb/mi/mi-parse.c
gdb/mi/mi-parse.h
gdb/python/py-micmd.c

index f8cae4131d89309dd3aa17a60d5afbc97fd99052..5ea31fc98d1a485f4fd66dd8eb96ffb09600655f 100644 (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:
index 7d671657a443f57301fb6a823a6c20692e8b163a..9108cf505c7cbf2d9600cff23e04decf1c04351e 100644 (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.  */
index aceecad6db6e205999f669e547cbd21b53e2e817..a9b9cdaf88fcd7dc3eb41901115f975a9f904e60 100644 (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);
index 78fb414b4f0004b50419fdce8bc7106211ceb07e..c729e94c1f0bd7abaa470789f3e78155c980085d 100644 (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;
index 7027210d0d84999fd4610c96fde9d3955d2bc455..01fc6060ece457abd3e7a0722d31adf6e0576a5c 100644 (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 ());