From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by sourceware.org (Postfix) with ESMTPS id 63B353947422 for ; Tue, 6 Sep 2022 07:22:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 63B353947422 X-IronPort-AV: E=McAfee;i="6500,9779,10461"; a="279540035" X-IronPort-AV: E=Sophos;i="5.93,293,1654585200"; d="scan'208";a="279540035" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2022 00:22:39 -0700 X-IronPort-AV: E=Sophos;i="5.93,293,1654585200"; d="scan'208";a="942351410" Received: from mulvlfelix.iul.intel.com (HELO localhost) ([172.28.48.92]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2022 00:22:38 -0700 From: Felix Willgerodt 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 Message-Id: <20220906072153.508130-1-felix.willgerodt@intel.com> X-Mailer: git-send-email 2.34.3 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Sep 2022 07:22:42 -0000 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 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