public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: binutils@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCHv3 1/3] gdb/python: add Type.is_scalar property
Date: Fri, 25 Feb 2022 11:31:10 +0000	[thread overview]
Message-ID: <1eeffb1d2496e11e63bd83f9ffb19d69cac7ba4c.1645788436.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1645788436.git.aburgess@redhat.com>

Add a new read-only property which is True for scalar types,
otherwise, it's False.
---
 gdb/NEWS                             |  3 +++
 gdb/doc/python.texi                  |  6 ++++++
 gdb/python/py-type.c                 | 15 +++++++++++++++
 gdb/testsuite/gdb.python/py-type.c   | 16 ++++++++++++++++
 gdb/testsuite/gdb.python/py-type.exp | 26 +++++++++++++++++++++++++-
 5 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index fdd42049994..874ec94b8a2 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -187,6 +187,9 @@ GNU/Linux/LoongArch    loongarch*-*-linux*
      set styling').  When false, which is the default if the argument
      is not given, then no styling is applied to the returned string.
 
+  ** New read-only attribute gdb.Type.is_scalar, which is True for
+     scalar types, and False for all other types.
+
 * 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 c1a3f5f2a7e..fa14dacf3ad 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -1263,6 +1263,12 @@
 there is no associated objfile.
 @end defvar
 
+@defvar Type.is_scalar
+This property is @code{True} if the type is a scalar type, otherwise,
+this property is @code{False}.  Examples of non-scalar types include
+structures, unions, and classes.
+@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 13dae1e2559..54761236d52 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -433,6 +433,19 @@ typy_get_objfile (PyObject *self, void *closure)
   return objfile_to_objfile_object (objfile).release ();
 }
 
+/* Return true if this is a scalar type, otherwise, returns false.  */
+
+static PyObject *
+typy_is_scalar (PyObject *self, void *closure)
+{
+  struct type *type = ((type_object *) self)->type;
+
+  if (is_scalar_type (type))
+    Py_RETURN_TRUE;
+  else
+    Py_RETURN_FALSE;
+}
+
 /* Return the type, stripped of typedefs. */
 static PyObject *
 typy_strip_typedefs (PyObject *self, PyObject *args)
@@ -1487,6 +1500,8 @@ static gdb_PyGetSetDef type_object_getset[] =
     "The tag name for this type, or None.", NULL },
   { "objfile", typy_get_objfile, NULL,
     "The objfile this type was defined in, or None.", NULL },
+  { "is_scalar", typy_is_scalar, nullptr,
+    "Is this a scalar type?", nullptr },
   { NULL }
 };
 
diff --git a/gdb/testsuite/gdb.python/py-type.c b/gdb/testsuite/gdb.python/py-type.c
index 10cbd3b0875..92297d5ad4b 100644
--- a/gdb/testsuite/gdb.python/py-type.c
+++ b/gdb/testsuite/gdb.python/py-type.c
@@ -32,6 +32,13 @@ TS ts;
 
 int aligncheck;
 
+union UU
+{
+  int i;
+  float f;
+  int a[5];
+};
+
 #ifdef __cplusplus
 struct C
 {
@@ -72,6 +79,14 @@ Temargs<D, 23, &C::c> temvar;
 
 #endif
 
+unsigned char global_unsigned_char;
+char global_char;
+signed char global_signed_char;
+
+unsigned int global_unsigned_int;
+int global_int;
+signed int global_signed_int;
+
 enum E
 { v1, v2, v3
 };
@@ -91,6 +106,7 @@ main ()
   int ar[2] = {1,2};
   struct s st;
   struct SS ss;
+  union UU uu;
 #ifdef __cplusplus
   C c;
   c.c = 1;
diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp
index 4990eeb7ddb..2bb2bf67218 100644
--- a/gdb/testsuite/gdb.python/py-type.exp
+++ b/gdb/testsuite/gdb.python/py-type.exp
@@ -270,6 +270,29 @@ proc test_template {} {
     gdb_test "python print (ttype.template_argument(2))" "&C::c"
 }
 
+# Test the gdb.Type.is_scalar property.
+proc test_is_scalar { lang } {
+    if {$lang == "c++"} {
+	gdb_test "python print(gdb.parse_and_eval ('c').type.is_scalar)" "False"
+	gdb_test "python print(gdb.parse_and_eval ('&c').type.is_scalar)" "True"
+    }
+
+    foreach type { char int } {
+	gdb_test "python print(gdb.parse_and_eval('global_unsigned_${type}').type.is_scalar)" "True"
+	gdb_test "python print(gdb.parse_and_eval('global_${type}').type.is_scalar)" "True"
+	gdb_test "python print(gdb.parse_and_eval('global_signed_${type}').type.is_scalar)" "True"
+    }
+
+    gdb_test "python print(gdb.parse_and_eval ('ss.x').type.is_scalar)" "True"
+    gdb_test "python print(gdb.parse_and_eval ('ss').type.is_scalar)" "False"
+    gdb_test "python print(gdb.parse_and_eval ('uu').type.is_scalar)" "False"
+
+    gdb_test "python print(gdb.parse_and_eval ('uu.i').type.is_scalar)" "True"
+    gdb_test "python print(gdb.parse_and_eval ('uu.f').type.is_scalar)" "True"
+    gdb_test "python print(gdb.parse_and_eval ('uu.a').type.is_scalar)"  "False"
+    gdb_test "python print(gdb.parse_and_eval ('&ss.x').type.is_scalar)" "True"
+}
+
 # Perform C Tests.
 if { [build_inferior "${binfile}" "c"] == 0 } {
   restart_gdb "${binfile}"
@@ -296,10 +319,10 @@ if { [build_inferior "${binfile}" "c"] == 0 } {
       runto_bp "break to inspect struct and array."
       test_fields "c"
       test_enums
+      test_is_scalar "c"
   }
 }
 
-
 # Perform C++ Tests.
 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
   restart_gdb "${binfile}-cxx"
@@ -310,5 +333,6 @@ if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
       test_range
       test_template
       test_enums
+      test_is_scalar "c++"
   }
 }
-- 
2.25.4


  reply	other threads:[~2022-02-25 11:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1638799543.git.aburgess@redhat.com>
2022-02-25 11:31 ` [PATCHv3 0/3] Add Type.is_scalar and Type.is_signed properties Andrew Burgess
2022-02-25 11:31   ` Andrew Burgess [this message]
2022-02-25 11:31   ` [PATCHv3 2/3] gdb/python: add Type.is_signed property Andrew Burgess
2022-02-25 11:31   ` [PATCHv3 3/3] gdb/testsuite: add new test for comparing char types in Python Andrew Burgess
2022-02-25 13:54   ` [PATCHv3 0/3] Add Type.is_scalar and Type.is_signed properties Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1eeffb1d2496e11e63bd83f9ffb19d69cac7ba4c.1645788436.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).