From patchwork Tue Nov 15 18:49:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 60660 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 087CC38A90B6 for ; Tue, 15 Nov 2022 18:50:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 087CC38A90B6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668538233; bh=3qz3L5qrjKuNAr+DbRqcrj8a8md5nvxELz6BiN+bqzk=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=j9A1YKXQTYbQNzzZu1QPmIgc6osoe1Gk3c2SEBnourdvKWpE/uC+H2Muaav/HAEMZ qquy7+z9Y21UkSuVyo2wglQDe5ow/se/dsMQdPRIBv7IDBx+VmPyvfzhmfisRyjWmL OItNeQjRg6GDBoMBLMmbvZUw81l1utcLpeQKQPYw= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by sourceware.org (Postfix) with ESMTPS id F2DE438A909A for ; Tue, 15 Nov 2022 18:50:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F2DE438A909A Received: by mail-io1-xd2a.google.com with SMTP id s10so11373495ioa.5 for ; Tue, 15 Nov 2022 10:50:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3qz3L5qrjKuNAr+DbRqcrj8a8md5nvxELz6BiN+bqzk=; b=IUZoZNNClL+XRZA+oVM4zoyapy8epxQQYsoABdtdmUdMDlbc8mL8Y8QGLNTLlM8M3v Vpq/XSvwKNh1D1FfYhUPNauillaiNoDDtf4Oi7e8F/7+B7HN3F9Eb9SuY6bF67gJ0olv aOQCTjW8BCIMUVlWHiR2xLpHOOLPoqTO1fPY3QHQ86//yigYGfxm3MtMjuhln1kx1Rns X3Vhtc0WaY87hKTP9J78bQMDLX+mOeOfTN0IPb1H8pmhRDdEjFwroDUH2sNTnjksV8H6 S6s/MbYqF2eJ+jKcuUojdwb1eYV94ZWMHHKjO+j1mvg6pNSaEPq+VtxDf10jHzJhMque ssAw== X-Gm-Message-State: ANoB5pm0yc3CBwweWaSkBHeCwhySoWbYjeGMm0n/RWwtWLixm7YJj02e nxldZ2tNZfYsJQ+ztLKfqjvc43Nl1GpP3Q== X-Google-Smtp-Source: AA0mqf6Kc2pE8rcftMtDFY4ZoSU9BKAIU0aJYvV+iwvo32QaYmyZ8x6b3ewYKJYht5B+mQc+obqWkg== X-Received: by 2002:a05:6602:3684:b0:6bb:fc84:1d4d with SMTP id bf4-20020a056602368400b006bbfc841d4dmr8407342iob.131.1668538207132; Tue, 15 Nov 2022 10:50:07 -0800 (PST) Received: from localhost.localdomain (97-122-76-186.hlrn.qwest.net. [97.122.76.186]) by smtp.gmail.com with ESMTPSA id q27-20020a056638345b00b0036374fc6523sm4967547jav.24.2022.11.15.10.50.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 10:50:06 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Fix range type signed-ness heuristic Date: Tue, 15 Nov 2022 11:49:57 -0700 Message-Id: <20221115184957.105406-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.3 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" 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. With this patch, if the underlying type of a range is unsigned, then the range will always be unsigned. A new test is included. Regression tested on x86-64 Fedora 34. We've also been using this internally at AdaCore for a while. --- 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(-) create mode 100644 gdb/testsuite/gdb.ada/unsigned_last.exp create mode 100644 gdb/testsuite/gdb.ada/unsigned_last/main.adb 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 type *index_type, if (index_type->code () == 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 type *index_type, range values will cause an unwanted sign extension. So, we have some heuristics here instead. */ else if (low_bound->kind () == PROP_CONST && low_bound->const_val () >= 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 () == 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 () == PROP_CONST && high_bound->const_val () < 0) + result_type->set_is_unsigned (false); + } 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.ada/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}] != ""} { + 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" "= 18446744073709551615" +gdb_test "print Unsigned_64'Last" "= 18446744073709551615" diff --git a/gdb/testsuite/gdb.ada/unsigned_last/main.adb b/gdb/testsuite/gdb.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 := Interfaces.Unsigned_64'Last; +begin + null; -- START +end Main;