Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb/guile: add get-basic-type
@ 2024-10-06 19:40 Andrew Burgess
  2024-10-11  1:51 ` Kevin Buettner
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2024-10-06 19:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

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
---
 gdb/doc/guile.texi                       |  2 +-
 gdb/guile/lib/gdb/types.scm              | 19 +++++++++++++++++++
 gdb/testsuite/gdb.guile/types-module.cc  |  4 ++++
 gdb/testsuite/gdb.guile/types-module.exp |  5 +++++
 4 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/gdb/doc/guile.texi b/gdb/doc/guile.texi
index a66e8bfe16b..bd1262ea0cb 100644
--- a/gdb/doc/guile.texi
+++ b/gdb/doc/guile.texi
@@ -3946,7 +3946,7 @@
 (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
 
diff --git a/gdb/guile/lib/gdb/types.scm b/gdb/guile/lib/gdb/types.scm
index 198de9d3b72..7d8d3c639b9 100644
--- a/gdb/guile/lib/gdb/types.scm
+++ b/gdb/guile/lib/gdb/types.scm
@@ -55,6 +55,25 @@
 
   (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.
 
diff --git a/gdb/testsuite/gdb.guile/types-module.cc b/gdb/testsuite/gdb.guile/types-module.cc
index 9b32f672ab0..f3992c04145 100644
--- a/gdb/testsuite/gdb.guile/types-module.cc
+++ b/gdb/testsuite/gdb.guile/types-module.cc
@@ -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)
 {
diff --git a/gdb/testsuite/gdb.guile/types-module.exp b/gdb/testsuite/gdb.guile/types-module.exp
index d95ff21df5e..ee558041fa6 100644
--- a/gdb/testsuite/gdb.guile/types-module.exp
+++ b/gdb/testsuite/gdb.guile/types-module.exp
@@ -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"

base-commit: bcb92f7ba7b22ac882c000cabfd7ca8bea47c184
-- 
2.25.4


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb/guile: add get-basic-type
  2024-10-06 19:40 [PATCH] gdb/guile: add get-basic-type Andrew Burgess
@ 2024-10-11  1:51 ` Kevin Buettner
  2024-10-21  9:37   ` Andrew Burgess
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Buettner @ 2024-10-11  1:51 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On Sun,  6 Oct 2024 20:40:23 +0100
Andrew Burgess <aburgess@redhat.com> wrote:

> 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
> ---
>  gdb/doc/guile.texi                       |  2 +-
>  gdb/guile/lib/gdb/types.scm              | 19 +++++++++++++++++++
>  gdb/testsuite/gdb.guile/types-module.cc  |  4 ++++
>  gdb/testsuite/gdb.guile/types-module.exp |  5 +++++
>  4 files changed, 29 insertions(+), 1 deletion(-)

LGTM.

Reviewed-by: Kevin Buettner <kevinb@redhat.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb/guile: add get-basic-type
  2024-10-11  1:51 ` Kevin Buettner
@ 2024-10-21  9:37   ` Andrew Burgess
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2024-10-21  9:37 UTC (permalink / raw)
  To: Kevin Buettner; +Cc: gdb-patches

Kevin Buettner <kevinb@redhat.com> writes:

> On Sun,  6 Oct 2024 20:40:23 +0100
> Andrew Burgess <aburgess@redhat.com> wrote:
>
>> 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
>> ---
>>  gdb/doc/guile.texi                       |  2 +-
>>  gdb/guile/lib/gdb/types.scm              | 19 +++++++++++++++++++
>>  gdb/testsuite/gdb.guile/types-module.cc  |  4 ++++
>>  gdb/testsuite/gdb.guile/types-module.exp |  5 +++++
>>  4 files changed, 29 insertions(+), 1 deletion(-)
>
> LGTM.
>
> Reviewed-by: Kevin Buettner <kevinb@redhat.com>

I've gone ahead and pushed this patch.  If there's any post-commit
feedback then I'm happy to address it.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-21  9:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-06 19:40 [PATCH] gdb/guile: add get-basic-type Andrew Burgess
2024-10-11  1:51 ` Kevin Buettner
2024-10-21  9:37   ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox