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