From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id 77F2C3858D39 for ; Thu, 10 Mar 2022 16:25:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 77F2C3858D39 Received: by mail-il1-x130.google.com with SMTP id d3so4095221ilr.10 for ; Thu, 10 Mar 2022 08:25:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LXrDF3zJNCHP8htYz7zeWYYacV7Du6yCyLx+yppJGlk=; b=6XVfvuSrHSYjeEDffVXt9sr02mKRrQxv5j+yd6RjwCt1OFZifpYBo9pCOB/mUl9avH 6V2lV/n3bVfO/aIzuE8cT3KHjowDxLCSOIZNArLeITwNXhgjIZ7RpFd/be+x3WvDmP9M FKXEwKzCxqTYjyOYoLAcsQMZkJR/OhDkIVu7PBbmVCaE9B6PmJYRdISs8wmdkRmsz+vs 2m+BXIeMeJpiD/BK2+L3AjDpLkQXYB6ViGXbYz0nG/9Y6P/AW33Wh3GsuCAKYtZPmIp0 uBJmGBu0UXNa6ZdeSW+nKEFadBJh2/hlO6Cdy+EcUpV4pRWSUPlYJbIfF8dAT4j4Nn3P jMmQ== X-Gm-Message-State: AOAM530jAH+UVzA3TUysQb9G5QKfWNr50+iGRWofX7HgTQ2VCuO7VGzX pXd4Fru7WtxsgpXoLaGV4flFTX/cyQr60Q== X-Google-Smtp-Source: ABdhPJy/qWfDr2FLkbnTJvsYpZ64+xx/jX5h9ej7splTjWiJIRL/nM3yq6+VkZjk+uWgOBz39suXig== X-Received: by 2002:a92:dc8a:0:b0:2c6:b0d:23ff with SMTP id c10-20020a92dc8a000000b002c60b0d23ffmr4151984iln.17.1646929558522; Thu, 10 Mar 2022 08:25:58 -0800 (PST) Received: from murgatroyd.Home (75-166-141-253.hlrn.qwest.net. [75.166.141.253]) by smtp.gmail.com with ESMTPSA id o11-20020a92a80b000000b002c1ec0ca545sm2846735ilh.18.2022.03.10.08.25.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Mar 2022 08:25:58 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Correctly print subrange types in generic_value_print Date: Thu, 10 Mar 2022 09:25:56 -0700 Message-Id: <20220310162556.430085-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org 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: , X-List-Received-Date: Thu, 10 Mar 2022 16:26:01 -0000 I noticed that generic_value_print assumes that a subrange type is always a subrange of an integer type. However, this isn't necessarily the case. In Ada, for example, one has subranges of character and enumeration types. This code isn't often exercised, I think, because languages with real subrange types tend to implement their own printers. However, it still seemed worth fixing. --- gdb/testsuite/gdb.dwarf2/subrange-enum.exp | 78 ++++++++++++++++++++++ gdb/valprint.c | 9 ++- 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.dwarf2/subrange-enum.exp diff --git a/gdb/testsuite/gdb.dwarf2/subrange-enum.exp b/gdb/testsuite/gdb.dwarf2/subrange-enum.exp new file mode 100644 index 00000000000..b9f7cb68233 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/subrange-enum.exp @@ -0,0 +1,78 @@ +# Copyright 2022 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test generic printing of subrange values. + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +if {![dwarf2_support]} { + return 0 +} + +standard_testfile main.c -dw.S + +# Make some DWARF for the test. +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + cu {} { + compile_unit {{language @DW_LANG_C}} { + declare_labels integer_label enum_label subrange_label + + integer_label: DW_TAG_base_type { + {DW_AT_byte_size 4 DW_FORM_sdata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name integer} + } + + enum_label: DW_TAG_enumeration_type { + {DW_AT_name E} + {DW_AT_type :$integer_label} + } { + DW_TAG_enumerator { + {DW_AT_name ONE} + {DW_AT_const_value 1 DW_FORM_sdata} + } + DW_TAG_enumerator { + {DW_AT_name TWO} + {DW_AT_const_value 2 DW_FORM_sdata} + } + DW_TAG_enumerator { + {DW_AT_name THREE} + {DW_AT_const_value 3 DW_FORM_sdata} + } + } + + subrange_label: DW_TAG_subrange_type { + {lower_bound 1 DW_FORM_sdata} + {upper_bound 2 DW_FORM_sdata} + {type :$enum_label} + } + + DW_TAG_variable { + {name rangeval} + {type :$subrange_label} + {const_value 2 DW_FORM_udata} + } + } + } +} + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}] } { + return -1 +} + +gdb_test "print rangeval" " = TWO" diff --git a/gdb/valprint.c b/gdb/valprint.c index d6ec64845f4..c2ffe6224bf 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -888,6 +888,14 @@ generic_value_print (struct value *val, struct ui_file *stream, int recurse, if (is_fixed_point_type (type)) type = type->fixed_point_type_base_type (); + /* Widen a subrange to its target type, then use that type's + printer. */ + while (type->code () == TYPE_CODE_RANGE) + { + type = check_typedef (TYPE_TARGET_TYPE (type)); + val = value_cast (type, val); + } + switch (type->code ()) { case TYPE_CODE_ARRAY: @@ -936,7 +944,6 @@ generic_value_print (struct value *val, struct ui_file *stream, int recurse, generic_value_print_bool (val, stream, options, decorations); break; - case TYPE_CODE_RANGE: case TYPE_CODE_INT: generic_value_print_int (val, stream, options); break; -- 2.34.1