1# Copyright 1998-2023 Free Software Foundation, Inc.
2
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.
7#
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.
12#
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/>.
15
16# This file is part of the gdb testsuite
17
18# Print out various class objects' members and check that the error
19# about the field or baseclass being ambiguous is emitted at the right
20# times.
21
22if { [skip_cplus_tests] } {  return }
23
24standard_testfile .cc
25
26set additional_flags ""
27if {[test_compiler_info {gcc-[0-9]-*}]} {
28    # GCCs prior to 10.1 do not support -Wno-inaccessible-base.
29    set additional_flags "additional_flags=-w"
30} elseif {[test_compiler_info gcc*] || [test_compiler_info clang*]} {
31    set additional_flags "additional_flags=-Wno-inaccessible-base"
32}
33
34if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
35     [list debug c++ $additional_flags]]} {
36    return -1
37}
38
39if {![runto_main]} {
40    return
41}
42
43# Run to a breakpoint after the variables have been initialized so we
44# can play with the variable values.
45
46set lineno [gdb_get_line_number "set breakpoint here"]
47
48gdb_breakpoint $lineno
49gdb_continue_to_breakpoint "breakpoint here"
50
51set number -?$decimal
52
53with_test_prefix "all vars" {
54    gdb_test "print a1" \
55	" = \{x = 1, y = 2\}"
56
57    gdb_test "print a2" \
58	" = \{x = 1, y = 2\}"
59
60    gdb_test "print a3" \
61	" = \{x = 1, y = 2\}"
62
63    gdb_test "print x" \
64	" = \{<A1> = \{x = 1, y = 2\}, <A2> = \{x = 3, y = 4\}, z = 5\}"
65
66    gdb_test "print l" \
67	" = \{<A1> = \{x = 1, y = 2\}, z = 3\}"
68
69    gdb_test "print m" \
70	" = \{<A2> = \{x = 1, y = 2\}, w = 3\}"
71
72    gdb_test "print n" \
73	" = \{<L> = \{<A1> = \{x = 1, y = 2\}, z = 7\}, <M> = \{<A2> = \{x = 3, y = 4\}, w = 5\}, r = 6\}"
74
75    gdb_test "print k" \
76	" = \{<A1> = \{x = 1, y = 2\}, i = 3\}"
77
78    gdb_test "print j" \
79	" = {<K> = {<A1> = {x = 1, y = 2}, i = 5}, <L> = {<A1> = {x = 3, y = 4}, z = 6}, j = 7}"
80
81    gdb_test "print jv" \
82	" = \{<KV> = \{<A1> = \{x = 1, y = 2\}, _vptr.KV = $hex <vtable for JV.*>, i = 3\}, <LV> = \{_vptr.LV = $hex <VTT for JV>, z = 4\}, jv = 5\}"
83
84    # No way to initialize one of the A1's, so just take any number there.
85    gdb_test "print jva1" \
86	" = \{<KV> = \{<A1> = \{x = 3, y = 4\}, _vptr.KV = $hex <vtable for JVA1.*>, i = 6\}, <LV> = \{_vptr.LV = $hex <VTT for JVA1>, z = 5\}, <A1> = \{x = $number, y = $number\}, jva1 = 7\}"
87
88    gdb_test "print jva2" \
89	" = \{<KV> = \{<A1> = \{x = 3, y = 4\}, _vptr.KV = $hex <vtable for JVA2.*>, i = 8\}, <LV> = \{_vptr.LV = $hex <VTT for JVA2>, z = 7\}, <A2> = \{x = 5, y = 6\}, jva2 = 9\}"
90
91    gdb_test "print jva1v" \
92	" = \{<KV> = \{<A1> = \{x = 1, y = 2\}, _vptr.KV = $hex <vtable for JVA1V+.*>, i = 4\}, <LV> = \{_vptr.LV = $hex <VTT for JVA1V>, z = 3\}, jva1v = 5\}"
93}
94
95# Check that we can access all the fields correctly, using the same
96# syntax as used in the .cc file.  Keep the order here in sync with
97# the .cc file.
98with_test_prefix "all fields" {
99    gdb_test "print a1.x" " = 1"
100    gdb_test "print a1.y" " = 2"
101
102    gdb_test "print a2.x" " = 1"
103    gdb_test "print a2.y" " = 2"
104
105    gdb_test "print a3.x" " = 1"
106    gdb_test "print a3.y" " = 2"
107
108    gdb_test "print x.A1::x" " = 1"
109    gdb_test "print x.A1::y" " = 2"
110    gdb_test "print x.A2::x" " = 3"
111    gdb_test "print x.A2::y" " = 4"
112    gdb_test "print x.z" " = 5"
113
114    gdb_test "print l.x" " = 1"
115    gdb_test "print l.y" " = 2"
116    gdb_test "print l.z" " = 3"
117
118    gdb_test "print m.x" " = 1"
119    gdb_test "print m.y" " = 2"
120    gdb_test "print m.w" " = 3"
121
122    gdb_test "print n.A1::x" " = 1"
123    gdb_test "print n.A1::y" " = 2"
124    gdb_test "print n.A2::x" " = 3"
125    gdb_test "print n.A2::y" " = 4"
126    gdb_test "print n.w" " = 5"
127    gdb_test "print n.r" " = 6"
128    gdb_test "print n.z" " = 7"
129
130    gdb_test "print k.x" " = 1"
131    gdb_test "print k.y" " = 2"
132    gdb_test "print k.i" " = 3"
133
134    gdb_test "print j.K::x" " = 1"
135    gdb_test "print j.K::y" " = 2"
136    gdb_test "print j.L::x" " = 3"
137    gdb_test "print j.L::y" " = 4"
138    gdb_test "print j.i" " = 5"
139    gdb_test "print j.z" " = 6"
140    gdb_test "print j.j" " = 7"
141
142    gdb_test "print jv.x" " = 1"
143    gdb_test "print jv.y" " = 2"
144    gdb_test "print jv.i" " = 3"
145    gdb_test "print jv.z" " = 4"
146    gdb_test "print jv.jv" " = 5"
147
148    setup_kfail "c++/26550" *-*-*
149    gdb_test "print jva1.KV::x" " = 1"
150    setup_kfail "c++/26550" *-*-*
151    gdb_test "print jva1.KV::y" " = 2"
152    setup_kfail "c++/26550" *-*-*
153    gdb_test "print jva1.LV::x" " = 3"
154    setup_kfail "c++/26550" *-*-*
155    gdb_test "print jva1.LV::y" " = 4"
156    gdb_test "print jva1.z" " = 5"
157    gdb_test "print jva1.i" " = 6"
158    gdb_test "print jva1.jva1" "= 7"
159
160    setup_kfail "c++/26550" *-*-*
161    gdb_test "print jva2.KV::x" " = 1"
162    setup_kfail "c++/26550" *-*-*
163    gdb_test "print jva2.KV::y" " = 2"
164    setup_kfail "c++/26550" *-*-*
165    gdb_test "print jva2.LV::x" " = 3"
166    setup_kfail "c++/26550" *-*-*
167    gdb_test "print jva2.LV::y" " = 4"
168    gdb_test "print jva2.A2::x" " = 5"
169    gdb_test "print jva2.A2::y" " = 6"
170    gdb_test "print jva2.z" " = 7"
171    gdb_test "print jva2.i" " = 8"
172    gdb_test "print jva2.jva2" "= 9"
173
174    gdb_test "print jva1v.x" " = 1"
175    gdb_test "print jva1v.y" " = 2"
176    gdb_test "print jva1v.z" " = 3"
177    gdb_test "print jva1v.i" " = 4"
178    gdb_test "print jva1v.jva1v" " = 5"
179}
180
181# Test that printing WHAT reports an error about FIELD being ambiguous
182# in TYPE, and that the candidates are CANDIDATES.
183proc test_ambiguous {what field type candidates} {
184    set msg "Request for member '$field' is ambiguous in type '$type'. Candidates are:"
185
186    foreach c $candidates {
187	set c_re [string_to_regexp $c]
188	append msg "\r\n  $c_re"
189    }
190
191    gdb_test "print $what" $msg
192}
193
194# X is derived from A1 and A2; both A1 and A2 have a member 'x'
195test_ambiguous "x.x" "x" "X" {
196    "'int A1::x' (X -> A1)"
197    "'int A2::x' (X -> A2)"
198}
199
200# N is derived from A1 and A2, but not immediately -- two steps
201# up in the hierarchy. Both A1 and A2 have a member 'x'.
202test_ambiguous "n.x" "x" "N" {
203    "'int A1::x' (N -> L -> A1)"
204    "'int A2::x' (N -> M -> A2)"
205}
206
207# J is derived from A1 twice.  A1 has a member x.
208test_ambiguous "j.x" "x" "J" {
209    "'int A1::x' (J -> K -> A1)"
210    "'int A1::x' (J -> L -> A1)"
211}
212
213# JV is derived from A1 but A1 is a virtual base. Should not
214# report an ambiguity in this case.
215gdb_test "print jv.x" " = 1"
216
217# JVA1 is derived from A1; A1 occurs as a virtual base in two
218# ancestors, and as a non-virtual immediate base. Ambiguity must
219# be reported.
220test_ambiguous "jva1.x" "x" "JVA1" {
221    "'int A1::x' (JVA1 -> KV -> A1)"
222    "'int A1::x' (JVA1 -> A1)"
223}
224
225# JVA2 is derived from A1 & A2; A1 occurs as a virtual base in two
226# ancestors, and A2 is a non-virtual immediate base. Ambiguity must
227# be reported as A1 and A2 both have a member 'x'.
228test_ambiguous "jva2.x" "x" "JVA2" {
229    "'int A1::x' (JVA2 -> KV -> A1)"
230    "'int A2::x' (JVA2 -> A2)"
231}
232
233# JVA1V is derived from A1; A1 occurs as a virtual base in two
234# ancestors, and also as a virtual immediate base. Ambiguity must
235# not be reported.
236gdb_test "print jva1v.x" " = 1"
237
238# Now check for ambiguous bases.
239
240# J is derived from A1 twice; report ambiguity if a J is
241# cast to an A1.
242gdb_test "print (A1)j" "base class 'A1' is ambiguous in type 'J'"
243
244# JV is derived from A1 twice, but A1 is a virtual base; should
245# not report ambiguity when a JV is cast to an A1.
246gdb_test "print (A1)jv" " = {x = 1, y = 2}"
247
248# JVA1 is derived from A1; A1 is a virtual base and also a
249# non-virtual base.  Must report ambiguity if a JVA1 is cast to an A1.
250gdb_test "print (A1)jva1" "base class 'A1' is ambiguous in type 'JVA1'"
251
252# Add an intermediate cast to KV, and it should work.
253setup_kfail "c++/26550" *-*-*
254gdb_test "print (KV)jva1" " = \{<A1> = \{x = 3, y = 4\}, _vptr.KV = $hex <VTT for KV>, i = 6\}"
255setup_kfail "c++/26550" *-*-*
256gdb_test "print (A1)(KV)jva1" " = \{x = 3, y = 4\}"
257
258# JVA1V is derived from A1; A1 is a virtual base indirectly
259# and also directly; must not report ambiguity when a JVA1V is cast to an A1.
260gdb_test "print (A1)jva1v" " = {x = 1, y = 2}"
261
262# C++20 introduced a way to have ambiguous fields with the same byte offset.
263# This class explicitly tests for that.
264# if this is tested with a compiler that can't handle [[no_unique_address]]
265# the code should still correctly identify the ambiguity because of
266# different byte offsets.
267test_ambiguous "je.x" "x" "JE" {
268    "'int A1::x' (JE -> A1)"
269    "'empty A4::x' (JE -> A4)"
270}
271