aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.cp/overload.exp
blob: 78f9836aaf96cef745843105b257ea541126aa58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Copyright 1998-2016 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# written by Elena Zannoni (ezannoni@cygnus.com)
# Rewritten by Michael Chastain <mec.gnu@mindspring.com>

# This file is part of the gdb testsuite

# Tests for overloaded member functions.

set ws "\[\r\n\t \]+"
set nl "\[\r\n\]+"


if { [skip_cplus_tests] } { continue }

standard_testfile .cc

if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
    return -1
}

# Set it up at a breakpoint so we can play with the variable values.

if ![runto 'marker1'] then {
    perror "couldn't run to marker1"
    continue
}

# Prevent symbol on address 0x0 being printed.
gdb_test_no_output "set print symbol off"

gdb_test "up" ".*main.*" "up from marker1"

# Print the monster class type.
# See virtfunc.exp for a discussion of ptype.
#
# This is hairy to begin with.  It is even more hairy because of the
# XX_* alternate patterns to catch the KFAIL and XFAIL cases.

set re_class	"((struct|class) foo \{${ws}public:|struct foo \{)"
set re_fields	"int ifoo;${ws}const char ?\\* ?ccpfoo;"
set XX_fields  	"int ifoo;${ws}char ?\\* ?ccpfoo;"
set re_ctor	"foo\\(int\\);${ws}foo\\(int, (char const|const char) ?\\*\\);${ws}foo\\(foo ?&\\);"
set re_dtor	"~foo\\((void|)\\);"
set XX_dtor	"~foo\\(int\\);"
set re_methods	                  "void foofunc\\(int\\);"
set re_methods	"${re_methods}${ws}void foofunc\\(int, signed char ?\\*\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\((void|)\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(char\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(signed char\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(unsigned char\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(short( int)?\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\((unsigned short|short unsigned)( int)?\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(int\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(unsigned int\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(long( int)?\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\((unsigned long|long unsigned)( int)?\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(float\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(double\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(int \\*\\);"
set re_methods	"${re_methods}${ws}int overload1arg\\(void \\*\\);"
set re_methods	"${re_methods}${ws}int overloadfnarg\\((void|)\\);"
set re_methods	"${re_methods}${ws}int overloadfnarg\\(int\\);"
set re_methods	"${re_methods}${ws}int overloadfnarg\\(int, int ?\\(\\*\\) ?\\(int\\)\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int\\);"
set re_methods	"${re_methods}${ws}int overloadargs\\(int, int, int, int, int, int, int, int, int, int, int\\);"
set re_synth	"foo & operator=\\(foo const ?&\\);"

gdb_test "print foo_instance1" "\\$\[0-9\]+ = \{ifoo = 111, ccpfoo = 0x0\}"

gdb_test_multiple "ptype foo_instance1" "ptype foo_instance1" {
    -re "type = $re_class${ws}$XX_fields${ws}$re_synth${ws}$re_dtor${ws}$re_ctor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
	# gcc 2.95.3 -gstabs+, no "const" on "const char *"
	# TODO: gdb.base/constvar.exp has XFAILed this kind of problem for a
	# long time, but an XFAIL really needs an external bug report.
	# -- chastain 2003-12-31
	# setup_xfail "*-*-*"
	# fail "ptype foo_instance1"
	# TODO: this should be a KFAIL.
	pass "ptype foo_instance1 (shorter match)"
    }
    -re "type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_dtor${ws}$re_ctor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
	# gcc 2.95.3 -gstabs+ if "const char *" ever gets fixed
	pass "ptype foo_instance1"
    }
    -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$XX_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
	# gcc 3.3.2 -gdwarf-2, "~foo(int)"
	# TODO: kfail this
	# kfail "gdb/1113" "ptype foo_instance1"
	pass "ptype foo_instance1 (shorter match)"
    }
    -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
	# gcc 3.3.2 -gdwarf-2, if the dtor bug gets fixed
	# gcc HEAD -gdwarf-2 (abi-2)
	# TODO: just pass this
	pass "ptype foo_instance1 (shorter match)"
    }
    -re "type = $re_class${ws}$re_fields${ws}$re_synth${ws}$re_ctor${ws}$re_dtor${ws}$re_methods$nl\}$nl$gdb_prompt $" {
	# gcc 3.3.2 -gstabs+
	# TODO: enough with the "shorter match"
	pass "ptype foo_instance1 (shorter match)"
    }
    -re "type = $re_class${ws}$re_fields${ws}$re_ctor${ws}$re_dtor${ws}$re_methods${ws}$re_synth$nl\}$nl$gdb_prompt $" {
	# gcc HEAD -gstabs+ (abi-2)
	pass "ptype foo_instance1 (shorter match)"
    }
}

