Remove make_cleanup_ui_out_redirect_pop
authorTom Tromey <tom@tromey.com>
Sun, 13 Aug 2017 16:47:32 +0000 (10:47 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 9 Sep 2017 19:46:08 +0000 (13:46 -0600)
This patch introduces ui_out_redirect_pop.  All uses of
make_cleanup_ui_out_redirect_pop are replaced with this new class.

ChangeLog
2017-09-09  Tom Tromey  <tom@tromey.com>

* mi/mi-interp.c (mi_user_selected_context_changed): Use
ui_out_redirect_pop.
* guile/scm-ports.c (ioscm_with_output_to_port_worker): Use
ui_out_redirect_pop.
* utils.c (do_ui_out_redirect_pop)
(make_cleanup_ui_out_redirect_pop): Remove.
* top.c (execute_command_to_string): Use ui_out_redirect_pop.
* utils.h (make_cleanup_ui_out_redirect_pop): Remove.
* ui-out.h (ui_out_redirect_pop): New class.

gdb/ChangeLog
gdb/guile/scm-ports.c
gdb/mi/mi-interp.c
gdb/top.c
gdb/ui-out.h
gdb/utils.c
gdb/utils.h

index 6997e0787bd324bd38ae717beb15607c18bdc0b0..736cdc170fd81fd947d303981eb8062ad12a52c5 100644 (file)
@@ -1,3 +1,15 @@
+2017-09-09  Tom Tromey  <tom@tromey.com>
+
+       * mi/mi-interp.c (mi_user_selected_context_changed): Use
+       ui_out_redirect_pop.
+       * guile/scm-ports.c (ioscm_with_output_to_port_worker): Use
+       ui_out_redirect_pop.
+       * utils.c (do_ui_out_redirect_pop)
+       (make_cleanup_ui_out_redirect_pop): Remove.
+       * top.c (execute_command_to_string): Use ui_out_redirect_pop.
+       * utils.h (make_cleanup_ui_out_redirect_pop): Remove.
+       * ui-out.h (ui_out_redirect_pop): New class.
+
 2017-09-09  Tom Tromey  <tom@tromey.com>
 
        * mi/mi-main.c (output_cores): Use ui_out_emit_list.
index 735abc28ae6e0fba0570425371972be64b4c61a3..78187c4e1d14463de4a4028d54c98359da20fc7a 100644 (file)
@@ -26,6 +26,7 @@
 #include "top.h"
 #include "target.h"
 #include "guile-internal.h"
+#include "common/gdb_optional.h"
 
 #ifdef HAVE_POLL
 #if defined (HAVE_POLL_H)
@@ -477,17 +478,20 @@ ioscm_with_output_to_port_worker (SCM port, SCM thunk, enum oport oport,
   scoped_restore save_file = make_scoped_restore (oport == GDB_STDERR
                                                  ? &gdb_stderr : &gdb_stdout);
 
-  if (oport == GDB_STDERR)
-    gdb_stderr = port_file.get ();
-  else
-    {
-      current_uiout->redirect (port_file.get ());
-      make_cleanup_ui_out_redirect_pop (current_uiout);
+  {
+    gdb::optional<ui_out_redirect_pop> redirect_popper;
+    if (oport == GDB_STDERR)
+      gdb_stderr = port_file.get ();
+    else
+      {
+       current_uiout->redirect (port_file.get ());
+       redirect_popper.emplace (current_uiout);
 
-      gdb_stdout = port_file.get ();
-    }
+       gdb_stdout = port_file.get ();
+      }
 
-  result = gdbscm_safe_call_0 (thunk, NULL);
+    result = gdbscm_safe_call_0 (thunk, NULL);
+  }
 
   do_cleanups (cleanups);
 
index ca8ff1437b740c69efe5aec49edd50470516dde7..f96c59a64b91bec48b73b954a86e5dd5d3405d9a 100644 (file)
@@ -1314,10 +1314,9 @@ mi_user_selected_context_changed (user_selected_what selection)
       mi_uiout = interp_ui_out (top_level_interpreter ());
 
       mi_uiout->redirect (mi->event_channel);
+      ui_out_redirect_pop redirect_popper (mi_uiout);
 
-      old_chain = make_cleanup_ui_out_redirect_pop (mi_uiout);
-
-      make_cleanup_restore_target_terminal ();
+      old_chain = make_cleanup_restore_target_terminal ();
       target_terminal_ours_for_output ();
 
       if (selection & USER_SELECTED_INFERIOR)
index a4fd262d294d4947976639c411a285e7132fbeb1..742c1e7a07e8fae2ee5a6b07b517f0e2ab00939c 100644 (file)
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -679,21 +679,23 @@ execute_command_to_string (char *p, int from_tty)
 
   string_file str_file;
 
-  current_uiout->redirect (&str_file);
-  make_cleanup_ui_out_redirect_pop (current_uiout);
-
-  scoped_restore save_stdout
-    = make_scoped_restore (&gdb_stdout, &str_file);
-  scoped_restore save_stderr
-    = make_scoped_restore (&gdb_stderr, &str_file);
-  scoped_restore save_stdlog
-    = make_scoped_restore (&gdb_stdlog, &str_file);
-  scoped_restore save_stdtarg
-    = make_scoped_restore (&gdb_stdtarg, &str_file);
-  scoped_restore save_stdtargerr
-    = make_scoped_restore (&gdb_stdtargerr, &str_file);
-
-  execute_command (p, from_tty);
+  {
+    current_uiout->redirect (&str_file);
+    ui_out_redirect_pop redirect_popper (current_uiout);
+
+    scoped_restore save_stdout
+      = make_scoped_restore (&gdb_stdout, &str_file);
+    scoped_restore save_stderr
+      = make_scoped_restore (&gdb_stderr, &str_file);
+    scoped_restore save_stdlog
+      = make_scoped_restore (&gdb_stdlog, &str_file);
+    scoped_restore save_stdtarg
+      = make_scoped_restore (&gdb_stdtarg, &str_file);
+    scoped_restore save_stdtargerr
+      = make_scoped_restore (&gdb_stdtargerr, &str_file);
+
+    execute_command (p, from_tty);
+  }
 
   do_cleanups (cleanup);
 
index d9838379b4b608de324da31e0d90016fff248876..9ed2bd29531954a3b68accd975bbe103c2fd3f48 100644 (file)
@@ -242,4 +242,27 @@ private:
   struct ui_out *m_uiout;
 };
 
+/* On destruction, pop the last redirection by calling the uiout's
+   redirect method with a NULL parameter.  */
+class ui_out_redirect_pop
+{
+public:
+
+  ui_out_redirect_pop (ui_out *uiout)
+    : m_uiout (uiout)
+  {
+  }
+
+  ~ui_out_redirect_pop ()
+  {
+    m_uiout->redirect (NULL);
+  }
+
+  ui_out_redirect_pop (const ui_out_redirect_pop &) = delete;
+  ui_out_redirect_pop &operator= (const ui_out_redirect_pop &) = delete;
+
+private:
+  struct ui_out *m_uiout;
+};
+
 #endif /* UI_OUT_H */
index 583c1552c8351e64dff46df96daccec8809c22d6..a7a97e25785f4e0af4446ffa38f4e416cd0f56cf 100644 (file)
@@ -137,25 +137,6 @@ show_pagination_enabled (struct ui_file *file, int from_tty,
    because while they use the "cleanup API" they are not part of the
    "cleanup API".  */
 
-/* Helper function for make_cleanup_ui_out_redirect_pop.  */
-
-static void
-do_ui_out_redirect_pop (void *arg)
-{
-  struct ui_out *uiout = (struct ui_out *) arg;
-
-  uiout->redirect (NULL);
-}
-
-/* Return a new cleanup that pops the last redirection by ui_out_redirect
-   with NULL parameter.  */
-
-struct cleanup *
-make_cleanup_ui_out_redirect_pop (struct ui_out *uiout)
-{
-  return make_cleanup (do_ui_out_redirect_pop, uiout);
-}
-
 static void
 do_free_section_addr_info (void *arg)
 {
index 1c8b95cd2ebe3dcd8623d6db2c6ae08a0f297a67..6d33e8d00dbf13efabc182e2dd8f9ea2251d9ead 100644 (file)
@@ -205,10 +205,6 @@ private:
 \f
 /* Cleanup utilities.  */
 
-struct ui_out;
-extern struct cleanup *
-  make_cleanup_ui_out_redirect_pop (struct ui_out *uiout);
-
 struct section_addr_info;
 extern struct cleanup *(make_cleanup_free_section_addr_info 
                         (struct section_addr_info *));