[v3,3/4] Introduce field_unsigned

Message ID 20190709152436.14078-4-tromey@adacore.com
State New
Headers show
Series
  • Remove many uses of field_fmt
Related show

Commit Message

Tom Tromey July 9, 2019, 3:24 p.m.
This adds field_unsigned and changes various places using field_fmt
with "%u" to use this instead.  This also replaces an existing
equivalent helper function in record-btrace.c.

gdb/ChangeLog
2019-07-09  Tom Tromey  <tromey@adacore.com>

	* mi/mi-out.h (class mi_ui_out) <do_field_unsigned>: Declare.
	* mi/mi-out.c (mi_ui_out::do_field_unsigned): New method.
	* cli-out.h (class cli_ui_out) <do_field_unsigned>: Declare.
	* cli-out.c (cli_ui_out::do_field_int): New method.
	* ui-out.c (ui_out::field_unsigned): New method.
	* symfile.c (generic_load): Use field_unsigned.
	(print_transfer_performance): Likewise.
	* record-btrace.c (ui_out_field_uint): Remove.
	(btrace_call_history_insn_range, btrace_call_history): Use
	field_unsigned.
	* disasm.c (gdb_pretty_print_disassembler::pretty_print_insn): Use
	field_unsigned.
	* ui-out.h (class ui_out) <field_unsigned>: New method.
	<do_field_unsigned>: Likewise.
---
 gdb/ChangeLog       | 17 +++++++++++++++++
 gdb/cli-out.c       | 13 +++++++++++++
 gdb/cli-out.h       |  3 +++
 gdb/disasm.c        |  2 +-
 gdb/mi/mi-out.c     | 10 ++++++++++
 gdb/mi/mi-out.h     |  3 +++
 gdb/record-btrace.c | 14 +++-----------
 gdb/symfile.c       | 12 ++++++------
 gdb/ui-out.c        | 14 ++++++++++++++
 gdb/ui-out.h        |  4 ++++
 10 files changed, 74 insertions(+), 18 deletions(-)

-- 
2.20.1

Comments

Pedro Alves July 16, 2019, 6:31 p.m. | #1
On 7/9/19 4:24 PM, Tom Tromey wrote:

>    void field_fmt_int (int width, ui_align align, const char *fldname,

>  		      int value);

> +  /* Like field_int, but print an unsigned value.  */

> +  void field_unsigned (const char *fldname, ULONGEST value);


FWIW, offhand the naming looks a bit surprising to me.

Given the existence of "field_int", I'd expect "unsigned" to print an
unsigned (int), not a ULONGEST.

(I was initialize surprised it wasn't called field_uint, like the
ui_out_field_uint function that is removed by this patch, until I
realized this works with a ULONGEST.)

To output a ULONGEST, I'd expect the method to be called field_ulongest.

Is your plan to rename field_int to field_signed, perhaps?

Thanks,
Pedro Alves
Tom Tromey July 17, 2019, 1:31 p.m. | #2
Pedro> Given the existence of "field_int", I'd expect "unsigned" to print an
Pedro> unsigned (int), not a ULONGEST.

Pedro> (I was initialize surprised it wasn't called field_uint, like the
Pedro> ui_out_field_uint function that is removed by this patch, until I
Pedro> realized this works with a ULONGEST.)

Pedro> To output a ULONGEST, I'd expect the method to be called field_ulongest.

Pedro> Is your plan to rename field_int to field_signed, perhaps?

I hadn't really considered it, but I can rename either one as you like.

I tend to think field_int should accept a LONGEST rather than an int.
So I suppose doing that & naming it field_signed would be a way to go.

What do think?

Tom
Pedro Alves July 17, 2019, 2:10 p.m. | #3
On 7/17/19 2:31 PM, Tom Tromey wrote:
> Pedro> Given the existence of "field_int", I'd expect "unsigned" to print an

> Pedro> unsigned (int), not a ULONGEST.

> 

> Pedro> (I was initialize surprised it wasn't called field_uint, like the

> Pedro> ui_out_field_uint function that is removed by this patch, until I

> Pedro> realized this works with a ULONGEST.)

> 

> Pedro> To output a ULONGEST, I'd expect the method to be called field_ulongest.

> 

> Pedro> Is your plan to rename field_int to field_signed, perhaps?

> 

> I hadn't really considered it, but I can rename either one as you like.

