public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Use discrete_position in ada-valprint.c
@ 2023-04-27 17:29 Tom Tromey
  2023-05-05 19:43 ` Keith Seitz
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2023-04-27 17:29 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

I found a couple of spots in ada-valprint.c that use an explicit loop,
but where discrete_position could be used instead.
---
 gdb/ada-valprint.c | 39 +++++++++------------------------------
 1 file changed, 9 insertions(+), 30 deletions(-)

diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c
index 24a7ea2ba79..ca5608f6090 100644
--- a/gdb/ada-valprint.c
+++ b/gdb/ada-valprint.c
@@ -364,9 +364,6 @@ ada_printchar (int c, struct type *type, struct ui_file *stream)
 void
 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
 {
-  unsigned int i;
-  unsigned len;
-
   if (!type)
     {
       print_longest (stream, 'd', 0, val);
@@ -379,23 +376,14 @@ ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
     {
 
     case TYPE_CODE_ENUM:
-      len = type->num_fields ();
-      for (i = 0; i < len; i++)
-	{
-	  if (type->field (i).loc_enumval () == val)
-	    {
-	      break;
-	    }
-	}
-      if (i < len)
-	{
-	  fputs_styled (ada_enum_name (type->field (i).name ()),
+      {
+	gdb::optional<LONGEST> posn = discrete_position (type, val);
+	if (posn.has_value ())
+	  fputs_styled (ada_enum_name (type->field (*posn).name ()),
 			variable_name_style.style (), stream);
-	}
-      else
-	{
+	else
 	  print_longest (stream, 'd', 0, val);
-	}
+      }
       break;
 
     case TYPE_CODE_INT:
@@ -818,8 +806,6 @@ static void
 ada_val_print_enum (struct value *value, struct ui_file *stream, int recurse,
 		    const struct value_print_options *options)
 {
-  int i;
-  unsigned int len;
   LONGEST val;
 
   if (options->format)
@@ -832,18 +818,11 @@ ada_val_print_enum (struct value *value, struct ui_file *stream, int recurse,
   const gdb_byte *valaddr = value->contents_for_printing ().data ();
   int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
 
-  len = type->num_fields ();
   val = unpack_long (type, valaddr + offset_aligned);
-  for (i = 0; i < len; i++)
-    {
-      QUIT;
-      if (val == type->field (i).loc_enumval ())
-	break;
-    }
-
-  if (i < len)
+  gdb::optional<LONGEST> posn = discrete_position (type, val);
+  if (posn.has_value ())
     {
-      const char *name = ada_enum_name (type->field (i).name ());
+      const char *name = ada_enum_name (type->field (*posn).name ());
 
       if (name[0] == '\'')
 	gdb_printf (stream, "%ld %ps", (long) val,
-- 
2.39.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-05-05 19:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-27 17:29 [PATCH] Use discrete_position in ada-valprint.c Tom Tromey
2023-05-05 19:43 ` Keith Seitz
2023-05-05 19:53   ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).