tizen 2.4 release
[external/binutils.git] / gdb / testsuite / gdb.cp / derivation.exp
1 # Copyright 1998-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 was written by Elena Zannoni (ezannoni@cygnus.com)
17 # And rewritten by Michael Chastain <mec.gnu@mindspring.com>
18
19 # This file is part of the gdb testsuite
20
21 # tests for inheritance, with several derivations types combinations
22 # (private, public, protected) 
23 # classes have simple members and member functions.
24
25 set ws "\[\r\n\t \]+"
26 set nl "\[\r\n\]+"
27
28 # Start program.
29
30
31 if { [skip_cplus_tests] } { continue }
32
33 load_lib "cp-support.exp"
34
35 standard_testfile derivation.cc derivation2.cc
36
37 if {[prepare_for_testing $testfile.exp $testfile \
38         [list $srcfile $srcfile2] {debug c++}]} {
39     return -1
40 }
41
42 # Check inheritance of typedefs.
43 with_test_prefix "before run" {
44     foreach klass {"A" "D" "E" "F" "A2" "D2"} {
45         gdb_test "ptype ${klass}::value_type" "type = int"
46         gdb_test "whatis ${klass}::value_type" "type = int"
47         gdb_test "p (${klass}::value_type) 0" " = 0"
48     }
49     foreach klass {"Z" "ZZ"} {
50         gdb_test "ptype ${klass}::value_type" "type = float"
51         gdb_test "whatis ${klass}::value_type" "type = float"
52         gdb_test "p (${klass}::value_type) 0" " = 0"
53     }
54 }
55
56 # Set it up at a breakpoint so we can play with the variable values.
57
58 if ![runto 'marker1'] then {
59     perror "couldn't run to marker1"
60     continue
61 }
62
63 # Check inheritance of typedefs again, but this time with an active block.
64 with_test_prefix "at marker1" {
65     foreach klass {"A" "D" "A2" "D2"} {
66         gdb_test "ptype ${klass}::value_type" "type = int"
67         gdb_test "whatis ${klass}::value_type" "type = int"
68         gdb_test "p (${klass}::value_type) 0" " = 0"
69     }
70 }
71
72 gdb_test "up" ".*main.*" "up from marker1"
73
74 # Print class types and values.
75
76 # class A
77
78 gdb_test "print a_instance" "\\$\[0-9\]+ = \{a = 1, aa = 2\}" "print value of a_instance"
79
80 cp_test_ptype_class \
81     "a_instance" "" "class" "A" \
82     {
83         { field  public "A::value_type a;" }
84         { field  public "A::value_type aa;" }
85         { method public "A();" }
86         { method public "A::value_type afoo();" }
87         { method public "A::value_type foo();" }
88         { typedef public "typedef int value_type;" }
89     }
90
91 # class D
92
93 gdb_test_multiple "print d_instance" "print value of d_instance" {
94     -re "\\$\[0-9\]+ = \{<(class A|A)> = \{a = 1, aa = 2\}, <(class B|B)> = \{b = 3, bb = 4\}, <(class C|C)> = \{c = 5, cc = 6\}, d = 7, dd = 8\}$nl$gdb_prompt $" {
95         pass "print value of d_instance"
96     }
97 }
98
99 cp_test_ptype_class \
100     "d_instance" "" "class" "D" \
101     {
102         { base          "private A" }
103         { base          "public B" }
104         { base          "protected C" }
105         { field  public "A::value_type d;" }
106         { field  public "A::value_type dd;" }
107         { method public "D();" }
108         { method public "A::value_type dfoo();" }
109         { method public "A::value_type foo();" }
110     } \
111     "" \
112     {
113         { "protected C" "private C" { setup_xfail "*-*-*" "gcc/13539" } }
114     }
115
116 # class E
117
118 gdb_test_multiple "print e_instance" "print value of e_instance" {
119     -re "\\$\[0-9\]+ = \{<(class A|A)> = \{a = 1, aa = 2\}, <(class B|B)> = \{b = 3, bb = 4\}, <(class C|C)> = \{c = 5, cc = 6\}, e = 9, ee = 10\}$nl$gdb_prompt $" {
120         pass "print value of e_instance"
121     }
122 }
123
124 cp_test_ptype_class \
125     "e_instance" "" "class" "E" \
126     {
127         { base          "public A" }
128         { base          "private B" }
129         { base          "protected C" }
130         { field  public "A::value_type e;" }
131         { field  public "A::value_type ee;" }
132         { method public "E();" }
133         { method public "A::value_type efoo();" }
134         { method public "A::value_type foo();" }
135     } \
136     "" \
137     {
138         { "protected C" "private C" { setup_xfail "*-*-*" "gcc/13539" } }
139     }
140
141 # class F
142
143 gdb_test_multiple "print f_instance" "print value of f_instance" {
144     -re "\\$\[0-9\]+ = \{<(class A|A)> = \{a = 1, aa = 2\}, <(class B|B)> = \{b = 3, bb = 4\}, <(class C|C)> = \{c = 5, cc = 6\}, f = 11, ff = 12\}$nl$gdb_prompt $" {
145         pass "print value of f_instance"
146     }
147 }
148
149 cp_test_ptype_class \
150     "f_instance" "" "class" "F" \
151     {
152         { base          "private A" }
153         { base          "public B" }
154         { base          "private C" }
155         { field  public "A::value_type f;" }
156         { field  public "A::value_type ff;" }
157         { method public "F();" }
158         { method public "A::value_type ffoo();" }
159         { method public "A::value_type foo();" }
160     }
161
162 # class G
163 cp_test_ptype_class \
164     "g_instance" "" "class" "G" \
165     {
166         { base          "private A" }
167         { base          "public B" }
168         { base          "protected C" }
169         { field public "int g;" }
170         { field public "int gg;" }
171         { field public "int a;" }
172         { field public "int b;" }
173         { field public "int c;" }
174         { method public "int gfoo();" }
175         { method public "int foo();" }
176     }
177
178 # Print individual fields.
179
180 gdb_test "print d_instance.a"  "\\$\[0-9\]+ = 1" "print value of d_instance.a"
181 gdb_test "print d_instance.aa" "\\$\[0-9\]+ = 2" "print value of d_instance.aa"
182 gdb_test "print d_instance.b"  "\\$\[0-9\]+ = 3" "print value of d_instance.b"
183 gdb_test "print d_instance.bb" "\\$\[0-9\]+ = 4" "print value of d_instance.bb"
184 gdb_test "print d_instance.c"  "\\$\[0-9\]+ = 5" "print value of d_instance.c"
185 gdb_test "print d_instance.cc" "\\$\[0-9\]+ = 6" "print value of d_instance.cc"
186 gdb_test "print d_instance.d"  "\\$\[0-9\]+ = 7" "print value of d_instance.d"
187 gdb_test "print d_instance.dd" "\\$\[0-9\]+ = 8" "print value of d_instance.dd"
188
189 # Print some fields which are defined in the top of class G
190 # and in its base classes.  This is not be ambiguous.
191
192 gdb_test "print g_instance.a"  "\\$\[0-9\]+ = 15" "print value of g_instance.a"
193 gdb_test "print g_instance.b"  "\\$\[0-9\]+ = 16" "print value of g_instance.b"
194 gdb_test "print g_instance.c"  "\\$\[0-9\]+ = 17" "print value of g_instance.c"
195
196 # Print a function call.
197
198 gdb_test "print g_instance.afoo()" "\\$\[0-9\]+ = 1" "print value of g_instance.afoo()"
199
200 # If GDB fails to restore the selected frame properly after the
201 # inferior function call above (see GDB PR 1155 for an explanation of
202 # why this might happen), all the subsequent tests will fail.  We
203 # should detect report that failure, but let the marker call finish so
204 # that the rest of the tests can run undisturbed.
205
206 gdb_test_multiple "frame" "re-selected 'main' frame after inferior call" {
207     -re "#0  marker1.*$gdb_prompt $" {
208         setup_kfail "gdb/1155" s390-*-linux-gnu
209         fail "re-selected 'main' frame after inferior call"
210         gdb_test "finish" ".*main.*at .*derivation.cc:.*// marker1-returns-here.*" \
211             "finish call to marker1"
212     }
213     -re "#1  ($hex in )?main.*$gdb_prompt $" {
214         pass "re-selected 'main' frame after inferior call"
215     }
216 }
217
218 gdb_test "print g_instance.bfoo()" "\\$\[0-9\]+ = 2" "print value of g_instance.bfoo()"
219 gdb_test "print g_instance.cfoo()" "\\$\[0-9\]+ = 3" "print value of g_instance.cfoo()"
220
221 # Check typedefs of fields
222 foreach Klass {"C" "G"} {
223     set klass [string tolower $Klass]
224     set instance "${klass}_instance"
225     set var "${instance}.$klass"
226     gdb_test "whatis $var" "int"
227     gdb_test "ptype $var" "int"
228 }
229
230 foreach Klass {"A" "B" "D" "E" "F"} {
231     set klass [string tolower $Klass]
232     set instance "${klass}_instance"
233     set var "${instance}.$klass"
234     gdb_test "whatis $var" "A::value_type"
235     gdb_test "ptype $var" "int"
236     if {![string equal $Klass "B"]} {
237         gdb_test "p (${Klass}::value_type) 0" " = 0"
238     }
239 }
240
241 foreach Klass {"Z" "ZZ"} {
242     set klass [string tolower $Klass]
243     set instance "${klass}_instance"
244     set var "${instance}.$klass"
245     gdb_test "whatis $var" "Z::value_type"
246     gdb_test "ptype $var" "float"
247     gdb_test "p (${Klass}::value_type) 0" " = 0"
248 }
249
250 # This is a regression test for a bug that caused a crash when trying
251 # to print the vtbl pointer.  We don't care about the output so much
252 # here (it is tested elsewhere), just that gdb doesn't crash.  We test
253 # "ptype" first because, before the gdb fix, that was the only code
254 # path calling get_vptr_fieldno.
255 gdb_test "ptype vderived" "type = .*"
256 gdb_test "print vderived" " = {.* inter = 0.*x = 0}"
257
258 # Test whether inheritance of typedefs is properly
259 # reported when stopped.
260 gdb_test "ptype N::value_type" "type = double"
261 gdb_test "ptype N::Derived::value_type" "type = int"
262
263 # Now run to N::Derived::doit and get the type of "value_type"
264 if {![runto "N::Derived::doit"]} {
265     perror "couldn't run to N::Derived::doit"
266     continue
267 }
268
269 gdb_test "ptype value_type" "type = int"