1 # cpexprs.exp - C++ expressions tests
3 # Copyright 2008-2013 Free Software Foundation, Inc.
5 # Contributed by Red Hat, originally written by Keith Seitz.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # This file is part of the gdb testsuite.
22 # A helper proc which sets a breakpoint at FUNC and attempts to
23 # run to the breakpoint.
24 proc test_breakpoint {func} {
27 # Return to the top of the test function every time.
29 if { ! [gdb_breakpoint test_function] } {
30 fail "set test_function breakpoint for $func"
31 } elseif { [gdb_test "continue" \
32 "Continuing.\r\n\r\nBreakpoint $DEC+,.*test_function.*" \
34 fail "continue to test_function for $func"
36 gdb_breakpoint "$func"
37 set i [expr {[string last : $func] + 1}]
38 set efunc [string_to_regexp [string range $func $i end]]
40 "Continuing.\r\n\r\nBreakpoint $DEC+,.*$efunc.*" \
45 # Add a function to the list of tested functions
46 # FUNC is the name of the function (which will be passed to gdb commands)
47 # TYPE is the type of the function, as expected from the "print" command
48 # PRINT is the name of the function, as expected result of the print command
49 # *OR* "-", indicating that FUNC should be used (needed for virtual/inherited
51 # LST is either the expected result of the list command (the comment from
52 # the source code) *OR* "-", in which case FUNC will be used
55 # add NAME TYPE PRINT LST
56 # add NAME TYPE PRINT -
57 proc add {func type print lst} {
58 global all_functions CONVAR ADDR
60 set all_functions($func,type) $type
65 # An exception: since gdb canonicalizes C++ output,
66 # "(void)" must be mutated to "()".
67 regsub {\(void\)} $print {()} print
69 set all_functions($func,print) \
70 "$CONVAR = {[string_to_regexp $type]} $ADDR <[string_to_regexp $print].*>"
74 set all_functions($func,list) ".*// [string_to_regexp $lst]"
79 return $all_functions($func,$cmd)
82 # Returns a list of function names for a given command
83 proc get_functions {cmd} {
86 foreach i [array names all_functions *,$cmd] {
87 if {$all_functions($i) != ""} {
88 set idx [string last , $i]
90 lappend result [string range $i 0 [expr {$idx - 1}]]
95 return [lsort $result]
98 # Some convenience variables for this test
99 set DEC {[0-9]}; # a decimal number
100 set HEX {[0-9a-fA-F]}; # a hexidecimal number
101 set CONVAR "\\\$$DEC+"; # convenience variable regexp
102 set ADDR "0x$HEX+"; # address
104 # An array of functions/methods that we are testing...
105 # Each element consists is indexed by NAME,COMMAND, where
106 # NAME is the function name and COMMAND is the gdb command that
107 # we are testing. The value of the array for any index pair is
108 # the expected result of running COMMAND with the NAME as argument.
110 # The array holding all functions/methods to test. Valid subindexes
111 # are (none need character escaping -- "add" will take care of that):
113 # add name type print_name list
114 # NAME,type: value is type of function
115 # NAME,print: value is print name of function (careful w/inherited/virtual!)
116 # NAME,list: value is comment in source code on first line of function
117 # (without the leading "//")
118 array set all_functions {}
120 # "Normal" functions/methods
121 add {test_function} \
122 {int (int, char **)} \
125 add {derived::a_function} \
126 {void (const derived * const)} \
129 add {base1::a_function} \
130 {void (const base1 * const)} \
133 add {base2::a_function} \
134 {void (const base2 * const)} \
140 # On targets using the ARM EABI, the constructor is expected to return
142 proc ctor { type arglist } {
143 if { [istarget arm*-*eabi*] } {
148 if { $arglist != "" } {
149 set arglist ", $arglist"
151 return "${ret}($type * const$arglist)"
154 add {derived::derived} \
158 add {base1::base1(void)} \
159 [ctor base1 "const void ** const"] \
162 add {base1::base1(int)} \
167 [ctor base2 "const void ** const"] \
170 add {base::base(void)} \
174 add {base::base(int)} \
181 # On targets using the ARM EABI, some destructors are expected
182 # to return "this". Others are void. For internal reasons,
183 # GCC returns void * instead of $type *; RealView appears to do
186 if { [istarget arm*-*eabi*] } {
191 return "${ret}($type * const)"
199 # Overloaded methods (all are const)
200 add {base::overload(void) const} \
201 {int (const base * const)} \
203 {base::overload(void) const}
204 add {base::overload(int) const} \
205 {int (const base * const, int)} \
208 add {base::overload(short) const} \
209 {int (const base * const, short)} \
212 add {base::overload(long) const} \
213 {int (const base * const, long)} \
216 add {base::overload(char*) const} \
217 {int (const base * const, char *)} \
220 add {base::overload(base&) const} \
221 {int (const base * const, base &)} \
226 add {base::operator+} \
227 {int (const base * const, const base &)} \
230 add {base::operator++} \
231 {base (base * const)} \
234 add {base::operator+=} \
235 {base (base * const, const base &)} \
238 add {base::operator-} \
239 {int (const base * const, const base &)} \
242 add {base::operator--} \
243 {base (base * const)} \
246 add {base::operator-=} \
247 {base (base * const, const base &)} \
250 add {base::operator*} \
251 {int (const base * const, const base &)} \
254 add {base::operator*=} \
255 {base (base * const, const base &)} \
258 add {base::operator/} \
259 {int (const base * const, const base &)} \
262 add {base::operator/=} \
263 {base (base * const, const base &)} \
266 add {base::operator%} \
267 {int (const base * const, const base &)} \
270 add {base::operator%=} \
271 {base (base * const, const base &)} \
274 add {base::operator<} \
275 {bool (const base * const, const base &)} \
278 add {base::operator<=} \
279 {bool (const base * const, const base &)} \
282 add {base::operator>} \
283 {bool (const base * const, const base &)} \
286 add {base::operator>=} \
287 {bool (const base * const, const base &)} \
290 add {base::operator!=} \
291 {bool (const base * const, const base &)} \
294 add {base::operator==} \
295 {bool (const base * const, const base &)} \
298 add {base::operator!} \
299 {bool (const base * const)} \
302 add {base::operator&&} \
303 {bool (const base * const, const base &)} \
306 add {base::operator||} \
307 {bool (const base * const, const base &)} \
310 add {base::operator<<} \
311 {int (const base * const, int)} \
314 add {base::operator<<=} \
315 {base (base * const, int)} \
318 add {base::operator>>} \
319 {int (const base * const, int)} \
322 add {base::operator>>=} \
323 {base (base * const, int)} \
326 add {base::operator~} \
327 {int (const base * const)} \
330 add {base::operator&} \
331 {int (const base * const, const base &)} \
334 add {base::operator&=} \
335 {base (base * const, const base &)} \
338 add {base::operator|} \
339 {int (const base * const, const base &)} \
342 add {base::operator|=} \
343 {base (base * const, const base &)} \
346 add {base::operator^} \
347 {int (const base * const, const base &)} \
350 add {base::operator^=} \
351 {base (base * const, const base &)} \
354 add {base::operator=} \
355 {base (base * const, const base &)} \
358 add {base::operator()} \
359 {void (const base * const)} \
362 add {base::operator[]} \
363 {int (const base * const, int)} \
366 add {base::operator new} \
370 add {base::operator delete} \
374 add {base::operator new[]} \
378 add {base::operator delete[]} \
382 add {base::operator char*} \
383 {char *(const base * const)} \
386 add {base::operator fluff*} \
387 {fluff *(const base * const)} \
390 add {base::operator fluff**} \
391 {fluff **(const base * const)} \
394 add {base::operator int} \
395 {int (const base * const)} \
400 add {tclass<char>::do_something} \
401 {void (tclass<char> * const)} \
404 add {tclass<int>::do_something} \
405 {void (tclass<int> * const)} \
408 add {tclass<long>::do_something} \
409 {void (tclass<long> * const)} \
412 add {tclass<short>::do_something} \
413 {void (tclass<short> * const)} \
416 add {tclass<base>::do_something} \
417 {void (tclass<base> * const)} \
420 add {flubber<int, int, int, int, int>} \
424 add {flubber<int, int, int, int, short>} \
428 add {flubber<int, int, int, int, long>} \
432 add {flubber<int, int, int, int, char>} \
436 add {flubber<int, int, int, short, int>} \
440 add {flubber<int, int, int, short, short>} \
444 add {flubber<int, int, int, short, long>} \
448 add {flubber<int, int, int, short, char>} \
452 add {flubber<int, int, int, long, int>} \
456 add {flubber<int, int, int, long, short>} \
460 add {flubber<int, int, int, long, long>} \
464 add {flubber<int, int, int, long, char>} \
468 add {flubber<int, int, int, char, int>} \
472 add {flubber<int, int, int, char, short>} \
476 add {flubber<int, int, int, char, long>} \
480 add {flubber<int, int, int, char, char>} \
484 add {flubber<int, int, short, int, int>} \
488 add {flubber<int, int, short, int, short>} \
492 add {flubber<int, int, short, int, long>} \
496 add {flubber<int, int, short, int, char>} \
500 add {flubber<int, int, short, short, int>} \
504 add {flubber<short, int, short, int, short>} \
508 add {flubber<long, short, long, short, long>} \
512 add {tclass<base>::do_something} \
513 {void (tclass<base> * const)} \
515 {tclass<T>::do_something}
516 add {policy1::policy} \
517 [ctor "policy<int, operation_1<void*> >" "int"] \
518 {policy<int, operation_1<void*> >::policy} \
519 {policy<T, Policy>::policy}
520 add {policy2::policy} \
521 [ctor "policy<int, operation_2<void*> >" int] \
522 {policy<int, operation_2<void*> >::policy} \
523 {policy<T, Policy>::policy}
524 add {policy3::policy} \
525 [ctor "policy<int, operation_3<void*> >" "int"] \
526 {policy<int, operation_3<void*> >::policy} \
527 {policy<T, Policy>::policy}
528 add {policy4::policy} \
529 [ctor "policy<int, operation_4<void*> >" "int"] \
530 {policy<int, operation_4<void*> >::policy} \
531 {policy<T, Policy>::policy}
532 add {policy1::function} \
534 {operation_1<void*>::function} \
535 {operation_1<T>::function}
536 add {policy2::function} \
538 {operation_2<void*>::function} \
539 {operation_2<T>::function}
540 add {policy3::function} \
542 {operation_3<void*>::function} \
543 {operation_3<T>::function}
544 add {policy4::function} \
546 {operation_4<void*>::function} \
547 {operation_4<T>::function}
548 add {policyd<int, operation_1<int> >::policyd} \
549 [ctor "policyd<int, operation_1<int> >" "int"] \
551 {policyd<T, Policy>::policyd}
552 add {policyd1::policyd} \
553 [ctor "policyd<int, operation_1<int> >" "int"] \
554 {policyd<int, operation_1<int> >::policyd} \
555 {policyd<T, Policy>::policyd}
556 add {policyd<int, operation_1<int> >::~policyd} \
557 [dtor "policyd<int, operation_1<int> >"] \
559 {policyd<T, Policy>::~policyd}
560 add {policyd1::~policyd} \
561 [dtor "policyd<int, operation_1<int> >"] \
562 {policyd<int, operation_1<int> >::~policyd} \
563 {policyd<T, Policy>::~policyd}
564 add {policyd<long, operation_1<long> >::policyd} \
565 [ctor "policyd<long, operation_1<long> >" "long"] \
567 {policyd<T, Policy>::policyd}
568 add {policyd2::policyd} \
569 [ctor "policyd<long, operation_1<long> >" "long"] \
570 {policyd<long, operation_1<long> >::policyd} \
571 {policyd<T, Policy>::policyd}
572 add {policyd<long, operation_1<long> >::~policyd} \
573 [dtor "policyd<long, operation_1<long> >"] \
575 {policyd<T, Policy>::~policyd}
576 add {policyd2::~policyd} \
577 [dtor "policyd<long, operation_1<long> >"] \
578 {policyd<long, operation_1<long> >::~policyd} \
579 {policyd<T, Policy>::~policyd}
580 add {policyd<char, operation_1<char> >::policyd} \
581 [ctor "policyd<char, operation_1<char> >" "char"] \
583 {policyd<T, Policy>::policyd}
584 add {policyd3::policyd} \
585 [ctor "policyd<char, operation_1<char> >" "char"] \
586 {policyd<char, operation_1<char> >::policyd} \
587 {policyd<T, Policy>::policyd}
588 add {policyd<char, operation_1<char> >::~policyd} \
589 [dtor "policyd<char, operation_1<char> >"] \
591 {policyd<T, Policy>::~policyd}
592 add {policyd3::~policyd} \
593 [dtor "policyd<char, operation_1<char> >"] \
594 {policyd<char, operation_1<char> >::~policyd} \
595 {policyd<T, Policy>::~policyd}
596 add {policyd<base, operation_1<base> >::policyd} \
597 [ctor "policyd<base, operation_1<base> >" "base"] \
599 {policyd<T, Policy>::policyd}
600 add {policyd4::policyd} \
601 [ctor "policyd<base, operation_1<base> >" "base"] \
602 {policyd<base, operation_1<base> >::policyd} \
603 {policyd<T, Policy>::policyd}
604 add {policyd<base, operation_1<base> >::~policyd} \
605 [dtor "policyd<base, operation_1<base> >"] \
607 {policyd<T, Policy>::~policyd}
608 add {policyd4::~policyd} \
609 [dtor "policyd<base, operation_1<base> >"] \
610 {policyd<base, operation_1<base> >::~policyd} \
611 {policyd<T, Policy>::~policyd}
612 add {policyd<tclass<int>, operation_1<tclass<int> > >::policyd} \
613 [ctor "policyd<tclass<int>, operation_1<tclass<int> > >" "tclass<int>"] \
615 {policyd<T, Policy>::policyd}
616 add {policyd5::policyd} \
617 [ctor "policyd<tclass<int>, operation_1<tclass<int> > >" "tclass<int>"] \
618 {policyd<tclass<int>, operation_1<tclass<int> > >::policyd} \
619 {policyd<T, Policy>::policyd}
620 add {policyd<tclass<int>, operation_1<tclass<int> > >::~policyd} \
621 [dtor "policyd<tclass<int>, operation_1<tclass<int> > >"] \
623 {policyd<T, Policy>::~policyd}
624 add {policyd5::~policyd} \
625 [dtor "policyd<tclass<int>, operation_1<tclass<int> > >"] \
626 {policyd<tclass<int>, operation_1<tclass<int> > >::~policyd} \
627 {policyd<T, Policy>::~policyd}
628 add {policyd<int, operation_1<int> >::function} \
630 {operation_1<int>::function}\
631 {operation_1<T>::function}
632 add {policyd1::function} \
634 {operation_1<int>::function} \
635 {operation_1<T>::function}
636 add {policyd2::function} \
638 {operation_1<long>::function} \
639 {operation_1<T>::function}
640 add {policyd<char, operation_1<char> >::function} \
642 {operation_1<char>::function} \
643 {operation_1<T>::function}
644 add {policyd3::function} \
646 {operation_1<char>::function} \
647 {operation_1<T>::function}
648 add {policyd<base, operation_1<base> >::function} \
650 {operation_1<base>::function} \
651 {operation_1<T>::function}
652 add {policyd4::function} \
654 {operation_1<base>::function} \
655 {operation_1<T>::function}
656 add {policyd<tclass<int>, operation_1<tclass<int> > >::function} \
658 {operation_1<tclass<int> >::function} \
659 {operation_1<T>::function}
660 add {policyd5::function} \
662 {operation_1<tclass<int> >::function} \
663 {operation_1<T>::function}
666 if {[skip_cplus_tests]} { continue }
668 # On SPU this test fails because the executable exceeds local storage size.
669 if { [istarget "spu*-*-*"] } {
674 # test running programs
677 standard_testfile .cc
679 if {[get_compiler_info "c++"]} {
683 if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}]} {
688 perror "couldn't run to breakpoint"
692 # Set the listsize to one. This will help with testing "list".
693 gdb_test "set listsize 1"
696 foreach name [get_functions print] {
697 gdb_test "print $name" [get $name print] "print $name"
701 foreach name [get_functions list] {
702 gdb_test "list $name" [get $name list] "list $name"
705 # Running to breakpoint -- use any function we can "list"
706 foreach name [get_functions list] {
707 # Skip "test_function", since test_breakpoint uses it
708 if {[string compare $name "test_function"] != 0} {
709 test_breakpoint $name
713 # Test c/v gets recognized even without quoting.
714 foreach cv {{} { const} { volatile} { const volatile}} {
715 set test "p 'CV::m(int)$cv'"
716 gdb_test_multiple $test $test {
717 -re "( = {.*} 0x\[0-9a-f\]+ <CV::m.*>)\r\n$gdb_prompt $" {
718 # = {void (CV * const, CV::t)} 0x400944 <CV::m(int)>
719 set correct $expect_out(1,string)
724 setup_kfail c++/14186 *-*-*
726 gdb_test "p CV::m(int)$cv" [string_to_regexp $correct]
729 # Test TYPENAME:: gets recognized even in parentheses.
730 gdb_test "p CV_f(int)" { = {int \(int\)} 0x[0-9a-f]+ <CV_f\(int\)>}
731 gdb_test "p CV_f(CV::t)" { = {int \(int\)} 0x[0-9a-f]+ <CV_f\(int\)>}
732 gdb_test "p CV_f(CV::i)" " = 43"
734 gdb_test "p CV_f('cpexprs.cc'::CV::t)" \
735 { = {int \(int\)} 0x[0-9a-f]+ <CV_f\(int\)>}