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