Use discrete_position in ada-valprint.c
Commit Message
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(-)
Comments
On 4/27/23 10:29, Tom Tromey via Gdb-patches wrote:
> I found a couple of spots in ada-valprint.c that use an explicit loop,
> but where discrete_position could be used instead.
Nice cleanup. Learn something new everyday!
Reviewed-by: Keith Seitz <keiths@redhat.com>
Keith
> ---
> 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,
>>>>> "Keith" == Keith Seitz <keiths@redhat.com> writes:
Keith> On 4/27/23 10:29, Tom Tromey via Gdb-patches wrote:
>> I found a couple of spots in ada-valprint.c that use an explicit loop,
>> but where discrete_position could be used instead.
Keith> Nice cleanup. Learn something new everyday!
Thanks, I've checked it in.
Tom
@@ -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,