Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright 2002-2020 Free Software Foundation, Inc. |
10abb1d4 JB |
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 | |
e22f8b7c | 5 | # the Free Software Foundation; either version 3 of the License, or |
10abb1d4 | 6 | # (at your option) any later version. |
e22f8b7c | 7 | # |
10abb1d4 JB |
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. | |
e22f8b7c | 12 | # |
10abb1d4 | 13 | # You should have received a copy of the GNU General Public License |
e22f8b7c | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
10abb1d4 | 15 | |
10abb1d4 JB |
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 | ||
10abb1d4 JB |
23 | # |
24 | # test running programs | |
25 | # | |
26 | ||
10abb1d4 JB |
27 | |
28 | if { [skip_cplus_tests] } { continue } | |
29 | ||
d41a5c09 | 30 | standard_testfile .cc casts03.cc |
10abb1d4 | 31 | |
4c93b1db | 32 | if [get_compiler_info "c++"] { |
ae59b1da | 33 | return -1 |
10abb1d4 JB |
34 | } |
35 | ||
5b362f04 | 36 | if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} { |
f5f3a911 | 37 | return -1 |
10abb1d4 JB |
38 | } |
39 | ||
6eac95e3 CV |
40 | if ![runto_main] then { |
41 | perror "couldn't run to breakpoint" | |
42 | continue | |
43 | } | |
44 | ||
775e0f04 YQ |
45 | # Prevent symbol on address 0x0 being printed. |
46 | gdb_test_no_output "set print symbol off" | |
47 | ||
10abb1d4 JB |
48 | gdb_test "break [gdb_get_line_number "casts.exp: 1"]" \ |
49 | "Breakpoint.*at.* file .*" \ | |
50 | "" | |
51 | ||
6eac95e3 | 52 | gdb_test "continue" "Breakpoint .* at .*casts.cc.*" "" |
10abb1d4 JB |
53 | |
54 | # Casting a pointer to a base class to a pointer to a derived class | |
55 | # should yield the entire derived class. Until August 2002, GDB got | |
56 | # the enclosing type on `(B *) a' wrong: while the value's static type | |
57 | # was `B *', as it should be, the enclosing type (which is supposed to | |
58 | # be the dynamic type) was `A *'. It's senseless to have a static | |
59 | # type derived from the dynamic type; it should be the other way | |
60 | # 'round. Dereferencing this oddly typed pointer yielded a value in | |
61 | # which only the base class's members were initialized, since GDB uses | |
62 | # the enclosing type to decide how many bytes to read. Members from | |
63 | # the derived class were garbage, from GDB's address space. | |
64 | gdb_test "print * (B *) a" ".* = {<A> = {a = 42}, b = 1729}" \ | |
65 | "cast base class pointer to derived class pointer" | |
66 | ||
67 | # Check also that we get the same results from letting the compiler do | |
68 | # the dereference. | |
69 | gdb_test "print * b" ".* = {<A> = {a = 42}, b = 1729}" \ | |
70 | "let compiler cast base class pointer to derived class pointer" | |
f0050c20 AR |
71 | |
72 | # Check upcasting (it is trivial but still). | |
73 | gdb_test "print * (A *) b" ".* = {a = 42}" \ | |
74 | "cast derived class pointer to base class pointer" | |
75 | ||
76 | # Casting References. | |
77 | # Check upcasting. | |
78 | gdb_test "print (A &) br" ".* = .A &.* {a = 42}" \ | |
79 | "cast derived class reference to base class reference" | |
80 | ||
81 | # Check downcasting. | |
82 | gdb_test "print (B &) ar" ".* = .B.* {<A> = {a = 42}, b = 1729}" \ | |
83 | "cast base class reference to derived class reference" | |
84 | ||
85 | # Check compiler casting | |
86 | gdb_test "print br" ".* = .B.* {<A> = {a = 42}, b = 1729}" \ | |
87 | "let compiler cast base class reference to derived class reference" | |
4e8f195d TT |
88 | |
89 | ||
90 | # A few basic tests of "new" casts. | |
91 | ||
92 | gdb_test "print const_cast<const B *> (b)" " = \\(const B \\*\\) $hex" \ | |
93 | "basic test of const_cast" | |
94 | ||
95 | gdb_test "print const_cast<void *> (0)" " = \\(void \\*\\) 0x0" \ | |
96 | "const_cast of 0" | |
97 | ||
98 | gdb_test "print static_cast<A *> (b)" " = \\(A \\*\\) $hex" \ | |
99 | "basic test of static_cast" | |
100 | ||
101 | gdb_test "print static_cast<A &> (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \ | |
102 | "static_cast to reference type" | |
103 | ||
104 | gdb_test "print reinterpret_cast<A *> (b)" " = \\(A \\*\\) $hex" \ | |
105 | "basic test of reinterpret_cast" | |
106 | ||
107 | gdb_test "print reinterpret_cast<void> (b)" "Invalid reinterpret_cast" \ | |
108 | "test invalid reinterpret_cast" | |
109 | ||
110 | gdb_test "print reinterpret_cast<A &> (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \ | |
111 | "reinterpret_cast to reference type" | |
112 | ||
608b4967 TT |
113 | # Basic tests using typeof. |
114 | ||
115 | foreach opname {__typeof__ __typeof __decltype} { | |
116 | gdb_test "print (${opname}(a)) (b)" " = \\(A \\*\\) $hex" \ | |
117 | "old-style cast using $opname" | |
118 | ||
119 | gdb_test "print static_cast<${opname}(a)> (b)" " = \\(A \\*\\) $hex" \ | |
120 | "static_cast using $opname" | |
121 | ||
122 | gdb_test "print reinterpret_cast<${opname}(a)> (b)" " = \\(A \\*\\) $hex" \ | |
123 | "reinterpret_cast using $opname" | |
124 | } | |
125 | ||
126 | gdb_test "whatis __decltype(*a)" "type = A \\&" | |
127 | ||
4e8f195d TT |
128 | # Tests of dynamic_cast. |
129 | ||
130 | set nonzero_hex "0x\[0-9A-Fa-f\]\[0-9A-Fa-f\]+" | |
131 | ||
132 | gdb_test "print dynamic_cast<void> (a)" \ | |
133 | ".*must be a pointer or reference type" \ | |
134 | "invalid dynamic_cast" | |
135 | ||
136 | gdb_test "print dynamic_cast<void *> (0)" \ | |
137 | " = \\(void \\*\\) 0x0" \ | |
138 | "dynamic_cast of 0 to void*" | |
139 | ||
140 | gdb_test "print dynamic_cast<Alpha *> (&derived)" \ | |
141 | " = \\(Alpha \\*\\) $nonzero_hex" \ | |
142 | "dynamic_cast simple upcast" | |
143 | ||
144 | gdb_test "print dynamic_cast<Alpha *> (&doublyderived)" \ | |
145 | " = \\(Alpha \\*\\) $nonzero_hex" \ | |
146 | "dynamic_cast upcast to unique base" | |
147 | ||
148 | gdb_test "print dynamic_cast<Alpha &> (derived)" \ | |
410528f0 | 149 | " = \\(Alpha \\&\\) @$nonzero_hex: {.* = ${nonzero_hex}( <vtable for Derived.*>)?}" \ |
4e8f195d TT |
150 | "dynamic_cast simple upcast to reference" |
151 | ||
152 | gdb_test "print dynamic_cast<Derived *> (ad)" \ | |
9cb709b6 | 153 | " = \\(Derived \\*\\) ${nonzero_hex}( <vtable for Derived.*>)?" \ |
4e8f195d TT |
154 | "dynamic_cast simple downcast" |
155 | ||
156 | gdb_test "print dynamic_cast<VirtuallyDerived *> (add)" \ | |
157 | " = \\(VirtuallyDerived \\*\\) $nonzero_hex" \ | |
158 | "dynamic_cast simple downcast to intermediate class" | |
159 | ||
160 | gdb_test "print dynamic_cast<VirtuallyDerived *> (ad)" \ | |
161 | " = \\(VirtuallyDerived \\*\\) 0x0" \ | |
162 | "dynamic_cast to non-existing base" | |
163 | ||
164 | gdb_test "print dynamic_cast<VirtuallyDerived &> (*ad)" \ | |
165 | "dynamic_cast failed" \ | |
166 | "dynamic_cast to reference to non-existing base" | |
167 | ||
168 | gdb_test "print dynamic_cast<DoublyDerived *> (add)" \ | |
9cb709b6 | 169 | " = \\(DoublyDerived \\*\\) ${nonzero_hex}( <vtable for DoublyDerived.*>)?" \ |
4e8f195d TT |
170 | "dynamic_cast unique downcast" |
171 | ||
172 | gdb_test "print dynamic_cast<Gamma *> (add)" \ | |
173 | " = \\(Gamma \\*\\) $nonzero_hex" \ | |
174 | "dynamic_cast to sibling" | |
d41a5c09 | 175 | |
5b362f04 | 176 | if {[prepare_for_testing "failed to prepare" ${testfile}03 $srcfile2 \ |
d41a5c09 JK |
177 | {debug c++ additional_flags=-std=c++03}]} { |
178 | return -1 | |
179 | } | |
180 | ||
181 | if ![runto_main] then { | |
182 | perror "couldn't run to breakpoint" | |
183 | continue | |
184 | } | |
185 | ||
186 | # Prevent symbol on address 0x0 being printed. | |
187 | gdb_test_no_output "set print symbol off" | |
188 | ||
189 | gdb_breakpoint [gdb_get_line_number "casts.exp: 1" $srcfile2] | |
190 | gdb_continue_to_breakpoint "end of casts03" | |
191 | ||
192 | # Test that keyword shadowing works. | |
193 | ||
194 | gdb_test "whatis decltype(5)" " = double" |