Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.cp / ambiguous.exp
blobb430b039887c2d7c743659498fd12a39b11396d9
1 # Copyright 1998-2023 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 # 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.
22 if { [skip_cplus_tests] } {  return }
24 standard_testfile .cc
26 set additional_flags ""
27 if {[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"
34 if {[prepare_for_testing "failed to prepare" $testfile $srcfile \
35      [list debug c++ $additional_flags]]} {
36     return -1
39 if {![runto_main]} {
40     return
43 # Run to a breakpoint after the variables have been initialized so we
44 # can play with the variable values.
46 set lineno [gdb_get_line_number "set breakpoint here"]
48 gdb_breakpoint $lineno
49 gdb_continue_to_breakpoint "breakpoint here"
51 set number -?$decimal
53 with_test_prefix "all vars" {
54     gdb_test "print a1" \
55         " = \{x = 1, y = 2\}"
57     gdb_test "print a2" \
58         " = \{x = 1, y = 2\}"
60     gdb_test "print a3" \
61         " = \{x = 1, y = 2\}"
63     gdb_test "print x" \
64         " = \{<A1> = \{x = 1, y = 2\}, <A2> = \{x = 3, y = 4\}, z = 5\}"
66     gdb_test "print l" \
67         " = \{<A1> = \{x = 1, y = 2\}, z = 3\}"
69     gdb_test "print m" \
70         " = \{<A2> = \{x = 1, y = 2\}, w = 3\}"
72     gdb_test "print n" \
73         " = \{<L> = \{<A1> = \{x = 1, y = 2\}, z = 7\}, <M> = \{<A2> = \{x = 3, y = 4\}, w = 5\}, r = 6\}"
75     gdb_test "print k" \
76         " = \{<A1> = \{x = 1, y = 2\}, i = 3\}"
78     gdb_test "print j" \
79         " = {<K> = {<A1> = {x = 1, y = 2}, i = 5}, <L> = {<A1> = {x = 3, y = 4}, z = 6}, j = 7}"
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\}"
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\}"
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\}"
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\}"
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.
98 with_test_prefix "all fields" {
99     gdb_test "print a1.x" " = 1"
100     gdb_test "print a1.y" " = 2"
102     gdb_test "print a2.x" " = 1"
103     gdb_test "print a2.y" " = 2"
105     gdb_test "print a3.x" " = 1"
106     gdb_test "print a3.y" " = 2"
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"
114     gdb_test "print l.x" " = 1"
115     gdb_test "print l.y" " = 2"
116     gdb_test "print l.z" " = 3"
118     gdb_test "print m.x" " = 1"
119     gdb_test "print m.y" " = 2"
120     gdb_test "print m.w" " = 3"
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"
130     gdb_test "print k.x" " = 1"
131     gdb_test "print k.y" " = 2"
132     gdb_test "print k.i" " = 3"
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"
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"
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"
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"
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"
181 # Test that printing WHAT reports an error about FIELD being ambiguous
182 # in TYPE, and that the candidates are CANDIDATES.
183 proc test_ambiguous {what field type candidates} {
184     set msg "Request for member '$field' is ambiguous in type '$type'. Candidates are:"
186     foreach c $candidates {
187         set c_re [string_to_regexp $c]
188         append msg "\r\n  $c_re"
189     }
191     gdb_test "print $what" $msg
194 # X is derived from A1 and A2; both A1 and A2 have a member 'x'
195 test_ambiguous "x.x" "x" "X" {
196     "'int A1::x' (X -> A1)"
197     "'int A2::x' (X -> A2)"
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'.
202 test_ambiguous "n.x" "x" "N" {
203     "'int A1::x' (N -> L -> A1)"
204     "'int A2::x' (N -> M -> A2)"
207 # J is derived from A1 twice.  A1 has a member x.
208 test_ambiguous "j.x" "x" "J" {
209     "'int A1::x' (J -> K -> A1)"
210     "'int A1::x' (J -> L -> A1)"
213 # JV is derived from A1 but A1 is a virtual base. Should not
214 # report an ambiguity in this case.
215 gdb_test "print jv.x" " = 1"
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.
220 test_ambiguous "jva1.x" "x" "JVA1" {
221     "'int A1::x' (JVA1 -> KV -> A1)"
222     "'int A1::x' (JVA1 -> A1)"
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'.
228 test_ambiguous "jva2.x" "x" "JVA2" {
229     "'int A1::x' (JVA2 -> KV -> A1)"
230     "'int A2::x' (JVA2 -> A2)"
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.
236 gdb_test "print jva1v.x" " = 1"
238 # Now check for ambiguous bases.
240 # J is derived from A1 twice; report ambiguity if a J is
241 # cast to an A1.
242 gdb_test "print (A1)j" "base class 'A1' is ambiguous in type 'J'"
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.
246 gdb_test "print (A1)jv" " = {x = 1, y = 2}"
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.
250 gdb_test "print (A1)jva1" "base class 'A1' is ambiguous in type 'JVA1'"
252 # Add an intermediate cast to KV, and it should work.
253 setup_kfail "c++/26550" *-*-*
254 gdb_test "print (KV)jva1" " = \{<A1> = \{x = 3, y = 4\}, _vptr.KV = $hex <VTT for KV>, i = 6\}"
255 setup_kfail "c++/26550" *-*-*
256 gdb_test "print (A1)(KV)jva1" " = \{x = 3, y = 4\}"
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.
260 gdb_test "print (A1)jva1v" " = {x = 1, y = 2}"
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.
267 test_ambiguous "je.x" "x" "JE" {
268     "'int A1::x' (JE -> A1)"
269     "'empty A4::x' (JE -> A4)"