From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 6787E385B19F; Mon, 28 Nov 2022 14:27:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6787E385B19F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669645647; bh=xfeN0zRfb7Bx6YTp6c65rAjsS8Q4lbXtO9xTO/2PcJI=; h=From:To:Subject:Date:From; b=S71tiQHLmzAUqI3V99GQTVacBHQo92KfnWMb7afsTeg7T9wn7VWI485PoC3JEbLqv pH6Cick+egTRw3b6d9F4AubFtUpCY5L77lowU7IFapj0XRZdL5SwrQq+tpJYLjrwkK U3QKzm0LiugfMsrZPF1C8EtdNnJNkPfVEtGGAsxg= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Fix range type signed-ness heuristic X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: ed14d866a31625c6f8c2cb6d4a445a8372b46161 X-Git-Newrev: 912b12ad84adf32af5827511af5afecedb108c63 Message-Id: <20221128142727.6787E385B19F@sourceware.org> Date: Mon, 28 Nov 2022 14:27:27 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D912b12ad84ad= f32af5827511af5afecedb108c63 commit 912b12ad84adf32af5827511af5afecedb108c63 Author: Tom Tromey Date: Thu Oct 20 07:08:48 2022 -0600 Fix range type signed-ness heuristic =20 The code to create a range type has a heuristic to decide whether the range is unsigned. However, this heuristic can fail if the upper bound of the range has its high bit set, because the test is done using LONGEST. =20 With this patch, if the underlying type of a range is unsigned, then the range will always be unsigned. A new test is included. =20 Regression tested on x86-64 Fedora 34. We've also been using this internally at AdaCore for a while. Diff: --- gdb/gdbtypes.c | 24 ++++++++++++------ gdb/testsuite/gdb.ada/unsigned_last.exp | 38 ++++++++++++++++++++++++= ++++ gdb/testsuite/gdb.ada/unsigned_last/main.adb | 22 ++++++++++++++++ 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index a43d9265ad2..5e8a486d28f 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -958,7 +958,13 @@ create_range_type (struct type *result_type, struct ty= pe *index_type, =20 if (index_type->code () =3D=3D TYPE_CODE_FIXED_POINT) result_type->set_is_unsigned (index_type->is_unsigned ()); - /* Note that the signed-ness of a range type can't simply be copied + else if (index_type->is_unsigned ()) + { + /* If the underlying type is unsigned, then the range + necessarily is. */ + result_type->set_is_unsigned (true); + } + /* Otherwise, the signed-ness of a range type can't simply be copied from the underlying type. Consider a case where the underlying type is 'int', but the range type can hold 0..65535, and where the range is further specified to fit into 16 bits. In this @@ -966,13 +972,15 @@ create_range_type (struct type *result_type, struct t= ype *index_type, range values will cause an unwanted sign extension. So, we have some heuristics here instead. */ else if (low_bound->kind () =3D=3D PROP_CONST && low_bound->const_val ()= >=3D 0) - result_type->set_is_unsigned (true); - /* Ada allows the declaration of range types whose upper bound is - less than the lower bound, so checking the lower bound is not - enough. Make sure we do not mark a range type whose upper bound - is negative as unsigned. */ - if (high_bound->kind () =3D=3D PROP_CONST && high_bound->const_val () < = 0) - result_type->set_is_unsigned (false); + { + result_type->set_is_unsigned (true); + /* Ada allows the declaration of range types whose upper bound is + less than the lower bound, so checking the lower bound is not + enough. Make sure we do not mark a range type whose upper bound + is negative as unsigned. */ + if (high_bound->kind () =3D=3D PROP_CONST && high_bound->const_val (= ) < 0) + result_type->set_is_unsigned (false); + } =20 result_type->set_endianity_is_not_default (index_type->endianity_is_not_default ()); diff --git a/gdb/testsuite/gdb.ada/unsigned_last.exp b/gdb/testsuite/gdb.ad= a/unsigned_last.exp new file mode 100644 index 00000000000..f07d9483bbd --- /dev/null +++ b/gdb/testsuite/gdb.ada/unsigned_last.exp @@ -0,0 +1,38 @@ +# 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 . + +# Check that 'Last correctly results in an unsigned integer in when +# the underlying type is unsigned. + +load_lib "ada.exp" + +if { [skip_ada_tests] } { return -1 } + +standard_ada_testfile main + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable {debug}] !=3D ""= } { + return -1 +} + +clean_restart ${testfile} + +set bp_location [gdb_get_line_number "START" ${testdir}/main.adb] +runto "main.adb:$bp_location" + +# Check that both X and Unsigned_64'Last are printed as unsigned +# values. Previously the heuristic used when determining if a range +# type was unsigned did not catch the latter case. +gdb_test "print x" "=3D 18446744073709551615" +gdb_test "print Unsigned_64'Last" "=3D 18446744073709551615" diff --git a/gdb/testsuite/gdb.ada/unsigned_last/main.adb b/gdb/testsuite/g= db.ada/unsigned_last/main.adb new file mode 100644 index 00000000000..f01e27e8ccc --- /dev/null +++ b/gdb/testsuite/gdb.ada/unsigned_last/main.adb @@ -0,0 +1,22 @@ +-- 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 . + +with Interfaces; + +procedure Main is + X : Interfaces.Unsigned_64 :=3D Interfaces.Unsigned_64'Last; +begin + null; -- START +end Main;