# Print variables and method calls.
# This is a walk in the park.

gdb_test "print foo_instance2" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}"
gdb_test "print foo_instance3" "\\$\[0-9\]+ = \{ifoo = 222, ccpfoo = $hex \"A\"\}"

gdb_test "print foo_instance1.overloadargs(1)" "\\$\[0-9\]+ = 1" \
    "print call overloaded func 1 arg"

# Regression test for overloading with function pointer type.
gdb_test "print foo_instance1.overloadfnarg(23, intintfunc)" " = 23"

# If GDB fails to restore the selected frame properly after the
# inferior function call above (see GDB PR 1155 for an explanation of
# why this might happen), all the subsequent tests will fail.  We
# should detect and report that failure, but let the marker call
# finish so that the rest of the tests can run undisturbed.

gdb_test_multiple "frame" "re-selected 'main' frame after inferior call" {
    -re "#0  marker1.*$gdb_prompt $" {
        setup_kfail "gdb/1155" s390-*-linux-gnu
        fail "re-selected 'main' frame after inferior call"
        gdb_test "finish" ".*main.*at .*overload.cc:.*// marker1-returns-here.*" \
            "finish call to marker1"
    }
    -re "#1  ($hex in )?main.*$gdb_prompt $" {
        pass "re-selected 'main' frame after inferior call"
    }
}

gdb_test "print foo_instance1.overloadargs(1, 2)" \
    "\\$\[0-9\]+ = 2" \
    "print call overloaded func 2 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3)" \
    "\\$\[0-9\]+ = 3" \
    "print call overloaded func 3 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4)" \
    "\\$\[0-9\]+ = 4" \
    "print call overloaded func 4 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5)" \
    "\\$\[0-9\]+ = 5" \
    "print call overloaded func 5 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6)" \
    "\\$\[0-9\]+ = 6" \
    "print call overloaded func 6 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7)" \
    "\\$\[0-9\]+ = 7" \
    "print call overloaded func 7 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8)" \
    "\\$\[0-9\]+ = 8" \
    "print call overloaded func 8 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9)" \
    "\\$\[0-9\]+ = 9" \
    "print call overloaded func 9 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" \
    "\\$\[0-9\]+ = 10" \
    "print call overloaded func 10 args"

gdb_test "print foo_instance1.overloadargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)" \
    "\\$\[0-9\]+ = 11" \
    "print call overloaded func 11 args"

gdb_test "print foo_instance1.overload1arg()" \
    "\\$\[0-9\]+ = 1" \
    "print call overloaded func void arg"

gdb_test "print foo_instance1.overload1arg((char)arg2)" \
    "\\$\[0-9\]+ = 2" \
    "print call overloaded func char arg"

gdb_test "print foo_instance1.overload1arg((signed char)arg3)" \
    "\\$\[0-9\]+ = 3" \
    "print call overloaded func signed char arg"

gdb_test "print foo_instance1.overload1arg((unsigned char)arg4)" \
    "\\$\[0-9\]+ = 4" \
    "print call overloaded func unsigned char arg"

gdb_test "print foo_instance1.overload1arg((short)arg5)" \
    "\\$\[0-9\]+ = 5" \
    "print call overloaded func short arg"

gdb_test "print foo_instance1.overload1arg((unsigned short)arg6)" \
    "\\$\[0-9\]+ = 6" \
    "print call overloaded func unsigned short arg"

gdb_test "print foo_instance1.overload1arg((int)arg7)" \
    "\\$\[0-9\]+ = 7" \
    "print call overloaded func int arg"

gdb_test "print foo_instance1.overload1arg((unsigned int)arg8)" \
    "\\$\[0-9\]+ = 8" \
    "print call overloaded func unsigned int arg"

gdb_test "print foo_instance1.overload1arg((long)arg9)" \
    "\\$\[0-9\]+ = 9" \
    "print call overloaded func long arg"

gdb_test "print foo_instance1.overload1arg((unsigned long)arg10)" \
    "\\$\[0-9\]+ = 10" \
    "print call overloaded func unsigned long arg"

gdb_test "print foo_instance1.overload1arg((float)arg11)" \
    "\\$\[0-9\]+ = 11" \
    "print call overloaded func float arg"

gdb_test "print foo_instance1.overload1arg((double)arg12)" \
    "\\$\[0-9\]+ = 12" \
    "print call overloaded func double arg"

