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 # 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>.
24 set wsopt "\[\r\n\t \]*"
32 if { [skip_cplus_tests] } { continue }
34 set testfile "virtfunc"
35 set srcfile ${testfile}.cc
36 set binfile ${objdir}/${subdir}/${testfile}
38 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {c++ debug}] != "" } {
39 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
42 # Test ptype of a class.
44 # Different C++ compilers produce different output. I build up regular
45 # expressions piece by piece to accommodate all the compilers that I
46 # have seen: gcc 2.95.3, gcc 3.3.2 (ABI 1), gcc 3.4 prerelease (ABI 2);
47 # and all the debug formats I have seen: dwarf-2 and stabs+.
49 # A complicated class declaration looks like this:
51 # class A : public virtual V { // re_class
53 # V * _vb$V; // re_vbptr
56 # public: // re_access_methods
57 # A & operator=(A const &); // re_synth_gcc_2
58 # A(int, A const &); // ...
60 # virtual int f(void); // re_methods
63 # RE_CLASS matches the class declaration. C++ allows multiple ways of
66 # struct ... { private: ... };
67 # class ... { private: ... };
70 # RE_VBPTR matches the virtual base declarations. gcc 2.95.3 sometimes
71 # emits these, but gcc 3.X.Y never emits these. The name depends on the
74 # RE_FIELDS matches the data fields of the class.
75 # RE_METHODS matches the methods explicitly declared for the class.
77 # RE_SYNTH_GCC_2 and RE_SYNTH_GCC_3 match the optional synthetic methods
78 # of the class. gcc -gstabs+ emits these methods, and gcc -gdwarf-2
81 # RE_ACCESS_METHODS is an access specifier after RE_FIELDS and before
82 # the methods (explicit methods and/or synthetic methods).
83 # There is also an RE_ACCESS_FIELDS.
85 # When I get HP-UX aCC, I hope that I can just add RE_SYNTH_ACC_NNN.
87 # Yet another twist: with gcc v2, ctor and dtor methods have a hidden
88 # argument in front, the "in-charge" flag. With gcc v3, there is no
89 # hidden argument; instead, there are multiple object functions for
92 # -- chastain 2004-01-01
94 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 } {
100 gdb_test_multiple "$command" "$testname" {
101 -re "type = $re_class${wsopt}$re_access_fields${wsopt}$re_fields${wsopt}$re_access_methods${wsopt}$re_methods$nl\}$re_star$nl$gdb_prompt $" {
102 # gcc 2.95.3, dwarf-2, no vbptr
103 # gcc 3.X, abi 1, dwarf-2
104 # gcc 3.X, abi 2, dwarf-2
107 -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 $" {
108 # gcc 2.95.3, dwarf-2, vbptr
109 # TODO: drow says the vbptr is a bug
112 -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 $" {
113 # gcc 2.95.3, stabs+, no vbptr
116 -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 $" {
117 # gcc 2.95.3, stabs+, vbptr
118 # TODO: drow says the vbptr is a bug
121 -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 $" {
122 # gcc 3.X, abi 1, stabs+
125 -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 $" {
126 # gcc 3.X, abi 2, stabs+
133 proc test_ptype_of_classes {} {
140 test_one_ptype "ptype VA" "ptype VA" \
141 "((struct|class) VA \{${ws}public:|struct VA \{)" \
145 "VA & operator=\\(VA const ?&\\);${ws}VA\\(VA const ?&\\);${ws}VA\\((void|)\\);" \
146 "VA & operator=\\(VA const ?&\\);${ws}VA\\(VA const ?&\\);${ws}VA\\((void|)\\);" \
151 test_one_ptype "ptype VB" "ptype VB" \
152 "((struct|class) VB \{${ws}public:|struct VB \{)" \
155 "" "int fvb\\((void|)\\);${ws}virtual int vvb\\((void|)\\);" \
156 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
157 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
162 test_one_ptype "ptype V" "ptype V" \
163 "class V : public VA, public VB \{${ws}public:" \
166 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
167 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
168 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
173 test_one_ptype "ptype A" "ptype A" \
174 "class A : public virtual V \{(${ws}private:|)" \
175 "V \\*(_vb.1V|_vb.V);" \
177 "public:" "virtual int f\\((void|)\\);" \
178 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
179 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
184 test_one_ptype "ptype B" "ptype B" \
185 "class B : public A \{(${ws}private:|)" \
186 "V \\*(_vb.1V|_vb.V);" \
188 "public:" "virtual int f\\((void|)\\);" \
189 "B & operator=\\(B const ?&\\);${ws}B\\(int, B const ?&\\);${ws}B\\(int\\);" \
190 "B & operator=\\(B const ?&\\);${ws}B\\(B const ?&\\);${ws}B\\((void|)\\);" \
195 test_one_ptype "ptype C" "ptype C" \
196 "class C : public virtual V \{(${ws}private:|)" \
197 "V \\*(_vb.1V|_vb.V);" \
200 "C & operator=\\(C const ?&\\);${ws}C\\(int, C const ?&\\);${ws}C\\(int\\);" \
201 "C & operator=\\(C const ?&\\);${ws}C\\(C const ?&\\);${ws}C\\((void|)\\);" \
206 test_one_ptype "ptype AD" "ptype AD" \
207 "((struct|class) AD \{${ws}public:|struct AD \{)" \
210 "" "virtual int vg\\((void|)\\);" \
211 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
212 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
217 test_one_ptype "ptype D" "ptype D" \
218 "class D : public AD, public virtual V \{(${ws}private:|)" \
219 "V \\*(_vb.1V|_vb.V);" \
221 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
222 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
223 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
227 # TODO: E does not show a vbptr for V. That seems strange.
229 test_one_ptype "ptype E" "ptype E" \
230 "class E : public B, public virtual V, public D, public C \{(${ws}private:|)" \
233 "" "virtual int f\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
234 "E & operator=\\(E const ?&\\);${ws}E\\(int, E const ?&\\);${ws}E\\(int\\);" \
235 "E & operator=\\(E const ?&\\);${ws}E\\(E const ?&\\);${ws}E\\((void|)\\);" \
240 test_one_ptype "ptype dd" "ptype dd" \
241 "class D : public AD, public virtual V \{(${ws}private:|)" \
242 "V \\*(_vb.1V|_vb.V);" \
244 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
245 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
246 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
251 test_one_ptype "ptype ppd" "ptype ppd" \
252 "class D : public AD, public virtual V \{(${ws}private:|)" \
253 "V \\*(_vb.1V|_vb.V);" \
255 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
256 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
257 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
260 # An instance of AD *
261 # TODO: this should be named pADd, not pAd.
263 test_one_ptype "ptype pAd" "ptype pAd" \
264 "((struct|class) AD \{${ws}public:|struct AD \{)" \
267 "" "virtual int vg\\((void|)\\);" \
268 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
269 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
274 test_one_ptype "ptype a" "ptype a" \
275 "class A : public virtual V \{(${ws}private:|)" \
276 "V \\*(_vb.1V|_vb.V);" \
278 "public:" "virtual int f\\((void|)\\);" \
279 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
280 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
285 test_one_ptype "ptype b" "ptype b" \
286 "class B : public A \{(${ws}private:|)" \
287 "V \\*(_vb.1V|_vb.V);" \
289 "public:" "virtual int f\\((void|)\\);" \
290 "B & operator=\\(B const ?&\\);${ws}B\\(int, B const ?&\\);${ws}B\\(int\\);" \
291 "B & operator=\\(B const ?&\\);${ws}B\\(B const ?&\\);${ws}B\\((void|)\\);" \
296 test_one_ptype "ptype c" "ptype c" \
297 "class C : public virtual V \{(${ws}private:|)" \
298 "V \\*(_vb.1V|_vb.V);" \
301 "C & operator=\\(C const ?&\\);${ws}C\\(int, C const ?&\\);${ws}C\\(int\\);" \
302 "C & operator=\\(C const ?&\\);${ws}C\\(C const ?&\\);${ws}C\\((void|)\\);" \
307 test_one_ptype "ptype d" "ptype d" \
308 "class D : public AD, public virtual V \{(${ws}private:|)" \
309 "V \\*(_vb.1V|_vb.V);" \
311 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
312 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
313 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
318 test_one_ptype "ptype e" "ptype e" \
319 "class E : public B, public virtual V, public D, public C \{(${ws}private:|)" \
322 "" "virtual int f\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
323 "E & operator=\\(E const ?&\\);${ws}E\\(int, E const ?&\\);${ws}E\\(int\\);" \
324 "E & operator=\\(E const ?&\\);${ws}E\\(E const ?&\\);${ws}E\\((void|)\\);" \
329 test_one_ptype "ptype v" "ptype v" \
330 "class V : public VA, public VB \{${ws}public:" \
333 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
334 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
335 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
340 test_one_ptype "ptype vb" "ptype vb" \
341 "((struct|class) VB \{${ws}public:|struct VB \{)" \
344 "" "int fvb\\((void|)\\);${ws}virtual int vvb\\((void|)\\);" \
345 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
346 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
351 test_one_ptype "ptype pAa" "ptype pAa" \
352 "class A : public virtual V \{(${ws}private:|)" \
353 "V \\*(_vb.1V|_vb.V);" \
355 "public:" "virtual int f\\((void|)\\);" \
356 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
357 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
362 test_one_ptype "ptype pAe" "ptype pAe" \
363 "class A : public virtual V \{(${ws}private:|)" \
364 "V \\*(_vb.1V|_vb.V);" \
366 "public:" "virtual int f\\((void|)\\);" \
367 "A & operator=\\(A const ?&\\);${ws}A\\(int, A const ?&\\);${ws}A\\(int\\);" \
368 "A & operator=\\(A const ?&\\);${ws}A\\(A const ?&\\);${ws}A\\((void|)\\);" \
373 test_one_ptype "ptype pBe" "ptype pBe" \
374 "class B : public A \{(${ws}private:|)" \
375 "V \\*(_vb.1V|_vb.V);" \
377 "public:" "virtual int f\\((void|)\\);" \
378 "B & operator=\\(B const ?&\\);${ws}B\\(int, B const ?&\\);${ws}B\\(int\\);" \
379 "B & operator=\\(B const ?&\\);${ws}B\\(B const ?&\\);${ws}B\\((void|)\\);" \
384 test_one_ptype "ptype pDd" "ptype pDd" \
385 "class D : public AD, public virtual V \{(${ws}private:|)" \
386 "V \\*(_vb.1V|_vb.V);" \
388 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
389 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
390 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
395 test_one_ptype "ptype pDe" "ptype pDe" \
396 "class D : public AD, public virtual V \{(${ws}private:|)" \
397 "V \\*(_vb.1V|_vb.V);" \
399 "" "static void s\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vd\\((void|)\\);${ws}int fd\\((void|)\\);" \
400 "D & operator=\\(D const ?&\\);${ws}D\\(int, D const ?&\\);${ws}D\\(int\\);" \
401 "D & operator=\\(D const ?&\\);${ws}D\\(D const ?&\\);${ws}D\\((void|)\\);" \
406 test_one_ptype "ptype pVa" "ptype pVa" \
407 "class V : public VA, public VB \{${ws}public:" \
410 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
411 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
412 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
417 test_one_ptype "ptype pVv" "ptype pVv" \
418 "class V : public VA, public VB \{${ws}public:" \
421 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
422 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
423 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
428 test_one_ptype "ptype pVe" "ptype pVe" \
429 "class V : public VA, public VB \{${ws}public:" \
432 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
433 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
434 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
439 test_one_ptype "ptype pVd" "ptype pVd" \
440 "class V : public VA, public VB \{${ws}public:" \
443 "" "int f\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
444 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
445 "V & operator=\\(V const ?&\\);${ws}V\\(V const ?&\\);${ws}V\\((void|)\\);" \
448 # An instance of AD *
450 test_one_ptype "ptype pADe" "ptype pADe" \
451 "((struct|class) AD \{${ws}public:|struct AD \{)" \
454 "" "virtual int vg\\((void|)\\);" \
455 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
456 "AD & operator=\\(AD const ?&\\);${ws}AD\\(AD const ?&\\);${ws}AD\\((void|)\\);" \
461 test_one_ptype "ptype pEe" "ptype pEe" \
462 "class E : public B, public virtual V, public D, public C \{(${ws}private:|)" \
465 "" "virtual int f\\((void|)\\);${ws}virtual int vg\\((void|)\\);${ws}virtual int vv\\((void|)\\);" \
466 "E & operator=\\(E const ?&\\);${ws}E\\(int, E const ?&\\);${ws}E\\(int\\);" \
467 "E & operator=\\(E const ?&\\);${ws}E\\(E const ?&\\);${ws}E\\((void|)\\);" \
470 # An instance of VB *
472 test_one_ptype "ptype pVB" "ptype pVB" \
473 "((struct|class) VB \{${ws}public:|struct VB \{)" \
476 "" "int fvb\\((void|)\\);${ws}virtual int vvb\\((void|)\\);" \
477 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
478 "VB & operator=\\(VB const ?&\\);${ws}VB\\(VB const ?&\\);${ws}VB\\((void|)\\);" \
482 # Call virtual functions.
484 proc test_virtual_calls {} {
488 if [target_info exists gdb,cannot_call_functions] {
489 setup_xfail "*-*-*" 2416
490 fail "This target can not call functions"
494 gdb_test "print pAe->f()" "\\$\[0-9\]+ = 20"
495 gdb_test "print pAa->f()" "\\$\[0-9\]+ = 1"
496 gdb_test "print pDe->vg()" "\\$\[0-9\]+ = 202"
497 gdb_test "print pADe->vg()" "\\$\[0-9\]+ = 202"
498 gdb_test "print pDd->vg()" "\\$\[0-9\]+ = 101"
499 gdb_test "print pEe->vvb()" "\\$\[0-9\]+ = 411"
500 gdb_test "print pVB->vvb()" "\\$\[0-9\]+ = 407"
501 gdb_test "print pBe->vvb()" "\\$\[0-9\]+ = 411"
502 gdb_test "print pDe->vvb()" "\\$\[0-9\]+ = 411"
503 gdb_test "print pEe->vd()" "\\$\[0-9\]+ = 282"
504 gdb_test "print pEe->fvb()" "\\$\[0-9\]+ = 311"
506 # more recent results:
508 # gcc 2.95.3 -gdwarf-2
509 # gcc 2.95.3 -gstabs+
510 # attempt to take addres of value not located in memory
511 # gcc 3.3.2 -gdwarf-2
514 # -- chastain 2003-12-31
516 gdb_test_multiple "print pEe->D::vg()" "print pEe->D::vg()" {
517 -re "\\$\[0-9]+ = 102$nl$gdb_prompt $" {
518 pass "print pEe->D::vg()"
520 -re "Attempt to take address of value not located in memory.$nl$gdb_prompt $" {
521 kfail "gdb/1064" "print pEe->D::vg()"
529 global srcdir subdir binfile
537 gdb_reinitialize_dir $srcdir/$subdir
540 gdb_test "set language c++" "" ""
541 gdb_test "set width 0" "" ""
544 test_ptype_of_classes
546 gdb_breakpoint test_calls
547 gdb_test "continue" ".*Breakpoint .* test_calls.*" ""