gdb/testsuite/
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.cp / overload.exp
CommitLineData
0b302171 1# Copyright 1998-2004, 2007-2012 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
d4f3574e
SS
27if { [skip_cplus_tests] } { continue }
28
c906108c
SS
29set testfile "overload"
30set srcfile ${testfile}.cc
31set binfile ${objdir}/${subdir}/${testfile}
32
33if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
b60f0898
JB
34 untested overload.exp
35 return -1
c906108c
SS
36}
37
c906108c
SS
38gdb_exit
39gdb_start
40gdb_reinitialize_dir $srcdir/$subdir
41gdb_load ${binfile}
42
b1379776 43# Set it up at a breakpoint so we can play with the variable values.
c906108c 44
db144853
MC
45if ![runto 'marker1'] then {
46 perror "couldn't run to marker1"
47 continue
48}
c906108c 49
db144853 50gdb_test "up" ".*main.*" "up from marker1"
c906108c 51
b1379776
MC
52# Print the monster class type.
53# See virtfunc.exp for a discussion of ptype.
54#
55# This is hairy to begin with. It is even more hairy because of the
56# XX_* alternate patterns to catch the KFAIL and XFAIL cases.
57
58set re_class "((struct|class) foo \{${ws}public:|struct foo \{)"
59set re_fields "int ifoo;${ws}const char ?\\* ?ccpfoo;"
60set XX_fields "int ifoo;${ws}char ?\\* ?ccpfoo;"
61set re_ctor "foo\\(int\\);${ws}foo\\(int, (char const|const char) ?\\*\\);${ws}foo\\(foo ?&\\);"
62set re_dtor "~foo\\((void|)\\);"
63set XX_dtor "~foo\\(int\\);"
64set re_methods "void foofunc\\(int\\);"
65set re_methods "${re_methods}${ws}void foofunc\\(int, signed char ?\\*\\);"
66set re_methods "${re_methods}${ws}int overload1arg\\((void|)\\);"
67set re_methods "${re_methods}${ws}int overload1arg\\(char\\);"
68set re_methods "${re_methods}${ws}int overload1arg\\(signed char\\);"
69set re_methods "${re_methods}${ws}int overload1arg\\(unsigned char\\);"
cec808ec
KS
70set re_methods "${re_methods}${ws}int overload1arg\\(short( int)?\\);"
71set re_methods "${re_methods}${ws}int overload1arg\\((unsigned short|short unsigned)( int)?\\);"
b1379776
MC
72set re_methods "${re_methods}${ws}int overload1arg\\(int\\);"
73set re_methods "${re_methods}${ws}int overload1arg\\(unsigned int\\);"
cec808ec
KS
74set re_methods "${re_methods}${ws}int overload1arg\\(long( int)?\\);"
75set re_methods "${re_methods}${ws}int overload1arg\\((unsigned long|long unsigned)( int)?\\);"
b1379776
MC
76set re_methods "${re_methods}${ws}int overload1arg\\(float\\);"
77set re_methods "${re_methods}${ws}int overload1arg\\(double\\);"
7062b0a0
SW
78set re_methods "${re_methods}${ws}int overload1arg\\(int \\*\\);"
79set re_methods "${re_methods}${ws}int overload1arg\\(void \\*\\);"
b1379776
MC
80set re_methods "${re_methods}${ws}int overloadfnarg\\((void|)\\);"
81set re_methods "${re_methods}${ws}int overloadfnarg\\(int\\);"
82set re_methods "${re_methods}${ws}int overloadfnarg\\(int, int ?\\(\\*\\) ?\\(int\\)\\);"
83set re_methods "${re_methods}${ws}int overloadargs\\(int\\);"
84set re_methods "${re_methods}${ws}int overloadargs\\(int, int\\);"
85set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int\\);"
86set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int\\);"
87set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int\\);"
88set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int\\);"
89set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int\\);"
90set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int\\);"
91set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int\\);"
92set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int\\);"
93set re_methods "${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int, int\\);"
94set re_synth "foo & operator=\\(foo const ?&\\);"
184ad485
MC
95
96gdb_test "print foo_instance1" "\\$\[0-9\]+ = \{ifoo = 111, ccpfoo = 0x0\}"
b1379776
MC
97
98gdb_test_multiple "ptype foo_instance1" "ptype foo_instance1" {
184ad485
MC
99 -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
100 # gcc 2.95.3 -gdwarf-2
101 # TODO: this is not a quirk!
102 pass "ptype foo_instance1 (HP aCC -- known quirk with ~foo parameter list)"
9e0b60a8 103 }
184ad485 104 -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
105 # gcc 2.95.3 -gstabs+, no "const" on "const char *"
106 # TODO: gdb.base/constvar.exp has XFAILed this kind of problem for a
107 # long time, but an XFAIL really needs an external bug report.
108 # -- chastain 2003-12-31
184ad485
MC
109 # setup_xfail "*-*-*"
110 # fail "ptype foo_instance1"
111 # TODO: this should be a KFAIL.
112 pass "ptype foo_instance1 (shorter match)"
113 }
114 -re "type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_dtor${ws}$re_ctor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
115 # gcc 2.95.3 -gstabs+ if "const char *" ever gets fixed
116 pass "ptype foo_instance1"
b1379776 117 }
184ad485 118 -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$XX_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
b1379776 119 # gcc 3.3.2 -gdwarf-2, "~foo(int)"
184ad485
MC
120 # TODO: kfail this
121 # kfail "gdb/1113" "ptype foo_instance1"
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$nl\}$nl$gdb_prompt $" {
125 # gcc 3.3.2 -gdwarf-2, if the dtor bug gets fixed
126 # gcc HEAD -gdwarf-2 (abi-2)
127 # TODO: just pass this
128 pass "ptype foo_instance1 (shorter match)"
129 }
130 -re "type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
131 # gcc 3.3.2 -gstabs+
132 # TODO: enough with the "shorter match"
133 pass "ptype foo_instance1 (shorter match)"
134 }
135 -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods${ws}$re_synth$nl\}$nl$gdb_prompt $" {
136 # gcc HEAD -gstabs+ (abi-2)
8f25f06b 137 pass "ptype foo_instance1 (shorter match)"
b1379776
MC
138 }
139}
c906108c 140
b1379776
MC
141# Print variables and method calls.
142# This is a walk in the park.
143
b1379776
MC
144gdb_test "print foo_instance2" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}"
145gdb_test "print foo_instance3" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}"
146
147gdb_test "print foo_instance1.overloadargs(1)" "\\$\[0-9\]+ = 1" \
148 "print call overloaded func 1 arg"
9e0b60a8 149
5624293a
JB
150# If GDB fails to restore the selected frame properly after the
151# inferior function call above (see GDB PR 1155 for an explanation of
152# why this might happen), all the subsequent tests will fail. We
153# should detect and report that failure, but let the marker call
154# finish so that the rest of the tests can run undisturbed.
b1379776 155
5624293a
JB
156gdb_test_multiple "frame" "re-selected 'main' frame after inferior call" {
157 -re "#0 marker1.*$gdb_prompt $" {
158 setup_kfail "gdb/1155" s390-*-linux-gnu
159 fail "re-selected 'main' frame after inferior call"
93201743 160 gdb_test "finish" ".*main.*at .*overload.cc:.*// marker1-returns-here.*" \
5624293a
JB
161 "finish call to marker1"
162 }
163 -re "#1 ($hex in )?main.*$gdb_prompt $" {
164 pass "re-selected 'main' frame after inferior call"
165 }
166}
167
b1379776
MC
168gdb_test "print foo_instance1.overloadargs(1, 2)" \
169 "\\$\[0-9\]+ = 2" \
170 "print call overloaded func 2 args"
5624293a 171
b1379776
MC
172gdb_test "print foo_instance1.overloadargs(1, 2, 3)" \
173 "\\$\[0-9\]+ = 3" \
174 "print call overloaded func 3 args"
c2c6d25f 175
b1379776
MC
176gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4)" \
177 "\\$\[0-9\]+ = 4" \
178 "print call overloaded func 4 args"
c2c6d25f 179
b1379776
MC
180gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5)" \
181 "\\$\[0-9\]+ = 5" \
182 "print call overloaded func 5 args"
183
184gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6)" \
185 "\\$\[0-9\]+ = 6" \
186 "print call overloaded func 6 args"
187
188gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7)" \
189 "\\$\[0-9\]+ = 7" \
190 "print call overloaded func 7 args"
cbfa24cd 191
b1379776
MC
192gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8)" \
193 "\\$\[0-9\]+ = 8" \
194 "print call overloaded func 8 args"
195
196gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9)" \
197 "\\$\[0-9\]+ = 9" \
198 "print call overloaded func 9 args"
199
200gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" \
201 "\\$\[0-9\]+ = 10" \
202 "print call overloaded func 10 args"
203
204gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)" \
205 "\\$\[0-9\]+ = 11" \
206 "print call overloaded func 11 args"
207
208gdb_test "print foo_instance1.overload1arg()" \
209 "\\$\[0-9\]+ = 1" \
210 "print call overloaded func void arg"
211
212gdb_test "print foo_instance1.overload1arg((char)arg2)" \
213 "\\$\[0-9\]+ = 2" \
214 "print call overloaded func char arg"
215
216gdb_test "print foo_instance1.overload1arg((signed char)arg3)" \
217 "\\$\[0-9\]+ = 3" \
218 "print call overloaded func signed char arg"
219
220gdb_test "print foo_instance1.overload1arg((unsigned char)arg4)" \
221 "\\$\[0-9\]+ = 4" \
222 "print call overloaded func unsigned char arg"
223
224gdb_test "print foo_instance1.overload1arg((short)arg5)" \
225 "\\$\[0-9\]+ = 5" \
226 "print call overloaded func short arg"
227
228gdb_test "print foo_instance1.overload1arg((unsigned short)arg6)" \
229 "\\$\[0-9\]+ = 6" \
230 "print call overloaded func unsigned short arg"
231
232gdb_test "print foo_instance1.overload1arg((int)arg7)" \
233 "\\$\[0-9\]+ = 7" \
234 "print call overloaded func int arg"
235
236gdb_test "print foo_instance1.overload1arg((unsigned int)arg8)" \
237 "\\$\[0-9\]+ = 8" \
238 "print call overloaded func unsigned int arg"
239
240gdb_test "print foo_instance1.overload1arg((long)arg9)" \
241 "\\$\[0-9\]+ = 9" \
242 "print call overloaded func long arg"
243
244gdb_test "print foo_instance1.overload1arg((unsigned long)arg10)" \
245 "\\$\[0-9\]+ = 10" \
246 "print call overloaded func unsigned long arg"
247
248gdb_test "print foo_instance1.overload1arg((float)arg11)" \
249 "\\$\[0-9\]+ = 11" \
250 "print call overloaded func float arg"
251
252gdb_test "print foo_instance1.overload1arg((double)arg12)" \
253 "\\$\[0-9\]+ = 12" \
254 "print call overloaded func double arg"
255
7062b0a0
SW
256gdb_test "print foo_instance1.overload1arg(&arg13)" \
257 "\\$\[0-9\]+ = 13" \
258 "print call overloaded func int\\* arg"
259
260gdb_test "print foo_instance1.overload1arg(&arg14)" \
261 "\\$\[0-9\]+ = 14" \
262 "print call overloaded func char\\* arg"
263
a9d5ef47
SW
264gdb_test "print bar(a)" "= 11"
265gdb_test "print bar(b)" "= 22"
266gdb_test "print bar(c)" "= 22"
267gdb_test "print bar(d)" "= 22"
268
b1379776
MC
269# ---
270
271# List overloaded functions.
272
273# The void case is tricky because some compilers say "(void)"
274# and some compilers say "()".
275
35ec993f 276gdb_test_no_output "set listsize 1" ""
b1379776 277gdb_test_multiple "info func overloadfnarg" "list overloaded function with no args" {
cbfa24cd 278 -re ".*overloadfnarg\\(void\\).*$gdb_prompt $" {
b1379776 279 # gcc 2
cbfa24cd
MS
280 gdb_test "list foo::overloadfnarg(void)"\
281 ".*int foo::overloadfnarg.*\\(void\\).*" \
282 "list overloaded function with no args"
283 }
284 -re ".*overloadfnarg\\(\\).*$gdb_prompt $" {
b1379776 285 # gcc 3
cbfa24cd
MS
286 gdb_test "list foo::overloadfnarg()"\
287 ".*int foo::overloadfnarg.*\\(void\\).*" \
288 "list overloaded function with no args"
289 }
cbfa24cd 290}
c2c6d25f 291
b1379776 292gdb_test "list foo::overloadfnarg(int)" \
c2c6d25f 293 "int foo::overloadfnarg.*\\(int arg\\).*" \
cbfa24cd 294 "list overloaded function with int arg"
c2c6d25f
JM
295
296gdb_test "list foo::overloadfnarg(int, int (*)(int))" \
297 "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \
cbfa24cd 298 "list overloaded function with function ptr args"
c2c6d25f 299
c2c6d25f
JM
300gdb_test "list \"foo::overloadfnarg(int, int (*)(int))\"" \
301 "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \
cbfa24cd 302 "list overloaded function with function ptr args - quotes around argument"
1c5cb38e 303
791dfb64
DJ
304# Test list with filename.
305
306gdb_test "list ${srcfile}:intToChar" "int intToChar.*"
307gdb_test "list ${srcfile}:intToChar(char)" "int intToChar.*"
308gdb_test "list ${srcfile}:'intToChar(char)'" "int intToChar.*"
309gdb_test "list '${srcfile}:intToChar(char)'" "int intToChar.*"
0c199f28
KS
310gdb_test "list '${srcfile}':intToChar(char)" "int intToChar.*"
311gdb_test "list '${srcfile}':'intToChar(char)'" "int intToChar.*"
791dfb64 312
c26106c4 313# And with filename and namespace.
791dfb64 314
c26106c4
TT
315gdb_test "list ${srcfile}:foo::overloadfnarg(int)" "int foo::overloadfnarg.*}"
316gdb_test "list ${srcfile}:'foo::overloadfnarg(int)'" "int foo::overloadfnarg.*}"
791dfb64 317
8d577d32
DC
318# Now some tests to see how overloading and namespaces interact.
319
320gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 1"
321gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 1"
322gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2"
323
714f19d5
TT
324# Static methods.
325gdb_test "print K::staticoverload ()" " = 1"
326gdb_test "print K::staticoverload (2)" " = 2"
327gdb_test "print K::staticoverload (2, 3)" " = 5"
328
329# Namespace-qualified functions.
330gdb_test "print N::nsoverload ()" " = 1"
331gdb_test "print N::nsoverload (2)" " = 2"
332gdb_test "print N::nsoverload (2, 3)" " = 5"
333
8d577d32
DC
334if ![runto 'XXX::marker2'] then {
335 perror "couldn't run to XXX::marker2"
336 continue
337}
338
339gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 3" "print overloadNamespace(1) in XXX"
340gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 3" "print overloadNamespace('a') in XXX"
341gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2" "print overloadNamespace(dummyInstance) in XXX"
342
b1379776
MC
343# One last mysterious test.
344# I wonder what this is for?
345
346gdb_test "print intToChar(1)" "\\$\[0-9\]+ = 297"
85feb311
KS
347
348# Test expression evaluation with overloaded methods
349gdb_test "print foo::overload1arg" \
350 "non-unique member `overload1arg' requires type instantiation" \
351 "print foo::overload1arg"
352
353gdb_test "print foo::overload1arg(char***)" \
354 "no member function matches that type instantiation" \
355 "print foo::overload1arg(char***)"
356
357gdb_test "print foo::overload1arg(void)" \
493f3c00 358 "\\$$decimal = {int \\(foo \\*( const|)\\)} $hex <foo::overload1arg\\(\\)>" \
85feb311
KS
359 "print foo::overload1arg(void)"
360
361foreach t [list char "signed char" "unsigned char" "short" \
362 "unsigned short" int "unsigned int" long "unsigned long" \
363 float double] {
364 gdb_test "print foo::overload1arg($t)" \
493f3c00 365 "\\$$decimal = {int \\(foo \\*( const|), $t\\)} $hex <foo::overload1arg\\($t\\)>" \
85feb311
KS
366 "print foo::overload1arg($t)"
367}
This page took 1.179816 seconds and 4 git commands to generate.