> 

> I tend to think field_int should accept a LONGEST rather than an int.

> So I suppose doing that & naming it field_signed would be a way to go.


Right, I meant to ask whether the plan was to rename AND change
to LONGEST, for didn't for some reason.

That would work for me.

> 

> What do think?


Agree. :-)

Thanks,
Pedro Alves
Tom Tromey July 17, 2019, 4:34 p.m. | #4
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:


Pedro> Right, I meant to ask whether the plan was to rename AND change
Pedro> to LONGEST, for didn't for some reason.

Pedro> That would work for me.

Ok, I sent a patch.

Tom

Patch

diff --git a/gdb/cli-out.c b/gdb/cli-out.c
index 55c8d2b3b1b..8d6b426203f 100644
--- a/gdb/cli-out.c
+++ b/gdb/cli-out.c
@@ -105,6 +105,19 @@  cli_ui_out::do_field_int (int fldno, int width, ui_align alignment,
 		   ui_out_style_kind::DEFAULT);
 }
 
+/* output an unsigned field */
+
+void
+cli_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
+			       const char *fldname, ULONGEST value)
+{
+  if (m_suppress_output)
+    return;
+
+  do_field_string (fldno, width, alignment, fldname, pulongest (value),
+		   ui_out_style_kind::DEFAULT);
+}
+
 /* used to omit a field */
 
 void
diff --git a/gdb/cli-out.h b/gdb/cli-out.h
index eeb555fbbec..fd1d6274a9f 100644
--- a/gdb/cli-out.h
+++ b/gdb/cli-out.h
@@ -47,6 +47,9 @@  protected:
   virtual void do_end (ui_out_type type) override;
   virtual void do_field_int (int fldno, int width, ui_align align,
 			     const char *fldname, int value) override;
+  virtual void do_field_unsigned (int fldno, int width, ui_align align,
+				  const char *fldname, ULONGEST value)
+    override;
   virtual void do_field_skip (int fldno, int width, ui_align align,
 			      const char *fldname) override;
   virtual void do_field_string (int fldno, int width, ui_align align,
diff --git a/gdb/disasm.c b/gdb/disasm.c
index 4e51bc5f6f6..5e1dd6e3a3d 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -209,7 +209,7 @@  gdb_pretty_print_disassembler::pretty_print_insn (struct ui_out *uiout,
 
     if (insn->number != 0)
       {
-	uiout->field_fmt ("insn-number", "%u", insn->number);
+	uiout->field_unsigned ("insn-number", insn->number);
 	uiout->text ("\t");
       }
 
diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index d8bee0f3927..dd99a2b7fb1 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -105,6 +105,16 @@  mi_ui_out::do_field_int (int fldno, int width, ui_align alignment,
 		   ui_out_style_kind::DEFAULT);
 }
 
+/* Output an unsigned field.  */
+
+void
+mi_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
+			      const char *fldname, ULONGEST value)
+{
+  do_field_string (fldno, width, alignment, fldname, pulongest (value),
+		   ui_out_style_kind::DEFAULT);
+}
+
 /* Used to omit a field.  */
 
 void
diff --git a/gdb/mi/mi-out.h b/gdb/mi/mi-out.h
index 82f77592da8..1328684ed2b 100644
--- a/gdb/mi/mi-out.h
+++ b/gdb/mi/mi-out.h
@@ -54,6 +54,9 @@  protected:
   virtual void do_end (ui_out_type type) override;
   virtual void do_field_int (int fldno, int width, ui_align align,
 			  const char *fldname, int value) override;
