C++ify mi_parse
authorTom Tromey <tom@tromey.com>
Fri, 7 Apr 2017 21:34:02 +0000 (15:34 -0600)
committerTom Tromey <tom@tromey.com>
Wed, 12 Apr 2017 17:16:18 +0000 (11:16 -0600)
This changes mi_parse to return a unique_ptr, and to use "new"; then
fixes up the users.  This allows removing one cleanup.

gdb/ChangeLog
2017-04-12  Tom Tromey  <tom@tromey.com>

* mi/mi-parse.h (struct mi_parse): Add constructor, destructor.
(mi_parse): Update return type.
(mi_parse_free): Remove.
* mi/mi-parse.c (mi_parse::mi_parse): New constructor.
(mi_parse::~mi_parse): Rename from mi_parse_free.
(mi_parse_cleanup): Remove.
(mi_parse): Return a unique_ptr.  Use new.
* mi/mi-main.c (mi_execute_command): Update.

gdb/ChangeLog
gdb/mi/mi-main.c
gdb/mi/mi-parse.c
gdb/mi/mi-parse.h

index 45c764ea2df6c2715e16d46cbcb04481f6bfdab5..feee3e738fc62a987b0ceb9bca25c93f6513b86b 100644 (file)
@@ -1,3 +1,14 @@
+2017-04-12  Tom Tromey  <tom@tromey.com>
+
+       * mi/mi-parse.h (struct mi_parse): Add constructor, destructor.
+       (mi_parse): Update return type.
+       (mi_parse_free): Remove.
+       * mi/mi-parse.c (mi_parse::mi_parse): New constructor.
+       (mi_parse::~mi_parse): Rename from mi_parse_free.
+       (mi_parse_cleanup): Remove.
+       (mi_parse): Return a unique_ptr.  Use new.
+       * mi/mi-main.c (mi_execute_command): Update.
+
 2017-04-12  Tom Tromey  <tom@tromey.com>
 
        * location.c (explicit_location_lex_one): Return a
index 91fe1044a168274427271838f894071f633f0c60..d99c40e928ebc883ea63de33a56acc0597018eb8 100644 (file)
@@ -2117,7 +2117,7 @@ void
 mi_execute_command (const char *cmd, int from_tty)
 {
   char *token;
-  struct mi_parse *command = NULL;
+  std::unique_ptr<struct mi_parse> command;
 
   /* This is to handle EOF (^D). We just quit gdb.  */
   /* FIXME: we should call some API function here.  */
@@ -2158,7 +2158,7 @@ mi_execute_command (const char *cmd, int from_tty)
 
       TRY
        {
-         captured_mi_execute_command (current_uiout, command);
+         captured_mi_execute_command (current_uiout, command.get ());
        }
       CATCH (result, RETURN_MASK_ALL)
        {
@@ -2186,7 +2186,7 @@ mi_execute_command (const char *cmd, int from_tty)
          && thread_count () != 0
          /* If the command already reports the thread change, no need to do it
             again.  */
-         && !command_notifies_uscc_observer (command))
+         && !command_notifies_uscc_observer (command.get ()))
        {
          struct mi_interp *mi = (struct mi_interp *) top_level_interpreter ();
          int report_change = 0;
@@ -2211,8 +2211,6 @@ mi_execute_command (const char *cmd, int from_tty)
            }
        }
 
-      mi_parse_free (command);
-
       do_cleanups (cleanup);
     }
 }
index 19cbb146af328464c2aa431558fdb4c7018a876f..fea85d549cd19b4377fb190135af92dce22bebbb 100644 (file)
@@ -208,45 +208,38 @@ mi_parse_argv (const char *args, struct mi_parse *parse)
     }
 }
 
-void
-mi_parse_free (struct mi_parse *parse)
+mi_parse::mi_parse ()
+  : op (MI_COMMAND),
+    command (NULL),
+    token (NULL),
+    cmd (NULL),
+    cmd_start (NULL),
+    args (NULL),
+    argv (NULL),
+    argc (0),
+    all (0),
+    thread_group (-1),
+    thread (-1),
+    frame (-1),
+    language (language_unknown)
 {
-  if (parse == NULL)
-    return;
-  if (parse->command != NULL)
-    xfree (parse->command);
-  if (parse->token != NULL)
-    xfree (parse->token);
-  if (parse->args != NULL)
-    xfree (parse->args);
-  if (parse->argv != NULL)
-    freeargv (parse->argv);
-  xfree (parse);
 }
 
-/* A cleanup that calls mi_parse_free.  */
-
-static void
-mi_parse_cleanup (void *arg)
+mi_parse::~mi_parse ()
 {
-  mi_parse_free ((struct mi_parse *) arg);
+  xfree (command);
+  xfree (token);
+  xfree (args);
+  freeargv (argv);
 }
 
-struct mi_parse *
+std::unique_ptr<struct mi_parse>
 mi_parse (const char *cmd, char **token)
 {
   const char *chp;
-  struct mi_parse *parse = XNEW (struct mi_parse);
   struct cleanup *cleanup;
 
-  memset (parse, 0, sizeof (*parse));
-  parse->all = 0;
-  parse->thread_group = -1;
-  parse->thread = -1;
-  parse->frame = -1;
-  parse->language = language_unknown;
-
-  cleanup = make_cleanup (mi_parse_cleanup, parse);
+  std::unique_ptr<struct mi_parse> parse (new struct mi_parse);
 
   /* Before starting, skip leading white space.  */
   cmd = skip_spaces_const (cmd);
@@ -265,8 +258,6 @@ mi_parse (const char *cmd, char **token)
       parse->command = xstrdup (chp);
       parse->op = CLI_COMMAND;
 
-      discard_cleanups (cleanup);
-
       return parse;
     }
 
@@ -383,7 +374,7 @@ mi_parse (const char *cmd, char **token)
      list.  */
   if (parse->cmd->argv_func != NULL)
     {
-      mi_parse_argv (chp, parse);
+      mi_parse_argv (chp, parse.get ());
       if (parse->argv == NULL)
        error (_("Problem parsing arguments: %s %s"), parse->command, chp);
     }
@@ -394,8 +385,6 @@ mi_parse (const char *cmd, char **token)
   if (parse->cmd->cli.cmd != NULL)
     parse->args = xstrdup (chp);
 
-  discard_cleanups (cleanup);
-
   /* Fully parsed, flag as an MI command.  */
   parse->op = MI_COMMAND;
   return parse;
index b11e5d383fd2118c84fa74f32c8dfd6afa6ae2b8..a4903df26a457e2d63e4f848cc37f50ae9a6958e 100644 (file)
@@ -41,6 +41,12 @@ enum mi_command_type
 
 struct mi_parse
   {
+    mi_parse ();
+    ~mi_parse ();
+
+    mi_parse (const mi_parse &) = delete;
+    mi_parse &operator= (const mi_parse &) = delete;
+
     enum mi_command_type op;
     char *command;
     char *token;
@@ -67,11 +73,8 @@ struct mi_parse
    the TOKEN field of the resultant mi_parse object, to be freed by
    mi_parse_free.  */
 
-extern struct mi_parse *mi_parse (const char *cmd, char **token);
-
-/* Free a command returned by mi_parse_command.  */
-
-extern void mi_parse_free (struct mi_parse *cmd);
+extern std::unique_ptr<struct mi_parse> mi_parse (const char *cmd,
+                                                 char **token);
 
 /* Parse a string argument into a print_values value.  */
 
This page took 0.030276 seconds and 4 git commands to generate.