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