1# cpexprs.exp - C++ expressions tests
2#
3# Copyright 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4#
5# Contributed by Red Hat, originally written by Keith Seitz.
6#
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.
11#
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.
16#
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/>.
19
20# This file is part of the gdb testsuite.
21
22# A helper proc which sets a breakpoint at FUNC and attempts to
23# run to the breakpoint.
24proc test_breakpoint {func} {
25    global DEC
26
27    # Return to the top of the test function every time.
28    delete_breakpoints
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.*" \
33		    ""] != 0 } {
34	fail "continue to test_function for $func"
35    } else {
36	gdb_breakpoint "$func"
37	set i [expr {[string last : $func] + 1}]
38	set efunc [string_to_regexp [string range $func $i end]]
39	gdb_test "continue" \
40	    "Continuing.\r\n\r\nBreakpoint $DEC+,.*$efunc.*" \
41	    "continue to $func"
42    }
43}
44
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
50#   funcs)
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
53#
54# Usage:
55# add NAME TYPE PRINT LST
56# add NAME TYPE PRINT -
57proc add {func type print lst} {
58    global all_functions CONVAR ADDR
59
60    set all_functions($func,type) $type
61    if {$print == "-"} {
62	set print $func
63    }
64
65    # An exception: since gdb canonicalizes C++ output,
66    # "(void)" must be mutated to "()".
67    regsub {\(void\)} $print {()} print
68
69    set all_functions($func,print) \
70	"$CONVAR = {[string_to_regexp $type]} $ADDR <[string_to_regexp $print].*>"
71    if {$lst == "-"} {
72	set lst "$func"
73    }
74    set all_functions($func,list) ".*// [string_to_regexp $lst]"
75}
76
77proc get {func cmd} {
78    global all_functions
79    return $all_functions($func,$cmd)
80}
81
82# Returns a list of function names for a given command
83proc get_functions {cmd} {
84    global all_functions
85    set result {}
86    foreach i [array names all_functions *,$cmd] {
87	if {$all_functions($i) != ""} {
88	    set idx [string last , $i]
89	    if {$idx != -1} {
90		lappend result [string range $i 0 [expr {$idx - 1}]]
91	    }
92	}
93    }
94
95    return [lsort $result]
96}
97
98# Some convenience variables for this test
99set DEC {[0-9]}; # a decimal number
100set HEX {[0-9a-fA-F]}; # a hexidecimal number
101set CONVAR "\\\$$DEC+"; # convenience variable regexp
102set ADDR "0x$HEX+"; # address
103
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.
109
110# The array holding all functions/methods to test. Valid subindexes
111# are (none need character escaping -- "add" will take care of that):
112
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 "//")
118array set all_functions {}
119
120# "Normal" functions/methods
121add {test_function} \
122    {int (int, char **)} \
123    - \
124    -
125add {derived::a_function} \
126    {void (const derived * const)} \
127    - \
128    -
129add {base1::a_function} \
130    {void (const base1 * const)} \
131    - \
132    -
133add {base2::a_function} \
134    {void (const base2 * const)} \
135    - \
136    -
137
138# Constructors
139
140# On targets using the ARM EABI, the constructor is expected to return
141# "this".
142proc ctor { type arglist } {
143    if { [istarget arm*-*eabi*] } {
144	set ret "$type *"
145    } else {
146	set ret "void "
147    }
148    if { $arglist != "" } {
149	set arglist ", $arglist"
150    }
151    return "${ret}($type * const$arglist)"
152}
153
154add {derived::derived} \
155    [ctor derived ""] \
156    - \
157    -
158add {base1::base1(void)} \
159    [ctor base1 "const void ** const"] \
160    - \
161    -
162add {base1::base1(int)} \
163    [ctor base1 "int"] \
164    - \
165    -
166add {base2::base2} \
167    [ctor base2 "const void ** const"] \
168    - \
169    -
170add {base::base(void)} \
171    [ctor base ""] \
172    - \
173    -
174add {base::base(int)} \
175    [ctor base "int"] \
176    - \
177    -
178
179# Destructors
180
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
184# the same.
185proc dtor { type } {
186    if { [istarget arm*-*eabi*] } {
187	set ret "void *"
188    } else {
189	set ret "void "
190    }
191    return "${ret}($type * const)"
192}
193
194add {base::~base} \
195    [dtor base] \
196    - \
197    -
198
199# Overloaded methods (all are const)
200add {base::overload(void) const} \
201    {int (const base * const)} \
202    - \
203    {base::overload(void) const}
204add {base::overload(int) const} \
205    {int (const base * const, int)} \
206    - \
207    -
208add {base::overload(short) const} \
209    {int (const base * const, short)} \
210    - \
211    -
212add {base::overload(long) const} \
213    {int (const base * const, long)} \
214    - \
215    -
216add {base::overload(char*) const} \
217    {int (const base * const, char *)} \
218    - \
219    -
220add {base::overload(base&) const} \
221    {int (const base * const, base &)} \
222    - \
223    -
224
225# Operators
226add {base::operator+} \
227    {int (const base * const, const base &)} \
228    - \
229    -
230add {base::operator++} \
231    {base (base * const)} \
232    - \
233    -
234add {base::operator+=} \
235    {base (base * const, const base &)} \
236    - \
237    -
238add {base::operator-} \
239    {int (const base * const, const base &)} \
240    - \
241    -
242add {base::operator--} \
243    {base (base * const)} \
244    - \
245    -
246add {base::operator-=} \
247    {base (base * const, const base &)} \
248    - \
249    -
250add {base::operator*} \
251    {int (const base * const, const base &)} \
252    - \
253    -
254add {base::operator*=} \
255    {base (base * const, const base &)} \
256    - \
257    -
258add {base::operator/} \
259    {int (const base * const, const base &)} \
260    - \
261    -
262add {base::operator/=} \
263    {base (base * const, const base &)} \
264    - \
265    -
266add {base::operator%} \
267    {int (const base * const, const base &)} \
268    - \
269    -
270add {base::operator%=} \
271    {base (base * const, const base &)} \
272    - \
273    -
274add {base::operator<} \
275    {bool (const base * const, const base &)} \
276    - \
277    -
278add {base::operator<=} \
279    {bool (const base * const, const base &)} \
280    - \
281    -
282add {base::operator>} \
283    {bool (const base * const, const base &)} \
284    - \
285    -
286add {base::operator>=} \
287    {bool (const base * const, const base &)} \
288    - \
289    -
290add {base::operator!=} \
291    {bool (const base * const, const base &)} \
292    - \
293    -
294add {base::operator==} \
295    {bool (const base * const, const base &)} \
296    - \
297    -
298add {base::operator!} \
299    {bool (const base * const)} \
300    - \
301    -
302add {base::operator&&} \
303    {bool (const base * const, const base &)} \
304    - \
305    -
306add {base::operator||} \
307    {bool (const base * const, const base &)} \
308    - \
309    -
310add {base::operator<<} \
311    {int (const base * const, int)} \
312    - \
313    -
314add {base::operator<<=} \
315    {base (base * const, int)} \
316    - \
317    -
318add {base::operator>>} \
319    {int (const base * const, int)} \
320    - \
321    -
322add {base::operator>>=} \
323    {base (base * const, int)} \
324    - \
325    -
326add {base::operator~} \
327    {int (const base * const)} \
328    - \
329    -
330add {base::operator&} \
331    {int (const base * const, const base &)} \
332    - \
333    -
334add {base::operator&=} \
335    {base (base * const, const base &)} \
336    - \
337    -
338add {base::operator|} \
339    {int (const base * const, const base &)} \
340    - \
341    -
342add {base::operator|=} \
343    {base (base * const, const base &)} \
344    - \
345    -
346add {base::operator^} \
347    {int (const base * const, const base &)} \
348    - \
349    -
350add {base::operator^=} \
351    {base (base * const, const base &)} \
352    - \
353    -
354add {base::operator=} \
355    {base (base * const, const base &)} \
356    - \
357    -
358add {base::operator()} \
359    {void (const base * const)} \
360    - \
361    -
362add {base::operator[]} \
363    {int (const base * const, int)} \
364    - \
365    -
366add {base::operator new} \
367    {void *(size_t)} \
368    - \
369    -
370add {base::operator delete} \
371    {void (void *)} \
372    - \
373    -
374add {base::operator new[]} \
375    {void *(size_t)} \
376    - \
377    -
378add {base::operator delete[]} \
379    {void (void *)} \
380    - \
381    -
382add {base::operator char*} \
383    {char *(const base * const)} \
384    - \
385    -
386add {base::operator fluff*} \
387    {fluff *(const base * const)} \
388    - \
389    -
390add {base::operator fluff**} \
391    {fluff **(const base * const)} \
392    - \
393    -
394add {base::operator int} \
395    {int (const base * const)} \
396    - \
397    -
398
399# Templates
400add {tclass<char>::do_something} \
401    {void (tclass<char> * const)} \
402    - \
403    -
404add {tclass<int>::do_something} \
405    {void (tclass<int> * const)} \
406    - \
407    -
408add {tclass<long>::do_something} \
409    {void (tclass<long> * const)} \
410    - \
411    -
412add {tclass<short>::do_something} \
413    {void (tclass<short> * const)} \
414    - \
415    -
416add {tclass<base>::do_something} \
417    {void (tclass<base> * const)} \
418    - \
419    -
420add {flubber<int, int, int, int, int>} \
421    {void (void)} \
422    - \
423    flubber
424add {flubber<int, int, int, int, short>} \
425    {void (void)} \
426    - \
427    flubber
428add {flubber<int, int, int, int, long>} \
429    {void (void)} \
430    - \
431    flubber
432add {flubber<int, int, int, int, char>} \
433    {void (void)} \
434    - \
435    flubber
436add {flubber<int, int, int, short, int>} \
437    {void (void)} \
438    - \
439    flubber
440add {flubber<int, int, int, short, short>} \
441    {void (void)} \
442    - \
443    flubber
444add {flubber<int, int, int, short, long>} \
445    {void (void)} \
446    - \
447    flubber
448add {flubber<int, int, int, short, char>} \
449    {void (void)} \
450    - \
451    flubber
452add {flubber<int, int, int, long, int>} \
453    {void (void)} \
454    - \
455    flubber
456add {flubber<int, int, int, long, short>} \
457    {void (void)} \
458    - \
459    flubber
460add {flubber<int, int, int, long, long>} \
461    {void (void)} \
462    - \
463    flubber
464add {flubber<int, int, int, long, char>} \
465    {void (void)} \
466    - \
467    flubber
468add {flubber<int, int, int, char, int>} \
469    {void (void)} \
470    - \
471    flubber
472add {flubber<int, int, int, char, short>} \
473    {void (void)} \
474    - \
475    flubber
476add {flubber<int, int, int, char, long>} \
477    {void (void)} \
478    - \
479    flubber
480add {flubber<int, int, int, char, char>} \
481    {void (void)} \
482    - \
483    flubber
484add {flubber<int, int, short, int, int>} \
485    {void (void)} \
486    - \
487    flubber
488add {flubber<int, int, short, int, short>} \
489    {void (void)} \
490    - \
491    flubber
492add {flubber<int, int, short, int, long>} \
493    {void (void)} \
494    - \
495    flubber
496add {flubber<int, int, short, int, char>} \
497    {void (void)} \
498    - \
499    flubber
500add {flubber<int, int, short, short, int>} \
501    {void (void)} \
502    - \
503    flubber
504add {flubber<short, int, short, int, short>} \
505    {void (void)} \
506    - \
507    flubber
508add {flubber<long, short, long, short, long>} \
509    {void (void)} \
510    - \
511    flubber
512add {tclass<base>::do_something} \
513    {void (tclass<base> * const)} \
514    - \
515    {tclass<T>::do_something}
516add {policy1::policy} \
517    [ctor "policy<int, operation_1<void*> >" "int"] \
518    {policy<int, operation_1<void*> >::policy} \
519    {policy<T, Policy>::policy}
520add {policy2::policy} \
521    [ctor "policy<int, operation_2<void*> >" int] \
522    {policy<int, operation_2<void*> >::policy} \
523    {policy<T, Policy>::policy}
524add {policy3::policy} \
525    [ctor "policy<int, operation_3<void*> >" "int"] \
526    {policy<int, operation_3<void*> >::policy} \
527    {policy<T, Policy>::policy}
528add {policy4::policy} \
529    [ctor "policy<int, operation_4<void*> >" "int"] \
530    {policy<int, operation_4<void*> >::policy} \
531    {policy<T, Policy>::policy}
532add {policy1::function} \
533    {void (void)} \
534    {operation_1<void*>::function} \
535    {operation_1<T>::function}
536add {policy2::function} \
537    {void (void)} \
538    {operation_2<void*>::function} \
539    {operation_2<T>::function}
540add {policy3::function} \
541    {void (void)} \
542    {operation_3<void*>::function} \
543    {operation_3<T>::function}
544add {policy4::function} \
545    {void (void)} \
546    {operation_4<void*>::function} \
547    {operation_4<T>::function}
548add {policyd<int, operation_1<int> >::policyd} \
549    [ctor "policyd<int, operation_1<int> >" "int"] \
550    - \
551    {policyd<T, Policy>::policyd}
552add {policyd1::policyd} \
553    [ctor "policyd<int, operation_1<int> >" "int"] \
554    {policyd<int, operation_1<int> >::policyd} \
555    {policyd<T, Policy>::policyd}
556add {policyd<int, operation_1<int> >::~policyd} \
557    [dtor "policyd<int, operation_1<int> >"] \
558    - \
559    {policyd<T, Policy>::~policyd}
560add {policyd1::~policyd} \
561    [dtor "policyd<int, operation_1<int> >"] \
562    {policyd<int, operation_1<int> >::~policyd} \
563    {policyd<T, Policy>::~policyd}
564add {policyd<long, operation_1<long> >::policyd} \
565    [ctor "policyd<long, operation_1<long> >" "long"] \
566    - \
567    {policyd<T, Policy>::policyd}
568add {policyd2::policyd} \
569    [ctor "policyd<long, operation_1<long> >" "long"] \
570    {policyd<long, operation_1<long> >::policyd} \
571    {policyd<T, Policy>::policyd}
572add {policyd<long, operation_1<long> >::~policyd} \
573    [dtor "policyd<long, operation_1<long> >"] \
574    - \
575    {policyd<T, Policy>::~policyd}
576add {policyd2::~policyd} \
577    [dtor "policyd<long, operation_1<long> >"] \
578    {policyd<long, operation_1<long> >::~policyd} \
579    {policyd<T, Policy>::~policyd}
580add {policyd<char, operation_1<char> >::policyd} \
581    [ctor "policyd<char, operation_1<char> >" "char"] \
582    - \
583    {policyd<T, Policy>::policyd}
584add {policyd3::policyd} \
585    [ctor "policyd<char, operation_1<char> >" "char"] \
586    {policyd<char, operation_1<char> >::policyd} \
587    {policyd<T, Policy>::policyd}
588add {policyd<char, operation_1<char> >::~policyd} \
589    [dtor "policyd<char, operation_1<char> >"] \
590    - \
591    {policyd<T, Policy>::~policyd}
592add {policyd3::~policyd} \
593    [dtor "policyd<char, operation_1<char> >"] \
594    {policyd<char, operation_1<char> >::~policyd} \
595    {policyd<T, Policy>::~policyd}
596add {policyd<base, operation_1<base> >::policyd} \
597    [ctor "policyd<base, operation_1<base> >" "base"] \
598    - \
599    {policyd<T, Policy>::policyd}
600add {policyd4::policyd} \
601    [ctor "policyd<base, operation_1<base> >" "base"] \
602    {policyd<base, operation_1<base> >::policyd} \
603    {policyd<T, Policy>::policyd}
604add {policyd<base, operation_1<base> >::~policyd} \
605    [dtor "policyd<base, operation_1<base> >"] \
606    - \
607    {policyd<T, Policy>::~policyd}
608add {policyd4::~policyd} \
609    [dtor "policyd<base, operation_1<base> >"] \
610    {policyd<base, operation_1<base> >::~policyd} \
611    {policyd<T, Policy>::~policyd}
612add {policyd<tclass<int>, operation_1<tclass<int> > >::policyd} \
613    [ctor "policyd<tclass<int>, operation_1<tclass<int> > >" "tclass<int>"] \
614    - \
615    {policyd<T, Policy>::policyd}
616add {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}
620add {policyd<tclass<int>, operation_1<tclass<int> > >::~policyd} \
621    [dtor "policyd<tclass<int>, operation_1<tclass<int> > >"] \
622    - \
623    {policyd<T, Policy>::~policyd}
624add {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}
628add {policyd<int, operation_1<int> >::function} \
629    {void (void)} \
630    {operation_1<int>::function}\
631    {operation_1<T>::function}
632add {policyd1::function} \
633    {void (void)} \
634    {operation_1<int>::function} \
635    {operation_1<T>::function}
636add {policyd2::function} \
637    {void (void)} \
638    {operation_1<long>::function} \
639    {operation_1<T>::function}
640add {policyd<char, operation_1<char> >::function} \
641    {void (void)} \
642    {operation_1<char>::function} \
643    {operation_1<T>::function}
644add {policyd3::function} \
645    {void (void)} \
646    {operation_1<char>::function} \
647    {operation_1<T>::function}
648add {policyd<base, operation_1<base> >::function} \
649    {void (void)} \
650    {operation_1<base>::function} \
651    {operation_1<T>::function}
652add {policyd4::function} \
653    {void (void)} \
654    {operation_1<base>::function} \
655    {operation_1<T>::function}
656add {policyd<tclass<int>, operation_1<tclass<int> > >::function} \
657    {void (void)} \
658    {operation_1<tclass<int> >::function} \
659    {operation_1<T>::function}
660add {policyd5::function} \
661    {void (void)} \
662    {operation_1<tclass<int> >::function} \
663    {operation_1<T>::function}
664
665# Start the test
666if {$tracelevel} {
667    strace $tracelevel
668}
669
670if {[skip_cplus_tests]} { continue }
671
672# On SPU this test fails because the executable exceeds local storage size.
673if { [istarget "spu*-*-*"] } {
674        return 0
675}
676
677#
678# test running programs
679#
680
681set testfile "cpexprs"
682set srcfile "${testfile}.cc"
683set binfile [file join $objdir $subdir $testfile]
684
685if  {[gdb_compile [file join $srcdir $subdir $srcfile] $binfile \
686	  executable {debug c++}] != "" } {
687    untested "$testfile.exp"
688    return -1
689}
690
691if {[get_compiler_info $binfile "c++"]} {
692    return -1
693}
694
695gdb_exit
696gdb_start
697gdb_reinitialize_dir [file join $srcdir $subdir]
698gdb_load $binfile
699
700if {![runto_main]} {
701    perror "couldn't run to breakpoint"
702    continue
703}
704
705# Set the listsize to one. This will help with testing "list".
706gdb_test "set listsize 1"
707
708# "print METHOD"
709foreach name [get_functions print] {
710    gdb_test "print $name" [get $name print] "print $name"
711}
712
713# "list METHOD"
714foreach name [get_functions list] {
715    gdb_test "list $name" [get $name list] "list $name"
716}
717
718# Running to breakpoint -- use any function we can "list"
719foreach name [get_functions list] {
720    # Skip "test_function", since test_breakpoint uses it
721    if {[string compare $name "test_function"] != 0} {
722	test_breakpoint $name
723    }
724}
725
726gdb_exit
727return 0
728