From: Felix Willgerodt <felix.willgerodt@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/1] gdb: Enable complete to show members of anonymous classes/structs.
Date: Tue, 6 Sep 2022 09:21:53 +0200 [thread overview]
Message-ID: <20220906072153.508130-1-felix.willgerodt@intel.com> (raw)
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
next reply other threads:[~2022-09-06 7:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-06 7:21 Felix Willgerodt [this message]
2022-09-06 14:41 ` Bruno Larsen
2022-09-20 9:48 ` [Ping] " Willgerodt, Felix
2022-10-17 8:33 ` [Ping v2] " Willgerodt, Felix
2022-10-26 12:06 ` [Ping v3] " Willgerodt, Felix
2022-11-08 11:00 ` [Ping v4] " Willgerodt, Felix
2022-11-18 20:33 ` Tom Tromey
2022-11-24 12:58 ` Willgerodt, Felix
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220906072153.508130-1-felix.willgerodt@intel.com \
--to=felix.willgerodt@intel.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).