1# Copyright 1992, 1997, 1999, 2001, 2002, 2003, 2004
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
21if $tracelevel then {
22	strace $tracelevel
23}
24
25if { [skip_cplus_tests] } { continue }
26
27set testfile "cplusfuncs"
28set srcfile ${testfile}.cc
29set binfile ${objdir}/${subdir}/${testfile}
30
31if { [get_compiler_info $binfile "c++"] } {
32    return -1
33}
34
35if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
36     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
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
59set dm_operator_comma		","
60set dm_type_char_star		"char*"
61set dm_type_char_star_quoted    "char\\*"
62set dm_type_foo_ref 		"foo&"
63set dm_type_int_star		"int*"
64set dm_type_long_star		"long*"
65set dm_type_unsigned_int	"unsigned"
66set dm_type_void		""
67set dm_type_void_star		"void*"
68
69proc 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
240proc info_func_regexp { name demangled } {
241    global gdb_prompt
242
243    send_gdb "info function $name\n"
244    gdb_expect {
245	-re ".*File .*:\r\n(class |)$demangled\r\n.*$gdb_prompt $" {
246	    pass "info function for \"$name\""
247	}
248	-re ".*$gdb_prompt $" {
249	    fail "info function for \"$name\""
250	}
251	timeout {
252	    fail "info function for \"$name\" (timeout)"
253	}
254    }
255}
256
257#
258#  Lookup a specific C++ function and print the demangled type.
259#  This form accepts the demangled type as a literal string.
260#
261
262proc info_func { name demangled } {
263    info_func_regexp "$name" [string_to_regexp "$demangled"]
264}
265
266#
267# Print the address of a function.
268# This checks that I can lookup a fully qualified C++ function.
269# This also checks the argument types on the return string.
270
271# Note: carlton/2003-01-16: If you modify this, make a corresponding
272# modification to print_addr_2_kfail.
273
274proc print_addr_2 { name good } {
275    global gdb_prompt
276    global hex
277
278    set good_pattern [string_to_regexp $good]
279
280    send_gdb "print &'$name'\n"
281    gdb_expect {
282	-re ".* = .* $hex <$good_pattern>\r\n$gdb_prompt $" {
283	    pass "print &'$name'"
284	}
285	-re ".*$gdb_prompt $" {
286	    fail "print &'$name'"
287	}
288	timeout {
289	    fail "print &'$name' (timeout)"
290	}
291    }
292}
293
294# NOTE: carlton/2003-01-16: hairyfunc5-6 fail on GCC 3.x (for at least
295# x=1 and x=2.1).  So I'm modifying print_addr_2 to accept a failure
296# condition.  FIXME: It would be nice if the failure condition were
297# conditional on the compiler version, but I'm not sufficiently
298# motivated.  I did hardwire in the versions of char * and int *,
299# which will give some compiler-specificity to the failure.
300
301proc print_addr_2_kfail { name good bad bugid } {
302    global gdb_prompt
303    global hex
304
305    set good_pattern [string_to_regexp $good]
306    set bad_pattern [string_to_regexp $bad]
307
308    send_gdb "print &'$name'\n"
309    gdb_expect {
310	-re ".* = .* $hex <$good_pattern>\r\n$gdb_prompt $" {
311	    pass "print &'$name'"
312	}
313	-re ".* = .* $hex <$bad_pattern>\r\n$gdb_prompt $" {
314	    kfail $bugid "print &'$name'"
315	}
316	-re ".*$gdb_prompt $" {
317	    fail "print &'$name'"
318	}
319	timeout {
320	    fail "print &'$name' (timeout)"
321	}
322    }
323}
324
325#
326#  Simple interfaces to print_addr_2.
327#
328
329proc print_addr { name } {
330    print_addr_2 "$name" "$name"
331}
332
333#
334# Test name demangling for operators.
335#
336# The '(' at the end of each regex input pattern is so that we match only
337# the one we are looking for.  I.E. "operator&" would match both
338# "operator&(foo &)" and "operator&&(foo &)".
339#
340# gdb-gnats bug gdb/18:
341#  "gdb can't parse "info func operator*" or "info func operator\*".
342#  The star in "operator*" is interpreted as a regexp, but the "\*"
343#  in  "operator\*" is not a legal operator.
344#
345
346proc test_lookup_operator_functions {} {
347    global dm_operator_comma
348    global dm_type_char_star
349    global dm_type_char_star_quoted
350    global dm_type_foo_ref
351    global dm_type_void
352    global dm_type_void_star
353
354    # operator* requires quoting so that GDB does not treat it as a regexp.
355    info_func "operator\\*("	"void foo::operator*($dm_type_foo_ref);"
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_void);"
365    info_func "operator++("	"void foo::operator++(int);"
366    info_func "operator=("	"void foo::operator=($dm_type_foo_ref);"
367    info_func "operator+=("	"void foo::operator+=($dm_type_foo_ref);"
368    # operator*= requires quoting so that GDB does not treat it as a regexp.
369    info_func "operator\\*=("	"void foo::operator*=($dm_type_foo_ref);"
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$dm_operator_comma\("	\
374    				"void foo::operator$dm_operator_comma\($dm_type_foo_ref);"
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<("	"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_void);"
385    info_func "operator--("	"void foo::operator--(int);"
386    info_func "operator->("	"foo *foo::operator->($dm_type_void);"
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_foo_ref);"
391    info_func "operator^=("	"void foo::operator^=($dm_type_foo_ref);"
392    # operator->* requires quoting so that GDB does not treat it as a regexp.
393    info_func "operator->\\*("	"void foo::operator->*($dm_type_foo_ref);"
394
395    # operator[] needs double backslashes, so that a single backslash
396    # will be sent to GDB, preventing the square brackets from being
397    # evaluated as a regular expression.
398    info_func "operator\\\[\\\](" "void foo::operator\[\]($dm_type_foo_ref);"
399
400    # These are gnarly because they might end with 'static'.
401    set dm_type_void_star_regexp [string_to_regexp $dm_type_void_star]
402    info_func_regexp "operator new("     "void \\*foo::operator new\\(.*\\)(| static);"
403    info_func_regexp "operator delete("  "void foo::operator delete\\($dm_type_void_star_regexp\\)(| static);"
404
405    info_func "operator int("	"int foo::operator int($dm_type_void);"
406    info_func "operator()("	"void foo::operator()($dm_type_foo_ref);"
407    info_func "operator $dm_type_char_star_quoted\(" \
408				"char *foo::operator $dm_type_char_star\($dm_type_void);"
409
410}
411
412
413proc test_paddr_operator_functions {} {
414    global hex
415    global hp_aCC_compiler
416    global dm_operator_comma
417    global dm_type_char_star
418    global dm_type_foo_ref
419    global dm_type_long_star
420    global dm_type_unsigned_int
421    global dm_type_void
422    global dm_type_void_star
423
424    print_addr "foo::operator*($dm_type_foo_ref)"
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_void)"
434    print_addr "foo::operator++(int)"
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_foo_ref)"
440    print_addr "foo::operator|=($dm_type_foo_ref)"
441    print_addr "foo::operator$dm_operator_comma\($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_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_void)"
452    print_addr "foo::operator--(int)"
453    print_addr "foo::operator->($dm_type_void)"
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_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
463    gdb_test "print &'foo::operator new'" \
464	" = .* $hex <foo::operator new\\(.*\\)(| static)>"
465    if { !$hp_aCC_compiler } {
466	print_addr "foo::operator delete($dm_type_void_star)"
467    } else {
468	gdb_test "print &'foo::operator delete($dm_type_void_star) static'" \
469	    " = .*(0x\[0-9a-f\]+|) <foo::operator delete.*>"
470    }
471
472    print_addr "foo::operator int($dm_type_void)"
473    print_addr "foo::operator $dm_type_char_star\($dm_type_void)"
474}
475
476#
477# Test overloaded functions (1 arg).
478#
479
480proc test_paddr_overloaded_functions {} {
481    global dm_type_unsigned_int
482    global dm_type_void
483
484    print_addr "overload1arg($dm_type_void)"
485    print_addr "overload1arg(char)"
486    print_addr "overload1arg(signed char)"
487    print_addr "overload1arg(unsigned char)"
488    print_addr "overload1arg(short)"
489    print_addr "overload1arg(unsigned short)"
490    print_addr "overload1arg(int)"
491    print_addr "overload1arg($dm_type_unsigned_int)"
492    print_addr "overload1arg(long)"
493    print_addr "overload1arg(unsigned long)"
494    print_addr "overload1arg(float)"
495    print_addr "overload1arg(double)"
496
497    print_addr "overloadargs(int)"
498    print_addr "overloadargs(int, int)"
499    print_addr "overloadargs(int, int, int)"
500    print_addr "overloadargs(int, int, int, int)"
501    print_addr "overloadargs(int, int, int, int, int)"
502    print_addr "overloadargs(int, int, int, int, int, int)"
503    print_addr "overloadargs(int, int, int, int, int, int, int)"
504    print_addr "overloadargs(int, int, int, int, int, int, int, int)"
505    print_addr "overloadargs(int, int, int, int, int, int, int, int, int)"
506    print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int)"
507    print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int, int)"
508}
509
510proc test_paddr_hairy_functions {} {
511    global gdb_prompt
512    global hex
513    global dm_type_char_star
514    global dm_type_int_star
515    global dm_type_long_star
516
517    print_addr_2 "hairyfunc1" "hairyfunc1(int)"
518    print_addr_2 "hairyfunc2" "hairyfunc2(int (*)($dm_type_char_star))"
519    print_addr_2 "hairyfunc3" "hairyfunc3(int (*)(short (*)($dm_type_long_star)))"
520    print_addr_2 "hairyfunc4" "hairyfunc4(int (*)(short (*)($dm_type_char_star)))"
521
522    # gdb-gnats bug gdb/19:
523    # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
524    print_addr_2_kfail "hairyfunc5" "hairyfunc5(int (*(*)($dm_type_char_star))(long))" "hairyfunc5(int (*)(long) (*)(char*))" "gdb/19"
525    print_addr_2_kfail "hairyfunc6" "hairyfunc6(int (*(*)($dm_type_int_star))(long))" "hairyfunc6(int (*)(long) (*)(int*))" "gdb/19"
526    print_addr_2_kfail "hairyfunc7" "hairyfunc7(int (*(*)(int (*)($dm_type_char_star)))(long))" "hairyfunc7(int (*)(long) (*)(int (*)(char*)))" "gdb/19"
527}
528
529proc do_tests {} {
530    global prms_id
531    global bug_id
532    global subdir
533    global objdir
534    global srcdir
535    global binfile
536    global gdb_prompt
537
538    set prms_id 0
539    set bug_id 0
540
541    # Start with a fresh gdb.
542
543    gdb_exit
544    gdb_start
545    gdb_reinitialize_dir $srcdir/$subdir
546    gdb_load $binfile
547
548    send_gdb "set language c++\n"
549    gdb_expect -re "$gdb_prompt $"
550    send_gdb "set width 0\n"
551    gdb_expect -re "$gdb_prompt $"
552
553    runto_main
554
555    probe_demangler
556    test_paddr_overloaded_functions
557    test_paddr_operator_functions
558    test_paddr_hairy_functions
559    test_lookup_operator_functions
560}
561
562do_tests
563