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