Use ui_out_emit_table and ui_out_emit_list in print_thread_info_1
authorTom Tromey <tom@tromey.com>
Sat, 9 Sep 2017 15:15:23 +0000 (09:15 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 9 Sep 2017 19:46:04 +0000 (13:46 -0600)
This changes print_thread_info_1 to use ui_out_emit_table and
ui_out_emit_list.  Which one is used depends on whether the ui-out is
mi-like; so the emitters are wrapped in gdb::optional.

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

* thread.c (print_thread_info_1): Use ui_out_emit_table,
ui_out_emit_list, gdb::optional.

gdb/ChangeLog
gdb/thread.c

index ae6f2c9..0078d2e 100644 (file)
@@ -1,3 +1,8 @@
+2017-09-09  Tom Tromey  <tom@tromey.com>
+
+       * thread.c (print_thread_info_1): Use ui_out_emit_table,
+       ui_out_emit_list, gdb::optional.
+
 2017-09-09  John Baldwin  <jhb@FreeBSD.org>
 
        * aarch64-linux-nat.c: Remove _initialize_aarch64_linux_nat
index 2185d50..af7900d 100644 (file)
@@ -45,6 +45,7 @@
 #include "thread-fsm.h"
 #include "tid-parse.h"
 #include <algorithm>
+#include "common/gdb_optional.h"
 
 /* Definition of struct thread_info exported to gdbthread.h.  */
 
@@ -1243,55 +1244,55 @@ print_thread_info_1 (struct ui_out *uiout, char *requested_threads,
   update_thread_list ();
   current_ptid = inferior_ptid;
 
-  struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
-
-  /* For backward compatibility, we make a list for MI.  A table is
-     preferable for the CLI, though, because it shows table
-     headers.  */
-  if (uiout->is_mi_like_p ())
-    make_cleanup_ui_out_list_begin_end (uiout, "threads");
-  else
-    {
-      int n_threads = 0;
+  {
+    /* For backward compatibility, we make a list for MI.  A table is
+       preferable for the CLI, though, because it shows table
+       headers.  */
+    gdb::optional<ui_out_emit_list> list_emitter;
+    gdb::optional<ui_out_emit_table> table_emitter;
+
+    if (uiout->is_mi_like_p ())
+      list_emitter.emplace (uiout, "threads");
+    else
+      {
+       int n_threads = 0;
 
-      for (tp = thread_list; tp; tp = tp->next)
-       {
-         if (!should_print_thread (requested_threads, default_inf_num,
-                                   global_ids, pid, tp))
-           continue;
+       for (tp = thread_list; tp; tp = tp->next)
+         {
+           if (!should_print_thread (requested_threads, default_inf_num,
+                                     global_ids, pid, tp))
+             continue;
 
-         ++n_threads;
-       }
+           ++n_threads;
+         }
 
-      if (n_threads == 0)
-       {
-         if (requested_threads == NULL || *requested_threads == '\0')
-           uiout->message (_("No threads.\n"));
-         else
-           uiout->message (_("No threads match '%s'.\n"),
-                           requested_threads);
-         do_cleanups (old_chain);
-         return;
-       }
+       if (n_threads == 0)
+         {
+           if (requested_threads == NULL || *requested_threads == '\0')
+             uiout->message (_("No threads.\n"));
+           else
+             uiout->message (_("No threads match '%s'.\n"),
+                             requested_threads);
+           return;
+         }
 
-      if (show_global_ids || uiout->is_mi_like_p ())
-       make_cleanup_ui_out_table_begin_end (uiout, 5, n_threads, "threads");
-      else
-       make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
+       table_emitter.emplace (uiout,
+                              (show_global_ids || uiout->is_mi_like_p ())
+                              ? 5 : 4,
+                              n_threads, "threads");
 
-      uiout->table_header (1, ui_left, "current", "");
+       uiout->table_header (1, ui_left, "current", "");
 
-      if (!uiout->is_mi_like_p ())
-       uiout->table_header (4, ui_left, "id-in-tg", "Id");
-      if (show_global_ids || uiout->is_mi_like_p ())
-       uiout->table_header (4, ui_left, "id", "GId");
-      uiout->table_header (17, ui_left, "target-id", "Target Id");
-      uiout->table_header (1, ui_left, "frame", "Frame");
-      uiout->table_body ();
-    }
+       if (!uiout->is_mi_like_p ())
+         uiout->table_header (4, ui_left, "id-in-tg", "Id");
+       if (show_global_ids || uiout->is_mi_like_p ())
+         uiout->table_header (4, ui_left, "id", "GId");
+       uiout->table_header (17, ui_left, "target-id", "Target Id");
+       uiout->table_header (1, ui_left, "frame", "Frame");
+       uiout->table_body ();
+      }
 
-  /* We'll be switching threads temporarily.  */
-  {
+    /* We'll be switching threads temporarily.  */
     scoped_restore_current_thread restore_thread;
 
     ALL_THREADS_BY_INFERIOR (inf, tp)
@@ -1379,14 +1380,13 @@ print_thread_info_1 (struct ui_out *uiout, char *requested_threads,
        core = target_core_of_thread (tp->ptid);
        if (uiout->is_mi_like_p () && core != -1)
          uiout->field_int ("core", core);
-    }
+      }
 
     /* This end scope restores the current thread and the frame
-       selected before the "info threads" command.  */
+       selected before the "info threads" command, and it finishes the
+       ui-out list or table.  */
   }
 
-  do_cleanups (old_chain);
-
   if (pid == -1 && requested_threads == NULL)
     {
       if (uiout->is_mi_like_p ()