1 # Copyright 1992, 1994-2004, 2006-2012 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 3 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, see <http://www.gnu.org/licenses/>.
16 # This file was written by Fred Fish. (fnf@cygnus.com)
17 # And rewritten by Michael Chastain <mec.gnu@mindspring.com>.
21 if { [skip_cplus_tests] } { continue }
23 load_lib "cp-support.exp"
25 set testfile "classes"
26 set srcfile ${testfile}.cc
27 set binfile ${objdir}/${subdir}/${testfile}
29 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
34 # Test ptype of class objects.
36 proc test_ptype_class_objects {} {
41 "ptype struct default_public_struct" "" "struct" "default_public_struct" \
43 { field public "int a;" }
44 { field public "int b;" }
47 # Another simple type.
50 "ptype struct explicit_public_struct" "" "struct" "explicit_public_struct" \
52 { field public "int a;" }
53 { field public "int b;" }
56 # Another simple type.
59 "ptype struct protected_struct" "" "struct" "protected_struct" \
61 { field protected "int a;" }
62 { field protected "int b;" }
65 # Another simple type.
68 "ptype struct private_struct" "" "struct" "private_struct" \
70 { field private "int a;" }
71 { field private "int b;" }
77 "ptype struct mixed_protection_struct" "" "struct" "mixed_protection_struct" \
79 { field public "int a;" }
80 { field public "int b;" }
81 { field private "int c;" }
82 { field private "int d;" }
83 { field protected "int e;" }
84 { field protected "int f;" }
85 { field public "int g;" }
86 { field private "int h;" }
87 { field protected "int i;" }
90 # All that again with "class" instead of "struct".
91 # gdb does not care about the difference anyways.
94 "ptype class public_class" "" "class" "public_class" \
96 { field public "int a;" }
97 { field public "int b;" }
100 # Another simple type.
102 cp_test_ptype_class \
103 "ptype class protected_class" "" "class" "protected_class" \
105 { field protected "int a;" }
106 { field protected "int b;" }
109 # Another simple type.
111 cp_test_ptype_class \
112 "ptype class default_private_class" "" "class" "default_private_class" \
114 { field private "int a;" }
115 { field private "int b;" }
118 # Another simple type.
120 cp_test_ptype_class \
121 "ptype class explicit_private_class" "" "class" "explicit_private_class" \
123 { field private "int a;" }
124 { field private "int b;" }
129 cp_test_ptype_class \
130 "ptype class mixed_protection_class" "" "class" "mixed_protection_class" \
133 { field public "int a;" }
134 { field public "int b;" }
135 { field private "int c;" }
136 { field private "int d;" }
137 { field protected "int e;" }
138 { field protected "int f;" }
139 { field public "int g;" }
140 { field private "int h;" }
141 { field protected "int i;" }
144 # Here are some classes with inheritance.
148 cp_test_ptype_class \
149 "ptype class A" "" "class" "A" \
151 { field public "int a;" }
152 { field public "int x;" }
157 cp_test_ptype_class \
158 "ptype class B" "" "class" "B" \
161 { field public "int b;" }
162 { field public "int x;" }
167 cp_test_ptype_class \
168 "ptype class C" "" "class" "C" \
171 { field public "int c;" }
172 { field public "int x;" }
175 # Derived class, multiple inheritance.
177 cp_test_ptype_class \
178 "ptype class D" "" "class" "D" \
182 { field public "int d;" }
183 { field public "int x;" }
188 cp_test_ptype_class \
189 "ptype class E" "" "class" "E" \
192 { field public "int e;" }
193 { field public "int x;" }
196 # This is a break from inheritance tests.
198 # gcc 2.X with stabs (stabs or stabs+?) used to have a problem with
199 # static methods whose name is the same as their argument mangling.
201 cp_test_ptype_class \
202 "ptype class Static" "" "class" "Static" \
204 { method public "static void ii(int, int);" }
207 # Here are some virtual inheritance tests.
209 # A virtual base class.
211 cp_test_ptype_class \
212 "ptype class vA" "" "class" "vA" \
214 { field public "int va;" }
215 { field public "int vx;" }
218 # A derived class with a virtual base.
220 cp_test_ptype_class \
221 "ptype class vB" "" "class" "vB" \
223 { base "public virtual vA" }
225 { field public "int vb;" }
226 { field public "int vx;" }
229 # Another derived class with a virtual base.
231 cp_test_ptype_class \
232 "ptype class vC" "" "class" "vC" \
234 { base "public virtual vA" }
236 { field public "int vc;" }
237 { field public "int vx;" }
240 # A classic diamond class.
242 cp_test_ptype_class \
243 "ptype class vD" "" "class" "vD" \
245 { base "public virtual vB" }
246 { base "public virtual vC" }
249 { field public "int vd;" }
250 { field public "int vx;" }
253 # A class derived from a diamond class.
255 cp_test_ptype_class \
256 "ptype class vE" "" "class" "vE" \
258 { base "public virtual vD" }
260 { field public "int ve;" }
261 { field public "int vx;" }
264 # Another inheritance series.
268 cp_test_ptype_class \
269 "ptype class Base1" "" "class" "Base1" \
271 { field public "int x;" }
272 { method public "Base1(int);" }
275 # Another base class.
277 cp_test_ptype_class \
278 "ptype class Foo" "" "class" "Foo" \
280 { field public "int x;" }
281 { field public "int y;" }
282 { field public "static int st;" }
283 { method public "Foo(int, int);" }
284 { method public "int operator!();" }
285 { method public "operator int();" }
286 { method public "int times(int);" }
292 "int operator int();"
293 { setup_kfail "gdb/1497" "*-*-*" }
297 "int operator int(void);"
298 { setup_kfail "gdb/1497" "*-*-*" }
302 # A multiple inheritance derived class.
304 cp_test_ptype_class \
305 "ptype class Bar" "" "class" "Bar" \
307 { base "public Base1" }
308 { base "public Foo" }
309 { field public "int z;" }
310 { method public "Bar(int, int, int);" }
315 # Test simple access to class members.
317 proc test_non_inherited_member_access {} {
319 # Print non-inherited members of g_A.
320 gdb_test "print g_A.a" ".* = 1"
321 gdb_test "print g_A.x" ".* = 2"
323 # Print non-inherited members of g_B.
324 gdb_test "print g_B.b" ".* = 5"
325 gdb_test "print g_B.x" ".* = 6"
327 # Print non-inherited members of g_C.
328 gdb_test "print g_C.c" ".* = 9"
329 gdb_test "print g_C.x" ".* = 10"
331 # Print non-inherited members of g_D.
332 gdb_test "print g_D.d" ".* = 19"
333 gdb_test "print g_D.x" ".* = 20"
335 # Print non-inherited members of g_E.
336 gdb_test "print g_E.e" ".* = 31"
337 gdb_test "print g_E.x" ".* = 32"
340 # Test access to members of other classes.
341 # gdb should refuse to print them.
342 # (I feel old -- I remember when this was legal in C -- chastain).
344 proc test_wrong_class_members {} {
345 gdb_test "print g_A.b" "There is no member( or method|) named b."
346 gdb_test "print g_B.c" "There is no member( or method|) named c."
347 gdb_test "print g_B.d" "There is no member( or method|) named d."
348 gdb_test "print g_C.b" "There is no member( or method|) named b."
349 gdb_test "print g_C.d" "There is no member( or method|) named d."
350 gdb_test "print g_D.e" "There is no member( or method|) named e."
353 # Test access to names that are not members of any class.
355 proc test_nonexistent_members {} {
356 gdb_test "print g_A.y" "There is no member( or method|) named y."
357 gdb_test "print g_B.z" "There is no member( or method|) named z."
358 gdb_test "print g_C.q" "There is no member( or method|) named q."
359 gdb_test "print g_D.p" "There is no member( or method|) named p."
362 # Call a method that expects a base class parameter with base, inherited,
363 # and unrelated class arguments.
365 proc test_method_param_class {} {
366 gdb_test "call class_param.Aptr_a (&g_A)" ".* = 1"
367 gdb_test "call class_param.Aptr_x (&g_A)" ".* = 2"
368 gdb_test "call class_param.Aptr_a (&g_B)" ".* = 3"
369 gdb_test "call class_param.Aptr_x (&g_B)" ".* = 4"
370 gdb_test "call class_param.Aref_a (g_A)" ".* = 1"
371 gdb_test "call class_param.Aref_x (g_A)" ".* = 2"
372 gdb_test "call class_param.Aref_a (g_B)" ".* = 3"
373 gdb_test "call class_param.Aref_x (g_B)" ".* = 4"
374 gdb_test "call class_param.Aval_a (g_A)" ".* = 1"
375 gdb_test "call class_param.Aval_x (g_A)" ".* = 2"
376 gdb_test "call class_param.Aval_a (g_B)" ".* = 3"
377 gdb_test "call class_param.Aval_x (g_B)" ".* = 4"
379 gdb_test "call class_param.Aptr_a (&foo)" "Cannot resolve .*" "unrelated class *param"
380 gdb_test "call class_param.Aref_a (foo)" "Cannot resolve .*" "unrelated class ¶m"
381 gdb_test "call class_param.Aval_a (foo)" "Cannot resolve .*" "unrelated class param"
384 # Examine a class with an enum field.
392 # We match the enum values with and without qualifiers. As of
393 # 2008-08-21 we can output the qualifiers for DWARF-2.
395 gdb_test "print obj_with_enum" \
396 "\\$\[0-9\]+ = \{priv_enum = (ClassWithEnum::)?red, x = 0\}" \
397 "print obj_with_enum (1)"
403 # print the object again
405 gdb_test "print obj_with_enum" \
406 "\\$\[0-9\]+ = \{priv_enum = (ClassWithEnum::)?green, x = 0\}" \
407 "print obj_with_enum (2)"
409 # print the enum member
411 gdb_test "print obj_with_enum.priv_enum" "\\$\[0-9\]+ = (ClassWithEnum::)?green"
413 # ptype on the enum member
415 gdb_test_multiple "ptype obj_with_enum.priv_enum" "ptype obj_with_enum.priv_enum" {
416 -re "type = enum ClassWithEnum::PrivEnum \{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" {
417 pass "ptype obj_with_enum.priv_enum"
419 -re "type = enum PrivEnum \{ ?(ClassWithEnum::)?red, (ClassWithEnum::)?green, (ClassWithEnum::)?blue, (ClassWithEnum::)?yellow = 42 ?\}$nl$gdb_prompt $" {
420 # gcc 2.95.3 -gdwarf-2
421 # gcc 3.3.2 -gdwarf-2
422 pass "ptype obj_with_enum.priv_enum"
424 -re "type = enum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
425 # This case case is a little dubious, but it's not clear what
426 # ought to be required of a ptype on a private enum...
429 # It bugs me that this happens with gcc 3.
430 # -- chastain 2003-12-30
432 # gcc 2.95.3 -gstabs+
434 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
435 pass "ptype obj_with_enum.priv_enum"
439 # ptype on the object
441 # NOTE: carlton/2003-02-28: One could certainly argue that plain
443 # is acceptable: PrivEnum is a member of ClassWithEnum, so
444 # there's no need to explicitly qualify its name with
445 # "ClassWithEnum::". The truth, though, is that GDB is simply
446 # forgetting that PrivEnum is a member of ClassWithEnum, so we do
447 # that output for a bad reason instead of a good reason. Under
448 # stabs, we probably can't get this right; under DWARF-2, we can.
450 cp_test_ptype_class \
451 "ptype obj_with_enum" "" "class" "ClassWithEnum" \
453 { field public "ClassWithEnum::PrivEnum priv_enum;" }
454 { field public "int x;" }
459 "ClassWithEnum::PrivEnum priv_enum;"
460 "PrivEnum priv_enum;"
461 { setup_kfail "gdb/57" "*-*-*" }
465 # I'll do this test two different ways, because of a parser bug.
468 gdb_test_multiple "print (ClassWithEnum::PrivEnum) 42" "print (ClassWithEnum::PrivEnum) 42" {
469 -re "\\$\[0-9\]+ = (ClassWithEnum::)?yellow$nl$gdb_prompt $" {
470 pass "print (ClassWithEnum::PrivEnum) 42"
472 -re "A (parse|syntax) error in expression, near `42'.$nl$gdb_prompt $" {
473 # "parse error" is bison 1.35.
474 # "syntax error" is bison 1.875.
475 kfail "gdb/1588" "print (ClassWithEnum::PrivEnum) 42"
479 gdb_test_multiple "print ('ClassWithEnum::PrivEnum') 42" "print ('ClassWithEnum::PrivEnum') 42" {
480 -re "\\$\[0-9\]+ = (ClassWithEnum::)?yellow$nl$gdb_prompt $" {
482 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
483 pass "print ('ClassWithEnum::PrivEnum') 42"
485 -re "No symbol \"ClassWithEnum::PrivEnum\" in current context.$nl$gdb_prompt $" {
486 # gcc 2.95.3 -gdwarf-2
487 # gcc 3.3.2 -gdwarf-2
488 # gcc HEAD 2003-12-28 21:08:30 UTC -gdwarf-2
489 # gcc 2.95.3 -gstabs+
490 kfail "gdb/57" "print ('ClassWithEnum::PrivEnum') 42"
495 # Pointers to class members
497 proc test_pointers_to_class_members {} {
498 gdb_test "print Bar::z" "Cannot reference non-static field \"z\""
499 gdb_test "print &Foo::x" "\\$\[0-9\]+ = &Foo::x"
500 gdb_test "print (int)&Foo::x" "\\$\[0-9\]+ = 0"
501 gdb_test "print (int)&Bar::y == 2*sizeof(int)" "\\$\[0-9\]+ = true"
503 gdb_test "ptype Bar::z" "type = int"
504 gdb_test "ptype &Bar::z" "type = int Bar::\\*"
506 # TODO: this is a bogus test. It's looking at a variable that
507 # has not even been declared yet, so it's accessing random junk
508 # on the stack and comparing that it's NOT equal to a specific
509 # value. It's been like this since gdb 4.10 in 1993!
510 # -- chastain 2004-01-01
511 gdb_test "print (int)pmi == sizeof(int)" ".* = false"
514 # Test static members.
516 proc test_static_members {} {
519 gdb_test "print Foo::st" "\\$\[0-9\]+ = 100"
520 gdb_test_no_output "set foo.st = 200" ""
521 gdb_test "print bar.st" "\\$\[0-9\]+ = 200"
522 gdb_test "print &foo.st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
523 gdb_test "print &Bar::st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
524 gdb_test "print *\$" "\\$\[0-9\]+ = 200"
526 gdb_test_no_output "set print static-members off"
527 gdb_test "print csi" \
529 "print csi without static members"
530 gdb_test "print cnsi" \
532 "print cnsi without static members"
534 gdb_test_no_output "set print static-members on"
535 gdb_test "print csi" \
536 "{x = 10, y = 20, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>}}" \
537 "print csi with static members"
538 gdb_test "print cnsi" \
539 "{x = 30, y = 40, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>, static yy = {z = 5, static xx = {x = 1, y = 2, static null = <same as static member of an already seen type>, static yy = <same as static member of an already seen type>}}}, static yy = <same as static member of an already seen type>}" \
540 "print cnsi with static members"
552 # Start with a fresh gdb.
556 gdb_reinitialize_dir $srcdir/$subdir
559 gdb_test_no_output "set language c++" ""
560 gdb_test_no_output "set width 0" ""
562 if ![runto_main ] then {
563 perror "couldn't run to breakpoint"
567 gdb_breakpoint inheritance2
568 gdb_test "continue" ".*Breakpoint .* inheritance2.*" ""
570 test_ptype_class_objects
571 test_non_inherited_member_access
572 test_wrong_class_members
573 test_nonexistent_members
574 test_method_param_class
576 gdb_breakpoint enums2
577 gdb_test "continue" ".*Breakpoint .* enums2.*" "continue to enums2(\\(\\)|)"
578 # Leave enums2. Make sure we reach the next line, in case there
579 # are any more instructions to finish the function call.
580 gdb_test_multiple "finish" "" {
581 -re "enums2 \\(\\);.*$gdb_prompt $" {
582 gdb_test "next" ".*" ""
584 -re "$gdb_prompt $" { }
588 gdb_test "finish" ".*" ""
589 test_pointers_to_class_members
592 # Now some random tests that were just thrown in here.
594 gdb_breakpoint marker_reg1
595 gdb_test "continue" ".*Breakpoint .* marker_reg1.*" ""
596 gdb_test "finish" "Run till exit from.*" "finish from marker_reg1"
598 # This class is so small that an instance of it can fit in a register.
599 # When gdb tries to call a method, it gets embarrassed about taking
600 # the address of a register.
602 # TODO: I think that message should be a PASS, not an XFAIL.
603 # gdb prints an informative message and declines to do something
606 # The method call actually succeeds if the compiler allocates very
607 # small classes in memory instead of registers. So this test does
608 # not tell us anything interesting if the call succeeds.
610 # -- chastain 2003-12-31
611 gdb_test_multiple "print v.method ()" "calling method for small class" {
612 -re "\\$\[0-9\]+ = 82$nl$gdb_prompt $" {
613 # gcc 3.3.2 -gdwarf-2
614 # gcc HEAD 2003-12-28 21:08:30 UTC -gdwarf-2
616 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
617 pass "calling method for small class"
619 -re "Address requested for identifier \"v\" which is in register .*$nl$gdb_prompt $" {
620 # gcc 2.95.3 -gdwarf-2
621 # gcc 2.95.3 -gstabs+
622 setup_xfail "*-*-*" 2972
623 fail "calling method for small class"
627 gdb_test "print base1::Base1" "<.*Base1.*>" "print ctor of typedef class"
628 gdb_test "print base1::~Base1" "<.*~Base1(\\(\\))?>" \
629 "print dtor of typedef class"
631 gdb_test "list ByAnyOtherName::times" ".*int Foo::times.*"