gdb
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.cp / cplusfuncs.exp
1 # Copyright 1992, 1997, 1999, 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010
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 3 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, see <http://www.gnu.org/licenses/>.
16
17 # This file was written by Fred Fish. (fnf@cygnus.com)
18 # Adapted for g++ 3.0 ABI by Michael Chastain. (chastain@redhat.com)
19
20 if $tracelevel then {
21 strace $tracelevel
22 }
23
24 if { [skip_cplus_tests] } { continue }
25
26 set testfile "cplusfuncs"
27 set srcfile ${testfile}.cc
28 set binfile ${objdir}/${subdir}/${testfile}
29
30 if { [get_compiler_info $binfile "c++"] } {
31 return -1
32 }
33
34 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
35 untested cplusfuncs.exp
36 return -1
37 }
38
39 #
40 # g++ changed its ABI between 2.95 and 3.0. gdb has two demanglers
41 # for the two different styles. The two demanglers have some subtle
42 # discrepancies in their output.
43 #
44 # old demangler new demangler
45 # --- --------- --- ---------
46 # "operator, " "operator,"
47 # "char *" "char*"
48 # "int *" "int*"
49 # "long *" "long*"
50 # "void *" "void*"
51 # "foo &" "foo&"
52 # "unsigned int" "unsigned"
53 # "void" ""
54 #
55 # I probe for the forms in use.
56 # The defaults are for the v3 demangler (as of 2001-02-13).
57 #
58
59 set dm_operator_comma ","
60 set dm_type_char_star "char*"
61 set dm_type_char_star_quoted "char\\*"
62 set dm_type_foo_ref "foo&"
63 set dm_type_int_star "int*"
64 set dm_type_long_star "long*"
65 set dm_type_unsigned_int "unsigned"
66 set dm_type_void "void"
67 set dm_type_void_star "void*"
68
69 proc probe_demangler { } {
70 global gdb_prompt
71 global dm_operator_comma
72 global dm_type_char_star
73 global dm_type_char_star_quoted
74 global dm_type_foo_ref
75 global dm_type_int_star
76 global dm_type_long_star
77 global dm_type_unsigned_int
78 global dm_type_void
79 global dm_type_void_star
80
81 send_gdb "print &foo::operator,(foo&)\n"
82 gdb_expect {
83 -re ".*foo::operator, \\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
84 # v2 demangler
85 set dm_operator_comma ", "
86 pass "detect dm_operator_comma"
87 }
88 -re ".*foo::operator,\\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
89 # v3 demangler
90 pass "detect dm_operator_comma"
91 }
92 -re ".*$gdb_prompt $" {
93 fail "detect dm_operator_comma"
94 }
95 timeout {
96 fail "detect dm_operator_comma"
97 }
98 }
99
100 send_gdb "print &dm_type_char_star\n"
101 gdb_expect {
102 -re ".*dm_type_char_star\\(char \\*\\).*\r\n$gdb_prompt $" {
103 # v2 demangler
104 set dm_type_char_star "char *"
105 set dm_type_char_star_quoted "char \\*"
106 pass "detect dm_type_char_star"
107 }
108 -re ".*dm_type_char_star\\(char\\*\\).*\r\n$gdb_prompt $" {
109 # v3 demangler
110 pass "detect dm_type_char_star"
111 }
112 -re ".*$gdb_prompt $" {
113 fail "detect dm_type_char_star"
114 }
115 timeout {
116 fail "detect dm_type_char_star (timeout)"
117 }
118 }
119
120 send_gdb "print &dm_type_foo_ref\n"
121 gdb_expect {
122 -re ".*dm_type_foo_ref\\(foo &\\).*\r\n$gdb_prompt $" {
123 # v2 demangler
124 set dm_type_foo_ref "foo &"
125 pass "detect dm_type_foo_ref"
126 }
127 -re ".*dm_type_foo_ref\\(foo&\\).*\r\n$gdb_prompt $" {
128 # v3 demangler
129 pass "detect dm_type_foo_ref"
130 }
131 -re ".*$gdb_prompt $" {
132 fail "detect dm_type_foo_ref"
133 }
134 timeout {
135 fail "detect dm_type_foo_ref (timeout)"
136 }
137 }
138
139 send_gdb "print &dm_type_int_star\n"
140 gdb_expect {
141 -re ".*dm_type_int_star\\(int \\*\\).*\r\n$gdb_prompt $" {
142 # v2 demangler
143 set dm_type_int_star "int *"
144 pass "detect dm_type_int_star"
145 }
146 -re ".*dm_type_int_star\\(int\\*\\).*\r\n$gdb_prompt $" {
147 # v3 demangler
148 pass "detect dm_type_int_star"
149 }
150 -re ".*$gdb_prompt $" {
151 fail "detect dm_type_int_star"
152 }
153 timeout {
154 fail "detect dm_type_int_star (timeout)"
155 }
156 }
157
158 send_gdb "print &dm_type_long_star\n"
159 gdb_expect {
160 -re ".*dm_type_long_star\\(long \\*\\).*\r\n$gdb_prompt $" {
161 # v2 demangler
162 set dm_type_long_star "long *"
163 pass "detect dm_type_long_star"
164 }
165 -re ".*dm_type_long_star\\(long\\*\\).*\r\n$gdb_prompt $" {
166 # v3 demangler
167 pass "detect dm_type_long_star"
168 }
169 -re ".*$gdb_prompt $" {
170 fail "detect dm_type_long_star"
171 }
172 timeout {
173 fail "detect dm_type_long_star (timeout)"
174 }
175 }
176
177 send_gdb "print &dm_type_unsigned_int\n"
178 gdb_expect {
179 -re ".*dm_type_unsigned_int\\(unsigned int\\).*\r\n$gdb_prompt $" {
180 # v2 demangler
181 set dm_type_unsigned_int "unsigned int"
182 pass "detect dm_type_unsigned_int"
183 }
184 -re ".*dm_type_unsigned_int\\(unsigned\\).*\r\n$gdb_prompt $" {
185 # v3 demangler
186 pass "detect dm_type_unsigned_int"
187 }
188 -re ".*$gdb_prompt $" {
189 fail "detect dm_type_unsigned_int"
190 }
191 timeout {
192 fail "detect dm_unsigned int (timeout)"
193 }
194 }
195
196 send_gdb "print &dm_type_void\n"
197 gdb_expect {
198 -re ".*dm_type_void\\(void\\).*\r\n$gdb_prompt $" {
199 # v2 demangler
200 set dm_type_void "void"
201 pass "detect dm_type_void"
202 }
203 -re ".*dm_type_void\\(\\).*\r\n$gdb_prompt $" {
204 # v3 demangler
205 pass "detect dm_type_void"
206 }
207 -re ".*$gdb_prompt $" {
208 fail "detect dm_type_void"
209 }
210 timeout {
211 fail "detect dm_type_void (timeout)"
212 }
213 }
214
215 send_gdb "print &dm_type_void_star\n"
216 gdb_expect {
217 -re ".*dm_type_void_star\\(void \\*\\).*\r\n$gdb_prompt $" {
218 # v2 demangler
219 set dm_type_void_star "void *"
220 pass "detect dm_type_void_star"
221 }
222 -re ".*dm_type_void_star\\(void\\*\\).*\r\n$gdb_prompt $" {
223 # v3 demangler
224 pass "detect dm_type_void_star"
225 }
226 -re ".*$gdb_prompt $" {
227 fail "detect dm_type_void_star"
228 }
229 timeout {
230 fail "detect dm_type_void_star (timeout)"
231 }
232 }
233 }
234
235 #
236 # Lookup a specific C++ function and print the demangled type.
237 # This form accepts the demangled type as a regexp.
238 #
239
240 proc info_func_regexp { name demangled } {
241 global gdb_prompt
242
243 send_gdb "info function $name\n"
244 regsub {\\\(void\\\)} $demangled {\(\)} demangled
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 regsub {\(void\)} $name {()} expected
332 if {[string first "::" $name] == -1} {
333 # C function -- must be qutoed
334 set name "'$name'"
335 }
336 print_addr_2 "$name" $expected
337 }
338
339 #
340 # Test name demangling for operators.
341 #
342 # The '(' at the end of each regex input pattern is so that we match only
343 # the one we are looking for. I.E. "operator&" would match both
344 # "operator&(foo &)" and "operator&&(foo &)".
345 #
346 # gdb-gnats bug gdb/18:
347 # "gdb can't parse "info func operator*" or "info func operator\*".
348 # The star in "operator*" is interpreted as a regexp, but the "\*"
349 # in "operator\*" is not a legal operator.
350 #
351
352 proc test_lookup_operator_functions {} {
353 global dm_operator_comma
354 global dm_type_char_star
355 global dm_type_char_star_quoted
356 global dm_type_foo_ref
357 global dm_type_void
358 global dm_type_void_star
359
360 # operator* requires quoting so that GDB does not treat it as a regexp.
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_foo_ref);"
367 info_func "operator>=(" "void foo::operator>=($dm_type_foo_ref);"
368 info_func "operator|(" "void foo::operator|($dm_type_foo_ref);"
369 info_func "operator&&(" "void foo::operator&&($dm_type_foo_ref);"
370 info_func "operator!(" "void foo::operator!($dm_type_void);"
371 info_func "operator++(" "void foo::operator++(int);"
372 info_func "operator=(" "void foo::operator=($dm_type_foo_ref);"
373 info_func "operator+=(" "void foo::operator+=($dm_type_foo_ref);"
374 # operator*= requires quoting so that GDB does not treat it as a regexp.
375 info_func "operator\\*=(" "void foo::operator*=($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$dm_operator_comma\(" \
380 "void foo::operator$dm_operator_comma\($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_foo_ref);"
387 info_func "operator&(" "void foo::operator&($dm_type_foo_ref);"
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_void);"
391 info_func "operator--(" "void foo::operator--(int);"
392 info_func "operator->(" "foo *foo::operator->($dm_type_void);"
393 info_func "operator-=(" "void foo::operator-=($dm_type_foo_ref);"
394 info_func "operator/=(" "void foo::operator/=($dm_type_foo_ref);"
395 info_func "operator<<=(" "void foo::operator<<=($dm_type_foo_ref);"
396 info_func "operator&=(" "void foo::operator&=($dm_type_foo_ref);"
397 info_func "operator^=(" "void foo::operator^=($dm_type_foo_ref);"
398 # operator->* requires quoting so that GDB does not treat it as a regexp.
399 info_func "operator->\\*(" "void foo::operator->*($dm_type_foo_ref);"
400
401 # operator[] needs double backslashes, so that a single backslash
402 # will be sent to GDB, preventing the square brackets from being
403 # evaluated as a regular expression.
404 info_func "operator\\\[\\\](" "void foo::operator\[\]($dm_type_foo_ref);"
405
406 # These are gnarly because they might end with 'static'.
407 set dm_type_void_star_regexp [string_to_regexp $dm_type_void_star]
408 info_func_regexp "operator new(" "void \\*foo::operator new\\(.*\\)(| static);"
409 info_func_regexp "operator delete(" "void foo::operator delete\\($dm_type_void_star_regexp\\)(| static);"
410
411 info_func "operator int(" "int foo::operator int($dm_type_void);"
412 info_func "operator()(" "void foo::operator()($dm_type_foo_ref);"
413 info_func "operator $dm_type_char_star_quoted\(" \
414 "char *foo::operator $dm_type_char_star\($dm_type_void);"
415
416 }
417
418
419 proc test_paddr_operator_functions {} {
420 global hex
421 global hp_aCC_compiler
422 global dm_operator_comma
423 global dm_type_char_star
424 global dm_type_foo_ref
425 global dm_type_long_star
426 global dm_type_unsigned_int
427 global dm_type_void
428 global dm_type_void_star
429
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_foo_ref)"
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_void)"
440 print_addr "foo::operator++(int)"
441 print_addr "foo::operator=($dm_type_foo_ref)"
442 print_addr "foo::operator+=($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_operator_comma\($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_foo_ref)"
454 print_addr "foo::operator&($dm_type_foo_ref)"
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_void)"
458 print_addr "foo::operator--(int)"
459 print_addr "foo::operator->($dm_type_void)"
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 print_addr "foo::operator^=($dm_type_foo_ref)"
465 print_addr "foo::operator->*($dm_type_foo_ref)"
466 print_addr "foo::operator\[\]($dm_type_foo_ref)"
467 print_addr "foo::operator()($dm_type_foo_ref)"
468
469 gdb_test "print &foo::operator new" \
470 " = .* $hex <foo::operator new\\(.*\\)(| static)>"
471 gdb_test "print &foo::operator new\[\]" \
472 " = .* $hex <foo::operator new\\\[\\\]\\(.*\\)(| static)>"
473 if { !$hp_aCC_compiler } {
474 print_addr "foo::operator delete($dm_type_void_star)"
475 print_addr "foo::operator delete[]($dm_type_void_star)"
476 } else {
477 gdb_test "print &'foo::operator delete($dm_type_void_star) static'" \
478 " = .*(0x\[0-9a-f\]+|) <foo::operator delete.*>"
479 }
480
481 print_addr "foo::operator int($dm_type_void)"
482 print_addr "foo::operator $dm_type_char_star\($dm_type_void)"
483 }
484
485 #
486 # Test overloaded functions (1 arg).
487 #
488
489 proc test_paddr_overloaded_functions {} {
490 global dm_type_unsigned_int
491 global dm_type_void
492
493 print_addr "overload1arg($dm_type_void)"
494 print_addr "overload1arg(char)"
495 print_addr "overload1arg(signed char)"
496 print_addr "overload1arg(unsigned char)"
497 print_addr "overload1arg(short)"
498 print_addr "overload1arg(unsigned short)"
499 print_addr "overload1arg(int)"
500 print_addr "overload1arg($dm_type_unsigned_int)"
501 print_addr "overload1arg(long)"
502 print_addr "overload1arg(unsigned long)"
503 print_addr "overload1arg(float)"
504 print_addr "overload1arg(double)"
505
506 print_addr "overloadargs(int)"
507 print_addr "overloadargs(int, int)"
508 print_addr "overloadargs(int, int, int)"
509 print_addr "overloadargs(int, int, int, int)"
510 print_addr "overloadargs(int, int, int, int, int)"
511 print_addr "overloadargs(int, int, int, int, int, int)"
512 print_addr "overloadargs(int, int, int, int, int, int, int)"
513 print_addr "overloadargs(int, int, int, int, int, int, int, int)"
514 print_addr "overloadargs(int, int, int, int, int, int, int, int, int)"
515 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int)"
516 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int, int)"
517 }
518
519 proc test_paddr_hairy_functions {} {
520 global gdb_prompt
521 global hex
522 global dm_type_char_star
523 global dm_type_int_star
524 global dm_type_long_star
525
526 print_addr_2 "hairyfunc1" "hairyfunc1(int)"
527 print_addr_2 "hairyfunc2" "hairyfunc2(int (*)($dm_type_char_star))"
528 print_addr_2 "hairyfunc3" "hairyfunc3(int (*)(short (*)($dm_type_long_star)))"
529 print_addr_2 "hairyfunc4" "hairyfunc4(int (*)(short (*)($dm_type_char_star)))"
530
531 # gdb-gnats bug gdb/19:
532 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
533 print_addr_2_kfail "hairyfunc5" "hairyfunc5(int (*(*)($dm_type_char_star))(long))" "hairyfunc5(int (*)(long) (*)(char*))" "gdb/19"
534 print_addr_2_kfail "hairyfunc6" "hairyfunc6(int (*(*)($dm_type_int_star))(long))" "hairyfunc6(int (*)(long) (*)(int*))" "gdb/19"
535 print_addr_2_kfail "hairyfunc7" "hairyfunc7(int (*(*)(int (*)($dm_type_char_star)))(long))" "hairyfunc7(int (*)(long) (*)(int (*)(char*)))" "gdb/19"
536 }
537
538 proc do_tests {} {
539 global prms_id
540 global bug_id
541 global subdir
542 global objdir
543 global srcdir
544 global binfile
545 global gdb_prompt
546 global dm_type_int_star
547
548 set prms_id 0
549 set bug_id 0
550
551 # Start with a fresh gdb.
552
553 gdb_exit
554 gdb_start
555 gdb_reinitialize_dir $srcdir/$subdir
556 gdb_load $binfile
557
558 send_gdb "set language c++\n"
559 gdb_expect -re "$gdb_prompt $"
560 send_gdb "set width 0\n"
561 gdb_expect -re "$gdb_prompt $"
562
563 runto_main
564
565 probe_demangler
566 test_paddr_overloaded_functions
567 test_paddr_operator_functions
568 test_paddr_hairy_functions
569 test_lookup_operator_functions
570
571 # A regression test on errors involving operators
572 gdb_test "list foo::operator $dm_type_int_star" \
573 ".*the class foo does not have any method named operator $dm_type_int_star.*"
574 }
575
576 do_tests
This page took 0.050202 seconds and 4 git commands to generate.