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