Re: [PATCH] Add an objfile getter to gdb.Type

Simon Marchi
2019-05-23T19:34:12Z

Hi Christian,

On 2019-05-23 2:39 p.m., Christian Biesinger via gdb-patches wrote:
> diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
> index 98e52bb770..f769ad03a2 100644
> --- a/gdb/doc/python.texi
> +++ b/gdb/doc/python.texi
> @@ -1087,6 +1087,11 @@ languages have this concept. If this type has no tag name, then
> @code{None} is returned.
> @end defvar
>
> +@defvar Type.objfile
> +The @code{gdb.Objfile} that this type was defined in, or @code{None} if
> +there is no associated objfile.
> +@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 22cc658a8b..722960e032 100644
> --- a/gdb/python/py-type.c
> +++ b/gdb/python/py-type.c
> @@ -413,6 +413,20 @@ typy_get_tag (PyObject *self, void *closure)
> return PyString_FromString (tagname);
> }
>
> +/* Return the type's tag, or None. */

copy-pasto: "type's objfile"

> +static PyObject *
> +typy_get_objfile (PyObject *self, void *closure)
> +{
> + struct type *type = ((type_object *) self)->type;
> + struct objfile *objfile = nullptr;
> +
> + objfile = TYPE_OBJFILE(type);

You can write it as

struct objfile *objfile = objfile = TYPE_OBJFILE (type);

directly.

We would need a corresponding test though. It should be a relatively easy addition
to testsuite/gdb.python/py-type.exp. See here for info about how to run just one
test:

https://sourceware.org/gdb/wiki/TestingGDB#Running_specific_tests

Thanks!

Simon