Copyright year update in most files of the GDB Project.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.cp / casts.exp
1 # Copyright 2002-2004, 2007-2012 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 3 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, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the gdb testsuite
17
18 # Test casting, especially between class types or pointer-to-class
19 # types.
20
21 # This file is part of the gdb testsuite
22
23 if $tracelevel then {
24 strace $tracelevel
25 }
26
27 #
28 # test running programs
29 #
30
31
32 if { [skip_cplus_tests] } { continue }
33
34 set testfile "casts"
35 set srcfile ${testfile}.cc
36 set binfile ${objdir}/${subdir}/${testfile}
37
38 if [get_compiler_info ${binfile} "c++"] {
39 return -1;
40 }
41
42 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
43 untested casts.exp
44 return -1
45 }
46
47
48 gdb_exit
49 gdb_start
50 gdb_reinitialize_dir $srcdir/$subdir
51 gdb_load ${binfile}
52
53 if ![runto_main] then {
54 perror "couldn't run to breakpoint"
55 continue
56 }
57
58 gdb_test "break [gdb_get_line_number "casts.exp: 1"]" \
59 "Breakpoint.*at.* file .*" \
60 ""
61
62 gdb_test "continue" "Breakpoint .* at .*casts.cc.*" ""
63
64 # Casting a pointer to a base class to a pointer to a derived class
65 # should yield the entire derived class. Until August 2002, GDB got
66 # the enclosing type on `(B *) a' wrong: while the value's static type
67 # was `B *', as it should be, the enclosing type (which is supposed to
68 # be the dynamic type) was `A *'. It's senseless to have a static
69 # type derived from the dynamic type; it should be the other way
70 # 'round. Dereferencing this oddly typed pointer yielded a value in
71 # which only the base class's members were initialized, since GDB uses
72 # the enclosing type to decide how many bytes to read. Members from
73 # the derived class were garbage, from GDB's address space.
74 gdb_test "print * (B *) a" ".* = {<A> = {a = 42}, b = 1729}" \
75 "cast base class pointer to derived class pointer"
76
77 # Check also that we get the same results from letting the compiler do
78 # the dereference.
79 gdb_test "print * b" ".* = {<A> = {a = 42}, b = 1729}" \
80 "let compiler cast base class pointer to derived class pointer"
81
82 # Check upcasting (it is trivial but still).
83 gdb_test "print * (A *) b" ".* = {a = 42}" \
84 "cast derived class pointer to base class pointer"
85
86 # Casting References.
87 # Check upcasting.
88 gdb_test "print (A &) br" ".* = .A &.* {a = 42}" \
89 "cast derived class reference to base class reference"
90
91 # Check downcasting.
92 gdb_test "print (B &) ar" ".* = .B.* {<A> = {a = 42}, b = 1729}" \
93 "cast base class reference to derived class reference"
94
95 # Check compiler casting
96 gdb_test "print br" ".* = .B.* {<A> = {a = 42}, b = 1729}" \
97 "let compiler cast base class reference to derived class reference"
98
99
100 # A few basic tests of "new" casts.
101
102 gdb_test "print const_cast<const B *> (b)" " = \\(const B \\*\\) $hex" \
103 "basic test of const_cast"
104
105 gdb_test "print const_cast<void *> (0)" " = \\(void \\*\\) 0x0" \
106 "const_cast of 0"
107
108 gdb_test "print static_cast<A *> (b)" " = \\(A \\*\\) $hex" \
109 "basic test of static_cast"
110
111 gdb_test "print static_cast<A &> (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \
112 "static_cast to reference type"
113
114 gdb_test "print reinterpret_cast<A *> (b)" " = \\(A \\*\\) $hex" \
115 "basic test of reinterpret_cast"
116
117 gdb_test "print reinterpret_cast<void> (b)" "Invalid reinterpret_cast" \
118 "test invalid reinterpret_cast"
119
120 gdb_test "print reinterpret_cast<A &> (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \
121 "reinterpret_cast to reference type"
122
123 # Tests of dynamic_cast.
124
125 set nonzero_hex "0x\[0-9A-Fa-f\]\[0-9A-Fa-f\]+"
126
127 gdb_test "print dynamic_cast<void> (a)" \
128 ".*must be a pointer or reference type" \
129 "invalid dynamic_cast"
130
131 gdb_test "print dynamic_cast<void *> (0)" \
132 " = \\(void \\*\\) 0x0" \
133 "dynamic_cast of 0 to void*"
134
135 gdb_test "print dynamic_cast<Alpha *> (&derived)" \
136 " = \\(Alpha \\*\\) $nonzero_hex" \
137 "dynamic_cast simple upcast"
138
139 gdb_test "print dynamic_cast<Alpha *> (&doublyderived)" \
140 " = \\(Alpha \\*\\) $nonzero_hex" \
141 "dynamic_cast upcast to unique base"
142
143 gdb_test "print dynamic_cast<Alpha &> (derived)" \
144 " = \\(Alpha \\&\\) @$nonzero_hex: {.* = $nonzero_hex}" \
145 "dynamic_cast simple upcast to reference"
146
147 gdb_test "print dynamic_cast<Derived *> (ad)" \
148 " = \\(Derived \\*\\) $nonzero_hex" \
149 "dynamic_cast simple downcast"
150
151 gdb_test "print dynamic_cast<VirtuallyDerived *> (add)" \
152 " = \\(VirtuallyDerived \\*\\) $nonzero_hex" \
153 "dynamic_cast simple downcast to intermediate class"
154
155 gdb_test "print dynamic_cast<VirtuallyDerived *> (ad)" \
156 " = \\(VirtuallyDerived \\*\\) 0x0" \
157 "dynamic_cast to non-existing base"
158
159 gdb_test "print dynamic_cast<VirtuallyDerived &> (*ad)" \
160 "dynamic_cast failed" \
161 "dynamic_cast to reference to non-existing base"
162
163 gdb_test "print dynamic_cast<DoublyDerived *> (add)" \
164 " = \\(DoublyDerived \\*\\) $nonzero_hex" \
165 "dynamic_cast unique downcast"
166
167 gdb_test "print dynamic_cast<Gamma *> (add)" \
168 " = \\(Gamma \\*\\) $nonzero_hex" \
169 "dynamic_cast to sibling"
This page took 0.033599 seconds and 5 git commands to generate.