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