[PATCH 1/1] gdb: Enable complete to show members of anonymous classes/structs.
Felix Willgerodt
felix.willgerodt@intel.com
Tue Sep 6 07:21:53 GMT 2022
This problem shows with anonymous structs/classes:
~~~
struct {
private:
int unique_name_foo = 5;
public:
int get() { return unique_name_foo; } /* breakpoint. */
} a;
~~~
Before:
~~~
(gdb) p unique_name_foo
$1 = 5
(gdb) complete p unique_name_fo
(gdb)
~~~
After:
~~~
(gdb) p unique_name_foo
$1 = 5
(gdb) complete p unique_name_fo
p unique_name_foo
(gdb)
~~~
As we are able to print the member we should be able to complete on it.
GDB doesn't look at "this" and its members for complete, while it does
when printing. So I tried fixing that.
I saw that "this" is always represented as a PTR type with the symbol
class LOC_COMPUTED (with g++ 11.3.1, clang++ 10.0.1 and icpx 2022.1).
Not knowing too much about LOC_COMPUTED, I am assuming that this is the right
symbol class for this case and that we should adjust
completion_list_add_fields() for it.
But it could very well be that I missed something. Any comments welcome!
---
gdb/symtab.c | 18 ++++++++++++------
gdb/testsuite/gdb.cp/cpcompletion.exp | 16 +++++++++++++---
gdb/testsuite/gdb.cp/pr9594.cc | 9 ++++++---
3 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 40887f59d1f..6edeb4ea9db 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -5688,14 +5688,20 @@ completion_list_add_fields (completion_tracker &tracker,
const lookup_name_info &lookup_name,
const char *text, const char *word)
{
- if (sym->aclass () == LOC_TYPEDEF)
+ struct type *t = sym->type ();
+
+ if (sym->aclass () == LOC_TYPEDEF
+ || (sym->aclass () == LOC_COMPUTED && t->code () == TYPE_CODE_PTR))
{
- struct type *t = sym->type ();
- enum type_code c = t->code ();
- int j;
+ /* Anonymous classes/structs are often/always represented as a
+ pointer with LOC_COMPUTED. Since we also want to show their
+ fields as a completion result (as we can print them) we resolve
+ their target type. */
+ if (t->code () == TYPE_CODE_PTR)
+ t = TYPE_TARGET_TYPE (t);
- if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
- for (j = TYPE_N_BASECLASSES (t); j < t->num_fields (); j++)
+ if (t->code () == TYPE_CODE_UNION || t->code () == TYPE_CODE_STRUCT)
+ for (int j = TYPE_N_BASECLASSES (t); j < t->num_fields (); j++)
if (t->field (j).name ())
completion_list_add_name (tracker, sym->language (),
t->field (j).name (),
diff --git a/gdb/testsuite/gdb.cp/cpcompletion.exp b/gdb/testsuite/gdb.cp/cpcompletion.exp
index d19ac9c1b69..07ec936e95a 100644
--- a/gdb/testsuite/gdb.cp/cpcompletion.exp
+++ b/gdb/testsuite/gdb.cp/cpcompletion.exp
@@ -76,10 +76,11 @@ test_class_complete Foo F "complete class methods beginning with F" \
# The tests below depend on the current code scope.
-set bp_location [gdb_get_line_number "Set breakpoint here" ${srcfile}]
+set bp_1 [gdb_get_line_number "BP1" ${srcfile}]
+set bp_2 [gdb_get_line_number "BP2" ${srcfile}]
-if {![runto "${srcfile}:$bp_location"]} {
- perror "test suppressed"
+if {![runto "${srcfile}:$bp_1"]} {
+ untested "failed to run to first breakpoint"
return
}
@@ -135,3 +136,12 @@ with_test_prefix "expression with namespace" {
# Add a disambiguating character and we get a unique completion.
test_gdb_complete_unique "p Test_NS::f" "p Test_NS::foo"
}
+
+# Test completion when stopped in a method of an anonymous struct.
+gdb_breakpoint "$srcfile:$bp_2"
+gdb_continue_to_breakpoint "continue to second bp" ".*$srcfile:$bp_2.*"
+
+# We should be able to complete on the members. We are able to print
+# them after all.
+gdb_test "p unique_name_foo" "= 5"
+gdb_test "complete p unique_name_fo" "p unique_name_foo"
diff --git a/gdb/testsuite/gdb.cp/pr9594.cc b/gdb/testsuite/gdb.cp/pr9594.cc
index 54ddaafc0ca..a3823f9b84f 100644
--- a/gdb/testsuite/gdb.cp/pr9594.cc
+++ b/gdb/testsuite/gdb.cp/pr9594.cc
@@ -56,10 +56,13 @@ int main ()
{
// Anonymous struct with method.
struct {
- int get() { return 5; }
+ private:
+ int unique_name_foo = 5;
+ public:
+ int get() { return unique_name_foo; } /* BP2. */
} a;
Foo foo1;
- foo1.set_foo (42); // Set breakpoint here.
- a.get(); // Prevent compiler from throwing 'a' away.
+ foo1.set_foo (42); /* BP1. */
+ a.get ();
return 0;
}
--
2.34.3
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
More information about the Gdb-patches
mailing list