+  virtual void do_field_unsigned (int fldno, int width, ui_align align,
+				  const char *fldname, ULONGEST value)
+    override;
   virtual void do_field_skip (int fldno, int width, ui_align align,
 			   const char *fldname) override;
   virtual void do_field_string (int fldno, int width, ui_align align,
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index ec06145585f..8e019d874e8 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -623,14 +623,6 @@  btrace_ui_out_decode_error (struct ui_out *uiout, int errcode,
   uiout->text (_("]\n"));
 }
 
-/* Print an unsigned int.  */
-
-static void
-ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
-{
-  uiout->field_fmt (fld, "%u", val);
-}
-
 /* A range of source lines.  */
 
 struct btrace_line_range
@@ -1032,9 +1024,9 @@  btrace_call_history_insn_range (struct ui_out *uiout,
   begin = bfun->insn_offset;
   end = begin + size - 1;
 
-  ui_out_field_uint (uiout, "insn begin", begin);
+  uiout->field_unsigned ("insn begin", begin);
   uiout->text (",");
-  ui_out_field_uint (uiout, "insn end", end);
+  uiout->field_unsigned ("insn end", end);
 }
 
 /* Compute the lowest and highest source line for the instructions in BFUN
@@ -1155,7 +1147,7 @@  btrace_call_history (struct ui_out *uiout,
       msym = bfun->msym;
 
       /* Print the function index.  */
-      ui_out_field_uint (uiout, "index", bfun->number);
+      uiout->field_unsigned ("index", bfun->number);
       uiout->text ("\t");
 
       /* Indicate gaps in the trace.  */
diff --git a/gdb/symfile.c b/gdb/symfile.c
index caa0e79bd9a..d2b88fc9e40 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2097,7 +2097,7 @@  generic_load (const char *args, int from_tty)
   uiout->text ("Start address ");
   uiout->field_core_addr ("address", target_gdbarch (), entry);
   uiout->text (", load size ");
-  uiout->field_fmt ("load-size", "%lu", total_progress.data_count);
+  uiout->field_unsigned ("load-size", total_progress.data_count);
   uiout->text ("\n");
   regcache_write_pc (get_current_regcache (), entry);
 
@@ -2140,29 +2140,29 @@  print_transfer_performance (struct ui_file *stream,
 
       if (uiout->is_mi_like_p ())
 	{
-	  uiout->field_fmt ("transfer-rate", "%lu", rate * 8);
+	  uiout->field_unsigned ("transfer-rate", rate * 8);
 	  uiout->text (" bits/sec");
 	}
       else if (rate < 1024)
 	{
-	  uiout->field_fmt ("transfer-rate", "%lu", rate);
+	  uiout->field_unsigned ("transfer-rate", rate);
 	  uiout->text (" bytes/sec");
 	}
       else
 	{
-	  uiout->field_fmt ("transfer-rate", "%lu", rate / 1024);
+	  uiout->field_unsigned ("transfer-rate", rate / 1024);
 	  uiout->text (" KB/sec");
 	}
     }
   else
     {
-      uiout->field_fmt ("transferred-bits", "%lu", (data_count * 8));
+      uiout->field_unsigned ("transferred-bits", (data_count * 8));
       uiout->text (" bits in <1 sec");
     }
   if (write_count > 0)
     {
       uiout->text (", ");
-      uiout->field_fmt ("write-rate", "%lu", data_count / write_count);
+      uiout->field_unsigned ("write-rate", data_count / write_count);
       uiout->text (" bytes/write");
     }
   uiout->text (".\n");
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 6851fd29c6a..65312aa4323 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -462,6 +462,20 @@  ui_out::field_fmt_int (int input_width, ui_align input_align,
   do_field_int (fldno, input_width, input_align, fldname, value);
 }
 
+/* See ui-out.h.  */
+
+void
+ui_out::field_unsigned (const char *fldname, ULONGEST value)
+{
+  int fldno;
+  int width;
+  ui_align align;
+
+  verify_field (&fldno, &width, &align);
+
+  do_field_unsigned (fldno, width, align, fldname, value);
+}
+
 /* Documented in ui-out.h.  */
 
 void
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 8574204b92a..afc1c41651d 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -110,6 +110,8 @@  class ui_out
   void field_int (const char *fldname, int value);
   void field_fmt_int (int width, ui_align align, const char *fldname,
 		      int value);
+  /* Like field_int, but print an unsigned value.  */
+  void field_unsigned (const char *fldname, ULONGEST value);
   void field_core_addr (const char *fldname, struct gdbarch *gdbarch,
 			CORE_ADDR address);
   void field_string (const char *fldname, const char *string,
@@ -157,6 +159,8 @@  class ui_out
   virtual void do_end (ui_out_type type) = 0;
   virtual void do_field_int (int fldno, int width, ui_align align,
 			     const char *fldname, int value) = 0;
+  virtual void do_field_unsigned (int fldno, int width, ui_align align,
+				  const char *fldname, ULONGEST value) = 0;
   virtual void do_field_skip (int fldno, int width, ui_align align,
 			      const char *fldname) = 0;
   virtual void do_field_string (int fldno, int width, ui_align align,