* gnu-v3-abi.c (struct value_and_voffset): New.
[external/binutils.git] / gdb / testsuite / gdb.cp / virtfunc.exp
1 # Copyright 1992, 1994-1999, 2001-2004, 2006-2012 Free Software
2 # 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 <http://www.gnu.org/licenses/>.
16
17 # This file was written by Fred Fish. (fnf@cygnus.com)
18 # And rewritten by Michael Chastain <mec.gnu@mindspring.com>.
19
20 set nl          "\[\r\n\]+"
21
22 if { [skip_cplus_tests] } { continue }
23
24 load_lib "cp-support.exp"
25
26 set testfile "virtfunc"
27 set srcfile ${testfile}.cc
28 set binfile ${objdir}/${subdir}/${testfile}
29
30 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {c++ debug}] != "" } {
31      untested virtfunc.exp
32      return -1
33 }
34
35 proc test_ptype_of_classes {} {
36
37     # class VA
38
39     cp_test_ptype_class \
40         "ptype VA" "" "class" "VA" \
41         {
42             { field public "int va;" }
43         }
44
45     # class VB
46
47     cp_test_ptype_class \
48         "ptype VB" "" "class" "VB" \
49         {
50             { field  public "int vb;" }
51             { method public "int fvb();" }
52             { method public "virtual int vvb();" }
53         }
54
55     # class V
56
57     cp_test_ptype_class \
58         "ptype V" "" "class" "V" \
59         {
60             { base          "public VA" }
61             { base          "public VB" }
62             { field  public "int w;" }
63             { method public "int f();" }
64             { method public "virtual int vv();" }
65         }
66
67     # class A
68
69     cp_test_ptype_class \
70         "ptype A" "" "class" "A" \
71         {
72             { base           "public virtual V" }
73             { vbase          "V" }
74             { field  private "int a;" }
75             { method public  "virtual int f();" }
76         }
77
78     # class B
79
80     cp_test_ptype_class \
81         "ptype B" "" "class" "B" \
82         {
83             { base           "public A" }
84             { field  private "int b;" }
85             { method public  "virtual int f();" }
86         }
87
88     # class C
89
90     cp_test_ptype_class \
91         "ptype C" "" "class" "C" \
92         {
93             { base         "public virtual V" }
94             { vbase        "V" }
95             { field public "int c;" }
96         }
97
98     # class AD
99
100     cp_test_ptype_class \
101         "ptype AD" "" "class" "AD" \
102         {
103             { method public "virtual int vg();" }
104         }
105
106     # class D
107
108     cp_test_ptype_class \
109         "ptype D" "" "class" "D" \
110         {
111             { base          "public AD" }
112             { base          "public virtual V" }
113             { vbase         "V" }
114             { method public "static void s();" }
115             { method public "virtual int vg();" }
116             { method public "virtual int vd();" }
117             { method public "int fd();" }
118             { field  public "int d;" }
119         }
120
121     # class E
122
123     cp_test_ptype_class \
124         "ptype E" "" "class" "E" \
125         {
126             { base          "public B" }
127             { base          "public virtual V" }
128             { base          "public D" }
129             { base          "public C" }
130             { vbase         "V" }
131             { method public "virtual int f();" }
132             { method public "virtual int vg();" }
133             { method public "virtual int vv();" }
134             { field  public "int e;" }
135         }
136
137     # An instance of D
138
139     cp_test_ptype_class "ptype dd" "" "class" "D" ibid
140
141     # An instance of D *
142
143     cp_test_ptype_class "ptype ppd" "" "class" "D" ibid "*"
144
145     # An instance of AD *
146     # TODO: this should be named pADd, not pAd.
147
148     cp_test_ptype_class "ptype pAd" "" "class" "AD" ibid "*"
149
150     # Instances of these classes.
151
152     cp_test_ptype_class "ptype a" "" "class" "A" ibid
153     cp_test_ptype_class "ptype b" "" "class" "B" ibid
154     cp_test_ptype_class "ptype c" "" "class" "C" ibid
155     cp_test_ptype_class "ptype d" "" "class" "D" ibid
156     cp_test_ptype_class "ptype e" "" "class" "E" ibid
157     cp_test_ptype_class "ptype v" "" "class" "V" ibid
158     cp_test_ptype_class "ptype vb" "" "class" "VB" ibid
159
160     # Instances of pointers to these classes.
161
162     cp_test_ptype_class "ptype pAa" "" "class" "A" ibid "*"
163     cp_test_ptype_class "ptype pAe" "" "class" "A" ibid "*"
164     cp_test_ptype_class "ptype pBe" "" "class" "B" ibid "*"
165     cp_test_ptype_class "ptype pDd" "" "class" "D" ibid "*"
166     cp_test_ptype_class "ptype pDe" "" "class" "D" ibid "*"
167     cp_test_ptype_class "ptype pVa" "" "class" "V" ibid "*"
168     cp_test_ptype_class "ptype pVv" "" "class" "V" ibid "*"
169     cp_test_ptype_class "ptype pVe" "" "class" "V" ibid "*"
170     cp_test_ptype_class "ptype pVd" "" "class" "V" ibid "*"
171     cp_test_ptype_class "ptype pADe" "" "class" "AD" ibid "*"
172     cp_test_ptype_class "ptype pEe" "" "class" "E" ibid "*"
173     cp_test_ptype_class "ptype pVB" "" "class" "VB" ibid "*"
174
175 }
176
177 # Call virtual functions.
178
179 proc test_virtual_calls {} {
180     global gdb_prompt
181     global nl
182
183     if [target_info exists gdb,cannot_call_functions] {
184         setup_xfail "*-*-*" 2416
185         fail "This target can not call functions"
186         return 0
187     }
188
189     gdb_test "print pAe->f()"   "\\$\[0-9\]+ = 20"
190     gdb_test "print pAa->f()"   "\\$\[0-9\]+ = 1"
191     gdb_test "print pDe->vg()"  "\\$\[0-9\]+ = 202"
192     gdb_test "print pADe->vg()" "\\$\[0-9\]+ = 202"
193     gdb_test "print pDd->vg()"  "\\$\[0-9\]+ = 101"
194     gdb_test "print pEe->vvb()" "\\$\[0-9\]+ = 411"
195     gdb_test "print pVB->vvb()" "\\$\[0-9\]+ = 407"
196     gdb_test "print pBe->vvb()" "\\$\[0-9\]+ = 411"
197     gdb_test "print pDe->vvb()" "\\$\[0-9\]+ = 411"
198     gdb_test "print pEe->vd()"  "\\$\[0-9\]+ = 282"
199     gdb_test "print pEe->fvb()" "\\$\[0-9\]+ = 311"
200
201     # more recent results:
202     # wrong value "202"
203     #   gcc 2.95.3 -gdwarf-2
204     #   gcc 2.95.3 -gstabs+
205     # attempt to take addres of value not located in memory
206     #   gcc 3.3.2 -gdwarf-2
207     #   gcc 3.3.2 -gstabs+
208     #
209     # -- chastain 2003-12-31
210
211     gdb_test_multiple "print pEe->D::vg()" "print pEe->D::vg()" {
212         -re "\\$\[0-9]+ = 102$nl$gdb_prompt $" {
213             pass "print pEe->D::vg()"
214         }
215         -re "\\$\[0-9]+ = 202$nl$gdb_prompt $" {
216             # To get this result, we have called pEe->*(&D::vg) ().
217             # That's how GDB interprets this, but it's wrong; in fact
218             # the explicit D:: means to bypass virtual function lookup,
219             # and call D::vg as if it were non-virtual.  We still have
220             # to e.g. adjust "this", though.
221             kfail "gdb/1064" "print pEe->D::vg()"
222         }
223         -re "Attempt to take address of value not located in memory.$nl$gdb_prompt $" {
224             kfail "gdb/1064" "print pEe->D::vg()"
225         }
226     }
227 }
228
229 # A helper proc that creates a regular expression matching a
230 # particular vtable.  NAME is the type name.  Each element of ARGS is
231 # the name of a function in the vtable.
232
233 proc make_one_vtable_result {name args} {
234     global hex
235
236     set nls "\[\r\n\]+"
237
238     set result "vtable for '${name}' @ $hex .subobject @ $hex.:$nls"
239     set count 0
240     foreach func $args {
241         append result ".${count}.: $hex <$func..>${nls}"
242         incr count
243     }
244
245     return $result
246 }
247
248 # Test "info vtbl".
249
250 proc test_info_vtbl {} {
251     global hex
252
253     set nls "\[\r\n\]+"
254
255     set vt_A [make_one_vtable_result A A::f]
256     set vt_B [make_one_vtable_result B B::f]
257     set vt_V [make_one_vtable_result V VB::vvb V::vv]
258     set vt_V2 [make_one_vtable_result V VB::vvb "virtual thunk to E::vv"]
259     set vt_D [make_one_vtable_result D D::vg D::vd]
260     set vt_D2 [make_one_vtable_result D "non-virtual thunk to E::vg" D::vd]
261     set vt_E [make_one_vtable_result E E::f E::vg E::vv]
262
263     gdb_test "info vtbl a" "${vt_A}${vt_V}"
264     gdb_test "info vtbl b" "${vt_B}${vt_V}"
265     gdb_test "info vtbl c" "${vt_V}"
266     gdb_test "info vtbl d" "${vt_D}${vt_V}"
267     gdb_test "info vtbl e" "${vt_E}${vt_D2}${vt_V2}"
268     gdb_test "info vtbl pEe" "${vt_E}${vt_D2}${vt_V2}"
269
270     gdb_test "info vtbl" "Argument required.*"
271     gdb_test "info vtbl va" \
272         "This object does not have a virtual function table.*"
273     gdb_test "info vtbl all_count" \
274         "This object does not have a virtual function table.*"
275 }
276
277 proc do_tests {} {
278     global srcdir subdir binfile
279     global gdb_prompt
280
281
282     gdb_exit
283     gdb_start
284     gdb_reinitialize_dir $srcdir/$subdir
285     gdb_load $binfile
286
287     gdb_test_no_output "set language c++" ""
288     gdb_test_no_output "set width 0" ""
289
290     if ![runto_main] then {
291         perror "couldn't run to breakpoint"
292         return
293     }
294     test_ptype_of_classes
295     test_info_vtbl
296
297     gdb_breakpoint test_calls
298     gdb_test "continue" ".*Breakpoint .* test_calls.*" ""
299     test_virtual_calls
300
301     gdb_test "next" ".*pAa->f.*" "next to pAa->f call"
302     gdb_test "next" ".*pDe->vg.*" "next to pDe->vg call"
303     gdb_test "step" ".*E::vg.*" "step through thunk into E::vg"
304 }
305
306 do_tests