1# Copyright 2002, 2003, 2004, 2007 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 23if $tracelevel then { 24 strace $tracelevel 25 } 26 27# 28# test running programs 29# 30 31set prms_id 0 32set bug_id 0 33 34if { [skip_cplus_tests] } { continue } 35 36set testfile "casts" 37set srcfile ${testfile}.cc 38set binfile ${objdir}/${subdir}/${testfile} 39 40if [get_compiler_info ${binfile} "c++"] { 41 return -1; 42} 43 44if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } { 45 untested casts.exp 46 return -1 47} 48 49 50gdb_exit 51gdb_start 52gdb_reinitialize_dir $srcdir/$subdir 53gdb_load ${binfile} 54 55if ![runto_main] then { 56 perror "couldn't run to breakpoint" 57 continue 58} 59 60gdb_test "break [gdb_get_line_number "casts.exp: 1"]" \ 61 "Breakpoint.*at.* file .*" \ 62 "" 63 64gdb_test "continue" "Breakpoint .* at .*casts.cc.*" "" 65 66# Casting a pointer to a base class to a pointer to a derived class 67# should yield the entire derived class. Until August 2002, GDB got 68# the enclosing type on `(B *) a' wrong: while the value's static type 69# was `B *', as it should be, the enclosing type (which is supposed to 70# be the dynamic type) was `A *'. It's senseless to have a static 71# type derived from the dynamic type; it should be the other way 72# 'round. Dereferencing this oddly typed pointer yielded a value in 73# which only the base class's members were initialized, since GDB uses 74# the enclosing type to decide how many bytes to read. Members from 75# the derived class were garbage, from GDB's address space. 76gdb_test "print * (B *) a" ".* = {<A> = {a = 42}, b = 1729}" \ 77 "cast base class pointer to derived class pointer" 78 79# Check also that we get the same results from letting the compiler do 80# the dereference. 81gdb_test "print * b" ".* = {<A> = {a = 42}, b = 1729}" \ 82 "let compiler cast base class pointer to derived class pointer" 83