]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.c++/member-ptr.exp
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.c++ / member-ptr.exp
1 # Copyright (C) 1998 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 # Tests for pointer-to-member support
20 # Written by Satish Pai <pai@apollo.hp.com> 1997-08-19
21
22 # This file is part of the gdb testsuite
23
24
25 if $tracelevel then {
26 strace $tracelevel
27 }
28
29 #
30 # test running programs
31 #
32
33 # Start with a fresh gdb
34 gdb_exit
35 gdb_start
36 gdb_reinitialize_dir $srcdir/$subdir
37
38 set prms_id 0
39 set bug_id 0
40
41 set testfile "member-ptr"
42 set srcfile ${testfile}.cc
43 set binfile ${objdir}/${subdir}/${testfile}
44
45 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
46 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
47 }
48
49
50 gdb_exit
51 gdb_start
52 gdb_reinitialize_dir $srcdir/$subdir
53 gdb_load ${binfile}
54
55
56 if ![runto_main] then {
57 perror "couldn't run to breakpoint"
58 continue
59 }
60
61 send_gdb "break 83\n"
62 gdb_expect {
63 -re "Breakpoint \[0-9\]*.*line 83\\.\r\n$gdb_prompt $" {
64 pass "set break at 83"
65 }
66 -re ".*$gdb_prompt $" { fail "set break at 83" }
67 timeout { fail "(timeout) set break at 83" }
68 }
69
70 send_gdb "continue\n"
71 gdb_expect {
72 -re "Continuing\\.\r\n\r\nBreakpoint.*at.*member-ptr\\.cc:83\r\n83\[ \t]*pmi = NULL;\r\n$gdb_prompt $" {
73 pass "continue to 83"
74 }
75 -re ".*$gdb_prompt $" { fail "continue to 83" }
76 timeout { fail "(timeout) continue to 83" }
77 }
78
79 # ptype on pointer to data member
80
81 send_gdb "ptype pmi\n"
82 gdb_expect {
83 -re "type = int \\( A::\\*\\)\r\n$gdb_prompt $" {
84 pass "ptype pmi"
85 }
86 -re ".*$gdb_prompt $" { fail "ptype pmi" }
87 timeout { fail "(timeout) ptype pmi" }
88 }
89
90 # print pointer to data member
91
92 send_gdb "print pmi\n"
93 gdb_expect {
94 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\) &A::j\r\n$gdb_prompt $" {
95 pass "print pmi"
96 }
97 -re ".*$gdb_prompt $" { fail "print pmi" }
98 timeout { fail "(timeout) print pmi" }
99 }
100
101
102 # print dereferenced pointer to data member
103
104 send_gdb "print a.*pmi\n"
105 gdb_expect {
106 -re "\\$\[0-9\]* = 121\r\n$gdb_prompt $" {
107 pass "print a.*pmi"
108 }
109 -re ".*$gdb_prompt $" { fail "print a.*pmi" }
110 timeout { fail "(timeout) print a.*pmi" }
111 }
112
113 # print dereferenced pointer to data member
114 # this time, dereferenced through a pointer
115
116 send_gdb "print a_p->*pmi\n"
117 gdb_expect {
118 -re "\\$\[0-9\]* = 121\r\n$gdb_prompt $" {
119 pass "print a->*pmi"
120 }
121 -re ".*$gdb_prompt $" { fail "print a->*pmi" }
122 timeout { fail "(timeout) print a->*pmi" }
123 }
124
125
126 # set the pointer to data member
127
128 send_gdb "set var pmi = &A::jj\n"
129 gdb_expect {
130 -re "$gdb_prompt $" {
131 pass "set var (not really a pass)"
132 }
133 timeout { fail "(timeout) " }
134 }
135
136 # Now print the pointer again
137
138 send_gdb "print pmi\n"
139 gdb_expect {
140 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\) &A::jj\r\n$gdb_prompt $" {
141 pass "print pmi after setting"
142 }
143 -re ".*$gdb_prompt $" { fail "print pmi after setting" }
144 timeout { fail "(timeout) print pmi after setting" }
145 }
146
147 # print dereferenced pointer to data member again
148
149 send_gdb "print a.*pmi\n"
150 gdb_expect {
151 -re "\\$\[0-9\]* = 1331\r\n$gdb_prompt $" {
152 pass "print a.*pmi after setting"
153 }
154 -re ".*$gdb_prompt $" { fail "print a.*pmi after setting" }
155 timeout { fail "(timeout) print a.*pmi after setting" }
156 }
157
158 # set the pointer to data member back to A::j
159
160 send_gdb "set var pmi = &A::j\n"
161 gdb_expect {
162 -re "$gdb_prompt $" {
163 pass "set var back to A::j (not really a pass)"
164 }
165 timeout { fail "(timeout) set var pmi" }
166 }
167
168 # print dereferenced pointer to data member yet again (extra check, why not)
169
170 send_gdb "print a.*pmi\n"
171 gdb_expect {
172 -re "\\$\[0-9\]* = 121\r\n$gdb_prompt $" {
173 pass "print a.*pmi after resetting"
174 }
175 -re ".*$gdb_prompt $" { fail "print a.*pmi after resetting" }
176 timeout { fail "(timeout) print a.*pmi after resetting" }
177 }
178
179 # Set the data member pointed to.
180
181 send_gdb "print a.*pmi = 33\n"
182 gdb_expect {
183 -re "\\$\[0-9\]* = 33\r\n$gdb_prompt $" {
184 pass "print command to set"
185 }
186 -re ".*$gdb_prompt $" { fail "print command to set" }
187 timeout { fail "(timeout) print command to set" }
188 }
189
190 # Now check that the data really was changed
191 send_gdb "print a.*pmi\n"
192 gdb_expect {
193 -re "\\$\[0-9\]* = 33\r\n$gdb_prompt $" {
194 pass "print a.*pmi after setting member pointed to"
195 }
196 -re ".*$gdb_prompt $" { fail "print a.*pmi after setting member pointed to" }
197 timeout { fail "(timeout) print a.*pmi after setting member pointed to" }
198 }
199
200 # Double-check by printing a.
201 send_gdb "print a\n"
202 gdb_expect {
203 -re "\\$\[0-9\]* = \{c = 120 'x', j = 33, jj = 1331, static s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
204 pass "print a after setting member pointed to by pmi"
205 }
206 -re ".*$gdb_prompt $" { fail "print a after setting member pointed to by pmi" }
207 timeout { fail "(timeout) print a after setting member pointed to by pmi" }
208 }
209
210
211 # Set the data member pointed to, using ->*
212
213 send_gdb "print a_p->*pmi = 44\n"
214 gdb_expect {
215 -re "\\$\[0-9\]* = 44\r\n$gdb_prompt $" {
216 pass "print command to set (->)"
217 }
218 -re ".*$gdb_prompt $" { fail "print command to set (->)" }
219 timeout { fail "(timeout) print command to set (->)" }
220 }
221
222 # Now check that the data really was changed
223 send_gdb "print a_p->*pmi\n"
224 gdb_expect {
225 -re "\\$\[0-9\]* = 44\r\n$gdb_prompt $" {
226 pass "print a_p->*pmi after setting member pointed to"
227 }
228 -re ".*$gdb_prompt $" { fail "print a_p->*pmi after setting member pointed to" }
229 timeout { fail "(timeout) print a_p->*pmi after setting member pointed to" }
230 }
231
232 # Double-check by printing a.
233 send_gdb "print a\n"
234 gdb_expect {
235 -re "\\$\[0-9\]* = \{c = 120 'x', j = 44, jj = 1331, static s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
236 pass "print a after setting member pointed to by pmi (->) "
237 }
238 -re ".*$gdb_prompt $" { fail "print a after setting member pointed to by pmi (->) " }
239 timeout { fail "(timeout) print a after setting member pointed to by pmi (->) " }
240 }
241
242
243 # Do a ptype on the dereferenced pointer to member
244 # pai/1997-08-20 Doesn't work
245
246 # send_gdb "ptype a.*pmi\n"
247 # gdb_expect {
248 # -re "type = int\r\n$gdb_prompt $" {
249 # pass "ptype a.*pmi"
250 # }
251 # -re ".*$gdb_prompt $" { fail "ptype a.*pmi" }
252 # timeout { fail "(timeout) ptype a.*pmi" }
253 #}
254
255 # Try to dereference the pointer to data member without any object
256
257 send_gdb "print *pmi\n"
258 gdb_expect {
259 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
260 pass "attempt to print ptr to member without object"
261 }
262 -re ".*$gdb_prompt $" { fail "attempt to print ptr to member without object" }
263 timeout { fail "(timeout) attempt to print ptr to member without object" }
264 }
265
266 # Try to ptype a dereference of the pointer to data member without any object
267
268 send_gdb "ptype *pmi\n"
269 gdb_expect {
270 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
271 pass "attempt to ptype ptr to member without object"
272 }
273 -re ".*$gdb_prompt $" { fail "attempt to ptype ptr to member without object" }
274 timeout { fail "(timeout) attempt to ptype ptr to member without object" }
275 }
276
277 # Ptype a pointer to a method.
278
279 send_gdb "ptype pmf\n"
280 gdb_expect {
281 -re "type = int \\( A::\\*\\)\\(\\.\\.\\.\\)\r\n$gdb_prompt $" {
282 pass "ptype pmf"
283 }
284 -re ".*$gdb_prompt $" { fail "ptype pmf" }
285 timeout { fail "(timeout) ptype pmf" }
286 }
287
288 # print a pointer to a method
289
290 send_gdb "print pmf\n"
291 gdb_expect {
292 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\(\\.\\.\\.\\)\\) \\?\\? <not supported with HP aCC>\r\n$gdb_prompt $" {
293 pass "print pmf"
294 }
295 -re ".*$gdb_prompt $" { fail "print pmf" }
296 timeout { fail "(timeout) print pmf" }
297 }
298
299
300 # Ptype a pointer to a pointer to a method
301
302 send_gdb "ptype pmf_p\n"
303 gdb_expect {
304 -re "type = int \\( A::\\*\\*\\)\\(\\.\\.\\.\\)\r\n$gdb_prompt $" {
305 pass "ptype pmf_p"
306 }
307 -re ".*$gdb_prompt $" { fail "ptype pmf_p" }
308 timeout { fail "(timeout) ptype pmf_p" }
309 }
310
311 # print a pointer to a pointer to a method
312
313 send_gdb "print pmf_p\n"
314 gdb_expect {
315 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\*\\)\\(\\.\\.\\.\\)\\) $hex\r\n$gdb_prompt $" {
316 pass "print pmf_p"
317 }
318 -re ".*$gdb_prompt $" { fail "print pmf_p" }
319 timeout { fail "(timeout) print pmf_p" }
320 }
321
322 # print dereferenced pointer to method
323
324 send_gdb "print a.*pmf\n"
325 gdb_expect {
326 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
327 pass "print a.*pmf (known aCC limitation)"
328 }
329 -re ".*$gdb_prompt $" { fail "print a.*pmf -- ??" }
330 timeout { fail "(timeout) print a.*pmf" }
331 }
332
333 # print dereferenced pointer to method, using ->*
334
335 send_gdb "print a_p->*pmf\n"
336 gdb_expect {
337 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
338 pass "print a_p->*pmf (known aCC limitation)"
339 }
340 -re ".*$gdb_prompt $" { fail "print a_p->*pmf -- ??" }
341 timeout { fail "(timeout) print a_p->*pmf" }
342 }
343
344 # set the pointer to data member
345
346 send_gdb "set var pmf = &A::foo\n"
347 gdb_expect {
348 -re "Assignment to pointers to methods not implemented with HP aCC\r\n$gdb_prompt $" {
349 pass "set var pmf (known aCC limitation)"
350 }
351 -re ".*$gdb_prompt $" { fail "set var pmf -- ??" }
352 timeout { fail "(timeout) set var pmf" }
353 }
354
355 # Try to dereference the pointer to method without any object
356
357 send_gdb "print *pmf\n"
358 gdb_expect {
359 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
360 pass "attempt to print ptr to method without object"
361 }
362 -re ".*$gdb_prompt $" { fail "attempt to print ptr to method without object" }
363 timeout { fail "(timeout) attempt to print ptr to method without object" }
364 }
365
366 # Try to ptype a dereference of the pointer to method without any object
367
368 send_gdb "ptype *pmi\n"
369 gdb_expect {
370 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
371 pass "attempt to ptype ptr to member without object"
372 }
373 -re ".*$gdb_prompt $" { fail "attempt to ptype ptr to member without object" }
374 timeout { fail "(timeout) attempt to ptype ptr to member without object" }
375 }
376
377 # Check cast of pointer to member to integer
378 send_gdb "print (int) pmi\n"
379 gdb_expect {
380 -re "\\$\[0-9\]* = 8\r\n$gdb_prompt $" {
381 pass "casting pmi to int"
382 }
383 -re ".*$gdb_prompt $" { fail "casting pmi to int" }
384 timeout { fail "(timeout) casting pmi to int" }
385 }
386
387 # Check cast of pointer to method to integer
388 send_gdb "print (int) pmf\n"
389 gdb_expect {
390 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
391 pass "casting pmf to int (known aCC limitation)"
392 }
393 -re ".*$gdb_prompt $" { fail "casting pmf to int -- ??" }
394 timeout { fail "(timeout) casting pmf to int" }
395 }
396
397 # Try to invoke a function through a pointer to data member
398 send_gdb "print (a.*pmi)(3)\n"
399 gdb_expect {
400 -re "Not implemented: function invocation through pointer to method with HP aCC\r\n$gdb_prompt $" {
401 pass "print (a.*pmi)(3) -- error message should be different"
402 }
403 -re ".*$gdb_prompt $" { fail "print (a.*pmi)(3) -- ???" }
404 timeout { fail "(timeout) print (a.*pmi)(3)" }
405 }
406
407 # Try to invoke a function through a pointer to a method
408 send_gdb "print (a.*pmf)(3)\n"
409 gdb_expect {
410 -re "Not implemented: function invocation through pointer to method with HP aCC\r\n$gdb_prompt $" {
411 pass "print (a.*pmi)(3) -- known aCC limitation"
412 }
413 -re ".*$gdb_prompt $" { fail "print (a.*pmf)(3) -- ???" }
414 timeout { fail "(timeout) print (a.*pmf)(3)" }
415 }
416
417
418 # Go past assignment of NULL to pmi
419 send_gdb "next\n"
420 gdb_expect {
421 -re "\r\n85\[ \t\]*k = \\(a.\\*pmf\\)\\(3\\);\r\n$gdb_prompt $" {
422 pass "next past 83"
423 }
424 -re ".*$gdb_prompt $" { fail "next past 83" }
425 timeout { fail "(timeout) next past 83" }
426 }
427
428 #send_gdb "print pmi\n"
429 #gdb_expect {
430 # -re "Attempted dereference of null pointer-to-member\r\n$gdb_prompt $" {
431 # pass ""
432 # }
433 # -re ".*$gdb_prompt $" { fail "" }
434 # timeout { fail "(timeout) " }
435 #}
436
437 # Dereference the null pointer to member
438 send_gdb "print a.*pmi\n"
439 gdb_expect {
440 -re "Attempted dereference of null pointer-to-member\r\n$gdb_prompt $" {
441 pass "print a.*NULL"
442 }
443 -re ".*$gdb_prompt $" { fail "print a.*NULL" }
444 timeout { fail "(timeout) print a.*NULL" }
445 }
446
447
448 # Go to another part of the program
449 send_gdb "break 91\n"
450 gdb_expect {
451 -re "Breakpoint \[0-9\]*.*line 91\\.\r\n$gdb_prompt $" {
452 pass "set break at 91"
453 }
454 -re ".*$gdb_prompt $" { fail "set break at 91" }
455 timeout { fail "(timeout) set break at 91" }
456 }
457
458 send_gdb "continue\n"
459 gdb_expect {
460 -re "Continuing\\.\r\n\r\nBreakpoint.*at.*member-ptr\\.cc:91\r\n91\[ \t]*k = \\(a.\\*pmf\\)\\(4\\);\r\n$gdb_prompt $" {
461 pass "continue to 91"
462 }
463 -re ".*$gdb_prompt $" { fail "continue to 91" }
464 timeout { fail "(timeout) continue to 91" }
465 }
466
467
468 # Now check again that pmi works even when not set to
469 # something that's at the beginning of the object
470
471 send_gdb "print pmi\n"
472 gdb_expect {
473 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\) &A::jj\r\n$gdb_prompt $" {
474 pass "print pmi (2)"
475 }
476 -re ".*$gdb_prompt $" { fail "print pmi (2)" }
477 timeout { fail "(timeout) print pmi (2)" }
478 }
479
480
481 # print dereferenced pointer to data member
482
483 send_gdb "print a.*pmi\n"
484 gdb_expect {
485 -re "\\$\[0-9\]* = 1331\r\n$gdb_prompt $" {
486 pass "print a.*pmi (2)"
487 }
488 -re ".*$gdb_prompt $" { fail "print a.*pmi (2)" }
489 timeout { fail "(timeout) print a.*pmi (2)" }
490 }
491
492 # print dereferenced pointer to data member
493 # this time, dereferenced through a pointer
494
495 send_gdb "print a_p->*pmi\n"
496 gdb_expect {
497 -re "\\$\[0-9\]* = 1331\r\n$gdb_prompt $" {
498 pass "print a->*pmi"
499 }
500 -re ".*$gdb_prompt $" { fail "print a->*pmi (2)" }
501 timeout { fail "(timeout) print a->*pmi (2)" }
502 }
503
504
505 # p a.*pmf - fail
506
507 # p pmi
508
509 # p a.*pmi
510