1 # Copyright 2013-2024 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file is part of the gdb testsuite
18 # Test expressions which assume an implicit "this" with a qualified
21 require allow_cplus_tests
25 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
29 set gcc_used [test_compiler_info gcc-*-*]
31 # First test expressions when there is no context.
32 with_test_prefix "before run" {
33 gdb_test "print i" "No symbol \"i\" in current context."
34 gdb_test "print D::i" "Cannot reference non-static field \"i\""
35 gdb_test "print D::B<int>::i" "Cannot reference non-static field \"i\""
36 gdb_test "print B<int>::i" "Cannot reference non-static field \"i\""
37 gdb_test "print D::C::i" "Cannot reference non-static field \"i\""
38 gdb_test "print C::i" "Cannot reference non-static field \"i\""
39 gdb_test "print D::B<int>::A<int>::i" \
40 "Cannot reference non-static field \"i\""
41 gdb_test "print B<int>::A<int>::i" "Cannot reference non-static field \"i\""
42 gdb_test "print A<int>::i" "Cannot reference non-static field \"i\""
43 gdb_test "print D::C::A<int>::i" "Cannot reference non-static field \"i\""
44 gdb_test "print C::A<int>::i" "Cannot reference non-static field \"i\""
45 gdb_test "print D::x" "Cannot reference non-static field \"x\""
46 gdb_test "print x" "No symbol \"x\" in current context."
47 gdb_test "print D::C::c" "Cannot reference non-static field \"c\""
48 gdb_test "print C::c" "Cannot reference non-static field \"c\""
49 gdb_test "print c" "No symbol \"c\" in current context."
50 gdb_test "print D::A<int>::i" "Cannot reference non-static field \"i\""
59 gdb_continue_to_breakpoint "continue to D::f"
61 # Now test valid expressions in the class hierarchy for D.
62 with_test_prefix "at D::f (valid expressions)" {
63 gdb_test "print i" "= 4"
64 gdb_test "print D::i" "= 4"
65 gdb_test "print D::B<int>::i" "= 2"
66 gdb_test "print B<int>::i" "= 2"
69 setup_xfail *-*-* gcc/60833
71 gdb_test "print D::Bint::i" "= 2"
73 gdb_test "print Bint::i" "= 2"
74 gdb_test "print D::C::i" "= 3"
75 gdb_test "print C::i" "= 3"
76 gdb_test "print D::B<int>::A<int>::i" "= 1"
77 gdb_test "print B<int>::A<int>::i" "= 1"
80 setup_xfail *-*-* gcc/60833
82 gdb_test "print D::Bint::A<int>::i" "= 1"
84 gdb_test "print Bint::A<int>::i" "= 1"
85 gdb_test "print A<int>::i" "= 1"
86 gdb_test "print D::C::A<int>::i" "= 1"
87 gdb_test "print C::A<int>::i" "= 1"
88 gdb_test "print D::x" "= 40"
89 gdb_test "print x" "= 40"
90 gdb_test "print D::C::c" "= 30"
91 gdb_test "print C::c" "= 30"
92 gdb_test "print c" "= 30"
93 gdb_test "print D::A<int>::i" "= 1"
96 # Test some invalid expressions
97 with_test_prefix "at D::f (invalid expressions)" {
98 gdb_test "print D::B<int>::c" "There is no field named c"
99 gdb_test "print D::B<int>::A<int>::c" "There is no field named c"
102 setup_xfail *-*-* gcc/60833
104 gdb_test "print D::Bint::c" "There is no field named c"
106 setup_xfail *-*-* gcc/60833
108 gdb_test "print D::Bint::A<int>::c" "There is no field named c"
110 gdb_test "print D::C::A<int>::c" "There is no field named c"
111 gdb_test "print B<int>::c" "There is no field named c"
112 gdb_test "print B<int>::A<int>::c" "There is no field named c"
113 gdb_test "print Bint::c" "There is no field named c"
114 gdb_test "print Bint::A<int>::c" "There is no field named c"
115 gdb_test "print C::A<int>::c" "There is no field named c"
116 gdb_test "print D::B<int>::x" "There is no field named x"
117 gdb_test "print D::B<int>::A<int>::x" "There is no field named x"
120 setup_xfail *-*-* gcc/60833
122 gdb_test "print D::Bint::x" "There is no field named x"
124 setup_xfail *-*-* gcc/60833
126 gdb_test "print D::Bint::A<int>::x" "There is no field named x"
128 gdb_test "print B<int>::x" "There is no field named x"
129 gdb_test "print B<int>::A<int>::x" "There is no field named x"
130 gdb_test "print Bint::x" "There is no field named x"
131 gdb_test "print Bint::A<int>::x" "There is no field named x"
132 gdb_test "print D::C::x" "There is no field named x"
133 gdb_test "print C::x" "There is no field named x"
134 gdb_test "print D::C::A<int>::x" "There is no field named x"
135 gdb_test "print C::A<int>::x" "There is no field named x"
138 # Test some ambiguous names
139 with_test_prefix "at D::f (ambiguous names)" {
140 gdb_test "print B<int>::common" " = 200"
141 gdb_test "print Bint::common" " = 200"
142 gdb_test "print C::common" " = 300"
143 gdb_test "print am.i" " = 1000"
144 gdb_test "print am.A<int>::i" \
145 "base class 'A<int>' is ambiguous in type 'Ambig'"
146 gdb_test "print am.BB::A<int>::i" \
147 "base class 'A<int>' is ambiguous in type 'Ambig'"
148 gdb_test "print am.CC::A<int>::i" \
149 "base class 'A<int>' is ambiguous in type 'Ambig'"