aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHannes Domani <ssbssa@yahoo.de>2020-12-18 16:17:46 +0100
committerHannes Domani <ssbssa@yahoo.de>2020-12-18 22:02:13 +0100
commitb3f9469bfa42ef352e1d96f8733817242dd41a2e (patch)
tree07def32995663d9caa9b5e4bca93573a6ec8a34e
parenta9e48095a8e595f04042f9455a50ce6acbd5232c (diff)
downloadgdb-b3f9469bfa42ef352e1d96f8733817242dd41a2e.zip
gdb-b3f9469bfa42ef352e1d96f8733817242dd41a2e.tar.gz
gdb-b3f9469bfa42ef352e1d96f8733817242dd41a2e.tar.bz2
Fix accessing a method's fields from Python
Considering this example: struct C { int func() { return 1; } } c; int main() { return c.func(); } Accessing the fields of C::func, when requesting the function by its type, works: (gdb) py print(gdb.parse_and_eval('C::func').type.fields()[0].type) C * const But when trying to do the same via a class instance, it fails: (gdb) py print(gdb.parse_and_eval('c')['func'].type.fields()[0].type) Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: Type is not a structure, union, enum, or function type. Error while executing Python code. The difference is that in the former the function type is TYPE_CODE_FUNC: (gdb) py print(gdb.parse_and_eval('C::func').type.code == gdb.TYPE_CODE_FUNC) True And in the latter the function type is TYPE_CODE_METHOD: (gdb) py print(gdb.parse_and_eval('c')['func'].type.code == gdb.TYPE_CODE_METHOD) True So this adds the functionality for TYPE_CODE_METHOD as well. gdb/ChangeLog: 2020-12-18 Hannes Domani <ssbssa@yahoo.de> * python/py-type.c (typy_get_composite): Add TYPE_CODE_METHOD. gdb/testsuite/ChangeLog: 2020-12-18 Hannes Domani <ssbssa@yahoo.de> * gdb.python/py-type.exp: Add tests for TYPE_CODE_METHOD.
-rw-r--r--gdb/ChangeLog4
-rw-r--r--gdb/python/py-type.c1
-rw-r--r--gdb/testsuite/ChangeLog4
-rw-r--r--gdb/testsuite/gdb.python/py-type.exp3
4 files changed, 12 insertions, 0 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 9b7b010..1a104b1 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,7 @@
+2020-12-18 Hannes Domani <ssbssa@yahoo.de>
+
+ * python/py-type.c (typy_get_composite): Add TYPE_CODE_METHOD.
+
2020-12-18 Jameson Nash <vtjnash@gmail.com>
* coffread.c (linetab_offset): Change type to file_ptr.
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 1c7cacb..3fc0f61 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -471,6 +471,7 @@ typy_get_composite (struct type *type)
if (type->code () != TYPE_CODE_STRUCT
&& type->code () != TYPE_CODE_UNION
&& type->code () != TYPE_CODE_ENUM
+ && type->code () != TYPE_CODE_METHOD
&& type->code () != TYPE_CODE_FUNC)
{
PyErr_SetString (PyExc_TypeError,
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 24df951..7342563 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2020-12-18 Hannes Domani <ssbssa@yahoo.de>
+
+ * gdb.python/py-type.exp: Add tests for TYPE_CODE_METHOD.
+
2020-12-18 Tom Tromey <tromey@adacore.com>
* gdb.ada/fixed_points.exp: Also run with
diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp
index c01442c..5166f9e 100644
--- a/gdb/testsuite/gdb.python/py-type.exp
+++ b/gdb/testsuite/gdb.python/py-type.exp
@@ -83,15 +83,18 @@ proc test_fields {lang} {
gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[0\].type)" "C \\* const"
gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[1\].type)" "int"
gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[2\].type)" "char"
+ gdb_test "python print (gdb.parse_and_eval ('c')\['a_method'\].type.fields ()\[0\].type)" "C \\* const"
gdb_test "python print (len (gdb.parse_and_eval ('C::a_const_method').type.fields ()))" "3"
gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[0\].type)" "const C \\* const"
gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[1\].type)" "int"
gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[2\].type)" "char"
+ gdb_test "python print (gdb.parse_and_eval ('c')\['a_const_method'\].type.fields ()\[0\].type)" "const C \\* const"
gdb_test "python print (len (gdb.parse_and_eval ('C::a_static_method').type.fields ()))" "2"
gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[0\].type)" "int"
gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[1\].type)" "char"
+ gdb_test "python print (gdb.parse_and_eval ('c')\['a_static_method'\].type.fields ()\[0\].type)" "int"
}
# Test normal fields usage in structs.