]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.cp/cplusfuncs.exp
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.cp / cplusfuncs.exp
1 # Copyright 1992, 1997, 1999, 2001, 2002, 2003, 2004, 2007
2 # Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 # This file was written by Fred Fish. (fnf@cygnus.com)
19 # Adapted for g++ 3.0 ABI by Michael Chastain. (chastain@redhat.com)
20
21 if $tracelevel then {
22 strace $tracelevel
23 }
24
25 if { [skip_cplus_tests] } { continue }
26
27 set testfile "cplusfuncs"
28 set srcfile ${testfile}.cc
29 set binfile ${objdir}/${subdir}/${testfile}
30
31 if { [get_compiler_info $binfile "c++"] } {
32 return -1
33 }
34
35 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
36 untested cplusfuncs.exp
37 return -1
38 }
39
40 #
41 # g++ changed its ABI between 2.95 and 3.0. gdb has two demanglers
42 # for the two different styles. The two demanglers have some subtle
43 # discrepancies in their output.
44 #
45 # old demangler new demangler
46 # --- --------- --- ---------
47 # "operator, " "operator,"
48 # "char *" "char*"
49 # "int *" "int*"
50 # "long *" "long*"
51 # "void *" "void*"
52 # "foo &" "foo&"
53 # "unsigned int" "unsigned"
54 # "void" ""
55 #
56 # I probe for the forms in use.
57 # The defaults are for the v3 demangler (as of 2001-02-13).
58 #
59
60 set dm_operator_comma ","
61 set dm_type_char_star "char*"
62 set dm_type_char_star_quoted "char\\*"
63 set dm_type_foo_ref "foo&"
64 set dm_type_int_star "int*"
65 set dm_type_long_star "long*"
66 set dm_type_unsigned_int "unsigned"
67 set dm_type_void ""
68 set dm_type_void_star "void*"
69
70 proc probe_demangler { } {
71 global gdb_prompt
72 global dm_operator_comma
73 global dm_type_char_star
74 global dm_type_char_star_quoted
75 global dm_type_foo_ref
76 global dm_type_int_star
77 global dm_type_long_star
78 global dm_type_unsigned_int
79 global dm_type_void
80 global dm_type_void_star
81
82 send_gdb "print &'foo::operator,(foo&)'\n"
83 gdb_expect {
84 -re ".*foo::operator, \\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
85 # v2 demangler
86 set dm_operator_comma ", "
87 pass "detect dm_operator_comma"
88 }
89 -re ".*foo::operator,\\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
90 # v3 demangler
91 pass "detect dm_operator_comma"
92 }
93 -re ".*$gdb_prompt $" {
94 fail "detect dm_operator_comma"
95 }
96 timeout {
97 fail "detect dm_operator_comma"
98 }
99 }
100
101 send_gdb "print &'dm_type_char_star'\n"
102 gdb_expect {
103 -re ".*dm_type_char_star\\(char \\*\\).*\r\n$gdb_prompt $" {
104 # v2 demangler
105 set dm_type_char_star "char *"
106 set dm_type_char_star_quoted "char \\*"
107 pass "detect dm_type_char_star"
108 }
109 -re ".*dm_type_char_star\\(char\\*\\).*\r\n$gdb_prompt $" {
110 # v3 demangler
111 pass "detect dm_type_char_star"
112 }
113 -re ".*$gdb_prompt $" {
114 fail "detect dm_type_char_star"
115 }
116 timeout {
117 fail "detect dm_type_char_star (timeout)"
118 }
119 }
120
121 send_gdb "print &'dm_type_foo_ref'\n"
122 gdb_expect {
123 -re ".*dm_type_foo_ref\\(foo &\\).*\r\n$gdb_prompt $" {
124 # v2 demangler
125 set dm_type_foo_ref "foo &"
126 pass "detect dm_type_foo_ref"
127 }
128 -re ".*dm_type_foo_ref\\(foo&\\).*\r\n$gdb_prompt $" {
129 # v3 demangler
130 pass "detect dm_type_foo_ref"
131 }
132 -re ".*$gdb_prompt $" {
133 fail "detect dm_type_foo_ref"
134 }
135 timeout {
136 fail "detect dm_type_foo_ref (timeout)"
137 }
138 }
139
140 send_gdb "print &'dm_type_int_star'\n"
141 gdb_expect {
142 -re ".*dm_type_int_star\\(int \\*\\).*\r\n$gdb_prompt $" {
143 # v2 demangler
144 set dm_type_int_star "int *"
145 pass "detect dm_type_int_star"
146 }
147 -re ".*dm_type_int_star\\(int\\*\\).*\r\n$gdb_prompt $" {
148 # v3 demangler
149 pass "detect dm_type_int_star"
150 }
151 -re ".*$gdb_prompt $" {
152 fail "detect dm_type_int_star"
153 }
154 timeout {
155 fail "detect dm_type_int_star (timeout)"
156 }
157 }
158
159 send_gdb "print &'dm_type_long_star'\n"
160 gdb_expect {
161 -re ".*dm_type_long_star\\(long \\*\\).*\r\n$gdb_prompt $" {
162 # v2 demangler
163 set dm_type_long_star "long *"
164 pass "detect dm_type_long_star"
165 }
166 -re ".*dm_type_long_star\\(long\\*\\).*\r\n$gdb_prompt $" {
167 # v3 demangler
168 pass "detect dm_type_long_star"
169 }
170 -re ".*$gdb_prompt $" {
171 fail "detect dm_type_long_star"
172 }
173 timeout {
174 fail "detect dm_type_long_star (timeout)"
175 }
176 }
177
178 send_gdb "print &'dm_type_unsigned_int'\n"
179 gdb_expect {
180 -re ".*dm_type_unsigned_int\\(unsigned int\\).*\r\n$gdb_prompt $" {
181 # v2 demangler
182 set dm_type_unsigned_int "unsigned int"
183 pass "detect dm_type_unsigned_int"
184 }
185 -re ".*dm_type_unsigned_int\\(unsigned\\).*\r\n$gdb_prompt $" {
186 # v3 demangler
187 pass "detect dm_type_unsigned_int"
188 }
189 -re ".*$gdb_prompt $" {
190 fail "detect dm_type_unsigned_int"
191 }
192 timeout {
193 fail "detect dm_unsigned int (timeout)"
194 }
195 }
196
197 send_gdb "print &'dm_type_void'\n"
198 gdb_expect {
199 -re ".*dm_type_void\\(void\\).*\r\n$gdb_prompt $" {
200 # v2 demangler
201 set dm_type_void "void"
202 pass "detect dm_type_void"
203 }
204 -re ".*dm_type_void\\(\\).*\r\n$gdb_prompt $" {
205 # v3 demangler
206 pass "detect dm_type_void"
207 }
208 -re ".*$gdb_prompt $" {
209 fail "detect dm_type_void"
210 }
211 timeout {
212 fail "detect dm_type_void (timeout)"
213 }
214 }
215
216 send_gdb "print &'dm_type_void_star'\n"
217 gdb_expect {
218 -re ".*dm_type_void_star\\(void \\*\\).*\r\n$gdb_prompt $" {
219 # v2 demangler
220 set dm_type_void_star "void *"
221 pass "detect dm_type_void_star"
222 }
223 -re ".*dm_type_void_star\\(void\\*\\).*\r\n$gdb_prompt $" {
224 # v3 demangler
225 pass "detect dm_type_void_star"
226 }
227 -re ".*$gdb_prompt $" {
228 fail "detect dm_type_void_star"
229 }
230 timeout {
231 fail "detect dm_type_void_star (timeout)"
232 }
233 }
234 }
235
236 #
237 # Lookup a specific C++ function and print the demangled type.
238 # This form accepts the demangled type as a regexp.
239 #
240
241 proc info_func_regexp { name demangled } {
242 global gdb_prompt
243
244 send_gdb "info function $name\n"
245 gdb_expect {
246 -re ".*File .*:\r\n(class |)$demangled\r\n.*$gdb_prompt $" {
247 pass "info function for \"$name\""
248 }
249 -re ".*$gdb_prompt $" {
250 fail "info function for \"$name\""
251 }
252 timeout {
253 fail "info function for \"$name\" (timeout)"
254 }
255 }
256 }
257
258 #
259 # Lookup a specific C++ function and print the demangled type.
260 # This form accepts the demangled type as a literal string.
261 #
262
263 proc info_func { name demangled } {
264 info_func_regexp "$name" [string_to_regexp "$demangled"]
265 }
266
267 #
268 # Print the address of a function.
269 # This checks that I can lookup a fully qualified C++ function.
270 # This also checks the argument types on the return string.
271
272 # Note: carlton/2003-01-16: If you modify this, make a corresponding
273 # modification to print_addr_2_kfail.
274
275 proc print_addr_2 { name good } {
276 global gdb_prompt
277 global hex
278
279 set good_pattern [string_to_regexp $good]
280
281 send_gdb "print &'$name'\n"
282 gdb_expect {
283 -re ".* = .* $hex <$good_pattern>\r\n$gdb_prompt $" {
284 pass "print &'$name'"
285 }
286 -re ".*$gdb_prompt $" {
287 fail "print &'$name'"
288 }
289 timeout {
290 fail "print &'$name' (timeout)"
291 }
292 }
293 }
294
295 # NOTE: carlton/2003-01-16: hairyfunc5-6 fail on GCC 3.x (for at least
296 # x=1 and x=2.1). So I'm modifying print_addr_2 to accept a failure
297 # condition. FIXME: It would be nice if the failure condition were
298 # conditional on the compiler version, but I'm not sufficiently
299 # motivated. I did hardwire in the versions of char * and int *,
300 # which will give some compiler-specificity to the failure.
301
302 proc print_addr_2_kfail { name good bad bugid } {
303 global gdb_prompt
304 global hex
305
306 set good_pattern [string_to_regexp $good]
307 set bad_pattern [string_to_regexp $bad]
308
309 send_gdb "print &'$name'\n"
310 gdb_expect {
311 -re ".* = .* $hex <$good_pattern>\r\n$gdb_prompt $" {
312 pass "print &'$name'"
313 }
314 -re ".* = .* $hex <$bad_pattern>\r\n$gdb_prompt $" {
315 kfail $bugid "print &'$name'"
316 }
317 -re ".*$gdb_prompt $" {
318 fail "print &'$name'"
319 }
320 timeout {
321 fail "print &'$name' (timeout)"
322 }
323 }
324 }
325
326 #
327 # Simple interfaces to print_addr_2.
328 #
329
330 proc print_addr { name } {
331 print_addr_2 "$name" "$name"
332 }
333
334 #
335 # Test name demangling for operators.
336 #
337 # The '(' at the end of each regex input pattern is so that we match only
338 # the one we are looking for. I.E. "operator&" would match both
339 # "operator&(foo &)" and "operator&&(foo &)".
340 #
341 # gdb-gnats bug gdb/18:
342 # "gdb can't parse "info func operator*" or "info func operator\*".
343 # The star in "operator*" is interpreted as a regexp, but the "\*"
344 # in "operator\*" is not a legal operator.
345 #
346
347 proc test_lookup_operator_functions {} {
348 global dm_operator_comma
349 global dm_type_char_star
350 global dm_type_char_star_quoted
351 global dm_type_foo_ref
352 global dm_type_void
353 global dm_type_void_star
354
355 # operator* requires quoting so that GDB does not treat it as a regexp.
356 info_func "operator\\*(" "void foo::operator*($dm_type_foo_ref);"
357 info_func "operator%(" "void foo::operator%($dm_type_foo_ref);"
358 info_func "operator-(" "void foo::operator-($dm_type_foo_ref);"
359 info_func "operator>>(" "void foo::operator>>($dm_type_foo_ref);"
360 info_func "operator!=(" "void foo::operator!=($dm_type_foo_ref);"
361 info_func "operator>(" "void foo::operator>($dm_type_foo_ref);"
362 info_func "operator>=(" "void foo::operator>=($dm_type_foo_ref);"
363 info_func "operator|(" "void foo::operator|($dm_type_foo_ref);"
364 info_func "operator&&(" "void foo::operator&&($dm_type_foo_ref);"
365 info_func "operator!(" "void foo::operator!($dm_type_void);"
366 info_func "operator++(" "void foo::operator++(int);"
367 info_func "operator=(" "void foo::operator=($dm_type_foo_ref);"
368 info_func "operator+=(" "void foo::operator+=($dm_type_foo_ref);"
369 # operator*= requires quoting so that GDB does not treat it as a regexp.
370 info_func "operator\\*=(" "void foo::operator*=($dm_type_foo_ref);"
371 info_func "operator%=(" "void foo::operator%=($dm_type_foo_ref);"
372 info_func "operator>>=(" "void foo::operator>>=($dm_type_foo_ref);"
373 info_func "operator|=(" "void foo::operator|=($dm_type_foo_ref);"
374 info_func "operator$dm_operator_comma\(" \
375 "void foo::operator$dm_operator_comma\($dm_type_foo_ref);"
376 info_func "operator/(" "void foo::operator/($dm_type_foo_ref);"
377 info_func "operator+(" "void foo::operator+($dm_type_foo_ref);"
378 info_func "operator<<(" "void foo::operator<<($dm_type_foo_ref);"
379 info_func "operator==(" "void foo::operator==($dm_type_foo_ref);"
380 info_func "operator<(" "void foo::operator<($dm_type_foo_ref);"
381 info_func "operator<=(" "void foo::operator<=($dm_type_foo_ref);"
382 info_func "operator&(" "void foo::operator&($dm_type_foo_ref);"
383 info_func "operator^(" "void foo::operator^($dm_type_foo_ref);"
384 info_func "operator||(" "void foo::operator||($dm_type_foo_ref);"
385 info_func "operator~(" "void foo::operator~($dm_type_void);"
386 info_func "operator--(" "void foo::operator--(int);"
387 info_func "operator->(" "foo *foo::operator->($dm_type_void);"
388 info_func "operator-=(" "void foo::operator-=($dm_type_foo_ref);"
389 info_func "operator/=(" "void foo::operator/=($dm_type_foo_ref);"
390 info_func "operator<<=(" "void foo::operator<<=($dm_type_foo_ref);"
391 info_func "operator&=(" "void foo::operator&=($dm_type_foo_ref);"
392 info_func "operator^=(" "void foo::operator^=($dm_type_foo_ref);"
393 # operator->* requires quoting so that GDB does not treat it as a regexp.
394 info_func "operator->\\*(" "void foo::operator->*($dm_type_foo_ref);"
395
396 # operator[] needs double backslashes, so that a single backslash
397 # will be sent to GDB, preventing the square brackets from being
398 # evaluated as a regular expression.
399 info_func "operator\\\[\\\](" "void foo::operator\[\]($dm_type_foo_ref);"
400
401 # These are gnarly because they might end with 'static'.
402 set dm_type_void_star_regexp [string_to_regexp $dm_type_void_star]
403 info_func_regexp "operator new(" "void \\*foo::operator new\\(.*\\)(| static);"
404 info_func_regexp "operator delete(" "void foo::operator delete\\($dm_type_void_star_regexp\\)(| static);"
405
406 info_func "operator int(" "int foo::operator int($dm_type_void);"
407 info_func "operator()(" "void foo::operator()($dm_type_foo_ref);"
408 info_func "operator $dm_type_char_star_quoted\(" \
409 "char *foo::operator $dm_type_char_star\($dm_type_void);"
410
411 }
412
413
414 proc test_paddr_operator_functions {} {
415 global hex
416 global hp_aCC_compiler
417 global dm_operator_comma
418 global dm_type_char_star
419 global dm_type_foo_ref
420 global dm_type_long_star
421 global dm_type_unsigned_int
422 global dm_type_void
423 global dm_type_void_star
424
425 print_addr "foo::operator*($dm_type_foo_ref)"
426 print_addr "foo::operator%($dm_type_foo_ref)"
427 print_addr "foo::operator-($dm_type_foo_ref)"
428 print_addr "foo::operator>>($dm_type_foo_ref)"
429 print_addr "foo::operator!=($dm_type_foo_ref)"
430 print_addr "foo::operator>($dm_type_foo_ref)"
431 print_addr "foo::operator>=($dm_type_foo_ref)"
432 print_addr "foo::operator|($dm_type_foo_ref)"
433 print_addr "foo::operator&&($dm_type_foo_ref)"
434 print_addr "foo::operator!($dm_type_void)"
435 print_addr "foo::operator++(int)"
436 print_addr "foo::operator=($dm_type_foo_ref)"
437 print_addr "foo::operator+=($dm_type_foo_ref)"
438 print_addr "foo::operator*=($dm_type_foo_ref)"
439 print_addr "foo::operator%=($dm_type_foo_ref)"
440 print_addr "foo::operator>>=($dm_type_foo_ref)"
441 print_addr "foo::operator|=($dm_type_foo_ref)"
442 print_addr "foo::operator$dm_operator_comma\($dm_type_foo_ref)"
443 print_addr "foo::operator/($dm_type_foo_ref)"
444 print_addr "foo::operator+($dm_type_foo_ref)"
445 print_addr "foo::operator<<($dm_type_foo_ref)"
446 print_addr "foo::operator==($dm_type_foo_ref)"
447 print_addr "foo::operator<($dm_type_foo_ref)"
448 print_addr "foo::operator<=($dm_type_foo_ref)"
449 print_addr "foo::operator&($dm_type_foo_ref)"
450 print_addr "foo::operator^($dm_type_foo_ref)"
451 print_addr "foo::operator||($dm_type_foo_ref)"
452 print_addr "foo::operator~($dm_type_void)"
453 print_addr "foo::operator--(int)"
454 print_addr "foo::operator->($dm_type_void)"
455 print_addr "foo::operator-=($dm_type_foo_ref)"
456 print_addr "foo::operator/=($dm_type_foo_ref)"
457 print_addr "foo::operator<<=($dm_type_foo_ref)"
458 print_addr "foo::operator&=($dm_type_foo_ref)"
459 print_addr "foo::operator^=($dm_type_foo_ref)"
460 print_addr "foo::operator->*($dm_type_foo_ref)"
461 print_addr "foo::operator\[\]($dm_type_foo_ref)"
462 print_addr "foo::operator()($dm_type_foo_ref)"
463
464 gdb_test "print &'foo::operator new'" \
465 " = .* $hex <foo::operator new\\(.*\\)(| static)>"
466 if { !$hp_aCC_compiler } {
467 print_addr "foo::operator delete($dm_type_void_star)"
468 } else {
469 gdb_test "print &'foo::operator delete($dm_type_void_star) static'" \
470 " = .*(0x\[0-9a-f\]+|) <foo::operator delete.*>"
471 }
472
473 print_addr "foo::operator int($dm_type_void)"
474 print_addr "foo::operator $dm_type_char_star\($dm_type_void)"
475 }
476
477 #
478 # Test overloaded functions (1 arg).
479 #
480
481 proc test_paddr_overloaded_functions {} {
482 global dm_type_unsigned_int
483 global dm_type_void
484
485 print_addr "overload1arg($dm_type_void)"
486 print_addr "overload1arg(char)"
487 print_addr "overload1arg(signed char)"
488 print_addr "overload1arg(unsigned char)"
489 print_addr "overload1arg(short)"
490 print_addr "overload1arg(unsigned short)"
491 print_addr "overload1arg(int)"
492 print_addr "overload1arg($dm_type_unsigned_int)"
493 print_addr "overload1arg(long)"
494 print_addr "overload1arg(unsigned long)"
495 print_addr "overload1arg(float)"
496 print_addr "overload1arg(double)"
497
498 print_addr "overloadargs(int)"
499 print_addr "overloadargs(int, int)"
500 print_addr "overloadargs(int, int, int)"
501 print_addr "overloadargs(int, int, int, int)"
502 print_addr "overloadargs(int, int, int, int, int)"
503 print_addr "overloadargs(int, int, int, int, int, int)"
504 print_addr "overloadargs(int, int, int, int, int, int, int)"
505 print_addr "overloadargs(int, int, int, int, int, int, int, int)"
506 print_addr "overloadargs(int, int, int, int, int, int, int, int, int)"
507 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int)"
508 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int, int)"
509 }
510
511 proc test_paddr_hairy_functions {} {
512 global gdb_prompt
513 global hex
514 global dm_type_char_star
515 global dm_type_int_star
516 global dm_type_long_star
517
518 print_addr_2 "hairyfunc1" "hairyfunc1(int)"
519 print_addr_2 "hairyfunc2" "hairyfunc2(int (*)($dm_type_char_star))"
520 print_addr_2 "hairyfunc3" "hairyfunc3(int (*)(short (*)($dm_type_long_star)))"
521 print_addr_2 "hairyfunc4" "hairyfunc4(int (*)(short (*)($dm_type_char_star)))"
522
523 # gdb-gnats bug gdb/19:
524 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
525 print_addr_2_kfail "hairyfunc5" "hairyfunc5(int (*(*)($dm_type_char_star))(long))" "hairyfunc5(int (*)(long) (*)(char*))" "gdb/19"
526 print_addr_2_kfail "hairyfunc6" "hairyfunc6(int (*(*)($dm_type_int_star))(long))" "hairyfunc6(int (*)(long) (*)(int*))" "gdb/19"
527 print_addr_2_kfail "hairyfunc7" "hairyfunc7(int (*(*)(int (*)($dm_type_char_star)))(long))" "hairyfunc7(int (*)(long) (*)(int (*)(char*)))" "gdb/19"
528 }
529
530 proc do_tests {} {
531 global prms_id
532 global bug_id
533 global subdir
534 global objdir
535 global srcdir
536 global binfile
537 global gdb_prompt
538
539 set prms_id 0
540 set bug_id 0
541
542 # Start with a fresh gdb.
543
544 gdb_exit
545 gdb_start
546 gdb_reinitialize_dir $srcdir/$subdir
547 gdb_load $binfile
548
549 send_gdb "set language c++\n"
550 gdb_expect -re "$gdb_prompt $"
551 send_gdb "set width 0\n"
552 gdb_expect -re "$gdb_prompt $"
553
554 runto_main
555
556 probe_demangler
557 test_paddr_overloaded_functions
558 test_paddr_operator_functions
559 test_paddr_hairy_functions
560 test_lookup_operator_functions
561 }
562
563 do_tests