From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id SM6xFInhu189TgAAWB0awg (envelope-from ) for ; Mon, 23 Nov 2020 11:21:29 -0500 Received: by simark.ca (Postfix, from userid 112) id 4D62F1F0AC; Mon, 23 Nov 2020 11:21:29 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id A01081E58E for ; Mon, 23 Nov 2020 11:21:27 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DE1A738708D4; Mon, 23 Nov 2020 16:21:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE1A738708D4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1606148486; bh=939c/z2uVwi4vchAFmI2oXrv2akBr76RmqRRV6aGWsI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=t8ffMUnkqMSDaia9pmohe1y7OZ1BhV/cPTH4B+yx8GtY2m3p9QtRzg8b7lZQ3zBXM BtC7jI0iCitU6vDJc0xRev4zpAFBU4hVTFXPBX7iY6ffRgsG4zlWBWP3NQXN3bA6Qp bw0yMTFI+kn20/ZJjEfYuk1wF+gSaFPRnS+BA1II= Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id 0626F38708BE for ; Mon, 23 Nov 2020 16:21:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0626F38708BE X-ASG-Debug-ID: 1606148481-0c856e6cd51cb3f0001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id lSyIfirOCALishDP (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 23 Nov 2020 11:21:21 -0500 (EST) X-Barracuda-Envelope-From: simon.marchi@efficios.com X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from epycamd.internal.efficios.com (192-222-181-218.qc.cable.ebox.net [192.222.181.218]) by smtp.ebox.ca (Postfix) with ESMTP id 89473441D65; Mon, 23 Nov 2020 11:21:21 -0500 (EST) X-Barracuda-RBL-IP: 192.222.181.218 X-Barracuda-Effective-Source-IP: 192-222-181-218.qc.cable.ebox.net[192.222.181.218] X-Barracuda-Apparent-Source-IP: 192.222.181.218 To: gdb-patches@sourceware.org Subject: [PATCH 1/4] gdb: make discrete_position return optional Date: Mon, 23 Nov 2020 11:21:17 -0500 X-ASG-Orig-Subj: [PATCH 1/4] gdb: make discrete_position return optional Message-Id: <20201123162120.3778679-2-simon.marchi@efficios.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201123162120.3778679-1-simon.marchi@efficios.com> References: <20201123162120.3778679-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1606148481 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-BRTS-Status: 1 X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 6228 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests=BSF_RULE_7582B X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.86072 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE_7582B Custom Rule 7582B X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Simon Marchi via Gdb-patches Reply-To: Simon Marchi Cc: Simon Marchi Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" Instead of returning a boolean status and returning the value through a pointer, return an optional that does both jobs. This helps in the following patches, and I think it is an improvement in general. gdb/ChangeLog: * ada-lang.c (ada_value_slice_from_ptr): Adjust. (ada_value_slice): Adjust. (pos_atr): Adjust. * gdbtypes.c (get_discrete_bounds): Adjust. (discrete_position): Return optional. * gdbtypes.h (discrete_position): Return optional. Change-Id: I758dbd8858b296ee472ed39ec35db1dbd624a5ae --- gdb/ada-lang.c | 27 ++++++++++++++++----------- gdb/gdbtypes.c | 33 ++++++++++++++++++++------------- gdb/gdbtypes.h | 4 +++- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 714227d24dd..9ff470d97a7 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -2817,11 +2817,13 @@ ada_value_slice_from_ptr (struct value *array_ptr, struct type *type, type0->dyn_prop (DYN_PROP_BYTE_STRIDE), TYPE_FIELD_BITSIZE (type0, 0)); int base_low = ada_discrete_type_low_bound (type0->index_type ()); - LONGEST base_low_pos, low_pos; + gdb::optional base_low_pos, low_pos; CORE_ADDR base; - if (!discrete_position (base_index_type, low, &low_pos) - || !discrete_position (base_index_type, base_low, &base_low_pos)) + low_pos = discrete_position (base_index_type, low); + base_low_pos = discrete_position (base_index_type, base_low); + + if (!low_pos.has_value () || !base_low_pos.has_value ()) { warning (_("unable to get positions in slice, use bounds instead")); low_pos = low; @@ -2832,7 +2834,7 @@ ada_value_slice_from_ptr (struct value *array_ptr, struct type *type, if (stride == 0) stride = TYPE_LENGTH (TYPE_TARGET_TYPE (type0)); - base = value_as_address (array_ptr) + (low_pos - base_low_pos) * stride; + base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride; return value_at_lazy (slice_type, base); } @@ -2848,10 +2850,13 @@ ada_value_slice (struct value *array, int low, int high) (NULL, TYPE_TARGET_TYPE (type), index_type, type->dyn_prop (DYN_PROP_BYTE_STRIDE), TYPE_FIELD_BITSIZE (type, 0)); - LONGEST low_pos, high_pos; + gdb::optional low_pos, high_pos; + - if (!discrete_position (base_index_type, low, &low_pos) - || !discrete_position (base_index_type, high, &high_pos)) + low_pos = discrete_position (base_index_type, low); + high_pos = discrete_position (base_index_type, high); + + if (!low_pos.has_value () || !high_pos.has_value ()) { warning (_("unable to get positions in slice, use bounds instead")); low_pos = low; @@ -2859,7 +2864,7 @@ ada_value_slice (struct value *array, int low, int high) } return value_cast (slice_type, - value_slice (array, low, high_pos - low_pos + 1)); + value_slice (array, low, *high_pos - *low_pos + 1)); } /* If type is a record type in the form of a standard GNAT array @@ -8929,15 +8934,15 @@ pos_atr (struct value *arg) { struct value *val = coerce_ref (arg); struct type *type = value_type (val); - LONGEST result; if (!discrete_type_p (type)) error (_("'POS only defined on discrete types")); - if (!discrete_position (type, value_as_long (val), &result)) + gdb::optional result = discrete_position (type, value_as_long (val)); + if (!result.has_value ()) error (_("enumeration value is invalid: can't find 'POS")); - return result; + return *result; } static struct value * diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index e6f70bbe2d3..09f33c21e28 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -1062,9 +1062,21 @@ get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp) if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ENUM) { - if (!discrete_position (TYPE_TARGET_TYPE (type), *lowp, lowp) - || ! discrete_position (TYPE_TARGET_TYPE (type), *highp, highp)) + gdb::optional low_pos + = discrete_position (TYPE_TARGET_TYPE (type), *lowp); + + if (!low_pos.has_value ()) + return 0; + + *lowp = *low_pos; + + gdb::optional high_pos + = discrete_position (TYPE_TARGET_TYPE (type), *highp); + + if (!high_pos.has_value ()) return 0; + + *highp = *high_pos; } return 1; case TYPE_CODE_ENUM: @@ -1160,8 +1172,8 @@ get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound) in which case the value of POS is unmodified. */ -int -discrete_position (struct type *type, LONGEST val, LONGEST *pos) +gdb::optional +discrete_position (struct type *type, LONGEST val) { if (type->code () == TYPE_CODE_RANGE) type = TYPE_TARGET_TYPE (type); @@ -1173,19 +1185,14 @@ discrete_position (struct type *type, LONGEST val, LONGEST *pos) for (i = 0; i < type->num_fields (); i += 1) { if (val == TYPE_FIELD_ENUMVAL (type, i)) - { - *pos = i; - return 1; - } + return i; } + /* Invalid enumeration value. */ - return 0; + return {}; } else - { - *pos = val; - return 1; - } + return val; } /* If the array TYPE has static bounds calculate and update its diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 2b6f599f4c7..59ff6fc6ce3 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -46,6 +46,7 @@ #include "hashtab.h" #include "gdbsupport/array-view.h" +#include "gdbsupport/gdb_optional.h" #include "gdbsupport/offset-type.h" #include "gdbsupport/enum-flags.h" #include "gdbsupport/underlying.h" @@ -2445,7 +2446,8 @@ extern int get_discrete_bounds (struct type *, LONGEST *, LONGEST *); extern bool get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound); -extern int discrete_position (struct type *type, LONGEST val, LONGEST *pos); +extern gdb::optional discrete_position (struct type *type, + LONGEST val); extern int class_types_same_p (const struct type *, const struct type *); -- 2.29.2