gdb_test "print foo_instance1.overload1arg(&arg13)" \
    "\\$\[0-9\]+ = 13" \
    "print call overloaded func int\\* arg"

gdb_test "print foo_instance1.overload1arg(&arg14)" \
    "\\$\[0-9\]+ = 14" \
    "print call overloaded func char\\* arg"

gdb_test "print bar(a)" "= 11"
gdb_test "print bar(b)" "= 22"
gdb_test "print bar(c)" "= 22"
gdb_test "print bar(d)" "= 22"

# ---

# List overloaded functions.

# The void case is tricky because some compilers say "(void)"
# and some compilers say "()".

gdb_test_no_output "set listsize 1" ""
gdb_test_multiple "info func overloadfnarg" "list overloaded function with no args" {
    -re ".*overloadfnarg\\(void\\).*$gdb_prompt $" {
	# gcc 2
	gdb_test "list foo::overloadfnarg(void)"\
		".*int foo::overloadfnarg.*\\(void\\).*" \
		"list overloaded function with no args"
    }
    -re ".*overloadfnarg\\(\\).*$gdb_prompt $" {
	# gcc 3
	gdb_test "list foo::overloadfnarg()"\
		".*int foo::overloadfnarg.*\\(void\\).*" \
		"list overloaded function with no args"
    }
}

gdb_test "list foo::overloadfnarg(int)" \
    "int foo::overloadfnarg.*\\(int arg\\).*" \
   "list overloaded function with int arg"

gdb_test "list foo::overloadfnarg(int, int (*)(int))" \
   "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \
   "list overloaded function with function ptr args"

gdb_test "list \"foo::overloadfnarg(int, int (*)(int))\"" \
   "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \
   "list overloaded function with function ptr args - quotes around argument"

# Test list with filename.

gdb_test "list ${srcfile}:intToChar" "int intToChar.*"
gdb_test "list ${srcfile}:intToChar(char)" "int intToChar.*"
gdb_test "list ${srcfile}:'intToChar(char)'" "int intToChar.*"
gdb_test "list '${srcfile}:intToChar(char)'" "int intToChar.*"
gdb_test "list '${srcfile}':intToChar(char)" "int intToChar.*"
gdb_test "list '${srcfile}':'intToChar(char)'" "int intToChar.*"

# And with filename and namespace.

gdb_test "list ${srcfile}:foo::overloadfnarg(int)" "int foo::overloadfnarg.*}"
gdb_test "list ${srcfile}:'foo::overloadfnarg(int)'" "int foo::overloadfnarg.*}"

# Now some tests to see how overloading and namespaces interact.

gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 1"
gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 1"
gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2"

# Static methods.
gdb_test "print K::staticoverload ()" " = 1"
gdb_test "print K::staticoverload (2)" " = 2"
gdb_test "print K::staticoverload (2, 3)" " = 5"

# Namespace-qualified functions.
gdb_test "print N::nsoverload ()" " = 1"
gdb_test "print N::nsoverload (2)" " = 2"
gdb_test "print N::nsoverload (2, 3)" " = 5"

if ![runto 'XXX::marker2'] then {
    perror "couldn't run to XXX::marker2"
    continue
}

gdb_test "print overloadNamespace(1)" ".\[0-9\]* = 3" "print overloadNamespace(1) in XXX"
gdb_test "print overloadNamespace('a')" ".\[0-9\]* = 3" "print overloadNamespace('a') in XXX"
gdb_test "print overloadNamespace(dummyInstance)" ".\[0-9\]* = 2" "print overloadNamespace(dummyInstance) in XXX"

# One last mysterious test.
# I wonder what this is for?

gdb_test "print intToChar(1)" "\\$\[0-9\]+ = 297"

# Test expression evaluation with overloaded methods
gdb_test "print foo::overload1arg" \
    "non-unique member `overload1arg' requires type instantiation" \
    "print foo::overload1arg"

gdb_test "print foo::overload1arg(char***)" \
    "no member function matches that type instantiation" \
    "print foo::overload1arg(char***)"

gdb_test "print foo::overload1arg(void)" \
    "\\$$decimal = {int \\(foo \\*( const|)\\)} $hex <foo::overload1arg\\(\\)>" \
    "print foo::overload1arg(void)"

foreach t [list char "signed char" "unsigned char" "short" \
	       "unsigned short" int "unsigned int" long "unsigned long" \
	       float double] {
    gdb_test "print foo::overload1arg($t)" \
	"\\$$decimal = {int \\(foo \\*( const|), $t\\)} $hex <foo::overload1arg\\($t\\)>" \
	"print foo::overload1arg($t)"
}