public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Bruno Larsen <blarsen@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] Improve GDB's baseclass detection with typedefs
Date: Thu, 29 Sep 2022 09:14:18 +0200	[thread overview]
Message-ID: <20220929071418.2621404-1-blarsen@redhat.com> (raw)

When a class inherits from a typedef'd baseclass, GDB may be unable to
find the baseclass if the user is not using the typedef'd name, as is
tested on gdb.cp/virtbase2.exp; the reason that test case is working
under gcc is that the dwarf generated by gcc links the class to the
original definition of the baseclass, not to the typedef.  If the
inheritance is linked to the typedef, such as how clang does it,
gdb.cp/virtbase2.exp starts failing.

This can also be seen in gdb.cp/impl-this.exp, when attempting to print
D::Bint::i, and GDB not being able to find the baseclass Bint.

This happens because searching for baseclasses only uses the macro
TYPE_BASECLASS_NAME, which returns the typedef'd name. However, we can't
switch that macro to checking for typedefs, otherwise we wouldn't be
able to find the typedef'd name anymore. This is fixed by searching for
members or baseclasses by name, we check both the saved name and the
name after checking for typedefs.

This also fixes said long-standing bug in gdb.cp/impl-this.exp when the
compiler adds information about typedefs in the debuginfo.
---
 gdb/cp-namespace.c                 |  5 ++--
 gdb/testsuite/gdb.cp/impl-this.exp | 45 ++++++++++++++++++++++--------
 gdb/valops.c                       |  4 +--
 3 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index 837eafc52f1..1d7759a7669 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -780,12 +780,13 @@ cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
     {
       struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
-      const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
+      const char *tdef_name = TYPE_BASECLASS_NAME (parent_type, i);
+      const char *base_name = type->name ();
 
       if (base_name == NULL)
 	continue;
 
-      if (streq (base_name, name))
+      if (streq (tdef_name, name) || streq (base_name, name))
 	return type;
 
       type = cp_find_type_baseclass_by_name (type, name);
diff --git a/gdb/testsuite/gdb.cp/impl-this.exp b/gdb/testsuite/gdb.cp/impl-this.exp
index 2910bf7d79d..4a11fb21823 100644
--- a/gdb/testsuite/gdb.cp/impl-this.exp
+++ b/gdb/testsuite/gdb.cp/impl-this.exp
@@ -26,6 +26,8 @@ if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
     return -1
 }
 
+set gcc_used [test_compiler_info gcc-*-*]
+
 # First test expressions when there is no context.
 with_test_prefix "before run" {
     gdb_test "print i" "No symbol \"i\" in current context."
@@ -63,15 +65,23 @@ with_test_prefix "at D::f (valid expressions)" {
     gdb_test "print D::i" "= 4"
     gdb_test "print D::B<int>::i" "= 2"
     gdb_test "print B<int>::i" "= 2"
-    gdb_test "print D::Bint::i" \
-	"No type \"Bint\" within class or namespace \"D\"."
+
+    if {$gcc_used} {
+	setup_xfail *-*-* gcc/60833
+    }
+    gdb_test "print D::Bint::i" "= 2"
+
     gdb_test "print Bint::i" "= 2"
     gdb_test "print D::C::i" "= 3"
     gdb_test "print C::i" "= 3"
     gdb_test "print D::B<int>::A<int>::i" "= 1"
     gdb_test "print B<int>::A<int>::i" "= 1"
-    gdb_test "print D::Bint::A<int>::i" \
-	"No type \"Bint\" within class or namespace \"D\"."
+
+    if {$gcc_used} {
+	setup_xfail *-*-* gcc/60833
+    }
+    gdb_test "print D::Bint::A<int>::i" "= 1"
+
     gdb_test "print Bint::A<int>::i" "= 1"
     gdb_test "print A<int>::i" "= 1"
     gdb_test "print D::C::A<int>::i" "= 1"
@@ -88,11 +98,16 @@ with_test_prefix "at D::f (valid expressions)" {
 with_test_prefix "at D::f (invalid expressions)" {
     gdb_test "print D::B<int>::c" "There is no field named c"
     gdb_test "print D::B<int>::A<int>::c" "There is no field named c"
-    gdb_test "print D::Bint::c" \
-	"No type \"Bint\" within class or namespace \"D\"."
 
-    gdb_test "print D::Bint::A<int>::c" \
-	"No type \"Bint\" within class or namespace \"D\"."
+    if {$gcc_used} {
+	setup_xfail *-*-* gcc/60833
+    }
+    gdb_test "print D::Bint::c" "There is no field named c"
+    if {$gcc_used} {
+	setup_xfail *-*-* gcc/60833
+    }
+    gdb_test "print D::Bint::A<int>::c" "There is no field named c"
+
     gdb_test "print D::C::A<int>::c" "There is no field named c"
     gdb_test "print B<int>::c" "There is no field named c"
     gdb_test "print B<int>::A<int>::c" "There is no field named c"
@@ -101,10 +116,16 @@ with_test_prefix "at D::f (invalid expressions)" {
     gdb_test "print C::A<int>::c" "There is no field named c"
     gdb_test "print D::B<int>::x" "There is no field named x"
     gdb_test "print D::B<int>::A<int>::x" "There is no field named x"
-    gdb_test "print D::Bint::x" \
-	"No type \"Bint\" within class or namespace \"D\"."
-    gdb_test "print D::Bint::A<int>::x" \
-	"No type \"Bint\" within class or namespace \"D\"."
+
+    if {$gcc_used} {
+	setup_xfail *-*-* gcc/60833
+    }
+    gdb_test "print D::Bint::x" "There is no field named x"
+    if {$gcc_used} {
+	setup_xfail *-*-* gcc/60833
+    }
+    gdb_test "print D::Bint::A<int>::x" "There is no field named x"
+
     gdb_test "print B<int>::x" "There is no field named x"
     gdb_test "print B<int>::A<int>::x" "There is no field named x"
     gdb_test "print Bint::x" "There is no field named x"
diff --git a/gdb/valops.c b/gdb/valops.c
index b4866d089ac..345be3ea5c9 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -2066,9 +2066,7 @@ struct_field_searcher::search (struct value *arg1, LONGEST offset,
 	 name is not yet filled in.  */
       int found_baseclass = (m_looking_for_baseclass
 			     && TYPE_BASECLASS_NAME (type, i) != NULL
-			     && (strcmp_iw (m_name,
-					    TYPE_BASECLASS_NAME (type,
-								 i)) == 0));
+			     && (strcmp_iw (m_name, basetype->name ()) == 0));
       LONGEST boffset = value_embedded_offset (arg1) + offset;
 
       if (BASETYPE_VIA_VIRTUAL (type, i))
-- 
2.37.3


             reply	other threads:[~2022-09-29  7:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29  7:14 Bruno Larsen [this message]
2022-09-29 17:54 ` Tom Tromey
2022-10-03  8:10   ` Bruno Larsen

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=20220929071418.2621404-1-blarsen@redhat.com \
    --to=blarsen@redhat.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).