aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.cp/virtfunc.exp
blob: d412bc998b16ebd4f3ec2f460f807caca3305127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004,
# 2006, 2007, 2008, 2009 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file was written by Fred Fish. (fnf@cygnus.com)
# And rewritten by Michael Chastain <mec.gnu@mindspring.com>.

set nl		"\[\r\n\]+"

if $tracelevel then {
    strace $tracelevel
}

if { [skip_cplus_tests] } { continue }

load_lib "cp-support.exp"

set testfile "virtfunc"
set srcfile ${testfile}.cc
set binfile ${objdir}/${subdir}/${testfile}

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {c++ debug}] != "" } {
     untested virtfunc.exp
     return -1
}

proc test_ptype_of_classes {} {

    # class VA

    cp_test_ptype_class \
	"ptype VA" "" "class" "VA" \
	{
	    { field public "int va;" }
	}

    # class VB

    cp_test_ptype_class \
	"ptype VB" "" "class" "VB" \
	{
	    { field  public "int vb;" }
	    { method public "int fvb();" }
	    { method public "virtual int vvb();" }
	}

    # class V

    cp_test_ptype_class \
	"ptype V" "" "class" "V" \
	{
	    { base          "public VA" }
	    { base          "public VB" }
	    { field  public "int w;" }
	    { method public "int f();" }
	    { method public "virtual int vv();" }
	}

    # class A

    cp_test_ptype_class \
	"ptype A" "" "class" "A" \
	{
	    { base           "public virtual V" }
	    { vbase          "V" }
	    { field  private "int a;" }
	    { method public  "virtual int f();" }
	}

    # class B

    cp_test_ptype_class \
	"ptype B" "" "class" "B" \
	{
	    { base           "public A" }
	    { field  private "int b;" }
	    { method public  "virtual int f();" }
	}

    # class C

    cp_test_ptype_class \
	"ptype C" "" "class" "C" \
	{
	    { base         "public virtual V" }
	    { vbase        "V" }
	    { field public "int c;" }
	}

    # class AD

    cp_test_ptype_class \
	"ptype AD" "" "class" "AD" \
	{
	    { method public "virtual int vg();" }
	}

    # class D

    cp_test_ptype_class \
	"ptype D" "" "class" "D" \
	{
	    { base          "public AD" }
	    { base          "public virtual V" }
	    { vbase         "V" }
	    { method public "static void s();" }
	    { method public "virtual int vg();" }
	    { method public "virtual int vd();" }
	    { method public "int fd();" }
	    { field  public "int d;" }
	}

    # class E

    cp_test_ptype_class \
	"ptype E" "" "class" "E" \
	{
	    { base          "public B" }
	    { base          "public virtual V" }
	    { base          "public D" }
	    { base          "public C" }
	    { vbase         "V" }
	    { method public "virtual int f();" }
	    { method public "virtual int vg();" }
	    { method public "virtual int vv();" }
	    { field  public "int e;" }
	}

    # An instance of D

    cp_test_ptype_class "ptype dd" "" "class" "D" ibid

    # An instance of D *

    cp_test_ptype_class "ptype ppd" "" "class" "D" ibid "*"

    # An instance of AD *
    # TODO: this should be named pADd, not pAd.

    cp_test_ptype_class "ptype pAd" "" "class" "AD" ibid "*"

    # Instances of these classes.

    cp_test_ptype_class "ptype a" "" "class" "A" ibid
    cp_test_ptype_class "ptype b" "" "class" "B" ibid
    cp_test_ptype_class "ptype c" "" "class" "C" ibid
    cp_test_ptype_class "ptype d" "" "class" "D" ibid
    cp_test_ptype_class "ptype e" "" "class" "E" ibid
    cp_test_ptype_class "ptype v" "" "class" "V" ibid
    cp_test_ptype_class "ptype vb" "" "class" "VB" ibid

    # Instances of pointers to these classes.

    cp_test_ptype_class "ptype pAa" "" "class" "A" ibid "*"
    cp_test_ptype_class "ptype pAe" "" "class" "A" ibid "*"
    cp_test_ptype_class "ptype pBe" "" "class" "B" ibid "*"
    cp_test_ptype_class "ptype pDd" "" "class" "D" ibid "*"
    cp_test_ptype_class "ptype pDe" "" "class" "D" ibid "*"
    cp_test_ptype_class "ptype pVa" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pVv" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pVe" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pVd" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pADe" "" "class" "AD" ibid "*"
    cp_test_ptype_class "ptype pEe" "" "class" "E" ibid "*"
    cp_test_ptype_class "ptype pVB" "" "class" "VB" ibid "*"

}

