From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Use discrete_position in ada-valprint.c
Date: Thu, 27 Apr 2023 11:29:53 -0600 [thread overview]
Message-ID: <20230427172953.3057526-1-tromey@adacore.com> (raw)
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
next reply other threads:[~2023-04-27 17:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-27 17:29 Tom Tromey [this message]
2023-05-05 19:43 ` Keith Seitz
2023-05-05 19:53 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230427172953.3057526-1-tromey@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).