public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 12/23] More uses of scoped_restore
Date: Wed, 03 May 2017 22:46:00 -0000	[thread overview]
Message-ID: <20170503224626.2818-13-tom@tromey.com> (raw)
In-Reply-To: <20170503224626.2818-1-tom@tromey.com>

There were a few more places in gdb that could easily use
scoped_restore, replacing some cleanups.

2017-05-02  Tom Tromey  <tom@tromey.com>

	* reverse.c (exec_direction_default): Remove.
	(exec_reverse_once): Use scoped_restore.
	* remote.c (restore_remote_timeout): Remove.
	(remote_flash_erase, remote_flash_write, remote_flash_done)
	(readchar, remote_serial_write): Use scoped_restore.
	* cli/cli-script.c (struct source_cleanup_lines_args)
	(source_cleanup_lines): Remove.
	(script_from_file): Use scoped_restore.
	* cli/cli-cmds.c (source_verbose_cleanup): Remove.
	(source_command): Use scoped_restore.
---
 gdb/ChangeLog        | 13 +++++++++++
 gdb/cli/cli-cmds.c   | 16 +-------------
 gdb/cli/cli-script.c | 60 +++++++++++++++-----------------------------------
 gdb/remote.c         | 62 +++++++++++++++++-----------------------------------
 gdb/reverse.c        | 18 ++++-----------
 5 files changed, 56 insertions(+), 113 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index ddf87a3..d288de4 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,18 @@
 2017-05-02  Tom Tromey  <tom@tromey.com>
 
+	* reverse.c (exec_direction_default): Remove.
+	(exec_reverse_once): Use scoped_restore.
+	* remote.c (restore_remote_timeout): Remove.
+	(remote_flash_erase, remote_flash_write, remote_flash_done)
+	(readchar, remote_serial_write): Use scoped_restore.
+	* cli/cli-script.c (struct source_cleanup_lines_args)
+	(source_cleanup_lines): Remove.
+	(script_from_file): Use scoped_restore.
+	* cli/cli-cmds.c (source_verbose_cleanup): Remove.
+	(source_command): Use scoped_restore.
+
+2017-05-02  Tom Tromey  <tom@tromey.com>
+
 	* utils.h (make_cleanup_free_so): Remove.
 	* utils.c (do_free_so, make_cleanup_free_so): Remove.
 	* solist.h (struct so_deleter): New.
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index c75dd16..3dc8b9c 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -633,26 +633,14 @@ source_script (const char *file, int from_tty)
   source_script_with_search (file, from_tty, 0);
 }
 
