1 # Copyright (C) 1998 Free Software Foundation, Inc.
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.
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.
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.
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
22 # This file is part of the gdb testsuite
30 # test running programs
33 # Start with a fresh gdb
36 gdb_reinitialize_dir $srcdir/$subdir
41 set testfile "member-ptr"
42 set srcfile ${testfile}.cc
43 set binfile ${objdir}/${subdir}/${testfile}
45 # Create and source the file that provides information about the compiler
46 # used to compile the test case.
48 if [get_compiler_info ${binfile} "c++"] {
52 # Nearly all of these tests fail when compiled with G++, so just give up
53 # until GDB gets enhanced. -sts 1999-06-22
59 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
60 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
66 gdb_reinitialize_dir $srcdir/$subdir
70 if ![runto_main] then {
71 perror "couldn't run to breakpoint"
77 -re "Breakpoint \[0-9\]*.*line 83\\.\r\n$gdb_prompt $" {
78 pass "set break at 83"
80 -re ".*$gdb_prompt $" { fail "set break at 83" }
81 timeout { fail "(timeout) set break at 83" }
86 -re "Continuing\\.\r\n\r\nBreakpoint.*at.*member-ptr\\.cc:83\r\n83\[ \t]*pmi = NULL;\r\n$gdb_prompt $" {
89 -re ".*$gdb_prompt $" { fail "continue to 83" }
90 timeout { fail "(timeout) continue to 83" }
93 # ptype on pointer to data member
95 send_gdb "ptype pmi\n"
97 -re "type = int \\( A::\\*\\)\r\n$gdb_prompt $" {
100 -re ".*$gdb_prompt $" { fail "ptype pmi" }
101 timeout { fail "(timeout) ptype pmi" }
104 # print pointer to data member
106 send_gdb "print pmi\n"
108 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\) &A::j\r\n$gdb_prompt $" {
111 -re ".*$gdb_prompt $" { fail "print pmi" }
112 timeout { fail "(timeout) print pmi" }
116 # print dereferenced pointer to data member
118 send_gdb "print a.*pmi\n"
120 -re "\\$\[0-9\]* = 121\r\n$gdb_prompt $" {
123 -re ".*$gdb_prompt $" { fail "print a.*pmi" }
124 timeout { fail "(timeout) print a.*pmi" }
127 # print dereferenced pointer to data member
128 # this time, dereferenced through a pointer
130 send_gdb "print a_p->*pmi\n"
132 -re "\\$\[0-9\]* = 121\r\n$gdb_prompt $" {
135 -re ".*$gdb_prompt $" { fail "print a->*pmi" }
136 timeout { fail "(timeout) print a->*pmi" }
140 # set the pointer to data member
142 send_gdb "set var pmi = &A::jj\n"
144 -re "$gdb_prompt $" {
145 pass "set var (not really a pass)"
147 timeout { fail "(timeout) " }
150 # Now print the pointer again
152 send_gdb "print pmi\n"
154 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\) &A::jj\r\n$gdb_prompt $" {
155 pass "print pmi after setting"
157 -re ".*$gdb_prompt $" { fail "print pmi after setting" }
158 timeout { fail "(timeout) print pmi after setting" }
161 # print dereferenced pointer to data member again
163 send_gdb "print a.*pmi\n"
165 -re "\\$\[0-9\]* = 1331\r\n$gdb_prompt $" {
166 pass "print a.*pmi after setting"
168 -re ".*$gdb_prompt $" { fail "print a.*pmi after setting" }
169 timeout { fail "(timeout) print a.*pmi after setting" }
172 # set the pointer to data member back to A::j
174 send_gdb "set var pmi = &A::j\n"
176 -re "$gdb_prompt $" {
177 pass "set var back to A::j (not really a pass)"
179 timeout { fail "(timeout) set var pmi" }
182 # print dereferenced pointer to data member yet again (extra check, why not)
184 send_gdb "print a.*pmi\n"
186 -re "\\$\[0-9\]* = 121\r\n$gdb_prompt $" {
187 pass "print a.*pmi after resetting"
189 -re ".*$gdb_prompt $" { fail "print a.*pmi after resetting" }
190 timeout { fail "(timeout) print a.*pmi after resetting" }
193 # Set the data member pointed to.
195 send_gdb "print a.*pmi = 33\n"
197 -re "\\$\[0-9\]* = 33\r\n$gdb_prompt $" {
198 pass "print command to set"
200 -re ".*$gdb_prompt $" { fail "print command to set" }
201 timeout { fail "(timeout) print command to set" }
204 # Now check that the data really was changed
205 send_gdb "print a.*pmi\n"
207 -re "\\$\[0-9\]* = 33\r\n$gdb_prompt $" {
208 pass "print a.*pmi after setting member pointed to"
210 -re ".*$gdb_prompt $" { fail "print a.*pmi after setting member pointed to" }
211 timeout { fail "(timeout) print a.*pmi after setting member pointed to" }
214 # Double-check by printing a.
217 -re "\\$\[0-9\]* = \{c = 120 'x', j = 33, jj = 1331, static s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
218 pass "print a after setting member pointed to by pmi"
220 -re ".*$gdb_prompt $" { fail "print a after setting member pointed to by pmi" }
221 timeout { fail "(timeout) print a after setting member pointed to by pmi" }
225 # Set the data member pointed to, using ->*
227 send_gdb "print a_p->*pmi = 44\n"
229 -re "\\$\[0-9\]* = 44\r\n$gdb_prompt $" {
230 pass "print command to set (->)"
232 -re ".*$gdb_prompt $" { fail "print command to set (->)" }
233 timeout { fail "(timeout) print command to set (->)" }
236 # Now check that the data really was changed
237 send_gdb "print a_p->*pmi\n"
239 -re "\\$\[0-9\]* = 44\r\n$gdb_prompt $" {
240 pass "print a_p->*pmi after setting member pointed to"
242 -re ".*$gdb_prompt $" { fail "print a_p->*pmi after setting member pointed to" }
243 timeout { fail "(timeout) print a_p->*pmi after setting member pointed to" }
246 # Double-check by printing a.
249 -re "\\$\[0-9\]* = \{c = 120 'x', j = 44, jj = 1331, static s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
250 pass "print a after setting member pointed to by pmi (->) "
252 -re ".*$gdb_prompt $" { fail "print a after setting member pointed to by pmi (->) " }
253 timeout { fail "(timeout) print a after setting member pointed to by pmi (->) " }
257 # Do a ptype on the dereferenced pointer to member
258 # pai/1997-08-20 Doesn't work
260 # send_gdb "ptype a.*pmi\n"
262 # -re "type = int\r\n$gdb_prompt $" {
263 # pass "ptype a.*pmi"
265 # -re ".*$gdb_prompt $" { fail "ptype a.*pmi" }
266 # timeout { fail "(timeout) ptype a.*pmi" }
269 # Try to dereference the pointer to data member without any object
271 send_gdb "print *pmi\n"
273 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
274 pass "attempt to print ptr to member without object"
276 -re ".*$gdb_prompt $" { fail "attempt to print ptr to member without object" }
277 timeout { fail "(timeout) attempt to print ptr to member without object" }
280 # Try to ptype a dereference of the pointer to data member without any object
282 send_gdb "ptype *pmi\n"
284 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
285 pass "attempt to ptype ptr to member without object"
287 -re ".*$gdb_prompt $" { fail "attempt to ptype ptr to member without object" }
288 timeout { fail "(timeout) attempt to ptype ptr to member without object" }
291 # Ptype a pointer to a method.
293 send_gdb "ptype pmf\n"
295 -re "type = int \\( A::\\*\\)\\(\\.\\.\\.\\)\r\n$gdb_prompt $" {
298 -re ".*$gdb_prompt $" { fail "ptype pmf" }
299 timeout { fail "(timeout) ptype pmf" }
302 # print a pointer to a method
304 send_gdb "print pmf\n"
306 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\(\\.\\.\\.\\)\\) \\?\\? <not supported with HP aCC>\r\n$gdb_prompt $" {
309 -re ".*$gdb_prompt $" { fail "print pmf" }
310 timeout { fail "(timeout) print pmf" }
314 # Ptype a pointer to a pointer to a method
316 send_gdb "ptype pmf_p\n"
318 -re "type = int \\( A::\\*\\*\\)\\(\\.\\.\\.\\)\r\n$gdb_prompt $" {
321 -re ".*$gdb_prompt $" { fail "ptype pmf_p" }
322 timeout { fail "(timeout) ptype pmf_p" }
325 # print a pointer to a pointer to a method
327 send_gdb "print pmf_p\n"
329 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\*\\)\\(\\.\\.\\.\\)\\) $hex\r\n$gdb_prompt $" {
332 -re ".*$gdb_prompt $" { fail "print pmf_p" }
333 timeout { fail "(timeout) print pmf_p" }
336 # print dereferenced pointer to method
338 send_gdb "print a.*pmf\n"
340 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
341 pass "print a.*pmf (known aCC limitation)"
343 -re ".*$gdb_prompt $" { fail "print a.*pmf -- ??" }
344 timeout { fail "(timeout) print a.*pmf" }
347 # print dereferenced pointer to method, using ->*
349 send_gdb "print a_p->*pmf\n"
351 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
352 pass "print a_p->*pmf (known aCC limitation)"
354 -re ".*$gdb_prompt $" { fail "print a_p->*pmf -- ??" }
355 timeout { fail "(timeout) print a_p->*pmf" }
358 # set the pointer to data member
360 send_gdb "set var pmf = &A::foo\n"
362 -re "Assignment to pointers to methods not implemented with HP aCC\r\n$gdb_prompt $" {
363 pass "set var pmf (known aCC limitation)"
365 -re ".*$gdb_prompt $" { fail "set var pmf -- ??" }
366 timeout { fail "(timeout) set var pmf" }
369 # Try to dereference the pointer to method without any object
371 send_gdb "print *pmf\n"
373 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
374 pass "attempt to print ptr to method without object"
376 -re ".*$gdb_prompt $" { fail "attempt to print ptr to method without object" }
377 timeout { fail "(timeout) attempt to print ptr to method without object" }
380 # Try to ptype a dereference of the pointer to method without any object
382 send_gdb "ptype *pmi\n"
384 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
385 pass "attempt to ptype ptr to member without object"
387 -re ".*$gdb_prompt $" { fail "attempt to ptype ptr to member without object" }
388 timeout { fail "(timeout) attempt to ptype ptr to member without object" }
391 # Check cast of pointer to member to integer
392 send_gdb "print (int) pmi\n"
394 -re "\\$\[0-9\]* = 8\r\n$gdb_prompt $" {
395 pass "casting pmi to int"
397 -re ".*$gdb_prompt $" { fail "casting pmi to int" }
398 timeout { fail "(timeout) casting pmi to int" }
401 # Check cast of pointer to method to integer
402 send_gdb "print (int) pmf\n"
404 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
405 pass "casting pmf to int (known aCC limitation)"
407 -re ".*$gdb_prompt $" { fail "casting pmf to int -- ??" }
408 timeout { fail "(timeout) casting pmf to int" }
411 # Try to invoke a function through a pointer to data member
412 send_gdb "print (a.*pmi)(3)\n"
414 -re "Not implemented: function invocation through pointer to method with HP aCC\r\n$gdb_prompt $" {
415 pass "print (a.*pmi)(3) -- error message should be different"
417 -re ".*$gdb_prompt $" { fail "print (a.*pmi)(3) -- ???" }
418 timeout { fail "(timeout) print (a.*pmi)(3)" }
421 # Try to invoke a function through a pointer to a method
422 send_gdb "print (a.*pmf)(3)\n"
424 -re "Not implemented: function invocation through pointer to method with HP aCC\r\n$gdb_prompt $" {
425 pass "print (a.*pmi)(3) -- known aCC limitation"
427 -re ".*$gdb_prompt $" { fail "print (a.*pmf)(3) -- ???" }
428 timeout { fail "(timeout) print (a.*pmf)(3)" }
432 # Go past assignment of NULL to pmi
435 -re "\r\n85\[ \t\]*k = \\(a.\\*pmf\\)\\(3\\);\r\n$gdb_prompt $" {
438 -re ".*$gdb_prompt $" { fail "next past 83" }
439 timeout { fail "(timeout) next past 83" }
442 #send_gdb "print pmi\n"
444 # -re "Attempted dereference of null pointer-to-member\r\n$gdb_prompt $" {
447 # -re ".*$gdb_prompt $" { fail "" }
448 # timeout { fail "(timeout) " }
451 # Dereference the null pointer to member
452 send_gdb "print a.*pmi\n"
454 -re "Attempted dereference of null pointer-to-member\r\n$gdb_prompt $" {
457 -re ".*$gdb_prompt $" { fail "print a.*NULL" }
458 timeout { fail "(timeout) print a.*NULL" }
462 # Go to another part of the program
463 send_gdb "break 91\n"
465 -re "Breakpoint \[0-9\]*.*line 91\\.\r\n$gdb_prompt $" {
466 pass "set break at 91"
468 -re ".*$gdb_prompt $" { fail "set break at 91" }
469 timeout { fail "(timeout) set break at 91" }
472 send_gdb "continue\n"
474 -re "Continuing\\.\r\n\r\nBreakpoint.*at.*member-ptr\\.cc:91\r\n91\[ \t]*k = \\(a.\\*pmf\\)\\(4\\);\r\n$gdb_prompt $" {
475 pass "continue to 91"
477 -re ".*$gdb_prompt $" { fail "continue to 91" }
478 timeout { fail "(timeout) continue to 91" }
482 # Now check again that pmi works even when not set to
483 # something that's at the beginning of the object
485 send_gdb "print pmi\n"
487 -re "\\$\[0-9\]* = \\(int \\( A::\\*\\)\\) &A::jj\r\n$gdb_prompt $" {
490 -re ".*$gdb_prompt $" { fail "print pmi (2)" }
491 timeout { fail "(timeout) print pmi (2)" }
495 # print dereferenced pointer to data member
497 send_gdb "print a.*pmi\n"
499 -re "\\$\[0-9\]* = 1331\r\n$gdb_prompt $" {
500 pass "print a.*pmi (2)"
502 -re ".*$gdb_prompt $" { fail "print a.*pmi (2)" }
503 timeout { fail "(timeout) print a.*pmi (2)" }
506 # print dereferenced pointer to data member
507 # this time, dereferenced through a pointer
509 send_gdb "print a_p->*pmi\n"
511 -re "\\$\[0-9\]* = 1331\r\n$gdb_prompt $" {
514 -re ".*$gdb_prompt $" { fail "print a->*pmi (2)" }
515 timeout { fail "(timeout) print a->*pmi (2)" }