Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 10/14] C++ify mi_parse
Date: Sat, 08 Apr 2017 20:12:00 -0000	[thread overview]
Message-ID: <20170408201208.2672-11-tom@tromey.com> (raw)
In-Reply-To: <20170408201208.2672-1-tom@tromey.com>

This changes mi_parse to return a unique_ptr, and to use "new"; then
fixes up the users.  This allows removing one cleanup.

2017-04-07  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): 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     | 10 ++++++++++
 gdb/mi/mi-main.c  |  8 +++-----
 gdb/mi/mi-parse.c | 40 +++++++++-------------------------------
 gdb/mi/mi-parse.h | 16 +++++++++++-----
 4 files changed, 33 insertions(+), 41 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f330664..87bfeb9 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,15 @@
 2017-04-07  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): 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-07  Tom Tromey  <tom@tromey.com>
+
 	* location.c (explicit_location_lex_one): Return a
 	unique_xmalloc_ptr.
 	(string_to_explicit_location): Update.  Remove cleanups.
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 91fe104..d99c40e 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -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);
     }
 }
diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c
index 19cbb14..198d5c6 100644
--- a/gdb/mi/mi-parse.c
+++ b/gdb/mi/mi-parse.c
@@ -208,46 +208,28 @@ mi_parse_argv (const char *args, struct mi_parse *parse)
     }
 }
 
-void
-mi_parse_free (struct mi_parse *parse)
+mi_parse::~mi_parse ()
 {
-  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);
+  xfree (command);
+  xfree (token);
+  xfree (args);
+  freeargv (argv);
 }
 
-/* A cleanup that calls mi_parse_free.  */
-
-static void
-mi_parse_cleanup (void *arg)
-{
-  mi_parse_free ((struct mi_parse *) arg);
-}
-
-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));
+  std::unique_ptr<struct mi_parse> parse (new struct mi_parse);
+  memset (parse.get (), 0, sizeof (struct mi_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);
-
   /* Before starting, skip leading white space.  */
   cmd = skip_spaces_const (cmd);
 
@@ -265,8 +247,6 @@ mi_parse (const char *cmd, char **token)
       parse->command = xstrdup (chp);
       parse->op = CLI_COMMAND;
 
-      discard_cleanups (cleanup);
-
       return parse;
     }
 
@@ -383,7 +363,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 +374,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;
diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h
index b11e5d3..eb19260 100644
--- a/gdb/mi/mi-parse.h
+++ b/gdb/mi/mi-parse.h
@@ -41,6 +41,15 @@ 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 +76,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.  */
 
-- 
2.9.3


  parent reply	other threads:[~2017-04-08 20:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-08 20:12 [RFA 00/14] miscellaneous C++-ificiation Tom Tromey
2017-04-08 20:12 ` [RFA 06/14] Remove cleanup_iconv Tom Tromey
2017-04-10  3:56   ` Simon Marchi
2017-04-10 23:20     ` Tom Tromey
2017-04-08 20:12 ` [RFA 01/14] Introduce event_location_up Tom Tromey
2017-04-10  1:33   ` Simon Marchi
2017-04-10 23:19     ` Tom Tromey
2017-04-08 20:12 ` [RFA 08/14] Remove some cleanups from gnu-v3-abi.c Tom Tromey
2017-04-10  4:09   ` Simon Marchi
2017-04-10 13:57     ` Tom Tromey
2017-04-08 20:12 ` [RFA 11/14] Use scoped_restore in more places Tom Tromey
2017-04-11  1:48   ` Simon Marchi
2017-04-08 20:12 ` [RFA 03/14] Change find_pcs_for_symtab_line to return a std::vector Tom Tromey
2017-04-10  2:49   ` Simon Marchi
2017-04-08 20:12 ` [RFA 09/14] Remove some cleanups from location.c Tom Tromey
2017-04-10  4:43   ` Simon Marchi
2017-04-08 20:12 ` Tom Tromey [this message]
2017-04-11  1:12   ` [RFA 10/14] C++ify mi_parse Simon Marchi
2017-04-08 20:12 ` [RFA 02/14] Introduce command_line_up Tom Tromey
2017-04-10  2:36   ` Simon Marchi
2017-04-10 23:21     ` Tom Tromey
2017-04-08 20:12 ` [RFA 14/14] Use std::vector in compile-loc2c.c Tom Tromey
2017-04-08 20:13 ` [RFA 13/14] Use std::vector in find_instruction_backward Tom Tromey
2017-04-08 20:13 ` [RFA 12/14] Use std::vector in reread_symbols Tom Tromey
2017-04-11  1:59   ` Simon Marchi
2017-04-08 20:13 ` [RFA 04/14] Introduce gdb_dlopen_up Tom Tromey
2017-04-10  3:13   ` Simon Marchi
2017-04-10  9:26   ` Pedro Alves
2017-04-10 23:31     ` Tom Tromey
2017-04-08 20:22 ` [RFA 07/14] Fix up wchar_iterator comment Tom Tromey
2017-04-10  4:05   ` Simon Marchi
2017-04-10 23:29     ` Tom Tromey
2017-04-08 20:23 ` [RFA 05/14] Change increment_reading_symtab to return a scoped_restore Tom Tromey
2017-04-10  3:27   ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170408201208.2672-11-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox