Remove cleanups from gdb_readline_wrapper
authorTom Tromey <tom@tromey.com>
Thu, 22 Mar 2018 16:31:41 +0000 (10:31 -0600)
committerTom Tromey <tom@tromey.com>
Tue, 27 Mar 2018 16:46:42 +0000 (10:46 -0600)
This removes some cleanups from gdb_readline_wrapper by changing the
existing gdb_readline_wrapper_cleanup struct to have a constructor and
destructor, and then changing gdb_readline_wrapper to simply
instantiate it on the stack.

gdb/ChangeLog
2018-03-27  Pedro Alves  <palves@redhat.com>
    Tom Tromey  <tom@tromey.com>

* top.c (class gdb_readline_wrapper_cleanup): Add constructor,
destructor.  Now a class.
(gdb_readline_wrapper_cleanup): Remove function.
(gdb_readline_wrapper): Remove cleanups.

gdb/ChangeLog
gdb/top.c

index a3e99e6164b8a85348a20343cb12827ee032b08f..ca67054f509540e8cedfcd8a745b5e6977e0f8d3 100644 (file)
@@ -1,3 +1,11 @@
+2018-03-27  Pedro Alves  <palves@redhat.com>
+           Tom Tromey  <tom@tromey.com>
+
+       * top.c (class gdb_readline_wrapper_cleanup): Add constructor,
+       destructor.  Now a class.
+       (gdb_readline_wrapper_cleanup): Remove function.
+       (gdb_readline_wrapper): Remove cleanups.
+
 2018-03-27  Tom Tromey  <tom@tromey.com>
 
        * typeprint.h (struct type_print_options) <local_typedefs,
index 14387b9f1a5ea7d7347db7309459d50c6d1ef0a6..8903a92983452bc5315538d6cbc6cb689bd67fae 100644 (file)
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -917,76 +917,72 @@ gdb_readline_wrapper_line (char *line)
     gdb_rl_callback_handler_remove ();
 }
 
-struct gdb_readline_wrapper_cleanup
-  {
-    void (*handler_orig) (char *);
-    int already_prompted_orig;
-
-    /* Whether the target was async.  */
-    int target_is_async_orig;
-  };
-
-static void
-gdb_readline_wrapper_cleanup (void *arg)
+class gdb_readline_wrapper_cleanup
 {
-  struct ui *ui = current_ui;
-  struct gdb_readline_wrapper_cleanup *cleanup
-    = (struct gdb_readline_wrapper_cleanup *) arg;
+public:
+  gdb_readline_wrapper_cleanup ()
+    : m_handler_orig (current_ui->input_handler),
+      m_already_prompted_orig (current_ui->command_editing
+                              ? rl_already_prompted : 0),
+      m_target_is_async_orig (target_is_async_p ()),
+      m_save_ui (&current_ui)
+  {
+    current_ui->input_handler = gdb_readline_wrapper_line;
+    current_ui->secondary_prompt_depth++;
 
-  if (ui->command_editing)
-    rl_already_prompted = cleanup->already_prompted_orig;
+    if (m_target_is_async_orig)
+      target_async (0);
+  }
 
-  gdb_assert (ui->input_handler == gdb_readline_wrapper_line);
-  ui->input_handler = cleanup->handler_orig;
+  ~gdb_readline_wrapper_cleanup ()
+  {
+    struct ui *ui = current_ui;
 
-  /* Don't restore our input handler in readline yet.  That would make
-     readline prep the terminal (putting it in raw mode), while the
-     line we just read may trigger execution of a command that expects
-     the terminal in the default cooked/canonical mode, such as e.g.,
-     running Python's interactive online help utility.  See
-     gdb_readline_wrapper_line for when we'll reinstall it.  */
+    if (ui->command_editing)
+      rl_already_prompted = m_already_prompted_orig;
 
-  gdb_readline_wrapper_result = NULL;
-  gdb_readline_wrapper_done = 0;
-  ui->secondary_prompt_depth--;
-  gdb_assert (ui->secondary_prompt_depth >= 0);
+    gdb_assert (ui->input_handler == gdb_readline_wrapper_line);
+    ui->input_handler = m_handler_orig;
 
-  after_char_processing_hook = saved_after_char_processing_hook;
-  saved_after_char_processing_hook = NULL;
+    /* Don't restore our input handler in readline yet.  That would make
+       readline prep the terminal (putting it in raw mode), while the
+       line we just read may trigger execution of a command that expects
+       the terminal in the default cooked/canonical mode, such as e.g.,
+       running Python's interactive online help utility.  See
+       gdb_readline_wrapper_line for when we'll reinstall it.  */
 
-  if (cleanup->target_is_async_orig)
-    target_async (1);
+    gdb_readline_wrapper_result = NULL;
+    gdb_readline_wrapper_done = 0;
+    ui->secondary_prompt_depth--;
+    gdb_assert (ui->secondary_prompt_depth >= 0);
 
-  xfree (cleanup);
-}
+    after_char_processing_hook = saved_after_char_processing_hook;
+    saved_after_char_processing_hook = NULL;
 
-char *
-gdb_readline_wrapper (const char *prompt)
-{
-  struct ui *ui = current_ui;
-  struct cleanup *back_to;
-  struct gdb_readline_wrapper_cleanup *cleanup;
-  char *retval;
+    if (m_target_is_async_orig)
+      target_async (1);
+  }
 
-  cleanup = XNEW (struct gdb_readline_wrapper_cleanup);
-  cleanup->handler_orig = ui->input_handler;
-  ui->input_handler = gdb_readline_wrapper_line;
+  DISABLE_COPY_AND_ASSIGN (gdb_readline_wrapper_cleanup);
 
-  if (ui->command_editing)
-    cleanup->already_prompted_orig = rl_already_prompted;
-  else
-    cleanup->already_prompted_orig = 0;
+private:
 
-  cleanup->target_is_async_orig = target_is_async_p ();
+  void (*m_handler_orig) (char *);
+  int m_already_prompted_orig;
 
-  ui->secondary_prompt_depth++;
-  back_to = make_cleanup (gdb_readline_wrapper_cleanup, cleanup);
+  /* Whether the target was async.  */
+  int m_target_is_async_orig;
 
   /* Processing events may change the current UI.  */
-  scoped_restore save_ui = make_scoped_restore (&current_ui);
+  scoped_restore_tmpl<struct ui *> m_save_ui;
+};
+
+char *
+gdb_readline_wrapper (const char *prompt)
+{
+  struct ui *ui = current_ui;
 
-  if (cleanup->target_is_async_orig)
-    target_async (0);
+  gdb_readline_wrapper_cleanup cleanup;
 
   /* Display our prompt and prevent double prompt display.  Don't pass
      down a NULL prompt, since that has special meaning for
@@ -1004,9 +1000,7 @@ gdb_readline_wrapper (const char *prompt)
     if (gdb_readline_wrapper_done)
       break;
 
-  retval = gdb_readline_wrapper_result;
-  do_cleanups (back_to);
-  return retval;
+  return gdb_readline_wrapper_result;
 }
 
 \f
This page took 0.034028 seconds and 4 git commands to generate.