1 |
227 |
jeremybenn |
# Copyright 2002, 2003, 2004, 2007, 2008, 2009, 2010
|
2 |
|
|
# Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
# This program is free software; you can redistribute it and/or modify
|
5 |
|
|
# it under the terms of the GNU General Public License as published by
|
6 |
|
|
# the Free Software Foundation; either version 3 of the License, or
|
7 |
|
|
# (at your option) any later version.
|
8 |
|
|
#
|
9 |
|
|
# This program is distributed in the hope that it will be useful,
|
10 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
|
|
# GNU General Public License for more details.
|
13 |
|
|
#
|
14 |
|
|
# You should have received a copy of the GNU General Public License
|
15 |
|
|
# along with this program. If not, see .
|
16 |
|
|
|
17 |
|
|
# This file is part of the gdb testsuite
|
18 |
|
|
|
19 |
|
|
# Test casting, especially between class types or pointer-to-class
|
20 |
|
|
# types.
|
21 |
|
|
|
22 |
|
|
# This file is part of the gdb testsuite
|
23 |
|
|
|
24 |
|
|
if $tracelevel then {
|
25 |
|
|
strace $tracelevel
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
#
|
29 |
|
|
# test running programs
|
30 |
|
|
#
|
31 |
|
|
|
32 |
|
|
set prms_id 0
|
33 |
|
|
set bug_id 0
|
34 |
|
|
|
35 |
|
|
if { [skip_cplus_tests] } { continue }
|
36 |
|
|
|
37 |
|
|
set testfile "casts"
|
38 |
|
|
set srcfile ${testfile}.cc
|
39 |
|
|
set binfile ${objdir}/${subdir}/${testfile}
|
40 |
|
|
|
41 |
|
|
if [get_compiler_info ${binfile} "c++"] {
|
42 |
|
|
return -1;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
|
46 |
|
|
untested casts.exp
|
47 |
|
|
return -1
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
gdb_exit
|
52 |
|
|
gdb_start
|
53 |
|
|
gdb_reinitialize_dir $srcdir/$subdir
|
54 |
|
|
gdb_load ${binfile}
|
55 |
|
|
|
56 |
|
|
if ![runto_main] then {
|
57 |
|
|
perror "couldn't run to breakpoint"
|
58 |
|
|
continue
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
gdb_test "break [gdb_get_line_number "casts.exp: 1"]" \
|
62 |
|
|
"Breakpoint.*at.* file .*" \
|
63 |
|
|
""
|
64 |
|
|
|
65 |
|
|
gdb_test "continue" "Breakpoint .* at .*casts.cc.*" ""
|
66 |
|
|
|
67 |
|
|
# Casting a pointer to a base class to a pointer to a derived class
|
68 |
|
|
# should yield the entire derived class. Until August 2002, GDB got
|
69 |
|
|
# the enclosing type on `(B *) a' wrong: while the value's static type
|
70 |
|
|
# was `B *', as it should be, the enclosing type (which is supposed to
|
71 |
|
|
# be the dynamic type) was `A *'. It's senseless to have a static
|
72 |
|
|
# type derived from the dynamic type; it should be the other way
|
73 |
|
|
# 'round. Dereferencing this oddly typed pointer yielded a value in
|
74 |
|
|
# which only the base class's members were initialized, since GDB uses
|
75 |
|
|
# the enclosing type to decide how many bytes to read. Members from
|
76 |
|
|
# the derived class were garbage, from GDB's address space.
|
77 |
|
|
gdb_test "print * (B *) a" ".* = { = {a = 42}, b = 1729}" \
|
78 |
|
|
"cast base class pointer to derived class pointer"
|
79 |
|
|
|
80 |
|
|
# Check also that we get the same results from letting the compiler do
|
81 |
|
|
# the dereference.
|
82 |
|
|
gdb_test "print * b" ".* = { = {a = 42}, b = 1729}" \
|
83 |
|
|
"let compiler cast base class pointer to derived class pointer"
|
84 |
|
|
|
85 |
|
|
# Check upcasting (it is trivial but still).
|
86 |
|
|
gdb_test "print * (A *) b" ".* = {a = 42}" \
|
87 |
|
|
"cast derived class pointer to base class pointer"
|
88 |
|
|
|
89 |
|
|
# Casting References.
|
90 |
|
|
# Check upcasting.
|
91 |
|
|
gdb_test "print (A &) br" ".* = .A &.* {a = 42}" \
|
92 |
|
|
"cast derived class reference to base class reference"
|
93 |
|
|
|
94 |
|
|
# Check downcasting.
|
95 |
|
|
gdb_test "print (B &) ar" ".* = .B.* { = {a = 42}, b = 1729}" \
|
96 |
|
|
"cast base class reference to derived class reference"
|
97 |
|
|
|
98 |
|
|
# Check compiler casting
|
99 |
|
|
gdb_test "print br" ".* = .B.* { = {a = 42}, b = 1729}" \
|
100 |
|
|
"let compiler cast base class reference to derived class reference"
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
# A few basic tests of "new" casts.
|
104 |
|
|
|
105 |
|
|
gdb_test "print const_cast (b)" " = \\(const B \\*\\) $hex" \
|
106 |
|
|
"basic test of const_cast"
|
107 |
|
|
|
108 |
|
|
gdb_test "print const_cast (0)" " = \\(void \\*\\) 0x0" \
|
109 |
|
|
"const_cast of 0"
|
110 |
|
|
|
111 |
|
|
gdb_test "print static_cast (b)" " = \\(A \\*\\) $hex" \
|
112 |
|
|
"basic test of static_cast"
|
113 |
|
|
|
114 |
|
|
gdb_test "print static_cast (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \
|
115 |
|
|
"static_cast to reference type"
|
116 |
|
|
|
117 |
|
|
gdb_test "print reinterpret_cast (b)" " = \\(A \\*\\) $hex" \
|
118 |
|
|
"basic test of reinterpret_cast"
|
119 |
|
|
|
120 |
|
|
gdb_test "print reinterpret_cast (b)" "Invalid reinterpret_cast" \
|
121 |
|
|
"test invalid reinterpret_cast"
|
122 |
|
|
|
123 |
|
|
gdb_test "print reinterpret_cast (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \
|
124 |
|
|
"reinterpret_cast to reference type"
|
125 |
|
|
|
126 |
|
|
# Tests of dynamic_cast.
|
127 |
|
|
|
128 |
|
|
set nonzero_hex "0x\[0-9A-Fa-f\]\[0-9A-Fa-f\]+"
|
129 |
|
|
|
130 |
|
|
gdb_test "print dynamic_cast (a)" \
|
131 |
|
|
".*must be a pointer or reference type" \
|
132 |
|
|
"invalid dynamic_cast"
|
133 |
|
|
|
134 |
|
|
gdb_test "print dynamic_cast (0)" \
|
135 |
|
|
" = \\(void \\*\\) 0x0" \
|
136 |
|
|
"dynamic_cast of 0 to void*"
|
137 |
|
|
|
138 |
|
|
gdb_test "print dynamic_cast (&derived)" \
|
139 |
|
|
" = \\(Alpha \\*\\) $nonzero_hex" \
|
140 |
|
|
"dynamic_cast simple upcast"
|
141 |
|
|
|
142 |
|
|
gdb_test "print dynamic_cast (&doublyderived)" \
|
143 |
|
|
" = \\(Alpha \\*\\) $nonzero_hex" \
|
144 |
|
|
"dynamic_cast upcast to unique base"
|
145 |
|
|
|
146 |
|
|
gdb_test "print dynamic_cast (derived)" \
|
147 |
|
|
" = \\(Alpha \\&\\) @$nonzero_hex: {.* = $nonzero_hex}" \
|
148 |
|
|
"dynamic_cast simple upcast to reference"
|
149 |
|
|
|
150 |
|
|
gdb_test "print dynamic_cast (ad)" \
|
151 |
|
|
" = \\(Derived \\*\\) $nonzero_hex" \
|
152 |
|
|
"dynamic_cast simple downcast"
|
153 |
|
|
|
154 |
|
|
gdb_test "print dynamic_cast (add)" \
|
155 |
|
|
" = \\(VirtuallyDerived \\*\\) $nonzero_hex" \
|
156 |
|
|
"dynamic_cast simple downcast to intermediate class"
|
157 |
|
|
|
158 |
|
|
gdb_test "print dynamic_cast (ad)" \
|
159 |
|
|
" = \\(VirtuallyDerived \\*\\) 0x0" \
|
160 |
|
|
"dynamic_cast to non-existing base"
|
161 |
|
|
|
162 |
|
|
gdb_test "print dynamic_cast (*ad)" \
|
163 |
|
|
"dynamic_cast failed" \
|
164 |
|
|
"dynamic_cast to reference to non-existing base"
|
165 |
|
|
|
166 |
|
|
gdb_test "print dynamic_cast (add)" \
|
167 |
|
|
" = \\(DoublyDerived \\*\\) $nonzero_hex" \
|
168 |
|
|
"dynamic_cast unique downcast"
|
169 |
|
|
|
170 |
|
|
gdb_test "print dynamic_cast (add)" \
|
171 |
|
|
" = \\(Gamma \\*\\) $nonzero_hex" \
|
172 |
|
|
"dynamic_cast to sibling"
|