From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4E31B3858D33 for ; Fri, 5 May 2023 19:43:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E31B3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683315804; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NkVB+EPXnRC8hsQWmnQs51d1wGDpNocqc5+LIhmXsXs=; b=c4Sp9bV2TXQtX/x8oksoodENCW1XXwVWPY/72pZzdXJrpPPcjeLQjH3AG+m7t4mZj/UdMK Dh0H8sz6doiHIDQBM6ayGlLwMNpKiU3WA3HmHSWsCqbCYtNa76jzD0t1c6Nx54KInasgai P4mDWR+447CzDs7uUreCUMRffYy4lfc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-317-voGhQOiRN2GXFYQEs4DrwA-1; Fri, 05 May 2023 15:43:23 -0400 X-MC-Unique: voGhQOiRN2GXFYQEs4DrwA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 09CB82814251; Fri, 5 May 2023 19:43:23 +0000 (UTC) Received: from [10.22.9.236] (unknown [10.22.9.236]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BFA671410F23; Fri, 5 May 2023 19:43:22 +0000 (UTC) Message-ID: <115bf68b-5335-023b-c223-90caf0b13e4d@redhat.com> Date: Fri, 5 May 2023 12:43:21 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH] Use discrete_position in ada-valprint.c To: Tom Tromey , gdb-patches@sourceware.org References: <20230427172953.3057526-1-tromey@adacore.com> From: Keith Seitz In-Reply-To: <20230427172953.3057526-1-tromey@adacore.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 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 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,