]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.cp/overload.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.cp / overload.exp
CommitLineData
1d506c26 1# Copyright 1998-2024 Free Software Foundation, Inc.
c906108c
SS
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
c906108c 6# (at your option) any later version.
e22f8b7c 7#
c906108c
SS
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.
e22f8b7c 12#
c906108c 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
c906108c 15
c906108c 16# written by Elena Zannoni (ezannoni@cygnus.com)
b1379776 17# Rewritten by Michael Chastain <mec.gnu@mindspring.com>
c906108c
SS
18
19# This file is part of the gdb testsuite
c906108c 20
b1379776 21# Tests for overloaded member functions.
c906108c 22
b1379776
MC
23set ws "\[\r\n\t \]+"
24set nl "\[\r\n\]+"
c906108c 25
c906108c 26
0b94d2b9 27require allow_cplus_tests
d4f3574e 28
f5f3a911 29standard_testfile .cc
c906108c 30
5b362f04 31if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
f5f3a911 32 return -1
c906108c
SS
33}
34
b1379776 35# Set it up at a breakpoint so we can play with the variable values.
c906108c 36
95701cae 37if {![runto 'marker1']} {
db144853
MC
38 continue
39}
c906108c 40
775e0f04
YQ
41# Prevent symbol on address 0x0 being printed.
42gdb_test_no_output "set print symbol off"
43
db144853 44gdb_test "up" ".*main.*" "up from marker1"
c906108c 45
b1379776
MC
46# Print the monster class type.
47# See virtfunc.exp for a discussion of ptype.
48#
49# This is hairy to begin with. It is even more hairy because of the
50# XX_* alternate patterns to catch the KFAIL and XFAIL cases.
51
52set re_class "((struct|class) foo \{${ws}public:|struct foo \{)"
53set re_fields "int ifoo;${ws}const char ?\\* ?ccpfoo;"
54set XX_fields "int ifoo;${ws}char ?\\* ?ccpfoo;"
55set re_ctor "foo\\(int\\);${ws}foo\\(int, (char const|const char) ?\\*\\);${ws}foo\\(foo ?&\\);"
56set re_dtor "~foo\\((void|)\\);"
57set XX_dtor "~foo\\(int\\);"
58set re_methods "void foofunc\\(int\\);"
59set re_methods "${re_methods}${ws}void foofunc\\(int, signed char ?\\*\\);"
60set re_methods "${re_methods}${ws}int overload1arg\\((void|)\\);"
61set re_methods "${re_methods}${ws}int overload1arg\\(char\\);"
62set re_methods "${re_methods}${ws}int overload1arg\\(signed char\\);"
63set re_methods "${re_methods}${ws}int overload1arg\\(unsigned char\\);"
cec808ec
KS
64set re_methods "${re_methods}${ws}int overload1arg\\(short( int)?\\);"
65set re_methods "${re_methods}${ws}int overload1arg\\((unsigned short|short unsigned)( int)?\\);"
b1379776
MC
66set re_methods "${re_methods}${ws}int overload1arg\\(int\\);"
67set re_methods "${re_methods}${ws}int overload1arg\\(unsigned int\\);"
cec808ec
KS
68set re_methods "${re_methods}${ws}int overload1arg\\(long( int)?\\);"
69set re_methods "${re_methods}${ws}int overload1arg\\((unsigned long|long unsigned)( int)?\\);"
b1379776
MC
70set re_methods "${re_methods}${ws}int overload1arg\\(float\\);"
71set re_methods "${re_methods}${ws}int overload1arg\\(double\\);"
7062b0a0
SW
72set re_methods "${re_methods}${ws}int overload1arg\\(int \\*\\);"
73set re_methods "${re_methods}${ws}int overload1arg\\(void \\*\\);"
b1379776
MC
74set re_methods "${re_methods}${ws}int overloadfnarg\\((void|)\\);"
75set re_methods "${re_methods}${ws}int overloadfnarg\\(int\\);"
76set re_methods "${re_methods}${ws}int overloadfnarg\\(int, int ?\\(\\*\\) ?\\(int\\)\\);"
77set re_methods "${re_methods}${ws}int overloadargs\\(int\\);"
78set re_methods "${re_methods}${ws}int overloadargs\\(int, int\\);"
79set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int\\);"
80set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int\\);"
81set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int\\);"
82set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int\\);"
83set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int\\);"
84set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int\\);"
85set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int\\);"
86set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int\\);"
87set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int, int\\);"
88set re_synth "foo & operator=\\(foo const ?&\\);"
184ad485
MC
89
90gdb_test "print foo_instance1" "\\$\[0-9\]+ = \{ifoo = 111, ccpfoo = 0x0\}"
b1379776
MC
91
92gdb_test_multiple "ptype foo_instance1" "ptype foo_instance1" {
184ad485 93 -re "type = $re_class${ws}$XX_fields${ws}$re_synth${ws}$re_dtor${ws}$re_ctor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
b1379776
MC
94 # gcc 2.95.3 -gstabs+, no "const" on "const char *"
95 # TODO: gdb.base/constvar.exp has XFAILed this kind of problem for a
96 # long time, but an XFAIL really needs an external bug report.
97 # -- chastain 2003-12-31
184ad485
MC
98 # setup_xfail "*-*-*"
99 # fail "ptype foo_instance1"
100 # TODO: this should be a KFAIL.
101 pass "ptype foo_instance1 (shorter match)"
102 }
103 -re "type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_dtor${ws}$re_ctor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
104 # gcc 2.95.3 -gstabs+ if "const char *" ever gets fixed
105 pass "ptype foo_instance1"
b1379776 106 }
184ad485 107 -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$XX_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
b1379776 108 # gcc 3.3.2 -gdwarf-2, "~foo(int)"
184ad485
MC
109 # TODO: kfail this
110 # kfail "gdb/1113" "ptype foo_instance1"
111 pass "ptype foo_instance1 (shorter match)"
112 }
113 -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
114 # gcc 3.3.2 -gdwarf-2, if the dtor bug gets fixed
115 # gcc HEAD -gdwarf-2 (abi-2)
116 # TODO: just pass this
117 pass "ptype foo_instance1 (shorter match)"
118 }
119 -re "type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
120 # gcc 3.3.2 -gstabs+
121 # TODO: enough with the "shorter match"
122 pass "ptype foo_instance1 (shorter match)"
123 }
124 -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods${ws}$re_synth$nl\}$nl$gdb_prompt $" {
125 # gcc HEAD -gstabs+ (abi-2)
8f25f06b 126 pass "ptype foo_instance1 (shorter match)"
b1379776
MC
127 }
128}
c906108c 129
b1379776
MC
130# Print variables and method calls.
131# This is a walk in the park.
132
b1379776
MC
133gdb_test "print foo_instance2" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}"
134gdb_test "print foo_instance3" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}"
135
136gdb_test "print foo_instance1.overloadargs(1)" "\\$\[0-9\]+ = 1" \
137 "print call overloaded func 1 arg"
9e0b60a8 138
9ce98649
TT
139# Regression test for overloading with function pointer type.
140gdb_test "print foo_instance1.overloadfnarg(23, intintfunc)" " = 23"
141
5624293a
JB
142# If GDB fails to restore the selected frame properly after the
143# inferior function call above (see GDB PR 1155 for an explanation of
144# why this might happen), all the subsequent tests will fail. We
145# should detect and report that failure, but let the marker call
146# finish so that the rest of the tests can run undisturbed.
b1379776 147
5624293a
JB
148gdb_test_multiple "frame" "re-selected 'main' frame after inferior call" {
149 -re "#0 marker1.*$gdb_prompt $" {
150 setup_kfail "gdb/1155" s390-*-linux-gnu
151 fail "re-selected 'main' frame after inferior call"
93201743 152 gdb_test "finish" ".*main.*at .*overload.cc:.*// marker1-returns-here.*" \
5624293a
JB
153 "finish call to marker1"
154 }
155 -re "#1 ($hex in )?main.*$gdb_prompt $" {
156 pass "re-selected 'main' frame after inferior call"
157 }
158}
159
b1379776
MC
160gdb_test "print foo_instance1.overloadargs(1, 2)" \
161 "\\$\[0-9\]+ = 2" \
162 "print call overloaded func 2 args"
5624293a 163
b1379776
MC
164gdb_test "print foo_instance1.overloadargs(1, 2, 3)" \
165 "\\$\[0-9\]+ = 3" \
166 "print call overloaded func 3 args"
c2c6d25f 167
b1379776
MC
168gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4)" \
169 "\\$\[0-9\]+ = 4" \
170 "print call overloaded func 4 args"
c2c6d25f 171
b1379776
MC
172gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5)" \
173 "\\$\[0-9\]+ = 5" \
174 "print call overloaded func 5 args"
175
176gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6)" \
177 "\\$\[0-9\]+ = 6" \
178 "print call overloaded func 6 args"
179
180gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7)" \
181 "\\$\[0-9\]+ = 7" \
182 "print call overloaded func 7 args"
cbfa24cd 183
b1379776
MC
184gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8)" \
185 "\\$\[0-9\]+ = 8" \
186 "print call overloaded func 8 args"
187
188gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9)" \
189 "\\$\[0-9\]+ = 9" \
190 "print call overloaded func 9 args"
191
192gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" \
193 "\\$\[0-9\]+ = 10" \
194 "print call overloaded func 10 args"
195
196gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)" \
197 "\\$\[0-9\]+ = 11" \
198 "print call overloaded func 11 args"
199
200gdb_test "print foo_instance1.overload1arg()" \
201 "\\$\[0-9\]+ = 1" \
202 "print call overloaded func void arg"
203
204gdb_test "print foo_instance1.overload1arg((char)arg2)" \
205 "\\$\[0-9\]+ = 2" \
206 "print call overloaded func char arg"
207
208gdb_test "print foo_instance1.overload1arg((signed char)arg3)" \
209 "\\$\[0-9\]+ = 3" \
210 "print call overloaded func signed char arg"
211
212gdb_test "print foo_instance1.overload1arg((unsigned char)arg4)" \
213 "\\$\[0-9\]+ = 4" \
214 "print call overloaded func unsigned char arg"
215
216gdb_test "print foo_instance1.overload1arg((short)arg5)" \
217 "\\$\[0-9\]+ = 5" \
218 "print call overloaded func short arg"
219
220gdb_test "print foo_instance1.overload1arg((unsigned short)arg6)" \
221 "\\$\[0-9\]+ = 6" \
222 "print call overloaded func unsigned short arg"
223
224gdb_test "print foo_instance1.overload1arg((int)arg7)" \
225 "\\$\[0-9\]+ = 7" \
226 "print call overloaded func int arg"
227
228gdb_test "print foo_instance1.overload1arg((unsigned int)arg8)" \
229 "\\$\[0-9\]+ = 8" \
230 "print call overloaded func unsigned int arg"
231
232gdb_test "print foo_instance1.overload1arg((long)arg9)" \
233 "\\$\[0-9\]+ = 9" \
234 "print call overloaded func long arg"
235
236gdb_test "print foo_instance1.overload1arg((unsigned long)arg10)" \
237 "\\$\[0-9\]+ = 10" \
238 "print call overloaded func unsigned long arg"
239
240gdb_test "print foo_instance1.overload1arg((float)arg11)" \
241 "\\$\[0-9\]+ = 11" \
242 "print call overloaded func float arg"
243
244gdb_test "print foo_instance1.overload1arg((double)arg12)" \
245 "\\$\[0-9\]+ = 12" \
246 "print call overloaded func double arg"
247
7062b0a0
SW
248gdb_test "print foo_instance1.overload1arg(&arg13)" \
249 "\\$\[0-9\]+ = 13" \
250 "print call overloaded func int\\* arg"
251
252gdb_test "print foo_instance1.overload1arg(&arg14)" \
253 "\\$\[0-9\]+ = 14" \
254 "print call overloaded func char\\* arg"
255
a9d5ef47
SW
256gdb_test "print bar(a)" "= 11"
257gdb_test "print bar(b)" "= 22"
258gdb_test "print bar(c)" "= 22"
259gdb_test "print bar(d)" "= 22"
260
ac03c8d8
TT
261# PR c++/28901 - gdb thought this was ambiguous.
262gdb_test "print bar2(e, b)" " = 33"
263
b1379776
MC
264# ---
265
266# List overloaded functions.
267
5277199a
PA
268gdb_test_no_output "set listsize 1" ""
269
270# Build source listing pattern based on an inclusive line range.
271
272proc line_range_pattern { range_start range_end } {
273 global line_re
274
275 for {set i $range_start} {$i <= $range_end} {incr i} {
276 append pattern "\r\n$i\[ \t\]\[^\r\n\]*"
277 }
278
279 verbose -log "pattern $pattern"
280 return $pattern
281}
282
b1379776
MC
283# The void case is tricky because some compilers say "(void)"
284# and some compilers say "()".
285
b1379776 286gdb_test_multiple "info func overloadfnarg" "list overloaded function with no args" {
cbfa24cd 287 -re ".*overloadfnarg\\(void\\).*$gdb_prompt $" {
b1379776 288 # gcc 2
cbfa24cd
MS
289 gdb_test "list foo::overloadfnarg(void)"\
290 ".*int foo::overloadfnarg.*\\(void\\).*" \
291 "list overloaded function with no args"
292 }
293 -re ".*overloadfnarg\\(\\).*$gdb_prompt $" {
b1379776 294 # gcc 3
cbfa24cd
MS
295 gdb_test "list foo::overloadfnarg()"\
296 ".*int foo::overloadfnarg.*\\(void\\).*" \
297 "list overloaded function with no args"
298 }
cbfa24cd 299}
c2c6d25f 300
b1379776 301gdb_test "list foo::overloadfnarg(int)" \
c2c6d25f 302 "int foo::overloadfnarg.*\\(int arg\\).*" \
cbfa24cd 303 "list overloaded function with int arg"
c2c6d25f
JM
304
305gdb_test "list foo::overloadfnarg(int, int (*)(int))" \
306 "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \
cbfa24cd 307 "list overloaded function with function ptr args"
c2c6d25f 308
c2c6d25f
JM
309gdb_test "list \"foo::overloadfnarg(int, int (*)(int))\"" \
310 "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \
cbfa24cd 311 "list overloaded function with function ptr args - quotes around argument"
1c5cb38e 312
791dfb64
DJ
313# Test list with filename.
314
315gdb_test "list ${srcfile}:intToChar" "int intToChar.*"
316gdb_test "list ${srcfile}:intToChar(char)" "int intToChar.*"
317gdb_test "list ${srcfile}:'intToChar(char)'" "int intToChar.*"
318gdb_test "list '${srcfile}:intToChar(char)'" "int intToChar.*"
0c199f28
KS
319gdb_test "list '${srcfile}':intToChar(char)" "int intToChar.*"
320gdb_test "list '${srcfile}':'intToChar(char)'" "int intToChar.*"
791dfb64 321
c26106c4 322# And with filename and namespace.
791dfb64 323
c26106c4
TT
324gdb_test "list ${srcfile}:foo::overloadfnarg(int)" "int foo::overloadfnarg.*}"
325gdb_test "list ${srcfile}:'foo::overloadfnarg(int)'" "int foo::overloadfnarg.*}"
791dfb64 326
8d577d32
DC
327# Now some tests to see how overloading and namespaces interact.
328
329gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 1"
330gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 1"
331gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2"
332
714f19d5
TT
333# Static methods.
334gdb_test "print K::staticoverload ()" " = 1"
335gdb_test "print K::staticoverload (2)" " = 2"
336gdb_test "print K::staticoverload (2, 3)" " = 5"
337
338# Namespace-qualified functions.
339gdb_test "print N::nsoverload ()" " = 1"
340gdb_test "print N::nsoverload (2)" " = 2"
341gdb_test "print N::nsoverload (2, 3)" " = 5"
342
5277199a
PA
343# Test "list function" when there are multiple "function" overloads.
344
345with_test_prefix "list all overloads" {
346 # Bump up listsize again, to make sure the number of lines to
347 # display before/after each location is computed correctly.
348 gdb_test_no_output "set listsize 10"
349
350 set line_bar_A [gdb_get_line_number "int bar (A)"]
351 set line_bar_B [gdb_get_line_number "int bar (B)"]
352 set lines1 [line_range_pattern [expr $line_bar_A - 5] [expr $line_bar_A + 4]]
353 set lines2 [line_range_pattern [expr $line_bar_B - 5] [expr $line_bar_B + 4]]
354
355 set any "\[^\r\n\]*"
06871ae8
PA
356 set h1_re "file: \"${any}overload.cc\", line number: $line_bar_A, symbol: \"bar\\(A\\)\""
357 set h2_re "file: \"${any}overload.cc\", line number: $line_bar_B, symbol: \"bar\\(B\\)\""
5277199a
PA
358 gdb_test "list bar" "${h1_re}${lines1}\r\n${h2_re}${lines2}"
359}
360
95701cae 361if {![runto 'XXX::marker2']} {
8d577d32
DC
362 continue
363}
364
365gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 3" "print overloadNamespace(1) in XXX"
366gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 3" "print overloadNamespace('a') in XXX"
367gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2" "print overloadNamespace(dummyInstance) in XXX"
368
b1379776
MC
369# One last mysterious test.
370# I wonder what this is for?
371
372gdb_test "print intToChar(1)" "\\$\[0-9\]+ = 297"
85feb311
KS
373
374# Test expression evaluation with overloaded methods
375gdb_test "print foo::overload1arg" \
e96ec2ba 376 "non-unique member `overload1arg' requires type instantiation"
85feb311
KS
377
378gdb_test "print foo::overload1arg(char***)" \
e96ec2ba 379 "no member function matches that type instantiation"
85feb311
KS
380
381gdb_test "print foo::overload1arg(void)" \
e96ec2ba 382 "\\$$decimal = {int \\(foo \\*( const|)\\)} $hex <foo::overload1arg\\(\\)>"
85feb311
KS
383
384foreach t [list char "signed char" "unsigned char" "short" \
385 "unsigned short" int "unsigned int" long "unsigned long" \
386 float double] {
387 gdb_test "print foo::overload1arg($t)" \
e96ec2ba 388 "\\$$decimal = {int \\(foo \\*( const|), $t\\)} $hex <foo::overload1arg\\($t\\)>"
85feb311 389}