From ee6a3d9e9496d469986a192aa374a43dffa83f96 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Fri, 25 Feb 2022 10:54:04 +0000 Subject: [PATCH] gdb/python: add Type.is_scalar property 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 e0c55e8274f..168b6f465a5 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -232,6 +232,9 @@ GNU/Linux/LoongArch loongarch*-*-linux* state information, or None, if there is no such additional information. + ** 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 4d9e77bf12c..2a7a4e8e3aa 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -1263,6 +1263,12 @@ The @code{gdb.Objfile} that this type was defined in, or @code{None} if 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 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.30.2