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