1 # Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004
2 # Free Software Foundation, Inc.
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 2 of the License, or
7 # (at your option) any later version.
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.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # This file was written by Fred Fish. (fnf@cygnus.com)
19 # And rewritten by Michael Chastain <mec.gnu@mindspring.com>.
21 set wsopt "\[\r\n\t \]*"
29 if { [skip_cplus_tests] } { continue }
31 set testfile "virtfunc"
32 set srcfile ${testfile}.cc
33 set binfile ${objdir}/${subdir}/${testfile}
35 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {c++ debug}] != "" } {
36 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
39 # Test ptype of a class.
41 # Different C++ compilers produce different output. I build up regular
42 # expressions piece by piece to accommodate all the compilers that I
43 # have seen: gcc 2.95.3, gcc 3.3.2 (ABI 1), gcc 3.4 prerelease (ABI 2);
44 # and all the debug formats I have seen: dwarf-2 and stabs+.
46 # A complicated class declaration looks like this:
48 # class A : public virtual V { // re_class
50 # V * _vb$V; // re_vbptr
53 # public: // re_access_methods
54 # A & operator=(A const &); // re_synth_gcc_2
55 # A(int, A const &); // ...
57 # virtual int f(void); // re_methods
60 # RE_CLASS matches the class declaration. C++ allows multiple ways of
63 # struct ... { private: ... };
64 # class ... { private: ... };
67 # RE_VBPTR matches the virtual base declarations. gcc 2.95.3 sometimes
68 # emits these, but gcc 3.X.Y never emits these. The name depends on the
71 # RE_FIELDS matches the data fields of the class.
72 # RE_METHODS matches the methods explicitly declared for the class.
74 # RE_SYNTH_GCC_2 and RE_SYNTH_GCC_3 match the optional synthetic methods
75 # of the class. gcc -gstabs+ emits these methods, and gcc -gdwarf-2
78 # RE_ACCESS_METHODS is an access specifier after RE_FIELDS and before
79 # the methods (explicit methods and/or synthetic methods).
80 # There is also an RE_ACCESS_FIELDS.
82 # When I get HP-UX aCC, I hope that I can just add RE_SYNTH_ACC_NNN.
84 # Yet another twist: with gcc v2, ctor and dtor methods have a hidden
85 # argument in front, the "in-charge" flag. With gcc v3, there is no
86 # hidden argument; instead, there are multiple object functions for
89 # -- chastain 2004-01-01
91 proc test_one_ptype { command testname re_class re_vbptr re_access_fields re_fields re_access_methods re_methods re_synth_gcc_2 re_synth_gcc_3 re_star } {
97 gdb_test_multiple "$command" "$testname" {
98 -re "type = $re_class${wsopt}$re_access_fields${wsopt}$re_fields${wsopt}$re_access_methods${wsopt}$re_methods$nl\}$re_star$nl$gdb_prompt $" {
99 # gcc 2.95.3, dwarf-2, no vbptr
100 # gcc 3.X, abi 1, dwarf-2
101 # gcc 3.X, abi 2, dwarf-2
104 -re "type = $re_class${wsopt}$re_vbptr${wsopt}$re_access_fields${wsopt}$re_fields${wsopt}$re_access_methods${wsopt}$re_methods$nl\}$re_star$nl$gdb_prompt $" {
105 # gcc 2.95.3, dwarf-2, vbptr
106 # TODO: drow says the vbptr is a bug
109 -re "type = $re_class${wsopt}$re_access_fields${wsopt}$re_fields${wsopt}$re_access_methods${wsopt}$re_synth_gcc_2${wsopt}$re_methods$nl\}$re_star$nl$gdb_prompt $" {
110 # gcc 2.95.3, stabs+, no vbptr
113 -re "type = $re_class${wsopt}$re_vbptr${wsopt}$re_access_fields${wsopt}$re_fields${wsopt}$re_access_methods${wsopt}$re_synth_gcc_2${wsopt}$re_methods$nl\}$re_star$nl$gdb_prompt $" {
114 # gcc 2.95.3, stabs+, vbptr
115 # TODO: drow says the vbptr is a bug
118 -re "type = $re_class${wsopt}$re_access_fields${wsopt}$re_fields${wsopt}$re_access_methods${wsopt}$re_synth_gcc_3${wsopt}$re_methods$nl\}$re_star$nl$gdb_prompt $" {
119 # gcc 3.X, abi 1, stabs+
122 -re "type = $re_class${wsopt}$re_access_fields${wsopt}$re_fields${wsopt}$re_access_methods${wsopt}$re_methods${wsopt}$re_synth_gcc_3$nl\}$re_star$nl$gdb_prompt $" {
123 # gcc 3.X, abi 2, stabs+
130 proc test_ptype_of_classes {} {
137 test_one_ptype "ptype VA" "ptype VA" \
138 "((struct|class) VA \{${ws}public:|struct VA \{)" \
142 "VA & operator=\\(VA const ?&\\);${ws}VA\\(VA const ?&\\);${ws}VA\\((void|)\\);" \
143 "VA & operator=\\(VA const ?&\\);${ws}VA\\(VA const ?&\\);${ws}VA\\((void|)\\);" \
148 test_one_ptype "ptype VB" "ptype VB" \
149 "((struct|class) VB \{${ws}public:|struct VB \{)" \
152 "" "int fvb\\((void|)\\);${ws}virtual int vvb\\((void|)\\);" \
153 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
154 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
159 test_one_ptype "ptype V" "ptype V" \
160 "class V : public VA, public VB \{${ws}public:" \
163 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
164 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
165 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
170 test_one_ptype "ptype A" "ptype A" \
171 "class A : public virtual V \{(${ws}private:|)" \
172 "V \\*(_vb.1V|_vb.V);" \
174 "public:" "virtual int f\\((void|)\\);" \
175 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
176 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
181 test_one_ptype "ptype B" "ptype B" \
182 "class B : public A \{(${ws}private:|)" \
183 "V \\*(_vb.1V|_vb.V);" \
185 "public:" "virtual int f\\((void|)\\);" \
186 "B & operator=\\(B const ?&\\);${ws}B\\(int, B const ?&\\);${ws}B\\(int\\);" \
187 "B & operator=\\(B const ?&\\);${ws}B\\(B const ?&\\);${ws}B\\((void|)\\);" \
192 test_one_ptype "ptype C" "ptype C" \
193 "class C : public virtual V \{(${ws}private:|)" \
194 "V \\*(_vb.1V|_vb.V);" \
197 "C & operator=\\(C const ?&\\);${ws}C\\(int, C const ?&\\);${ws}C\\(int\\);" \
198 "C & operator=\\(C const ?&\\);${ws}C\\(C const ?&\\);${ws}C\\((void|)\\);" \
203 test_one_ptype "ptype AD" "ptype AD" \
204 "((struct|class) AD \{${ws}public:|struct AD \{)" \
207 "" "virtual int vg\\((void|)\\);" \
208 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
209 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
214 test_one_ptype "ptype D" "ptype D" \
215 "class D : public AD, public virtual V \{(${ws}private:|)" \
216 "V \\*(_vb.1V|_vb.V);" \
218 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
219 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
220 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
224 # TODO: E does not show a vbptr for V. That seems strange.
226 test_one_ptype "ptype E" "ptype E" \
227 "class E : public B, public virtual V, public D, public C \{(${ws}private:|)" \
230 "" "virtual int f\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
231 "E & operator=\\(E const ?&\\);${ws}E\\(int, E const ?&\\);${ws}E\\(int\\);" \
232 "E & operator=\\(E const ?&\\);${ws}E\\(E const ?&\\);${ws}E\\((void|)\\);" \
237 test_one_ptype "ptype dd" "ptype dd" \
238 "class D : public AD, public virtual V \{(${ws}private:|)" \
239 "V \\*(_vb.1V|_vb.V);" \
241 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
242 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
243 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
248 test_one_ptype "ptype ppd" "ptype ppd" \
249 "class D : public AD, public virtual V \{(${ws}private:|)" \
250 "V \\*(_vb.1V|_vb.V);" \
252 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
253 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
254 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
257 # An instance of AD *
258 # TODO: this should be named pADd, not pAd.
260 test_one_ptype "ptype pAd" "ptype pAd" \
261 "((struct|class) AD \{${ws}public:|struct AD \{)" \
264 "" "virtual int vg\\((void|)\\);" \
265 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
266 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
271 test_one_ptype "ptype a" "ptype a" \
272 "class A : public virtual V \{(${ws}private:|)" \
273 "V \\*(_vb.1V|_vb.V);" \
275 "public:" "virtual int f\\((void|)\\);" \
276 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
277 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
282 test_one_ptype "ptype b" "ptype b" \
283 "class B : public A \{(${ws}private:|)" \
284 "V \\*(_vb.1V|_vb.V);" \
286 "public:" "virtual int f\\((void|)\\);" \
287 "B & operator=\\(B const ?&\\);${ws}B\\(int, B const ?&\\);${ws}B\\(int\\);" \
288 "B & operator=\\(B const ?&\\);${ws}B\\(B const ?&\\);${ws}B\\((void|)\\);" \
293 test_one_ptype "ptype c" "ptype c" \
294 "class C : public virtual V \{(${ws}private:|)" \
295 "V \\*(_vb.1V|_vb.V);" \
298 "C & operator=\\(C const ?&\\);${ws}C\\(int, C const ?&\\);${ws}C\\(int\\);" \
299 "C & operator=\\(C const ?&\\);${ws}C\\(C const ?&\\);${ws}C\\((void|)\\);" \
304 test_one_ptype "ptype d" "ptype d" \
305 "class D : public AD, public virtual V \{(${ws}private:|)" \
306 "V \\*(_vb.1V|_vb.V);" \
308 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
309 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
310 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
315 test_one_ptype "ptype e" "ptype e" \
316 "class E : public B, public virtual V, public D, public C \{(${ws}private:|)" \
319 "" "virtual int f\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
320 "E & operator=\\(E const ?&\\);${ws}E\\(int, E const ?&\\);${ws}E\\(int\\);" \
321 "E & operator=\\(E const ?&\\);${ws}E\\(E const ?&\\);${ws}E\\((void|)\\);" \
326 test_one_ptype "ptype v" "ptype v" \
327 "class V : public VA, public VB \{${ws}public:" \
330 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
331 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
332 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
337 test_one_ptype "ptype vb" "ptype vb" \
338 "((struct|class) VB \{${ws}public:|struct VB \{)" \
341 "" "int fvb\\((void|)\\);${ws}virtual int vvb\\((void|)\\);" \
342 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
343 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
348 test_one_ptype "ptype pAa" "ptype pAa" \
349 "class A : public virtual V \{(${ws}private:|)" \
350 "V \\*(_vb.1V|_vb.V);" \
352 "public:" "virtual int f\\((void|)\\);" \
353 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
354 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
359 test_one_ptype "ptype pAe" "ptype pAe" \
360 "class A : public virtual V \{(${ws}private:|)" \
361 "V \\*(_vb.1V|_vb.V);" \
363 "public:" "virtual int f\\((void|)\\);" \
364 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
365 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
370 test_one_ptype "ptype pBe" "ptype pBe" \
371 "class B : public A \{(${ws}private:|)" \
372 "V \\*(_vb.1V|_vb.V);" \
374 "public:" "virtual int f\\((void|)\\);" \
375 "B & operator=\\(B const ?&\\);${ws}B\\(int, B const ?&\\);${ws}B\\(int\\);" \
376 "B & operator=\\(B const ?&\\);${ws}B\\(B const ?&\\);${ws}B\\((void|)\\);" \
381 test_one_ptype "ptype pDd" "ptype pDd" \
382 "class D : public AD, public virtual V \{(${ws}private:|)" \
383 "V \\*(_vb.1V|_vb.V);" \
385 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
386 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
387 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
392 test_one_ptype "ptype pDe" "ptype pDe" \
393 "class D : public AD, public virtual V \{(${ws}private:|)" \
394 "V \\*(_vb.1V|_vb.V);" \
396 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
397 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
398 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
403 test_one_ptype "ptype pVa" "ptype pVa" \
404 "class V : public VA, public VB \{${ws}public:" \
407 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
408 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
409 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
414 test_one_ptype "ptype pVv" "ptype pVv" \
415 "class V : public VA, public VB \{${ws}public:" \
418 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
419 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
420 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
425 test_one_ptype "ptype pVe" "ptype pVe" \
426 "class V : public VA, public VB \{${ws}public:" \
429 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
430 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
431 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
436 test_one_ptype "ptype pVd" "ptype pVd" \
437 "class V : public VA, public VB \{${ws}public:" \
440 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
441 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
442 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
445 # An instance of AD *
447 test_one_ptype "ptype pADe" "ptype pADe" \
448 "((struct|class) AD \{${ws}public:|struct AD \{)" \
451 "" "virtual int vg\\((void|)\\);" \
452 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
453 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
458 test_one_ptype "ptype pEe" "ptype pEe" \
459 "class E : public B, public virtual V, public D, public C \{(${ws}private:|)" \
462 "" "virtual int f\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
463 "E & operator=\\(E const ?&\\);${ws}E\\(int, E const ?&\\);${ws}E\\(int\\);" \
464 "E & operator=\\(E const ?&\\);${ws}E\\(E const ?&\\);${ws}E\\((void|)\\);" \
467 # An instance of VB *
469 test_one_ptype "ptype pVB" "ptype pVB" \
470 "((struct|class) VB \{${ws}public:|struct VB \{)" \
473 "" "int fvb\\((void|)\\);${ws}virtual int vvb\\((void|)\\);" \
474 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
475 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
479 # Call virtual functions.
481 proc test_virtual_calls {} {
485 if [target_info exists gdb,cannot_call_functions] {
486 setup_xfail "*-*-*" 2416
487 fail "This target can not call functions"
491 gdb_test "print pAe->f()" "\\$\[0-9\]+ = 20"
492 gdb_test "print pAa->f()" "\\$\[0-9\]+ = 1"
493 gdb_test "print pDe->vg()" "\\$\[0-9\]+ = 202"
494 gdb_test "print pADe->vg()" "\\$\[0-9\]+ = 202"
495 gdb_test "print pDd->vg()" "\\$\[0-9\]+ = 101"
496 gdb_test "print pEe->vvb()" "\\$\[0-9\]+ = 411"
497 gdb_test "print pVB->vvb()" "\\$\[0-9\]+ = 407"
498 gdb_test "print pBe->vvb()" "\\$\[0-9\]+ = 411"
499 gdb_test "print pDe->vvb()" "\\$\[0-9\]+ = 411"
500 gdb_test "print pEe->vd()" "\\$\[0-9\]+ = 282"
501 gdb_test "print pEe->fvb()" "\\$\[0-9\]+ = 311"
503 # more recent results:
505 # gcc 2.95.3 -gdwarf-2
506 # gcc 2.95.3 -gstabs+
507 # attempt to take addres of value not located in memory
508 # gcc 3.3.2 -gdwarf-2
511 # -- chastain 2003-12-31
513 gdb_test_multiple "print pEe->D::vg()" "print pEe->D::vg()" {
514 -re "\\$\[0-9]+ = 102$nl$gdb_prompt $" {
515 pass "print pEe->D::vg()"
517 -re "Attempt to take address of value not located in memory.$nl$gdb_prompt $" {
518 kfail "gdb/1064" "print pEe->D::vg()"
526 global srcdir subdir binfile
534 gdb_reinitialize_dir $srcdir/$subdir
537 gdb_test "set language c++" "" ""
538 gdb_test "set width 0" "" ""
541 test_ptype_of_classes
543 gdb_breakpoint test_calls
544 gdb_test "continue" ".*Breakpoint .* test_calls.*" ""