From 100c7a99a5d4c30bb73d49a65eb45d1039646eaa Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 27 Apr 2023 10:59:59 -0600 Subject: Use discrete_position in ada-valprint.c I found a couple of spots in ada-valprint.c that use an explicit loop, but where discrete_position could be used instead. Reviewed-by: Keith Seitz --- 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 24a7ea2..ca5608f 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 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 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, -- cgit v1.1