# Call virtual functions.

proc test_virtual_calls {} {
    global gdb_prompt
    global nl

    if [target_info exists gdb,cannot_call_functions] {
	setup_xfail "*-*-*" 2416
	fail "This target can not call functions"
	return 0
    }

    gdb_test "print pAe->f()"   "\\$\[0-9\]+ = 20"
    gdb_test "print pAa->f()"   "\\$\[0-9\]+ = 1"
    gdb_test "print pDe->vg()"  "\\$\[0-9\]+ = 202"
    gdb_test "print pADe->vg()" "\\$\[0-9\]+ = 202"
    gdb_test "print pDd->vg()"  "\\$\[0-9\]+ = 101"
    gdb_test "print pEe->vvb()" "\\$\[0-9\]+ = 411"
    gdb_test "print pVB->vvb()" "\\$\[0-9\]+ = 407"
    gdb_test "print pBe->vvb()" "\\$\[0-9\]+ = 411"
    gdb_test "print pDe->vvb()" "\\$\[0-9\]+ = 411"
    gdb_test "print pEe->vd()"  "\\$\[0-9\]+ = 282"
    gdb_test "print pEe->fvb()" "\\$\[0-9\]+ = 311"

    # more recent results:
    # wrong value "202"
    #   gcc 2.95.3 -gdwarf-2
    #   gcc 2.95.3 -gstabs+
    # attempt to take addres of value not located in memory
    #   gcc 3.3.2 -gdwarf-2
    #   gcc 3.3.2 -gstabs+
    #
    # -- chastain 2003-12-31

    gdb_test_multiple "print pEe->D::vg()" "print pEe->D::vg()" {
	-re "\\$\[0-9]+ = 102$nl$gdb_prompt $" {
	    pass "print pEe->D::vg()"
	}
	-re "\\$\[0-9]+ = 202$nl$gdb_prompt $" {
	    # To get this result, we have called pEe->*(&D::vg) ().
	    # That's how GDB interprets this, but it's wrong; in fact
	    # the explicit D:: means to bypass virtual function lookup,
	    # and call D::vg as if it were non-virtual.  We still have
	    # to e.g. adjust "this", though.
	    kfail "gdb/1064" "print pEe->D::vg()"
	}
	-re "Attempt to take address of value not located in memory.$nl$gdb_prompt $" {
	    kfail "gdb/1064" "print pEe->D::vg()"
	}
    }
}

proc do_tests {} {
    global prms_id
    global bug_id
    global srcdir subdir binfile
    global gdb_prompt

    set prms_id 0
    set bug_id 0

    gdb_exit
    gdb_start
    gdb_reinitialize_dir $srcdir/$subdir
    gdb_load $binfile

    gdb_test "set language c++" "" ""
    gdb_test "set width 0" "" ""

    if ![runto_main] then {
	perror "couldn't run to breakpoint"
	return
    }
    test_ptype_of_classes

    gdb_breakpoint test_calls
    gdb_test "continue" ".*Breakpoint .* test_calls.*" ""
    test_virtual_calls

    gdb_test "next" ".*pAa->f.*" "next to pAa->f call"
    gdb_test "next" ".*pDe->vg.*" "next to pDe->vg call"
    gdb_test "step" ".*E::vg.*" "step through thunk into E::vg"
}

do_tests