1 # Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 # 2004, 2006, 2007 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>.
27 if { [skip_cplus_tests] } { continue }
29 load_lib "cp-support.exp"
31 set testfile "classes"
32 set srcfile ${testfile}.cc
33 set binfile ${objdir}/${subdir}/${testfile}
35 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
40 # Test ptype of class objects.
42 proc test_ptype_class_objects {} {
47 "ptype struct default_public_struct" "" "struct" "default_public_struct" \
49 { field public "int a;" }
50 { field public "int b;" }
53 # Another simple type.
56 "ptype struct explicit_public_struct" "" "struct" "explicit_public_struct" \
58 { field public "int a;" }
59 { field public "int b;" }
62 # Another simple type.
65 "ptype struct protected_struct" "" "struct" "protected_struct" \
67 { field protected "int a;" }
68 { field protected "int b;" }
71 # Another simple type.
74 "ptype struct private_struct" "" "struct" "private_struct" \
76 { field private "int a;" }
77 { field private "int b;" }
83 "ptype struct mixed_protection_struct" "" "struct" "mixed_protection_struct" \
85 { field public "int a;" }
86 { field public "int b;" }
87 { field private "int c;" }
88 { field private "int d;" }
89 { field protected "int e;" }
90 { field protected "int f;" }
91 { field public "int g;" }
92 { field private "int h;" }
93 { field protected "int i;" }
96 # All that again with "class" instead of "struct".
97 # gdb does not care about the difference anyways.
100 "ptype class public_class" "" "class" "public_class" \
102 { field public "int a;" }
103 { field public "int b;" }
106 # Another simple type.
108 cp_test_ptype_class \
109 "ptype class protected_class" "" "class" "protected_class" \
111 { field protected "int a;" }
112 { field protected "int b;" }
115 # Another simple type.
117 cp_test_ptype_class \
118 "ptype class default_private_class" "" "class" "default_private_class" \
120 { field private "int a;" }
121 { field private "int b;" }
124 # Another simple type.
126 cp_test_ptype_class \
127 "ptype class explicit_private_class" "" "class" "explicit_private_class" \
129 { field private "int a;" }
130 { field private "int b;" }
135 cp_test_ptype_class \
136 "ptype class mixed_protection_class" "" "class" "mixed_protection_class" \
139 { field public "int a;" }
140 { field public "int b;" }
141 { field private "int c;" }
142 { field private "int d;" }
143 { field protected "int e;" }
144 { field protected "int f;" }
145 { field public "int g;" }
146 { field private "int h;" }
147 { field protected "int i;" }
150 # Here are some classes with inheritance.
154 cp_test_ptype_class \
155 "ptype class A" "" "class" "A" \
157 { field public "int a;" }
158 { field public "int x;" }
163 cp_test_ptype_class \
164 "ptype class B" "" "class" "B" \
167 { field public "int b;" }
168 { field public "int x;" }
173 cp_test_ptype_class \
174 "ptype class C" "" "class" "C" \
177 { field public "int c;" }
178 { field public "int x;" }
181 # Derived class, multiple inheritance.
183 cp_test_ptype_class \
184 "ptype class D" "" "class" "D" \
188 { field public "int d;" }
189 { field public "int x;" }
194 cp_test_ptype_class \
195 "ptype class E" "" "class" "E" \
198 { field public "int e;" }
199 { field public "int x;" }
202 # This is a break from inheritance tests.
204 # gcc 2.X with stabs (stabs or stabs+?) used to have a problem with
205 # static methods whose name is the same as their argument mangling.
207 cp_test_ptype_class \
208 "ptype class Static" "" "class" "Static" \
210 { method public "static void ii(int, int);" }
213 # Here are some virtual inheritance tests.
215 # A virtual base class.
217 cp_test_ptype_class \
218 "ptype class vA" "" "class" "vA" \
220 { field public "int va;" }
221 { field public "int vx;" }
224 # A derived class with a virtual base.
226 cp_test_ptype_class \
227 "ptype class vB" "" "class" "vB" \
229 { base "public virtual vA" }
231 { field public "int vb;" }
232 { field public "int vx;" }
235 # Another derived class with a virtual base.
237 cp_test_ptype_class \
238 "ptype class vC" "" "class" "vC" \
240 { base "public virtual vA" }
242 { field public "int vc;" }
243 { field public "int vx;" }
246 # A classic diamond class.
248 cp_test_ptype_class \
249 "ptype class vD" "" "class" "vD" \
251 { base "public virtual vB" }
252 { base "public virtual vC" }
255 { field public "int vd;" }
256 { field public "int vx;" }
259 # A class derived from a diamond class.
261 cp_test_ptype_class \
262 "ptype class vE" "" "class" "vE" \
264 { base "public virtual vD" }
266 { field public "int ve;" }
267 { field public "int vx;" }
270 # Another inheritance series.
274 cp_test_ptype_class \
275 "ptype class Base1" "" "class" "Base1" \
277 { field public "int x;" }
278 { method public "Base1(int);" }
281 # Another base class.
283 cp_test_ptype_class \
284 "ptype class Foo" "" "class" "Foo" \
286 { field public "int x;" }
287 { field public "int y;" }
288 { field public "static int st;" }
289 { method public "Foo(int, int);" }
290 { method public "int operator!();" }
291 { method public "operator int();" }
292 { method public "int times(int);" }
298 "int operator int();"
299 { setup_kfail "gdb/1497" "*-*-*" }
303 "int operator int(void);"
304 { setup_kfail "gdb/1497" "*-*-*" }
308 # A multiple inheritance derived class.
310 cp_test_ptype_class \
311 "ptype class Bar" "" "class" "Bar" \
313 { base "public Base1" }
314 { base "public Foo" }
315 { field public "int z;" }
316 { method public "Bar(int, int, int);" }
321 # Test simple access to class members.
323 proc test_non_inherited_member_access {} {
325 # Print non-inherited members of g_A.
326 gdb_test "print g_A.a" ".* = 1"
327 gdb_test "print g_A.x" ".* = 2"
329 # Print non-inherited members of g_B.
330 gdb_test "print g_B.b" ".* = 5"
331 gdb_test "print g_B.x" ".* = 6"
333 # Print non-inherited members of g_C.
334 gdb_test "print g_C.c" ".* = 9"
335 gdb_test "print g_C.x" ".* = 10"
337 # Print non-inherited members of g_D.
338 gdb_test "print g_D.d" ".* = 19"
339 gdb_test "print g_D.x" ".* = 20"
341 # Print non-inherited members of g_E.
342 gdb_test "print g_E.e" ".* = 31"
343 gdb_test "print g_E.x" ".* = 32"
346 # Test access to members of other classes.
347 # gdb should refuse to print them.
348 # (I feel old -- I remember when this was legal in C -- chastain).
350 proc test_wrong_class_members {} {
351 gdb_test "print g_A.b" "There is no member( or method|) named b."
352 gdb_test "print g_B.c" "There is no member( or method|) named c."
353 gdb_test "print g_B.d" "There is no member( or method|) named d."
354 gdb_test "print g_C.b" "There is no member( or method|) named b."
355 gdb_test "print g_C.d" "There is no member( or method|) named d."
356 gdb_test "print g_D.e" "There is no member( or method|) named e."
359 # Test access to names that are not members of any class.
361 proc test_nonexistent_members {} {
362 gdb_test "print g_A.y" "There is no member( or method|) named y."
363 gdb_test "print g_B.z" "There is no member( or method|) named z."
364 gdb_test "print g_C.q" "There is no member( or method|) named q."
365 gdb_test "print g_D.p" "There is no member( or method|) named p."
368 # Call a method that expects a base class parameter with base, inherited,
369 # and unrelated class arguments.
371 proc test_method_param_class {} {
372 gdb_test "call class_param.Aptr_a (&g_A)" ".* = 1"
373 gdb_test "call class_param.Aptr_x (&g_A)" ".* = 2"
374 gdb_test "call class_param.Aptr_a (&g_B)" ".* = 3"
375 gdb_test "call class_param.Aptr_x (&g_B)" ".* = 4"
376 gdb_test "call class_param.Aref_a (g_A)" ".* = 1"
377 gdb_test "call class_param.Aref_x (g_A)" ".* = 2"
378 gdb_test "call class_param.Aref_a (g_B)" ".* = 3"
379 gdb_test "call class_param.Aref_x (g_B)" ".* = 4"
380 gdb_test "call class_param.Aval_a (g_A)" ".* = 1"
381 gdb_test "call class_param.Aval_x (g_A)" ".* = 2"
382 gdb_test "call class_param.Aval_a (g_B)" ".* = 3"
383 gdb_test "call class_param.Aval_x (g_B)" ".* = 4"
385 gdb_test "call class_param.Aptr_a (&foo)" "Cannot resolve .*" "unrelated class *param"
386 gdb_test "call class_param.Aref_a (foo)" "Cannot resolve .*" "unrelated class ¶m"
387 gdb_test "call class_param.Aval_a (foo)" "Cannot resolve .*" "unrelated class param"
390 # Examine a class with an enum field.
398 gdb_test "print obj_with_enum" \
399 "\\$\[0-9\]+ = \{priv_enum = red, x = 0\}" \
400 "print obj_with_enum (1)"
406 # print the object again
408 gdb_test "print obj_with_enum" \
409 "\\$\[0-9\]+ = \{priv_enum = green, x = 0\}" \
410 "print obj_with_enum (2)"
412 # print the enum member
414 gdb_test "print obj_with_enum.priv_enum" "\\$\[0-9\]+ = green"
416 # ptype on the enum member
418 gdb_test_multiple "ptype obj_with_enum.priv_enum" "ptype obj_with_enum.priv_enum" {
419 -re "type = enum ClassWithEnum::PrivEnum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
420 pass "ptype obj_with_enum.priv_enum"
422 -re "type = enum PrivEnum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
423 # gcc 2.95.3 -gdwarf-2
424 # gcc 3.3.2 -gdwarf-2
425 pass "ptype obj_with_enum.priv_enum"
427 -re "type = enum \{ ?red, green, blue, yellow = 42 ?\}$nl$gdb_prompt $" {
428 # This case case is a little dubious, but it's not clear what
429 # ought to be required of a ptype on a private enum...
432 # It bugs me that this happens with gcc 3.
433 # -- chastain 2003-12-30
435 # gcc 2.95.3 -gstabs+
437 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
438 pass "ptype obj_with_enum.priv_enum"
442 # ptype on the object
444 # NOTE: carlton/2003-02-28: One could certainly argue that plain
446 # is acceptable: PrivEnum is a member of ClassWithEnum, so
447 # there's no need to explicitly qualify its name with
448 # "ClassWithEnum::". The truth, though, is that GDB is simply
449 # forgetting that PrivEnum is a member of ClassWithEnum, so we do
450 # that output for a bad reason instead of a good reason. Under
451 # stabs, we probably can't get this right; under DWARF-2, we can.
453 cp_test_ptype_class \
454 "ptype obj_with_enum" "" "class" "ClassWithEnum" \
456 { field public "ClassWithEnum::PrivEnum priv_enum;" }
457 { field public "int x;" }
462 "ClassWithEnum::PrivEnum priv_enum;"
463 "PrivEnum priv_enum;"
464 { setup_kfail "gdb/57" "*-*-*" }
468 # I'll do this test two different ways, because of a parser bug.
471 gdb_test_multiple "print (ClassWithEnum::PrivEnum) 42" "print (ClassWithEnum::PrivEnum) 42" {
472 -re "\\$\[0-9\]+ = yellow$nl$gdb_prompt $" {
473 pass "print (ClassWithEnum::PrivEnum) 42"
475 -re "A (parse|syntax) error in expression, near `42'.$nl$gdb_prompt $" {
476 # "parse error" is bison 1.35.
477 # "syntax error" is bison 1.875.
478 kfail "gdb/1588" "print (ClassWithEnum::PrivEnum) 42"
482 gdb_test_multiple "print ('ClassWithEnum::PrivEnum') 42" "print ('ClassWithEnum::PrivEnum') 42" {
483 -re "\\$\[0-9\]+ = yellow$nl$gdb_prompt $" {
485 # gcc HEAD 2003-12-28 21:08:30 UTC -gstabs+
486 pass "print ('ClassWithEnum::PrivEnum') 42"
488 -re "No symbol \"ClassWithEnum::PrivEnum\" in current context.$nl$gdb_prompt $" {
489 # gcc 2.95.3 -gdwarf-2
490 # gcc 3.3.2 -gdwarf-2
491 # gcc HEAD 2003-12-28 21:08:30 UTC -gdwarf-2
492 # gcc 2.95.3 -gstabs+
493 kfail "gdb/57" "print ('ClassWithEnum::PrivEnum') 42"
498 # Pointers to class members
500 proc test_pointers_to_class_members {} {
501 gdb_test "print Bar::z" "Cannot reference non-static field \"z\""
502 gdb_test "print &Foo::x" "\\$\[0-9\]+ = &Foo::x"
503 gdb_test "print (int)&Foo::x" "\\$\[0-9\]+ = 0"
504 gdb_test "print (int)&Bar::y == 2*sizeof(int)" "\\$\[0-9\]+ = true"
506 gdb_test "ptype Bar::z" "type = int"
507 gdb_test "ptype &Bar::z" "type = int Bar::\\*"
509 # TODO: this is a bogus test. It's looking at a variable that
510 # has not even been declared yet, so it's accessing random junk
511 # on the stack and comparing that it's NOT equal to a specific
512 # value. It's been like this since gdb 4.10 in 1993!
513 # -- chastain 2004-01-01
514 gdb_test "print (int)pmi == sizeof(int)" ".* = false"
517 # Test static members.
519 proc test_static_members {} {
522 gdb_test "print Foo::st" "\\$\[0-9\]+ = 100"
523 gdb_test "set foo.st = 200" "" ""
524 gdb_test "print bar.st" "\\$\[0-9\]+ = 200"
525 gdb_test "print &foo.st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
526 gdb_test "print &Bar::st" "\\$\[0-9\]+ = \\(int ?\\*\\) $hex"
527 gdb_test "print *\$" "\\$\[0-9\]+ = 200"
529 gdb_test "set print static-members off" ""
530 gdb_test "print csi" \
532 "print csi without static members"
533 gdb_test "print cnsi" \
535 "print cnsi without static members"
537 gdb_test "set print static-members on" ""
538 gdb_test "print csi" \
539 "{x = 10, y = 20, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>}}" \
540 "print csi with static members"
541 gdb_test "print cnsi" \
542 "{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>}" \
543 "print cnsi with static members"
559 # Start with a fresh gdb.
563 gdb_reinitialize_dir $srcdir/$subdir
566 gdb_test "set language c++" "" ""
567 gdb_test "set width 0" "" ""
569 if ![runto_main ] then {
570 perror "couldn't run to breakpoint"
574 gdb_breakpoint inheritance2
575 gdb_test "continue" ".*Breakpoint .* inheritance2.*" ""
577 test_ptype_class_objects
578 test_non_inherited_member_access
579 test_wrong_class_members
580 test_nonexistent_members
581 test_method_param_class
583 gdb_breakpoint enums2
584 gdb_test "continue" ".*Breakpoint .* enums2.*" "continue to enums2(\\(\\)|)"
585 gdb_test "finish" "" ""
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"