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.133.124]) by sourceware.org (Postfix) with ESMTPS id 53E663857C4C for ; Fri, 25 Feb 2022 11:31:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 53E663857C4C Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-604-BqEvfByDOUuEnQ5GVQls_w-1; Fri, 25 Feb 2022 06:31:22 -0500 X-MC-Unique: BqEvfByDOUuEnQ5GVQls_w-1 Received: by mail-wm1-f70.google.com with SMTP id n31-20020a05600c3b9f00b003812242973aso1206224wms.4 for ; Fri, 25 Feb 2022 03:31:22 -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:in-reply-to :references:mime-version:content-transfer-encoding; bh=s9ffEGCNlygF1/Q4nEmtFVyNpwk80lw08e2yVmFmmm8=; b=WSser8FE3t5B3ZfCzMD1VHNnDkFalZbp+RsGeasMmtRWdWbyteU3oSQdo3QrEnbEQD EUeOiA4u/mp+DYTnX9ZU7zE4QrXoPUcyjHWFF5gDjLrAjCCNvJ1ESQK/eO2m5p2ZalGk Jns9hHQ2lOfqGqHHypATnGy/Kjwv/hj0617kYY7SiiXiUn2NC37rrHJLUwAB55M7inSP pjG1+SfF03wgx+W0N0+zhIsYNnw+yKxOyKwD182Q6OvgF2i4WJ4uJBiaAu9lHEZeSg+a UyjaRTce2l0ciYCvDl6W4bHlsbfaNOLgA/McIflZuHV/DCWwbBfQGJUaswxJl5HZZ334 RM/w== X-Gm-Message-State: AOAM530VEikelp2zNg5NEc09d+67oztlAabyGHRsBFxWlP5hbeTo69pK A1BMC9Rn/1PGHqbLvYUCG3nbNpXqwNMZmoJESORBA0CBTrVzaDB/fbMrCakm2wmymnTEYH24PWD gpilonUD9yGdq3V1g4ZKF2pFe1XGRcidH6j+GZCCF+jj8BZnTZedIa1AAC2anAkZuSZrhjA== X-Received: by 2002:adf:8b85:0:b0:1ed:b97b:493d with SMTP id o5-20020adf8b85000000b001edb97b493dmr5710158wra.108.1645788681316; Fri, 25 Feb 2022 03:31:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJzG3ou01B3+7b6di+2pEj9ggWosJ3NX/BLEEYH0d0xAc+ThrfoNi96LoiPgiwR3Kxshi4h2vQ== X-Received: by 2002:adf:8b85:0:b0:1ed:b97b:493d with SMTP id o5-20020adf8b85000000b001edb97b493dmr5710129wra.108.1645788680918; Fri, 25 Feb 2022 03:31:20 -0800 (PST) Received: from localhost (host86-169-131-29.range86-169.btcentralplus.com. [86.169.131.29]) by smtp.gmail.com with ESMTPSA id f13-20020a05600c4e8d00b0037bc5ce6042sm9330223wmq.27.2022.02.25.03.31.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Feb 2022 03:31:20 -0800 (PST) From: Andrew Burgess To: binutils@sourceware.org Cc: Andrew Burgess Subject: [PATCHv3 2/3] gdb/python: add Type.is_signed property Date: Fri, 25 Feb 2022 11:31:11 +0000 Message-Id: <88dc4ed2fffa491d839d2d50bbd169ccca6d4a45.1645788436.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Feb 2022 11:31:26 -0000 Add a new read-only property, Type.is_signed, which is True for signed types, and False otherwise. This property should only be read on types for which Type.is_scalar is true, attempting to read this property for non-scalar types will raise a ValueError. I chose 'is_signed' rather than 'is_unsigned' in order to match the existing Architecture.integer_type method, which takes a 'signed' parameter. As far as I could find, that was the only existing signed/unsigned selector in the Python API, so it seemed reasonable to stay consistent. --- gdb/NEWS | 5 +++++ gdb/doc/python.texi | 10 ++++++++++ gdb/python/py-type.c | 23 +++++++++++++++++++++++ gdb/testsuite/gdb.python/py-arch.exp | 15 ++++++++++++++- gdb/testsuite/gdb.python/py-type.exp | 28 ++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) diff --git a/gdb/NEWS b/gdb/NEWS index 874ec94b8a2..7c3cc2c8ef7 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -190,6 +190,11 @@ GNU/Linux/LoongArch loongarch*-*-linux* ** New read-only attribute gdb.Type.is_scalar, which is True for scalar types, and False for all other types. + ** New read-only attribute gdb.Type.is_signed. This attribute + should only be read when Type.is_scalar is True, and will be True + for signed types, and False for all other types. Attempting to + read this attribute for non-scalar types will raise a ValueError. + * New features in the GDB remote stub, GDBserver ** GDBserver is now supported on OpenRISC GNU/Linux. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index fa14dacf3ad..bcd7213c707 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -1269,6 +1269,16 @@ structures, unions, and classes. @end defvar +@defvar Type.is_signed +For scalar types (those for which @code{Type.is_scalar} is +@code{True}), this property is @code{True} if the type is signed, +otherwise this property is @code{False}. + +Attempting to read this property for a non-scalar type (a type for +which @code{Type.is_scalar} is @code{False}), will raise a +@code{ValueError}. +@end defvar + The following methods are provided: @defun Type.fields () diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index 54761236d52..7be3f3276c2 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -446,6 +446,27 @@ typy_is_scalar (PyObject *self, void *closure) Py_RETURN_FALSE; } +/* Return true if this type is signed. Raises a ValueError if this type + is not a scalar type. */ + +static PyObject * +typy_is_signed (PyObject *self, void *closure) +{ + struct type *type = ((type_object *) self)->type; + + if (!is_scalar_type (type)) + { + PyErr_SetString (PyExc_ValueError, + _("Type must be a scalar type")); + return nullptr; + } + + if (type->is_unsigned ()) + Py_RETURN_FALSE; + else + Py_RETURN_TRUE; +} + /* Return the type, stripped of typedefs. */ static PyObject * typy_strip_typedefs (PyObject *self, PyObject *args) @@ -1502,6 +1523,8 @@ static gdb_PyGetSetDef type_object_getset[] = "The objfile this type was defined in, or None.", NULL }, { "is_scalar", typy_is_scalar, nullptr, "Is this a scalar type?", nullptr }, + { "is_signed", typy_is_signed, nullptr, + "Is this an signed type?", nullptr }, { NULL } }; diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/py-arch.exp index b55778b0b72..58f6cb06b3e 100644 --- a/gdb/testsuite/gdb.python/py-arch.exp +++ b/gdb/testsuite/gdb.python/py-arch.exp @@ -64,12 +64,25 @@ if { ![is_address_zero_readable] } { } foreach size {0 1 2 3 4 8 16} { - foreach sign {"" ", True" ", False" ", None" ", \"blah\""} { + foreach sign_data {{"" True} \ + {", True" True} \ + {", False" False} \ + {", None" False} \ + {", \"blah\"" True}} { + set sign [lindex $sign_data 0] + # GDB's 0 bit type is always signed. + if { $size == 0 } { + set sign_result True + } else { + set sign_result [lindex $sign_data 1] + } set fullsize [expr 8 * $size] gdb_test_no_output "python t = arch.integer_type($fullsize$sign)" \ "get integer type for $size$sign" gdb_test "python print(t.sizeof)" "$size" \ "print size of integer type for $size$sign" + gdb_test "python print(t.is_signed == ${sign_result})" "True" \ + "check signedness of type for $size$sign" } } diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp index 2bb2bf67218..86cf8f3ff69 100644 --- a/gdb/testsuite/gdb.python/py-type.exp +++ b/gdb/testsuite/gdb.python/py-type.exp @@ -270,6 +270,32 @@ proc test_template {} { gdb_test "python print (ttype.template_argument(2))" "&C::c" } +# Check the is_signed property of some types. +proc test_is_signed {lang} { + if {$lang == "c++"} { + gdb_test "python print(gdb.parse_and_eval ('c').type.is_signed)" \ + "ValueError: Type must be a scalar type.*" + gdb_test "python print(gdb.parse_and_eval ('&c').type.is_signed == False)" "True" + } + + gdb_test "python print(gdb.parse_and_eval('global_unsigned_char').type.is_signed == False)" "True" + gdb_test "python print(gdb.parse_and_eval('global_char').type.is_signed)" "True|False" + gdb_test "python print(gdb.parse_and_eval('global_signed_char').type.is_signed == True)" "True" + + gdb_test "python print(gdb.parse_and_eval ('ss.x').type.is_signed == True)" "True" + gdb_test "python print(gdb.parse_and_eval ('ss').type.is_signed)" \ + "ValueError: Type must be a scalar type.*" + gdb_test "python print(gdb.parse_and_eval ('uu').type.is_signed)" \ + "ValueError: Type must be a scalar type.*" + gdb_test "python print(gdb.parse_and_eval ('uu.i').type.is_signed == True)" "True" + gdb_test "python print(gdb.parse_and_eval ('uu.f').type.is_signed == True)" "True" + gdb_test "python print(gdb.parse_and_eval ('uu.a').type.is_signed)" \ + "ValueError: Type must be a scalar type.*" + + gdb_test "python print(gdb.parse_and_eval ('&ss.x').type.is_signed == False)" "True" + gdb_test "python print(gdb.parse_and_eval ('&uu').type.is_signed == False)" "True" +} + # Test the gdb.Type.is_scalar property. proc test_is_scalar { lang } { if {$lang == "c++"} { @@ -320,6 +346,7 @@ if { [build_inferior "${binfile}" "c"] == 0 } { test_fields "c" test_enums test_is_scalar "c" + test_is_signed "c" } } @@ -334,5 +361,6 @@ if { [build_inferior "${binfile}-cxx" "c++"] == 0 } { test_template test_enums test_is_scalar "c++" + test_is_signed "c++" } } -- 2.25.4