binutils-gdb/gdb/testsuite/gdb.cp/impl-this.exp
Bruno Larsen 2820f08f23 Improve GDB's baseclass detection with typedefs
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.
2022-10-03 10:09:30 +02:00

151 lines
5.5 KiB
Text

# Copyright 2013-2022 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This file is part of the gdb testsuite
# Test expressions which assume an implicit "this" with a qualified
# name.
if {[skip_cplus_tests]} { return }
standard_testfile .cc
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."
gdb_test "print D::i" "Cannot reference non-static field \"i\""
gdb_test "print D::B<int>::i" "Cannot reference non-static field \"i\""
gdb_test "print B<int>::i" "Cannot reference non-static field \"i\""
gdb_test "print D::C::i" "Cannot reference non-static field \"i\""
gdb_test "print C::i" "Cannot reference non-static field \"i\""
gdb_test "print D::B<int>::A<int>::i" \
"Cannot reference non-static field \"i\""
gdb_test "print B<int>::A<int>::i" "Cannot reference non-static field \"i\""
gdb_test "print A<int>::i" "Cannot reference non-static field \"i\""
gdb_test "print D::C::A<int>::i" "Cannot reference non-static field \"i\""
gdb_test "print C::A<int>::i" "Cannot reference non-static field \"i\""
gdb_test "print D::x" "Cannot reference non-static field \"x\""
gdb_test "print x" "No symbol \"x\" in current context."
gdb_test "print D::C::c" "Cannot reference non-static field \"c\""
gdb_test "print C::c" "Cannot reference non-static field \"c\""
gdb_test "print c" "No symbol \"c\" in current context."
gdb_test "print D::A<int>::i" "Cannot reference non-static field \"i\""
}
# Run to D::f.
if {![runto_main]} {
perror "couldn't run to main"
return
}
gdb_breakpoint "D::f"
gdb_continue_to_breakpoint "continue to D::f"
# Now test valid expressions in the class hierarchy for D.
with_test_prefix "at D::f (valid expressions)" {
gdb_test "print i" "= 4"
gdb_test "print D::i" "= 4"
gdb_test "print D::B<int>::i" "= 2"
gdb_test "print B<int>::i" "= 2"
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"
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"
gdb_test "print C::A<int>::i" "= 1"
gdb_test "print D::x" "= 40"
gdb_test "print x" "= 40"
gdb_test "print D::C::c" "= 30"
gdb_test "print C::c" "= 30"
gdb_test "print c" "= 30"
gdb_test "print D::A<int>::i" "= 1"
}
# Test some invalid 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"
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"
gdb_test "print Bint::c" "There is no field named c"
gdb_test "print Bint::A<int>::c" "There is no field named c"
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"
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"
gdb_test "print Bint::A<int>::x" "There is no field named x"
gdb_test "print D::C::x" "There is no field named x"
gdb_test "print C::x" "There is no field named x"
gdb_test "print D::C::A<int>::x" "There is no field named x"
gdb_test "print C::A<int>::x" "There is no field named x"
}
# Test some ambiguous names
with_test_prefix "at D::f (ambiguous names)" {
gdb_test "print B<int>::common" " = 200"
gdb_test "print Bint::common" " = 200"
gdb_test "print C::common" " = 300"
gdb_test "print am.i" " = 1000"
gdb_test "print am.A<int>::i" \
"base class 'A<int>' is ambiguous in type 'Ambig'"
gdb_test "print am.BB::A<int>::i" \
"base class 'A<int>' is ambiguous in type 'Ambig'"
gdb_test "print am.CC::A<int>::i" \
"base class 'A<int>' is ambiguous in type 'Ambig'"
}