[2/2] Decode Ada types in Python layer

Message ID 20210617191230.71887-3-tromey@adacore.com
State New
Headers show
Series
  • Change Ada type names reported by Python
Related show

Commit Message

Tom Tromey June 17, 2021, 7:12 p.m.
GNAT emits encoded type names, but these aren't usually of interest to
users.  The Ada language code in gdb hides this oddity -- but the
Python layer does not.  This patch changes the Python code to use the
decoded Ada type name, when appropriate.

I looked at decoding Ada type names during construction, as that would
be cleaner.  However, the Ada support in gdb relies on the encodings
at various points, so this isn't really doable right now.

gdb/ChangeLog
2021-06-17  Tom Tromey  <tromey@adacore.com>

	* python/py-type.c (typy_get_name): Decode an Ada type name.

gdb/testsuite/ChangeLog
2021-06-17  Tom Tromey  <tromey@adacore.com>

	* gdb.ada/py_range.exp: Add type name test cases.
---
 gdb/ChangeLog                      | 4 ++++
 gdb/python/py-type.c               | 9 +++++++++
 gdb/testsuite/ChangeLog            | 4 ++++
 gdb/testsuite/gdb.ada/py_range.exp | 5 +++++
 4 files changed, 22 insertions(+)

-- 
2.26.3

Comments

Metzger, Markus T via Gdb-patches June 17, 2021, 9:10 p.m. | #1
On Thu, Jun 17, 2021 at 2:31 PM Tom Tromey <tromey@adacore.com> wrote:
> @@ -393,6 +394,14 @@ typy_get_name (PyObject *self, void *closure)

>

>    if (type->name () == NULL)

>      Py_RETURN_NONE;

> +  /* Ada type names are encoded, but it is better for users to see the

> +     decoded form.  */

> +  if (ADA_TYPE_P (type))

> +    {

> +      std::string name = ada_decode (type->name (), false);


Would type->demangled_name() do the right thing? (And should this
respect the demangle option, maybe by calling type->print_name()?)

Christian
Tom Tromey June 18, 2021, 3:09 p.m. | #2
>> +  /* Ada type names are encoded, but it is better for users to see the

>> +     decoded form.  */

>> +  if (ADA_TYPE_P (type))

>> +    {

>> +      std::string name = ada_decode (type->name (), false);


Christian> Would type->demangled_name() do the right thing? (And should this
Christian> respect the demangle option, maybe by calling type->print_name()?)

I don't think there is a demangled_name method on type, only on symbol.
It may make sense to add one, though this is really only an issue for
Ada AFAIK.

Tom
Metzger, Markus T via Gdb-patches June 18, 2021, 4:25 p.m. | #3
On Fri, Jun 18, 2021 at 10:09 AM Tom Tromey <tromey@adacore.com> wrote:
>

> >> +  /* Ada type names are encoded, but it is better for users to see the

> >> +     decoded form.  */

> >> +  if (ADA_TYPE_P (type))

> >> +    {

> >> +      std::string name = ada_decode (type->name (), false);

>

> Christian> Would type->demangled_name() do the right thing? (And should this

> Christian> respect the demangle option, maybe by calling type->print_name()?)

>

> I don't think there is a demangled_name method on type, only on symbol.

> It may make sense to add one, though this is really only an issue for

> Ada AFAIK.


Oops, yes, I got myself confused. But even so, should it respect the
"demangle" setting?

Christian
Tom Tromey June 22, 2021, 4:56 p.m. | #4
>> I don't think there is a demangled_name method on type, only on symbol.

>> It may make sense to add one, though this is really only an issue for

>> Ada AFAIK.


Christian> Oops, yes, I got myself confused. But even so, should it respect the
Christian> "demangle" setting?

I don't think that would be all that useful really.
For one thing, that would mean some user option affects the python
API -- this can already happen, but it's a pain when it does.
If the encoded Ada name is needed, it would be better to add a separate
method.  But, I don't think it's needed.  It's just an artifact of the
way that GNAT worked around DWARF limitations in a particular era (an
ongoing era in some ways).

Tom

Patch

diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 4f5f42529c2..04d1c7a0ee7 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -27,6 +27,7 @@ 
 #include "objfiles.h"
 #include "language.h"
 #include "typeprint.h"
+#include "ada-lang.h"
 
 struct type_object
 {
@@ -393,6 +394,14 @@  typy_get_name (PyObject *self, void *closure)
 
   if (type->name () == NULL)
     Py_RETURN_NONE;
+  /* Ada type names are encoded, but it is better for users to see the
+     decoded form.  */
+  if (ADA_TYPE_P (type))
+    {
+      std::string name = ada_decode (type->name (), false);
+      if (!name.empty ())
+	return PyString_FromString (name.c_str ());
+    }
   return PyString_FromString (type->name ());
 }
 
diff --git a/gdb/testsuite/gdb.ada/py_range.exp b/gdb/testsuite/gdb.ada/py_range.exp
index 1a619b70ef7..3e6efa3e932 100644
--- a/gdb/testsuite/gdb.ada/py_range.exp
+++ b/gdb/testsuite/gdb.ada/py_range.exp
@@ -40,3 +40,8 @@  gdb_test "python print(int(gdb.parse_and_eval('si')))" \
 
 gdb_test "python print(int(gdb.parse_and_eval('ir')))" \
          "974"
+
+gdb_test "python print(gdb.parse_and_eval('si').type)" \
+    "foo\\.small_integer" "print type"
+gdb_test "python print(gdb.parse_and_eval('si').type.name)" \
+    "foo\\.small_integer" "print type name"