OSDN Git Service

2003-08-22 Michael Chastain <mec@shout.net>
[pf3gnuchains/pf3gnuchains4x.git] / gdb / testsuite / gdb.cp / casts.exp
1 # Copyright 2002, 2003 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 2 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 # This file is part of the gdb testsuite
21
22 # Test casting, especially between class types or pointer-to-class
23 # types.
24
25 # This file is part of the gdb testsuite
26
27 if $tracelevel then {
28         strace $tracelevel
29         }
30
31 #
32 # test running programs
33 #
34
35 set prms_id 0
36 set bug_id 0
37
38 if { [skip_cplus_tests] } { continue }
39
40 set testfile "casts"
41 set srcfile ${testfile}.cc
42 set binfile ${objdir}/${subdir}/${testfile}
43
44 if [get_compiler_info ${binfile} "c++"] {
45     return -1;
46 }
47
48 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
49      gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
50 }
51
52
53 gdb_exit
54 gdb_start
55 gdb_reinitialize_dir $srcdir/$subdir
56 gdb_load ${binfile}
57
58 if ![runto_main] then {
59     perror "couldn't run to breakpoint"
60     continue
61 }
62
63 gdb_test "break [gdb_get_line_number "casts.exp: 1"]" \
64     "Breakpoint.*at.* file .*" \
65     ""
66
67 gdb_test "continue" "Breakpoint .* at .*casts.cc.*" ""
68
69 # Casting a pointer to a base class to a pointer to a derived class
70 # should yield the entire derived class.  Until August 2002, GDB got
71 # the enclosing type on `(B *) a' wrong: while the value's static type
72 # was `B *', as it should be, the enclosing type (which is supposed to
73 # be the dynamic type) was `A *'.  It's senseless to have a static
74 # type derived from the dynamic type; it should be the other way
75 # 'round.  Dereferencing this oddly typed pointer yielded a value in
76 # which only the base class's members were initialized, since GDB uses
77 # the enclosing type to decide how many bytes to read.  Members from
78 # the derived class were garbage, from GDB's address space.
79 gdb_test "print * (B *) a" ".* = {<A> = {a = 42}, b = 1729}" \
80     "cast base class pointer to derived class pointer"
81
82 # Check also that we get the same results from letting the compiler do
83 # the dereference.
84 gdb_test "print * b" ".* = {<A> = {a = 42}, b = 1729}" \
85     "let compiler cast base class pointer to derived class pointer"