-/* Return the source_verbose global variable to its previous state
-   on exit from the source command, by whatever means.  */
-static void
-source_verbose_cleanup (void *old_value)
-{
-  source_verbose = *(int *)old_value;
-  xfree (old_value);
-}
-
 static void
 source_command (char *args, int from_tty)
 {
-  struct cleanup *old_cleanups;
   char *file = args;
   int *old_source_verbose = XNEW (int);
   int search_path = 0;
 
-  *old_source_verbose = source_verbose;
-  old_cleanups = make_cleanup (source_verbose_cleanup, 
-			       old_source_verbose);
+  scoped_restore save_source_verbose = make_scoped_restore (&source_verbose);
 
   /* -v causes the source command to run in verbose mode.
      -s causes the file to be searched in the source search path,
@@ -693,8 +681,6 @@ source_command (char *args, int from_tty)
     }
 
   source_script_with_search (file, from_tty, search_path);
-
-  do_cleanups (old_cleanups);
 }
 
 
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index e0e27ef..0aa1848 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -1584,58 +1584,34 @@ document_command (char *comname, int from_tty)
   }
 }
 \f
-struct source_cleanup_lines_args
-{
-  int old_line;
-  const char *old_file;
-};
-
-static void
-source_cleanup_lines (void *args)
-{
-  struct source_cleanup_lines_args *p =
-    (struct source_cleanup_lines_args *) args;
-
-  source_line_number = p->old_line;
-  source_file_name = p->old_file;
-}
-
 /* Used to implement source_command.  */
 
 void
 script_from_file (FILE *stream, const char *file)
 {
-  struct cleanup *old_cleanups;
-  struct source_cleanup_lines_args old_lines;
-
   if (stream == NULL)
     internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
 
-  old_lines.old_line = source_line_number;
-  old_lines.old_file = source_file_name;
-  old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
-  source_line_number = 0;
-  source_file_name = file;
-
-  {
-    scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
+  scoped_restore restore_line_number
+    = make_scoped_restore (&source_line_number, 0);
+  scoped_restore resotre_file
+    = make_scoped_restore (&source_file_name, file);
 
-    TRY
-      {
-	read_command_file (stream);
-      }
-    CATCH (e, RETURN_MASK_ERROR)
-      {
-	/* Re-throw the error, but with the file name information
-	   prepended.  */
-	throw_error (e.error,
-		     _("%s:%d: Error in sourced command file:\n%s"),
-		     source_file_name, source_line_number, e.message);
-      }
-    END_CATCH
-  }
+  scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
-  do_cleanups (old_cleanups);
+  TRY
+    {
+      read_command_file (stream);
+    }
+  CATCH (e, RETURN_MASK_ERROR)
+    {
+      /* Re-throw the error, but with the file name information
+	 prepended.  */
+      throw_error (e.error,
+		   _("%s:%d: Error in sourced command file:\n%s"),
+		   source_file_name, source_line_number, e.message);
+    }
+  END_CATCH
 }
 
 /* Print the definition of user command C to STREAM.  Or, if C is a
diff --git a/gdb/remote.c b/gdb/remote.c
index 1db2e57..ecd0ce0 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -72,6 +72,7 @@
 #include "btrace.h"
 #include "record-btrace.h"
 #include <algorithm>
+#include "common/scoped_restore.h"
 
 /* Temp hacks for tracepoint encoding migration.  */
 static char *target_buf;
@@ -8474,14 +8475,6 @@ remote_send_printf (const char *format, ...)
   return packet_check_result (rs->buf);
 }
 
-static void
-restore_remote_timeout (void *p)
-{
-  int value = *(int *)p;
-
-  remote_timeout = value;
-}
-
 /* Flash writing can take quite some time.  We'll set
    effectively infinite timeout for flash operations.
    In future, we'll need to decide on a better approach.  */
