1 # Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003
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 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-gdb@prep.ai.mit.edu
21 # This file was written by Fred Fish. (fnf@cygnus.com)
22 # And rewritten by Michael Chastain <mec.gnu@mindspring.com>.
31 if { [skip_cplus_tests] } { continue }
33 set testfile "virtfunc"
34 set srcfile ${testfile}.cc
35 set binfile ${objdir}/${subdir}/${testfile}
37 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {c++ debug}] != "" } {
38 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
41 # Test ptype of class objects.
43 # Different C++ compilers produce different output. I build up regular
44 # expressions piece by piece to accommodate all the compilers that I
45 # have seen: gcc 2.95.3, gcc 3.3.2 (ABI 1), gcc 3.4 prerelease (ABI 2);
46 # and all the debug formats I have seen: dwarf-2 and stabs+.
48 # A complicated class declaration looks like this:
50 # class A : public virtual V { // re_class
52 # V * _vb$V; // re_vbptr
56 # A & operator=(A const &); // re_synth_gcc_2
57 # A(int, A const &); // ...
59 # virtual int f(void); // re_methods
62 # RE_CLASS matches the class declaration. C++ allows multiple ways of
65 # struct ... { private: ... };
66 # class ... { private: ... };
69 # RE_VBPTR matches the virtual base declarations. gcc 2.95.3 emits
70 # these, but gcc 3.X.Y does not. The name depends on the debug format.
72 # RE_FIELDS matches the data fields of the class.
73 # RE_METHODS matches the methods explicitly declared for the class.
75 # RE_SYNTH_GCC_2 and RE_SYNTH_GCC_3 match the optional synthetic methods
76 # of the class. gcc -gstabs+ emits these methods, and gcc -gdwarf-2
79 # RE_ALL_METHODS combines RE_METHODS and the optional synthetic methods.
80 # Up to gcc 3.3.X, gcc defaults to gcc ABI 1, with synthetic methods at
81 # the beginning. Starting with gcc 3.4.X, gcc defaults to gcc ABI 2,
82 # with synthetic methods at the end.
84 # So the possible choices for RE_ALL_METHODS are:
86 # RE_METHODS // any gcc with dwarf-2
87 # RE_SYNTH_GCC_2|RE_METHODS // gcc 2.95.3, stabs+
88 # RE_SYNTH_GCC_3|RE_METHODS // gcc 3.3.2, stabs+
89 # RE_METHODS|RE_SYNTH_GCC_3 // gcc 3.4.0, stabs+
91 # When I get HP-UX aCC, I hope that I can just add RE_SYNTH_ACC_FOO
92 # and enlarge RE_ALL_METHODS.
94 # Yet another twist: with gcc v2, ctor and dtor methods have a hidden
95 # argument in front, the "in-charge" flag. With gcc v3, there is no
96 # hidden argument; instead, there are multiple object functions for
99 # I use gdb_test_multiple with only one arm. I could use gdb_test,
100 # but gdb_test_multiple makes it easier to add KFAIL arms as needed.
102 # -- chastain 2003-12-31
104 proc test_ptype_of_classes {} {
111 set re_class "((struct|class) VA \{${ws}public:|struct VA \{)"
112 set re_fields "int va;"
113 set re_synth_gcc_23 "VA & operator=\\(VA const ?&\\);${ws}VA\\(VA const ?&\\);${ws}VA\\((void|)\\);"
114 set re_all_methods "(|$re_synth_gcc_23)"
116 gdb_test_multiple "ptype VA" "ptype VA" {
117 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\}$nl$gdb_prompt $" {
124 set re_class "((struct|class) VB \{${ws}public:|struct VB \{)"
125 set re_fields "int vb;"
126 set re_methods "int fvb\\((void|)\\);${ws}virtual int vvb\\((void|)\\);"
127 set re_synth_gcc_23 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);"
128 set re_all_methods "($re_methods|$re_methods${ws}$re_synth_gcc_23|$re_synth_gcc_23${ws}$re_methods)"
130 gdb_test_multiple "ptype VB" "ptype VB" {
131 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\}$nl$gdb_prompt $" {
138 gdb_test_multiple "ptype vb" "ptype vb" {
139 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\}$nl$gdb_prompt $" {
144 # An instance of VB *
146 gdb_test_multiple "ptype pVB" "ptype pVB" {
147 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\} \\*$nl$gdb_prompt $" {
154 set re_class "class V : public VA, public VB \{${ws}public:"
155 set re_fields "int w;"
156 set re_methods "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);"
157 set re_synth_gcc_23 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);"
158 set re_all_methods "($re_methods|$re_methods${ws}$re_synth_gcc_23|$re_synth_gcc_23${ws}$re_methods)"
160 gdb_test_multiple "ptype V" "ptype V" {
161 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\}$nl$gdb_prompt $" {
168 gdb_test_multiple "ptype v" "ptype v" {
169 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\}$nl$gdb_prompt $" {
176 gdb_test_multiple "ptype pVa" "ptype pVa" {
177 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\} \\*$nl$gdb_prompt $" {
184 gdb_test_multiple "ptype pVv" "ptype pVv" {
185 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\} \\*$nl$gdb_prompt $" {
192 gdb_test_multiple "ptype pVe" "ptype pVe" {
193 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\} \\*$nl$gdb_prompt $" {
200 gdb_test_multiple "ptype pVd" "ptype pVd" {
201 -re "type = $re_class${ws}$re_fields${ws}(public:${ws}|)$re_all_methods$nl\} \\*$nl$gdb_prompt $" {
208 set re_class "class A : public virtual V \{(${ws}private:|)"
209 set re_vbptr "V \\*(_vb.1V|_vb.V);"
210 set re_fields "int a;"
211 set re_methods "virtual int f\\((void|)\\);"
212 # gcc 2 adds an "in-charge" arg to the ctor.
213 set re_synth_gcc_2 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);"
214 set re_synth_gcc_3 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\(\\);"
215 set re_all_methods "($re_methods|$re_synth_gcc_2${ws}$re_methods|$re_synth_gcc_3${ws}$re_methods|$re_methods${ws}$re_synth_gcc_3)"
217 gdb_test_multiple "ptype A" "ptype A" {
218 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)${re_fields}${ws}public:${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
225 gdb_test_multiple "ptype a" "ptype a" {
226 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)${re_fields}${ws}public:${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
233 gdb_test_multiple "ptype pAa" "ptype pAa" {
234 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)${re_fields}${ws}public:${ws}${re_all_methods}$nl\} \\*$nl$gdb_prompt $" {
241 gdb_test_multiple "ptype pAe" "ptype pAe" {
242 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)${re_fields}${ws}public:${ws}${re_all_methods}$nl\} \\*$nl$gdb_prompt $" {
249 set re_class "class B : public A \{(${ws}private:|)"
250 set re_vbptr "V \\*(_vb.1V|_vb.V);"
251 set re_fields "int b;"
252 set re_methods "virtual int f\\((void|)\\);"
253 set re_synth_gcc_2 "B & operator=\\(B const ?&\\);${ws}B\\(int, B const ?&\\);${ws}B\\(int\\);"
254 set re_synth_gcc_3 "B & operator=\\(B const ?&\\);${ws}B\\(B const ?&\\);${ws}B\\(\\);"
255 set re_all_methods "($re_methods|$re_synth_gcc_2${ws}$re_methods|$re_synth_gcc_3${ws}$re_methods|$re_methods${ws}$re_synth_gcc_3)"
257 gdb_test_multiple "ptype B" "ptype B" {
258 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)${re_fields}${ws}public:${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
265 gdb_test_multiple "ptype b" "ptype b" {
266 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)${re_fields}${ws}public:${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
273 gdb_test_multiple "ptype pBe" "ptype pBe" {
274 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)${re_fields}${ws}public:${ws}${re_all_methods}$nl\} \\*$nl$gdb_prompt $" {
281 set re_class "class C : public virtual V \{(${ws}private:|)"
282 set re_vbptr "V \\*(_vb.1V|_vb.V);"
283 set re_fields "int c;"
284 set re_synth_gcc_2 "C & operator=\\(C const ?&\\);${ws}C\\(int, C const ?&\\);${ws}C\\(int\\);"
285 set re_synth_gcc_3 "C & operator=\\(C const ?&\\);${ws}C\\(C const ?&\\);${ws}C\\(\\);"
286 set re_all_methods "(|$re_synth_gcc_2|$re_synth_gcc_3)"
288 gdb_test_multiple "ptype C" "ptype C" {
289 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}(public:${ws}|)${re_all_methods}$nl\}$nl$gdb_prompt $" {
296 gdb_test_multiple "ptype c" "ptype c" {
297 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}(public:${ws}|)${re_all_methods}$nl\}$nl$gdb_prompt $" {
304 set re_class "((struct|class) AD \{${ws}public:|struct AD \{)"
305 set re_methods "virtual int vg\\((void|)\\);"
306 set re_synth_gcc_23 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);"
307 set re_all_methods "($re_methods|$re_methods${ws}$re_synth_gcc_23|$re_synth_gcc_23${ws}$re_methods)"
309 gdb_test_multiple "ptype AD" "ptype AD" {
310 -re "type = $re_class${ws}$re_all_methods$nl\}$nl$gdb_prompt $" {
315 # An instance of AD *
316 # TODO: this should be named pADd, not pAd.
318 gdb_test_multiple "ptype pAd" "ptype pAd" {
319 -re "type = $re_class${ws}$re_all_methods$nl\} \\*$nl$gdb_prompt $" {
324 # An instance of a AD *
326 gdb_test_multiple "ptype pADe" "ptype pADe" {
327 -re "type = $re_class${ws}$re_all_methods$nl\} \\*$nl$gdb_prompt $" {
334 set re_class "class D : public AD, public virtual V \{(${ws}private:|)"
335 set re_vbptr "V \\*(_vb.1V|_vb.V);"
336 set re_fields "int d;"
337 set re_methods "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);"
338 set re_synth_gcc_2 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);"
339 set re_synth_gcc_3 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\(\\);"
340 set re_all_methods "($re_methods|$re_synth_gcc_2${ws}$re_methods|$re_synth_gcc_3${ws}$re_methods|$re_methods${ws}$re_synth_gcc_3)"
342 gdb_test_multiple "ptype D" "ptype D" {
343 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
350 gdb_test_multiple "ptype d" "ptype d" {
351 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
358 gdb_test_multiple "ptype dd" "ptype dd" {
359 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
366 gdb_test_multiple "ptype ppd" "ptype ppd" {
367 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}${re_all_methods}$nl\} \\*$nl$gdb_prompt $" {
374 gdb_test_multiple "ptype pDd" "ptype pDd" {
375 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}${re_all_methods}$nl\} \\*$nl$gdb_prompt $" {
382 gdb_test_multiple "ptype pDe" "ptype pDe" {
383 -re "type = ${re_class}${ws}(${re_vbptr}${ws}|)public:${ws}${re_fields}${ws}${re_all_methods}$nl\} \\*$nl$gdb_prompt $" {
389 # TODO: E does not show a vbptr for V. That seems strange.
391 set re_class "class E : public B, public virtual V, public D, public C \{(${ws}private:|)"
392 set re_fields "int e;"
393 set re_methods "virtual int f\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vv\\((void|)\\);"
394 set re_synth_gcc_2 "E & operator=\\(E const ?&\\);${ws}E\\(int, E const ?&\\);${ws}E\\(int\\);"
395 set re_synth_gcc_3 "E & operator=\\(E const ?&\\);${ws}E\\(E const ?&\\);${ws}E\\(\\);"
396 set re_all_methods "($re_methods|$re_synth_gcc_2${ws}$re_methods|$re_synth_gcc_3${ws}$re_methods|$re_methods${ws}$re_synth_gcc_3)"
398 gdb_test_multiple "ptype E" "ptype E" {
399 -re "type = ${re_class}${ws}public:${ws}${re_fields}${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
406 gdb_test_multiple "ptype e" "ptype e" {
407 -re "type = ${re_class}${ws}public:${ws}${re_fields}${ws}${re_all_methods}$nl\}$nl$gdb_prompt $" {
414 gdb_test_multiple "ptype pEe" "ptype pEe" {
415 -re "type = ${re_class}${ws}public:${ws}${re_fields}${ws}${re_all_methods}$nl\} \\*$nl$gdb_prompt $" {
421 # Call virtual functions.
423 proc test_virtual_calls {} {
427 if [target_info exists gdb,cannot_call_functions] {
428 setup_xfail "*-*-*" 2416
429 fail "This target can not call functions"
433 gdb_test "print pAe->f()" "\\$\[0-9\]+ = 20"
434 gdb_test "print pAa->f()" "\\$\[0-9\]+ = 1"
435 gdb_test "print pDe->vg()" "\\$\[0-9\]+ = 202"
436 gdb_test "print pADe->vg()" "\\$\[0-9\]+ = 202"
437 gdb_test "print pDd->vg()" "\\$\[0-9\]+ = 101"
438 gdb_test "print pEe->vvb()" "\\$\[0-9\]+ = 411"
439 gdb_test "print pVB->vvb()" "\\$\[0-9\]+ = 407"
440 gdb_test "print pBe->vvb()" "\\$\[0-9\]+ = 411"
441 gdb_test "print pDe->vvb()" "\\$\[0-9\]+ = 411"
442 gdb_test "print pEe->vd()" "\\$\[0-9\]+ = 282"
443 gdb_test "print pEe->fvb()" "\\$\[0-9\]+ = 311"
445 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
446 # gdb=HEAD%2002-02-16, gcc=2.95.3, goption=-gdwarf-2.
448 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
449 # gdb=HEAD%2002-02-16, gcc=2.95.3, goption=-gstabs+.
451 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
452 # gdb=HEAD%2002-02-16, gcc=3.0.3, goption=-gdwarf-2.
454 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
455 # gdb=HEAD%2002-02-16, gcc=3.0.3, goption=-gstabs+.
457 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
458 # gdb=HEAD%2002-02-16, gcc=3.0.4-20020215, goption=-gdwarf-2.
460 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
461 # gdb=HEAD%2002-02-16, gcc=3.0.4-20020215, goption=-gstabs+.
463 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
464 # gdb=HEAD%2002-02-16, gcc=gcc-3_0-branch%2002-02-16, goption=-gdwarf-2.
466 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
467 # gdb=HEAD%2002-02-16, gcc=gcc-3_0-branch%2002-02-16, goption=-gstabs+.
469 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
470 # gdb=HEAD%2002-02-16, gcc=HEAD%2002-02-16, goption=-gdwarf-2.
472 # fails on target=native, host=i686-pc-linux-gnu%rh-7.2,
473 # gdb=HEAD%2002-02-16, gcc=HEAD%2002-02-16, goption=-gstabs+.
475 # -- chastain 2002-02-20
477 # more recent results:
479 # gcc 2.95.3 -gdwarf-2
480 # gcc 2.95.3 -gstabs+
481 # attempt to take addres of value not located in memory
482 # gcc 3.3.2 -gdwarf-2
485 # -- chastain 2003-12-31
487 gdb_test_multiple "print pEe->D::vg()" "print pEe->D::vg()" {
488 -re "\\$\[0-9]+ = 102$nl$gdb_prompt $" {
489 pass "print pEe->D::vg()"
491 -re "Attempt to take address of value not located in memory.$nl$gdb_prompt $" {
492 kfail "gdb/1064" "print pEe->D::vg()"
500 global srcdir subdir binfile
508 gdb_reinitialize_dir $srcdir/$subdir
511 gdb_test "set language c++" "" ""
512 gdb_test "set width 0" "" ""
515 test_ptype_of_classes
517 gdb_breakpoint test_calls
518 gdb_test "continue" ".*Breakpoint .* test_calls.*" ""