[PATCH 1/2] Introduce ui_out::field_check_mark

Tom Tromey
2025-05-09T20:19:19Z

This adds a new ui_out method, field_check_mark. This method is to be
used to indicate the "current" or "selected" row in a table. This
patch updates all the relevant tables to call this method. No change
should be expected.
---
gdb/ada-tasks.c | 5 +----
gdb/inferior.c | 6 +-----
gdb/linux-fork.c | 5 +----
gdb/progspace.c | 6 +-----
gdb/target-connection.c | 6 ++----
gdb/thread.c | 6 +-----
gdb/ui-out.c | 9 +++++++++
gdb/ui-out.h | 13 +++++++++++++
8 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index 259512377f085423d4feda1a8a67e409109179a2..2f152ec3581f1076110df2ca149a2add5a825e9e 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -1163,10 +1163,7 @@ print_ada_task_info (struct ui_out *uiout,

/* Print a star if this task is the current task (or the task
currently selected). */
- if (task_info->ptid == inferior_ptid)
- uiout->field_string ("current", "*");
- else
- uiout->field_skip ("current");
+ uiout->field_check_mark ("current", task_info->ptid == inferior_ptid);

/* Print the task number. */
uiout->field_signed ("id", taskno);
diff --git a/gdb/inferior.c b/gdb/inferior.c
index e8a28cd2a0e1a4f76946d4fc9c1abdfab08b5d3e..221dbce73d6721f5ed883745c0474e78d94c405d 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -598,11 +598,7 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)

ui_out_emit_tuple tuple_emitter (uiout, NULL);

- if (inf == current_inf)
- uiout->field_string ("current", "*");
- else
- uiout->field_skip ("current");
-
+ uiout->field_check_mark ("current", inf == current_inf);
uiout->field_signed ("number", inf->num);

/* Because target_pid_to_str uses the current inferior,
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index 338ba032a0e75862d449b9c02c3c61cfc5492dbe..dc50391b22a0ba2d5728d7ae5a4505a73d864fd9 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -849,10 +849,7 @@ print_checkpoints (struct ui_out *uiout, inferior *req_inf, fork_info *req_fi)

ui_out_emit_tuple tuple_emitter (uiout, nullptr);

- if (is_current && cur_inf == inf)
- uiout->field_string ("current", "*");
- else
- uiout->field_skip ("current");
+ uiout->field_check_mark ("current", is_current && cur_inf == inf);

if (print_inf)
uiout->field_fmt ("id", "%d.%d", inf->num, fi.num);
diff --git a/gdb/progspace.c b/gdb/progspace.c
index eda63790aec9169724119c766b7e4c72da3cb550..7f1b0652516f53bd0a69e88fb409887d8639e4c5 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -305,11 +305,7 @@ print_program_space (struct ui_out *uiout, int requested)

ui_out_emit_tuple tuple_emitter (uiout, NULL);

- if (pspace == current_program_space)
- uiout->field_string ("current", "*");
- else
- uiout->field_skip ("current");
-
+ uiout->field_check_mark ("current", pspace == current_program_space);
uiout->field_signed ("id", pspace->num);

if (pspace->exec_filename () != nullptr)
diff --git a/gdb/target-connection.c b/gdb/target-connection.c
index 60d7732b56e465ac6fda1164846302e1b9167456..69e5765959ef6bf0bfbc2fb482f5a26fa1a03c79 100644
--- a/gdb/target-connection.c
+++ b/gdb/target-connection.c
@@ -126,10 +126,8 @@ print_connection (struct ui_out *uiout, const char *requested_connections)

ui_out_emit_tuple tuple_emitter (uiout, NULL);

- if (current_inferior ()->process_target () == t)
- uiout->field_string ("current", "*");
- else
- uiout->field_skip ("current");
+ uiout->field_check_mark ("current",
+ current_inferior ()->process_target () == t);

uiout->field_signed ("number", t->connection_number);

diff --git a/gdb/thread.c b/gdb/thread.c
index b659463ef0246992a5290c1eb3b1175094a2e826..2ce94a01efbdfdfb2587d90a160f716171fd63c6 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1122,11 +1122,7 @@ do_print_thread (ui_out *uiout, const char *requested_threads,

if (!uiout->is_mi_like_p ())
{
- if (tp == current_thread)
- uiout->field_string ("current", "*");
- else
- uiout->field_skip ("current");
-
+ uiout->field_check_mark ("current", tp == current_thread);
uiout->field_string ("id-in-tg", print_thread_id (tp));
}

diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 106a896c07fdadeae6c902a5bd541f81900c6453..a45a7af20d69170fb3ecac62f3f0345395bd88c2 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -557,6 +557,15 @@ ui_out::field_fmt (const char *fldname, const ui_file_style &style,
va_end (args);
}

+void
+ui_out::field_check_mark (const char *fldname, bool value)
+{
+ if (value)
+ field_string (fldname, get_check_mark ());
+ else
+ field_skip (fldname);
+}
+
void
ui_out::call_do_message (const ui_file_style &style, const char *format,
...)
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 1796e9c9eb3439422a767603d2799f4a7c167cf2..11d12024c2c15dd203a1df9d62eec72039a94c5e 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -206,6 +206,11 @@ class ui_out
const char *format, ...)
ATTRIBUTE_PRINTF (4, 5);

+ /* This is used when a table has a "current" field or the like. If
+ VALUE is true, a check mark of some kind is emitted for the
+ field. If VALUE is false, the field is skipped instead. */
+ void field_check_mark (const char *fldname, bool value);
+
void spaces (int numspaces) { do_spaces (numspaces); }
void text (const char *string) { do_text (string); }
void text (const std::string &string) { text (string.c_str ()); }
@@ -374,6 +379,14 @@ class ui_out
double, double) = 0;
virtual void do_progress_end () = 0;

+ /* Return the appropriate check-mark string. */
+ virtual const char *get_check_mark ()
+ {
+ /* The default is "*" because that's what was done
+ historically. */
+ return "*";
+ }
+
/* Set as not MI-like by default. It is overridden in subclasses if
necessary. */

--
2.49.0