@@ -8492,12 +8485,9 @@ remote_flash_erase (struct target_ops *ops,
                     ULONGEST address, LONGEST length)
 {
   int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
-  int saved_remote_timeout = remote_timeout;
   enum packet_result ret;
-  struct cleanup *back_to = make_cleanup (restore_remote_timeout,
-                                          &saved_remote_timeout);
-
-  remote_timeout = remote_flash_timeout;
+  scoped_restore restore_timeout
+    = make_scoped_restore (&remote_timeout, remote_flash_timeout);
 
   ret = remote_send_printf ("vFlashErase:%s,%s",
 			    phex (address, addr_size),
@@ -8511,8 +8501,6 @@ remote_flash_erase (struct target_ops *ops,
     default:
       break;
     }
-
-  do_cleanups (back_to);
 }
 
 static enum target_xfer_status
@@ -8520,30 +8508,21 @@ remote_flash_write (struct target_ops *ops, ULONGEST address,
 		    ULONGEST length, ULONGEST *xfered_len,
 		    const gdb_byte *data)
 {
-  int saved_remote_timeout = remote_timeout;
-  enum target_xfer_status ret;
-  struct cleanup *back_to = make_cleanup (restore_remote_timeout,
-					  &saved_remote_timeout);
-
-  remote_timeout = remote_flash_timeout;
-  ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
-				xfered_len,'X', 0);
-  do_cleanups (back_to);
-
-  return ret;
+  scoped_restore restore_timeout
+    = make_scoped_restore (&remote_timeout, remote_flash_timeout);
+  return remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
+				 xfered_len,'X', 0);
 }
 
 static void
 remote_flash_done (struct target_ops *ops)
 {
-  int saved_remote_timeout = remote_timeout;
   int ret;
-  struct cleanup *back_to = make_cleanup (restore_remote_timeout,
-                                          &saved_remote_timeout);
 
-  remote_timeout = remote_flash_timeout;
+  scoped_restore restore_timeout
+    = make_scoped_restore (&remote_timeout, remote_flash_timeout);
+
   ret = remote_send_printf ("vFlashDone");
-  do_cleanups (back_to);
 
   switch (ret)
     {
@@ -8591,18 +8570,18 @@ readchar (int timeout)
 {
   int ch;
   struct remote_state *rs = get_remote_state ();
-  struct cleanup *old_chain;
 
-  old_chain = make_cleanup_override_quit_handler (remote_serial_quit_handler);
-
-  rs->got_ctrlc_during_io = 0;
+  {
+    scoped_restore restore_quit
+      = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
 
-  ch = serial_readchar (rs->remote_desc, timeout);
+    rs->got_ctrlc_during_io = 0;
 
-  if (rs->got_ctrlc_during_io)
-    set_quit_flag ();
+    ch = serial_readchar (rs->remote_desc, timeout);
 
-  do_cleanups (old_chain);
+    if (rs->got_ctrlc_during_io)
+      set_quit_flag ();
+  }
 
   if (ch >= 0)
     return ch;
@@ -8635,7 +8614,8 @@ remote_serial_write (const char *str, int len)
   struct remote_state *rs = get_remote_state ();
   struct cleanup *old_chain;
 
-  old_chain = make_cleanup_override_quit_handler (remote_serial_quit_handler);
+  scoped_restore restore_quit
+    = make_scoped_restore (&quit_handler, remote_serial_quit_handler);
 
   rs->got_ctrlc_during_io = 0;
 
@@ -8647,8 +8627,6 @@ remote_serial_write (const char *str, int len)
 
   if (rs->got_ctrlc_during_io)
     set_quit_flag ();
-
-  do_cleanups (old_chain);
 }
 
 /* Send the command in *BUF to the remote machine, and read the reply
diff --git a/gdb/reverse.c b/gdb/reverse.c
index 4080616..374a215 100644
--- a/gdb/reverse.c
+++ b/gdb/reverse.c
@@ -30,13 +30,6 @@
 /* User interface:
    reverse-step, reverse-next etc.  */
 
-static void
-exec_direction_default (void *notused)
-{
-  /* Return execution direction to default state.  */
-  execution_direction = EXEC_FORWARD;
-}
-
 /* exec_reverse_once -- accepts an arbitrary gdb command (string), 
    and executes it with exec-direction set to 'reverse'.
 
@@ -45,9 +38,7 @@ exec_direction_default (void *notused)
 static void
 exec_reverse_once (const char *cmd, char *args, int from_tty)
 {
-  char *reverse_command;
   enum exec_direction_kind dir = execution_direction;
-  struct cleanup *old_chain;
 
   if (dir == EXEC_REVERSE)
     error (_("Already in reverse mode.  Use '%s' or 'set exec-dir forward'."),
@@ -56,12 +47,11 @@ exec_reverse_once (const char *cmd, char *args, int from_tty)
   if (!target_can_execute_reverse)
     error (_("Target %s does not support this command."), target_shortname);
 
-  reverse_command = xstrprintf ("%s %s", cmd, args ? args : "");
-  old_chain = make_cleanup (exec_direction_default, NULL);
-  make_cleanup (xfree, reverse_command);
+  std::string reverse_command = string_printf ("%s %s", cmd, args ? args : "");
+  scoped_restore restore_exec_dir
+    = make_scoped_restore (&execution_direction, EXEC_REVERSE);
   execution_direction = EXEC_REVERSE;
-  execute_command (reverse_command, from_tty);
-  do_cleanups (old_chain);
+  execute_command (&reverse_command[0], from_tty);
 }
 
 static void
-- 
2.9.3

  parent reply	other threads:[~2017-05-03 22:46 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-03 22:46 [RFA 00/23] More miscellaneous C++-ification Tom Tromey
2017-05-03 22:46 ` [RFA 23/23] Use gdb_argv_up in Python Tom Tromey
2017-06-05 16:29   ` Pedro Alves
2017-07-19 22:52     ` Tom Tromey
2017-07-19 22:51       ` Tom Tromey
2017-05-03 22:46 ` [RFA 02/23] Introduce and use gdb_file_up Tom Tromey
2017-06-02 17:08   ` Pedro Alves
2017-05-03 22:46 ` [RFA 09/23] Remove close cleanup Tom Tromey
2017-06-02 18:08   ` Pedro Alves
2017-07-19 22:52     ` Tom Tromey
2017-07-31 19:08       ` Simon Marchi
2017-08-01 21:52         ` Tom Tromey
2017-05-03 22:46 ` [RFA 01/23] Introduce and use ui_out_emit_table Tom Tromey
2017-06-02 17:04   ` Pedro Alves
2017-05-03 22:46 ` [RFA 10/23] Remove make_cleanup_restore_current_language Tom Tromey
2017-06-02 18:18   ` Pedro Alves
2017-06-05 13:09     ` Tom Tromey
2017-05-03 22:46 ` [RFA 03/23] Use gdb_file_up in find_and_open_script Tom Tromey
2017-06-02 17:24   ` Pedro Alves
2017-07-23 16:08     ` Tom Tromey
2017-05-03 22:46 ` Tom Tromey [this message]
2017-06-02 18:31   ` [RFA 12/23] More uses of scoped_restore Pedro Alves
2017-05-03 22:46 ` [RFA 20/23] Avoid some manual memory management in Python Tom Tromey
2017-06-05 13:55   ` Pedro Alves
2017-05-03 22:46 ` [RFA 13/23] Replace tui_restore_gdbout with scoped_restore Tom Tromey
2017-06-02 18:34   ` Pedro Alves
2017-05-03 22:46 ` [RFA 17/23] Use a scoped_restore for user_call_depth Tom Tromey
2017-06-05 13:32   ` Pedro Alves
2017-06-05 16:56     ` Tom Tromey
2017-05-03 22:46 ` [RFA 04/23] Use gdb_file_up in fbsd-nat.c Tom Tromey
2017-05-03 23:52   ` John Baldwin
2017-05-03 22:46 ` [RFA 14/23] Use unique_xmalloc_ptr in jit.c Tom Tromey
2017-06-02 18:42   ` Pedro Alves
2017-06-05 13:09     ` Tom Tromey
2017-05-03 22:46 ` [RFA 21/23] Remove a cleanup in Python Tom Tromey
2017-06-05 13:56   ` Pedro Alves
2017-05-03 22:46 ` [RFA 08/23] Remove an unlink cleanup Tom Tromey
2017-06-02 17:37   ` Pedro Alves
2017-05-03 22:46 ` [RFA 22/23] Make gdb_buildargv return a unique pointer Tom Tromey
2017-06-05 16:21   ` Pedro Alves
2017-07-23 17:26     ` Tom Tromey
2017-05-03 22:46 ` [RFA 11/23] Remove make_cleanup_free_so Tom Tromey
2017-06-02 18:25   ` Pedro Alves
2017-05-03 22:46 ` [RFA 06/23] Change open_terminal_stream to return a gdb_file_up Tom Tromey
2017-06-02 17:31   ` Pedro Alves
2017-05-03 22:46 ` [RFA 18/23] Use a scoped_restore for command_nest_depth Tom Tromey
2017-06-05 13:38   ` Pedro Alves
2017-05-03 22:46 ` [RFA 16/23] Remove in_user_command Tom Tromey
2017-06-05 13:27   ` Pedro Alves
2017-05-03 22:46 ` [RFA 19/23] Replace do_restore_instream_cleanup with scoped_restore Tom Tromey
2017-06-05 13:49   ` Pedro Alves
2017-05-03 22:46 ` [RFA 15/23] Use std::vector to avoid cleanups Tom Tromey
2017-06-02 19:22   ` Pedro Alves
2017-07-23 16:06     ` Tom Tromey
2017-05-03 22:46 ` [RFA 07/23] Remove make_cleanup_fclose Tom Tromey
2017-06-02 17:32   ` Pedro Alves
2017-05-03 22:46 ` [RFA 05/23] Use gdb_file_up in source.c Tom Tromey
2017-06-02 17:27   ` Pedro Alves
2017-05-29 17:31 ` [RFA 00/23] More miscellaneous C++-ification Tom Tromey

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=20170503224626.2818-13-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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).