]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/guile: add get-basic-type
authorAndrew Burgess <aburgess@redhat.com>
Sun, 6 Oct 2024 19:32:16 +0000 (20:32 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Mon, 21 Oct 2024 09:36:32 +0000 (10:36 +0100)
A question was asked on stackoverflow.com about the guile function
get-basic-type[1] which is mentioned in the docs along with an example
of its use.

The problem is, the function was apparently never actually added to
GDB.  But it turns out that it's pretty easy to implement, so lets add
it now.  Better late than never.

The implementation mirrors the Python get_basic_type function.  I've
added a test which is a copy of the documentation example.

One issue is that the docs suggest that the type will be returned as
just "int", however, I'm not sure what this actually means.  It makes
more sense that the function return a gdb:type object which would be
represented as "#<gdb:type int>", so I've updated the docs to show
this output.

[1] https://stackoverflow.com/questions/79058691/unbound-variable-get-basic-type-in-gdb-guile-session

Reviewed-By: Kevin Buettner <kevinb@redhat.com>
gdb/doc/guile.texi
gdb/guile/lib/gdb/types.scm
gdb/testsuite/gdb.guile/types-module.cc
gdb/testsuite/gdb.guile/types-module.exp

index a66e8bfe16b9ef01d3d783624bcaffaa02bd8d86..bd1262ea0cba88911776a0882b5c19f84a5bf980 100644 (file)
@@ -3946,7 +3946,7 @@ Then in gdb:
 (gdb) guile (use-modules (gdb) (gdb types))
 (gdb) guile (define foo-ref (parse-and-eval "foo_ref"))
 (gdb) guile (get-basic-type (value-type foo-ref))
-int
+#<gdb:type int>
 @end smallexample
 @end deffn
 
index 198de9d3b723671d62ba20724ef2f62bb3584e1a..7d8d3c639b9c4083067f1876acccc5f2f5ab7fa3 100644 (file)
 
   (search-class type))
 
+(define-public (get-basic-type type)
+  "Return the \"basic\" type of a given type.
+
+  Arguments:
+    type: The type to reduce to its basic type.
+
+  Returns:
+    TYPE with const/volatile stripped away, and typedefs/references
+    converted to the underlying type."
+
+  (while (or (= (type-code type) TYPE_CODE_REF)
+            (= (type-code type) TYPE_CODE_RVALUE_REF)
+            (= (type-code type) TYPE_CODE_TYPEDEF))
+        (if (= (type-code type) TYPE_CODE_TYPEDEF)
+            (set! type (type-strip-typedefs type))
+            (set! type (type-target type))))
+
+  (type-unqualified type))
+
 (define-public (make-enum-hashtable enum-type)
   "Return a hash table from a program's enum type.
 
index 9b32f672ab0bea2575f9d2de2a8517155a3f179c..f3992c04145696d3f38e0af317815267e0820247 100644 (file)
@@ -31,6 +31,10 @@ class derived : public base
 
 derived d;
 
+typedef const int const_int;
+const_int foo (3);
+const_int &foo_ref (foo);
+
 int
 main (void)
 {
index d95ff21df5e108bf6dfb86a7a7468ee266836edf..ee558041fa66387d58198d51c6002343ea390d5d 100644 (file)
@@ -59,3 +59,8 @@ gdb_test "guile (define bad-enum-htab (make-enum-hashtable #f))" \
 gdb_test "guile (define bad-enum-htab (make-enum-hashtable (lookup-type \"int\")))" \
     "Wrong type argument in position 1 \\(expecting enum\\): #<gdb:type int>.*" \
     "make-enum-hashtable from int"
+
+gdb_test_no_output "guile (define foo-ref (parse-and-eval \"foo_ref\"))" \
+    "get foo-ref value"
+gdb_test "guile (get-basic-type (value-type foo-ref))" "#<gdb:type int>" \
+    "check get-basic-type"